Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :mod:`readline` --- GNU readline interface |
| 2 | ========================================== |
| 3 | |
| 4 | .. module:: readline |
| 5 | :platform: Unix |
| 6 | :synopsis: GNU readline support for Python. |
Skip Montanaro | 5466246 | 2007-12-08 15:26:16 +0000 | [diff] [blame] | 7 | .. sectionauthor:: Skip Montanaro <skip@pobox.com> |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | The :mod:`readline` module defines a number of functions to facilitate |
| 11 | completion and reading/writing of history files from the Python interpreter. |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 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 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 14 | made using this module affect the behaviour of both the interpreter's |
| 15 | interactive prompt and the prompts offered by the :func:`raw_input` and |
| 16 | :func:`input` built-in functions. |
| 17 | |
Ezio Melotti | 8198256 | 2010-01-04 09:00:11 +0000 | [diff] [blame] | 18 | .. note:: |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 19 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 20 | The underlying Readline library API may be implemented by |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 21 | the ``libedit`` library instead of GNU readline. |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 22 | On MacOS X the :mod:`readline` module detects which library is being used |
| 23 | at run time. |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 24 | |
| 25 | The configuration file for ``libedit`` is different from that |
Georg Brandl | 0930228 | 2010-10-06 09:32:48 +0000 | [diff] [blame] | 26 | of GNU readline. If you programmatically load configuration strings |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 27 | you can check for the text "libedit" in :const:`readline.__doc__` |
| 28 | to differentiate between GNU readline and libedit. |
| 29 | |
| 30 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 31 | Init file |
| 32 | --------- |
| 33 | |
| 34 | The following functions relate to the init file and user configuration: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | .. function:: parse_and_bind(string) |
| 38 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 39 | Execute the init line provided in the *string* argument. This calls |
| 40 | :c:func:`rl_parse_and_bind` in the underlying library. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | .. function:: read_init_file([filename]) |
| 44 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 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: |
| 53 | |
| 54 | |
| 55 | .. function:: get_line_buffer() |
| 56 | |
| 57 | Return the current contents of the line buffer (:c:data:`rl_line_buffer` |
| 58 | in the underlying library). |
| 59 | |
| 60 | |
| 61 | .. function:: insert_text(string) |
| 62 | |
| 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. |
| 66 | |
| 67 | |
| 68 | .. function:: redisplay() |
| 69 | |
| 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: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | .. function:: read_history_file([filename]) |
| 81 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | .. function:: write_history_file([filename]) |
| 88 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 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. |
| 92 | |
| 93 | |
| 94 | .. function:: get_history_length() |
| 95 | set_history_length(length) |
| 96 | |
| 97 | Set or return the desired number of lines to save in the history file. |
| 98 | The :func:`write_history_file` function uses this value to truncate |
| 99 | the history file, by calling :c:func:`history_truncate_file` in |
| 100 | the underlying library. Negative values imply |
| 101 | unlimited history file size. |
| 102 | |
| 103 | |
| 104 | History list |
| 105 | ------------ |
| 106 | |
| 107 | The following functions operate on a global history list: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 108 | |
| 109 | |
| 110 | .. function:: clear_history() |
| 111 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 112 | Clear the current history. This calls :c:func:`clear_history` in the |
| 113 | underlying library. The Python function only exists if Python was |
| 114 | compiled for a version of the library that supports it. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 115 | |
| 116 | .. versionadded:: 2.4 |
| 117 | |
| 118 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 119 | .. function:: get_current_history_length() |
| 120 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 121 | Return the number of items currently in the history. (This is different from |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 122 | :func:`get_history_length`, which returns the maximum number of lines that will |
| 123 | be written to a history file.) |
| 124 | |
| 125 | .. versionadded:: 2.3 |
| 126 | |
| 127 | |
| 128 | .. function:: get_history_item(index) |
| 129 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 130 | Return the current contents of history item at *index*. The item index |
| 131 | is one-based. This calls :c:func:`history_get` in the underlying library. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 132 | |
| 133 | .. versionadded:: 2.3 |
| 134 | |
| 135 | |
| 136 | .. function:: remove_history_item(pos) |
| 137 | |
| 138 | Remove history item specified by its position from the history. |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 139 | The position is zero-based. This calls :c:func:`remove_history` in |
| 140 | the underlying library. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 141 | |
| 142 | .. versionadded:: 2.4 |
| 143 | |
| 144 | |
| 145 | .. function:: replace_history_item(pos, line) |
| 146 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 147 | Replace history item specified by its position with *line*. |
| 148 | The position is zero-based. This calls :c:func:`replace_history_entry` |
| 149 | in the underlying library. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 150 | |
| 151 | .. versionadded:: 2.4 |
| 152 | |
| 153 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 154 | .. function:: add_history(line) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 155 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 156 | Append *line* to the history buffer, as if it was the last line typed. |
| 157 | This calls :c:func:`add_history` in the underlying library. |
| 158 | |
| 159 | |
| 160 | Startup hooks |
| 161 | ------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 162 | |
| 163 | .. versionadded:: 2.3 |
| 164 | |
| 165 | |
| 166 | .. function:: set_startup_hook([function]) |
| 167 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 168 | Set or remove the function invoked by the :c:data:`rl_startup_hook` |
| 169 | callback of the underlying library. If *function* is specified, it will |
| 170 | be used as the new hook function; if omitted or ``None``, any function |
| 171 | already installed is removed. The hook is called with no |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 172 | arguments just before readline prints the first prompt. |
| 173 | |
| 174 | |
| 175 | .. function:: set_pre_input_hook([function]) |
| 176 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 177 | Set or remove the function invoked by the :c:data:`rl_pre_input_hook` |
| 178 | callback of the underlying library. If *function* is specified, it will |
| 179 | be used as the new hook function; if omitted or ``None``, any |
| 180 | function already installed is removed. The hook is called |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 181 | with no arguments after the first prompt has been printed and just before |
| 182 | readline starts reading input characters. |
| 183 | |
| 184 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 185 | Completion |
| 186 | ---------- |
| 187 | |
| 188 | The following functions relate to implementing a custom word completion |
| 189 | function. This is typically operated by the Tab key, and can suggest and |
| 190 | automatically complete a word being typed. By default, Readline is set up |
| 191 | to be used by :mod:`rlcompleter` to complete Python identifiers for |
| 192 | the interactive interpreter. If the :mod:`readline` module is to be used |
| 193 | with a custom completer, a different set of word delimiters should be set. |
| 194 | |
| 195 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 196 | .. function:: set_completer([function]) |
| 197 | |
| 198 | Set or remove the completer function. If *function* is specified, it will be |
| 199 | used as the new completer function; if omitted or ``None``, any completer |
| 200 | function already installed is removed. The completer function is called as |
| 201 | ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it |
| 202 | returns a non-string value. It should return the next possible completion |
| 203 | starting with *text*. |
| 204 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 205 | The installed completer function is invoked by the *entry_func* callback |
| 206 | passed to :c:func:`rl_completion_matches` in the underlying library. |
| 207 | The *text* string comes from the first parameter to the |
| 208 | :c:data:`rl_attempted_completion_function` callback of the |
| 209 | underlying library. |
| 210 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 211 | |
| 212 | .. function:: get_completer() |
| 213 | |
| 214 | Get the completer function, or ``None`` if no completer function has been set. |
| 215 | |
| 216 | .. versionadded:: 2.3 |
| 217 | |
| 218 | |
Martin v. Löwis | 58bd49f | 2007-09-04 13:13:14 +0000 | [diff] [blame] | 219 | .. function:: get_completion_type() |
| 220 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 221 | Get the type of completion being attempted. This returns the |
| 222 | :c:data:`rl_completion_type` variable in the underlying library as |
| 223 | an integer. |
Martin v. Löwis | 58bd49f | 2007-09-04 13:13:14 +0000 | [diff] [blame] | 224 | |
| 225 | .. versionadded:: 2.6 |
| 226 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 227 | .. function:: get_begidx() |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 228 | get_endidx() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 229 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 230 | Get the beginning or ending index of the completion scope. |
| 231 | These indexes are the *start* and *end* arguments passed to the |
| 232 | :c:data:`rl_attempted_completion_function` callback of the |
| 233 | underlying library. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 234 | |
| 235 | |
| 236 | .. function:: set_completer_delims(string) |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 237 | get_completer_delims() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 238 | |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 239 | Set or get the word delimiters for completion. These determine the |
| 240 | start of the word to be considered for completion (the completion scope). |
| 241 | These functions access the :c:data:`rl_completer_word_break_characters` |
| 242 | variable in the underlying library. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 243 | |
Martin v. Löwis | 58bd49f | 2007-09-04 13:13:14 +0000 | [diff] [blame] | 244 | .. function:: set_completion_display_matches_hook([function]) |
| 245 | |
| 246 | Set or remove the completion display function. If *function* is |
| 247 | specified, it will be used as the new completion display function; |
| 248 | if omitted or ``None``, any completion display function already |
Martin Panter | aad86a6 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 249 | installed is removed. This sets or clears the |
| 250 | :c:data:`rl_completion_display_matches_hook` callback in the |
| 251 | underlying library. The completion display function is called as |
Martin v. Löwis | 58bd49f | 2007-09-04 13:13:14 +0000 | [diff] [blame] | 252 | ``function(substitution, [matches], longest_match_length)`` once |
| 253 | each time matches need to be displayed. |
| 254 | |
| 255 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 256 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 257 | .. _readline-example: |
| 258 | |
| 259 | Example |
| 260 | ------- |
| 261 | |
| 262 | The following example demonstrates how to use the :mod:`readline` module's |
| 263 | history reading and writing functions to automatically load and save a history |
| 264 | file named :file:`.pyhist` from the user's home directory. The code below would |
| 265 | normally be executed automatically during interactive sessions from the user's |
| 266 | :envvar:`PYTHONSTARTUP` file. :: |
| 267 | |
| 268 | import os |
Georg Brandl | db235c1 | 2010-10-06 09:33:55 +0000 | [diff] [blame] | 269 | import readline |
Éric Araujo | 8bea9a5 | 2011-03-26 01:24:47 +0100 | [diff] [blame] | 270 | histfile = os.path.join(os.path.expanduser("~"), ".pyhist") |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 271 | try: |
| 272 | readline.read_history_file(histfile) |
Ezio Melotti | 167c336 | 2016-01-11 23:30:15 +0200 | [diff] [blame] | 273 | # default history len is -1 (infinite), which may grow unruly |
| 274 | readline.set_history_length(1000) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 275 | except IOError: |
| 276 | pass |
| 277 | import atexit |
| 278 | atexit.register(readline.write_history_file, histfile) |
| 279 | del os, histfile |
| 280 | |
| 281 | The following example extends the :class:`code.InteractiveConsole` class to |
| 282 | support history save/restore. :: |
| 283 | |
| 284 | import code |
| 285 | import readline |
| 286 | import atexit |
| 287 | import os |
| 288 | |
| 289 | class HistoryConsole(code.InteractiveConsole): |
| 290 | def __init__(self, locals=None, filename="<console>", |
| 291 | histfile=os.path.expanduser("~/.console-history")): |
Georg Brandl | b29709a | 2009-09-16 09:24:57 +0000 | [diff] [blame] | 292 | code.InteractiveConsole.__init__(self, locals, filename) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 293 | self.init_history(histfile) |
| 294 | |
| 295 | def init_history(self, histfile): |
| 296 | readline.parse_and_bind("tab: complete") |
| 297 | if hasattr(readline, "read_history_file"): |
| 298 | try: |
| 299 | readline.read_history_file(histfile) |
| 300 | except IOError: |
| 301 | pass |
| 302 | atexit.register(self.save_history, histfile) |
| 303 | |
| 304 | def save_history(self, histfile): |
Ezio Melotti | 167c336 | 2016-01-11 23:30:15 +0200 | [diff] [blame] | 305 | readline.set_history_length(1000) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 306 | readline.write_history_file(histfile) |
| 307 | |