Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`readline` --- GNU readline interface |
| 2 | ========================================== |
| 3 | |
| 4 | .. module:: readline |
| 5 | :platform: Unix |
| 6 | :synopsis: GNU readline support for Python. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | |
Christian Heimes | 895627f | 2007-12-08 17:28:33 +0000 | [diff] [blame] | 8 | .. sectionauthor:: Skip Montanaro <skip@pobox.com> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 10 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
| 12 | The :mod:`readline` module defines a number of functions to facilitate |
| 13 | completion and reading/writing of history files from the Python interpreter. |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 14 | This module can be used directly, or via the :mod:`rlcompleter` module, which |
| 15 | supports completion of Python identifiers at the interactive prompt. Settings |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | made using this module affect the behaviour of both the interpreter's |
Georg Brandl | 96593ed | 2007-09-07 14:15:41 +0000 | [diff] [blame] | 17 | interactive prompt and the prompts offered by the built-in :func:`input` |
| 18 | function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | |
Zvezdan Petkovic | c2f082e | 2018-05-17 02:45:10 -0400 | [diff] [blame] | 20 | Readline keybindings may be configured via an initialization file, typically |
| 21 | ``.inputrc`` in your home directory. See `Readline Init File |
| 22 | <https://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#SEC9>`_ |
| 23 | in the GNU Readline manual for information about the format and |
| 24 | allowable constructs of that file, and the capabilities of the |
| 25 | Readline library in general. |
| 26 | |
Ezio Melotti | 6e40e27 | 2010-01-04 09:29:10 +0000 | [diff] [blame] | 27 | .. note:: |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 28 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 29 | The underlying Readline library API may be implemented by |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 30 | the ``libedit`` library instead of GNU readline. |
Zvezdan Petkovic | c2f082e | 2018-05-17 02:45:10 -0400 | [diff] [blame] | 31 | On macOS the :mod:`readline` module detects which library is being used |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 32 | at run time. |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 33 | |
| 34 | The configuration file for ``libedit`` is different from that |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 35 | of GNU readline. If you programmatically load configuration strings |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 36 | you can check for the text "libedit" in :const:`readline.__doc__` |
| 37 | to differentiate between GNU readline and libedit. |
| 38 | |
Zvezdan Petkovic | c2f082e | 2018-05-17 02:45:10 -0400 | [diff] [blame] | 39 | If you use *editline*/``libedit`` readline emulation on macOS, the |
| 40 | initialization file located in your home directory is named |
| 41 | ``.editrc``. For example, the following content in ``~/.editrc`` will |
| 42 | turn ON *vi* keybindings and TAB completion:: |
| 43 | |
| 44 | python:bind -v |
| 45 | python:bind ^I rl_complete |
Martin Panter | 553245c | 2016-06-10 00:27:46 +0000 | [diff] [blame] | 46 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 47 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 48 | Init file |
| 49 | --------- |
| 50 | |
| 51 | The following functions relate to the init file and user configuration: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | .. function:: parse_and_bind(string) |
| 55 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 56 | Execute the init line provided in the *string* argument. This calls |
| 57 | :c:func:`rl_parse_and_bind` in the underlying library. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | .. function:: read_init_file([filename]) |
| 61 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 62 | Execute a readline initialization file. The default filename is the last filename |
| 63 | used. This calls :c:func:`rl_read_init_file` in the underlying library. |
| 64 | |
| 65 | |
| 66 | Line buffer |
| 67 | ----------- |
| 68 | |
| 69 | The following functions operate on the line buffer: |
| 70 | |
| 71 | |
| 72 | .. function:: get_line_buffer() |
| 73 | |
| 74 | Return the current contents of the line buffer (:c:data:`rl_line_buffer` |
| 75 | in the underlying library). |
| 76 | |
| 77 | |
| 78 | .. function:: insert_text(string) |
| 79 | |
| 80 | Insert text into the line buffer at the cursor position. This calls |
| 81 | :c:func:`rl_insert_text` in the underlying library, but ignores |
| 82 | the return value. |
| 83 | |
| 84 | |
| 85 | .. function:: redisplay() |
| 86 | |
| 87 | Change what's displayed on the screen to reflect the current contents of the |
| 88 | line buffer. This calls :c:func:`rl_redisplay` in the underlying library. |
| 89 | |
| 90 | |
| 91 | History file |
| 92 | ------------ |
| 93 | |
| 94 | The following functions operate on a history file: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | .. function:: read_history_file([filename]) |
| 98 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 99 | Load a readline history file, and append it to the history list. |
| 100 | The default filename is :file:`~/.history`. This calls |
| 101 | :c:func:`read_history` in the underlying library. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | .. function:: write_history_file([filename]) |
| 105 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 106 | Save the history list to a readline history file, overwriting any |
| 107 | existing file. The default filename is :file:`~/.history`. This calls |
| 108 | :c:func:`write_history` in the underlying library. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
| 110 | |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 111 | .. function:: append_history_file(nelements[, filename]) |
| 112 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 113 | Append the last *nelements* items of history to a file. The default filename is |
| 114 | :file:`~/.history`. The file must already exist. This calls |
Martin Panter | 6afbc65 | 2016-06-14 08:45:43 +0000 | [diff] [blame] | 115 | :c:func:`append_history` in the underlying library. This function |
| 116 | only exists if Python was compiled for a version of the library |
| 117 | that supports it. |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 118 | |
| 119 | .. versionadded:: 3.5 |
| 120 | |
| 121 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 122 | .. function:: get_history_length() |
| 123 | set_history_length(length) |
| 124 | |
| 125 | Set or return the desired number of lines to save in the history file. |
| 126 | The :func:`write_history_file` function uses this value to truncate |
| 127 | the history file, by calling :c:func:`history_truncate_file` in |
| 128 | the underlying library. Negative values imply |
| 129 | unlimited history file size. |
| 130 | |
| 131 | |
| 132 | History list |
| 133 | ------------ |
| 134 | |
| 135 | The following functions operate on a global history list: |
| 136 | |
| 137 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | .. function:: clear_history() |
| 139 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 140 | Clear the current history. This calls :c:func:`clear_history` in the |
| 141 | underlying library. The Python function only exists if Python was |
| 142 | compiled for a version of the library that supports it. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
| 144 | |
| 145 | .. function:: get_current_history_length() |
| 146 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 147 | Return the number of items currently in the history. (This is different from |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | :func:`get_history_length`, which returns the maximum number of lines that will |
| 149 | be written to a history file.) |
| 150 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
| 152 | .. function:: get_history_item(index) |
| 153 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 154 | Return the current contents of history item at *index*. The item index |
| 155 | is one-based. This calls :c:func:`history_get` in the underlying library. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
| 158 | .. function:: remove_history_item(pos) |
| 159 | |
| 160 | Remove history item specified by its position from the history. |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 161 | The position is zero-based. This calls :c:func:`remove_history` in |
| 162 | the underlying library. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | |
| 165 | .. function:: replace_history_item(pos, line) |
| 166 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 167 | Replace history item specified by its position with *line*. |
| 168 | The position is zero-based. This calls :c:func:`replace_history_entry` |
| 169 | in the underlying library. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 171 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 172 | .. function:: add_history(line) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 173 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 174 | Append *line* to the history buffer, as if it was the last line typed. |
| 175 | This calls :c:func:`add_history` in the underlying library. |
| 176 | |
| 177 | |
Martin Panter | f0dbf7a | 2016-05-15 01:26:25 +0000 | [diff] [blame] | 178 | .. function:: set_auto_history(enabled) |
| 179 | |
| 180 | Enable or disable automatic calls to :c:func:`add_history` when reading |
| 181 | input via readline. The *enabled* argument should be a Boolean value |
Serhiy Storchaka | 7d6dda4 | 2016-10-19 18:36:51 +0300 | [diff] [blame] | 182 | that when true, enables auto history, and that when false, disables |
Martin Panter | f0dbf7a | 2016-05-15 01:26:25 +0000 | [diff] [blame] | 183 | auto history. |
| 184 | |
| 185 | .. versionadded:: 3.6 |
| 186 | |
| 187 | .. impl-detail:: |
| 188 | Auto history is enabled by default, and changes to this do not persist |
| 189 | across multiple sessions. |
| 190 | |
| 191 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 192 | Startup hooks |
| 193 | ------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 194 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 195 | |
| 196 | .. function:: set_startup_hook([function]) |
| 197 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 198 | Set or remove the function invoked by the :c:data:`rl_startup_hook` |
| 199 | callback of the underlying library. If *function* is specified, it will |
| 200 | be used as the new hook function; if omitted or ``None``, any function |
| 201 | already installed is removed. The hook is called with no |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | arguments just before readline prints the first prompt. |
| 203 | |
| 204 | |
| 205 | .. function:: set_pre_input_hook([function]) |
| 206 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 207 | Set or remove the function invoked by the :c:data:`rl_pre_input_hook` |
| 208 | callback of the underlying library. If *function* is specified, it will |
| 209 | be used as the new hook function; if omitted or ``None``, any |
| 210 | function already installed is removed. The hook is called |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 211 | with no arguments after the first prompt has been printed and just before |
Martin Panter | 6afbc65 | 2016-06-14 08:45:43 +0000 | [diff] [blame] | 212 | readline starts reading input characters. This function only exists |
| 213 | if Python was compiled for a version of the library that supports it. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 | |
| 215 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 216 | Completion |
| 217 | ---------- |
| 218 | |
| 219 | The following functions relate to implementing a custom word completion |
| 220 | function. This is typically operated by the Tab key, and can suggest and |
| 221 | automatically complete a word being typed. By default, Readline is set up |
| 222 | to be used by :mod:`rlcompleter` to complete Python identifiers for |
| 223 | the interactive interpreter. If the :mod:`readline` module is to be used |
| 224 | with a custom completer, a different set of word delimiters should be set. |
| 225 | |
| 226 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 227 | .. function:: set_completer([function]) |
| 228 | |
| 229 | Set or remove the completer function. If *function* is specified, it will be |
| 230 | used as the new completer function; if omitted or ``None``, any completer |
| 231 | function already installed is removed. The completer function is called as |
| 232 | ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it |
| 233 | returns a non-string value. It should return the next possible completion |
| 234 | starting with *text*. |
| 235 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 236 | The installed completer function is invoked by the *entry_func* callback |
| 237 | passed to :c:func:`rl_completion_matches` in the underlying library. |
| 238 | The *text* string comes from the first parameter to the |
| 239 | :c:data:`rl_attempted_completion_function` callback of the |
| 240 | underlying library. |
| 241 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | |
| 243 | .. function:: get_completer() |
| 244 | |
| 245 | Get the completer function, or ``None`` if no completer function has been set. |
| 246 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 247 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 248 | .. function:: get_completion_type() |
| 249 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 250 | Get the type of completion being attempted. This returns the |
| 251 | :c:data:`rl_completion_type` variable in the underlying library as |
| 252 | an integer. |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 253 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 254 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 | .. function:: get_begidx() |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 256 | get_endidx() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 257 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 258 | Get the beginning or ending index of the completion scope. |
| 259 | These indexes are the *start* and *end* arguments passed to the |
| 260 | :c:data:`rl_attempted_completion_function` callback of the |
| 261 | underlying library. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 262 | |
| 263 | |
| 264 | .. function:: set_completer_delims(string) |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 265 | get_completer_delims() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 266 | |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 267 | Set or get the word delimiters for completion. These determine the |
| 268 | start of the word to be considered for completion (the completion scope). |
| 269 | These functions access the :c:data:`rl_completer_word_break_characters` |
| 270 | variable in the underlying library. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 271 | |
Georg Brandl | 6554cb9 | 2007-12-02 23:15:43 +0000 | [diff] [blame] | 272 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 273 | .. function:: set_completion_display_matches_hook([function]) |
| 274 | |
| 275 | Set or remove the completion display function. If *function* is |
| 276 | specified, it will be used as the new completion display function; |
| 277 | if omitted or ``None``, any completion display function already |
Martin Panter | 0f76739 | 2016-04-05 07:37:22 +0000 | [diff] [blame] | 278 | installed is removed. This sets or clears the |
| 279 | :c:data:`rl_completion_display_matches_hook` callback in the |
| 280 | underlying library. The completion display function is called as |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 281 | ``function(substitution, [matches], longest_match_length)`` once |
| 282 | each time matches need to be displayed. |
| 283 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 285 | .. _readline-example: |
| 286 | |
| 287 | Example |
| 288 | ------- |
| 289 | |
| 290 | The following example demonstrates how to use the :mod:`readline` module's |
| 291 | history reading and writing functions to automatically load and save a history |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 292 | file named :file:`.python_history` from the user's home directory. The code |
| 293 | below would normally be executed automatically during interactive sessions |
| 294 | from the user's :envvar:`PYTHONSTARTUP` file. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 296 | import atexit |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 297 | import os |
Georg Brandl | a102ae3 | 2010-10-06 05:08:32 +0000 | [diff] [blame] | 298 | import readline |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 299 | |
| 300 | histfile = os.path.join(os.path.expanduser("~"), ".python_history") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 301 | try: |
| 302 | readline.read_history_file(histfile) |
Ezio Melotti | 7c018aa | 2016-01-11 23:30:56 +0200 | [diff] [blame] | 303 | # default history len is -1 (infinite), which may grow unruly |
| 304 | readline.set_history_length(1000) |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 305 | except FileNotFoundError: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 306 | pass |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 307 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 308 | atexit.register(readline.write_history_file, histfile) |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 309 | |
| 310 | This code is actually automatically run when Python is run in |
| 311 | :ref:`interactive mode <tut-interactive>` (see :ref:`rlcompleter-config`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 312 | |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 313 | The following example achieves the same goal but supports concurrent interactive |
| 314 | sessions, by only appending the new history. :: |
| 315 | |
| 316 | import atexit |
| 317 | import os |
Berker Peksag | 964ec8b | 2015-11-01 00:55:12 +0300 | [diff] [blame] | 318 | import readline |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 319 | histfile = os.path.join(os.path.expanduser("~"), ".python_history") |
| 320 | |
| 321 | try: |
| 322 | readline.read_history_file(histfile) |
Brad Smith | eeb5ffd | 2017-10-10 17:52:58 -0400 | [diff] [blame] | 323 | h_len = readline.get_current_history_length() |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 324 | except FileNotFoundError: |
| 325 | open(histfile, 'wb').close() |
| 326 | h_len = 0 |
| 327 | |
| 328 | def save(prev_h_len, histfile): |
Brad Smith | eeb5ffd | 2017-10-10 17:52:58 -0400 | [diff] [blame] | 329 | new_h_len = readline.get_current_history_length() |
Ezio Melotti | 7c018aa | 2016-01-11 23:30:56 +0200 | [diff] [blame] | 330 | readline.set_history_length(1000) |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 331 | readline.append_history_file(new_h_len - prev_h_len, histfile) |
| 332 | atexit.register(save, h_len, histfile) |
| 333 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 334 | The following example extends the :class:`code.InteractiveConsole` class to |
| 335 | support history save/restore. :: |
| 336 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | import atexit |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 338 | import code |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 339 | import os |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 340 | import readline |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 341 | |
| 342 | class HistoryConsole(code.InteractiveConsole): |
| 343 | def __init__(self, locals=None, filename="<console>", |
| 344 | histfile=os.path.expanduser("~/.console-history")): |
Georg Brandl | ee8783d | 2009-09-16 16:00:31 +0000 | [diff] [blame] | 345 | code.InteractiveConsole.__init__(self, locals, filename) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | self.init_history(histfile) |
| 347 | |
| 348 | def init_history(self, histfile): |
| 349 | readline.parse_and_bind("tab: complete") |
| 350 | if hasattr(readline, "read_history_file"): |
| 351 | try: |
| 352 | readline.read_history_file(histfile) |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 353 | except FileNotFoundError: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 354 | pass |
| 355 | atexit.register(self.save_history, histfile) |
| 356 | |
| 357 | def save_history(self, histfile): |
Ezio Melotti | 7c018aa | 2016-01-11 23:30:56 +0200 | [diff] [blame] | 358 | readline.set_history_length(1000) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 359 | readline.write_history_file(histfile) |