2007-08-15 11:28:01 -03:00
|
|
|
:mod:`readline` --- GNU readline interface
|
|
|
|
==========================================
|
|
|
|
|
|
|
|
.. module:: readline
|
|
|
|
:platform: Unix
|
|
|
|
:synopsis: GNU readline support for Python.
|
2007-12-08 11:26:16 -04:00
|
|
|
.. sectionauthor:: Skip Montanaro <skip@pobox.com>
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
The :mod:`readline` module defines a number of functions to facilitate
|
|
|
|
completion and reading/writing of history files from the Python interpreter.
|
2016-04-05 04:37:22 -03:00
|
|
|
This module can be used directly, or via the :mod:`rlcompleter` module, which
|
|
|
|
supports completion of Python identifiers at the interactive prompt. Settings
|
2007-08-15 11:28:01 -03:00
|
|
|
made using this module affect the behaviour of both the interpreter's
|
|
|
|
interactive prompt and the prompts offered by the :func:`raw_input` and
|
|
|
|
:func:`input` built-in functions.
|
|
|
|
|
2010-01-04 05:00:11 -04:00
|
|
|
.. note::
|
2009-09-20 11:18:15 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
The underlying Readline library API may be implemented by
|
2009-09-20 11:18:15 -03:00
|
|
|
the ``libedit`` library instead of GNU readline.
|
2016-04-05 04:37:22 -03:00
|
|
|
On MacOS X the :mod:`readline` module detects which library is being used
|
|
|
|
at run time.
|
2009-09-20 11:18:15 -03:00
|
|
|
|
|
|
|
The configuration file for ``libedit`` is different from that
|
2010-10-06 06:32:48 -03:00
|
|
|
of GNU readline. If you programmatically load configuration strings
|
2009-09-20 11:18:15 -03:00
|
|
|
you can check for the text "libedit" in :const:`readline.__doc__`
|
|
|
|
to differentiate between GNU readline and libedit.
|
|
|
|
|
2016-06-09 21:27:46 -03:00
|
|
|
Readline keybindings may be configured via an initialization file, typically
|
|
|
|
``.inputrc`` in your home directory. See `Readline Init File
|
|
|
|
<https://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#SEC9>`_
|
|
|
|
in the GNU Readline manual for information about the format and
|
|
|
|
allowable constructs of that file, and the capabilities of the
|
|
|
|
Readline library in general.
|
|
|
|
|
2009-09-20 11:18:15 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Init file
|
|
|
|
---------
|
|
|
|
|
|
|
|
The following functions relate to the init file and user configuration:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: parse_and_bind(string)
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Execute the init line provided in the *string* argument. This calls
|
|
|
|
:c:func:`rl_parse_and_bind` in the underlying library.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: read_init_file([filename])
|
|
|
|
|
|
|
|
Execute a readline initialization file. The default filename is the last filename
|
|
|
|
used. This calls :c:func:`rl_read_init_file` in the underlying library.
|
|
|
|
|
|
|
|
|
|
|
|
Line buffer
|
|
|
|
-----------
|
|
|
|
|
|
|
|
The following functions operate on the line buffer:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: get_line_buffer()
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Return the current contents of the line buffer (:c:data:`rl_line_buffer`
|
|
|
|
in the underlying library).
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: insert_text(string)
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Insert text into the line buffer at the cursor position. This calls
|
|
|
|
:c:func:`rl_insert_text` in the underlying library, but ignores
|
|
|
|
the return value.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
.. function:: redisplay()
|
|
|
|
|
|
|
|
Change what's displayed on the screen to reflect the current contents of the
|
|
|
|
line buffer. This calls :c:func:`rl_redisplay` in the underlying library.
|
|
|
|
|
|
|
|
|
|
|
|
History file
|
|
|
|
------------
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
The following functions operate on a history file:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: read_history_file([filename])
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Load a readline history file, and append it to the history list.
|
|
|
|
The default filename is :file:`~/.history`. This calls
|
|
|
|
:c:func:`read_history` in the underlying library.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: write_history_file([filename])
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Save the history list to a readline history file, overwriting any
|
|
|
|
existing file. The default filename is :file:`~/.history`. This calls
|
|
|
|
:c:func:`write_history` in the underlying library.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
.. function:: get_history_length()
|
|
|
|
set_history_length(length)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Set or return the desired number of lines to save in the history file.
|
|
|
|
The :func:`write_history_file` function uses this value to truncate
|
|
|
|
the history file, by calling :c:func:`history_truncate_file` in
|
|
|
|
the underlying library. Negative values imply
|
|
|
|
unlimited history file size.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
History list
|
|
|
|
------------
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
The following functions operate on a global history list:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
.. function:: clear_history()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Clear the current history. This calls :c:func:`clear_history` in the
|
|
|
|
underlying library. The Python function only exists if Python was
|
|
|
|
compiled for a version of the library that supports it.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
.. versionadded:: 2.4
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: get_current_history_length()
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Return the number of items currently in the history. (This is different from
|
2007-08-15 11:28:01 -03:00
|
|
|
:func:`get_history_length`, which returns the maximum number of lines that will
|
|
|
|
be written to a history file.)
|
|
|
|
|
|
|
|
.. versionadded:: 2.3
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: get_history_item(index)
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Return the current contents of history item at *index*. The item index
|
|
|
|
is one-based. This calls :c:func:`history_get` in the underlying library.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. versionadded:: 2.3
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: remove_history_item(pos)
|
|
|
|
|
|
|
|
Remove history item specified by its position from the history.
|
2016-04-05 04:37:22 -03:00
|
|
|
The position is zero-based. This calls :c:func:`remove_history` in
|
|
|
|
the underlying library.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. versionadded:: 2.4
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: replace_history_item(pos, line)
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Replace history item specified by its position with *line*.
|
|
|
|
The position is zero-based. This calls :c:func:`replace_history_entry`
|
|
|
|
in the underlying library.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. versionadded:: 2.4
|
|
|
|
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
.. function:: add_history(line)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Append *line* to the history buffer, as if it was the last line typed.
|
|
|
|
This calls :c:func:`add_history` in the underlying library.
|
|
|
|
|
|
|
|
|
|
|
|
Startup hooks
|
|
|
|
-------------
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. versionadded:: 2.3
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: set_startup_hook([function])
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Set or remove the function invoked by the :c:data:`rl_startup_hook`
|
|
|
|
callback of the underlying library. If *function* is specified, it will
|
|
|
|
be used as the new hook function; if omitted or ``None``, any function
|
|
|
|
already installed is removed. The hook is called with no
|
2007-08-15 11:28:01 -03:00
|
|
|
arguments just before readline prints the first prompt.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: set_pre_input_hook([function])
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Set or remove the function invoked by the :c:data:`rl_pre_input_hook`
|
|
|
|
callback of the underlying library. If *function* is specified, it will
|
|
|
|
be used as the new hook function; if omitted or ``None``, any
|
|
|
|
function already installed is removed. The hook is called
|
2007-08-15 11:28:01 -03:00
|
|
|
with no arguments after the first prompt has been printed and just before
|
|
|
|
readline starts reading input characters.
|
|
|
|
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Completion
|
|
|
|
----------
|
|
|
|
|
|
|
|
The following functions relate to implementing a custom word completion
|
|
|
|
function. This is typically operated by the Tab key, and can suggest and
|
|
|
|
automatically complete a word being typed. By default, Readline is set up
|
|
|
|
to be used by :mod:`rlcompleter` to complete Python identifiers for
|
|
|
|
the interactive interpreter. If the :mod:`readline` module is to be used
|
|
|
|
with a custom completer, a different set of word delimiters should be set.
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
.. function:: set_completer([function])
|
|
|
|
|
|
|
|
Set or remove the completer function. If *function* is specified, it will be
|
|
|
|
used as the new completer function; if omitted or ``None``, any completer
|
|
|
|
function already installed is removed. The completer function is called as
|
|
|
|
``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it
|
|
|
|
returns a non-string value. It should return the next possible completion
|
|
|
|
starting with *text*.
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
The installed completer function is invoked by the *entry_func* callback
|
|
|
|
passed to :c:func:`rl_completion_matches` in the underlying library.
|
|
|
|
The *text* string comes from the first parameter to the
|
|
|
|
:c:data:`rl_attempted_completion_function` callback of the
|
|
|
|
underlying library.
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. function:: get_completer()
|
|
|
|
|
|
|
|
Get the completer function, or ``None`` if no completer function has been set.
|
|
|
|
|
|
|
|
.. versionadded:: 2.3
|
|
|
|
|
|
|
|
|
2007-09-04 10:13:14 -03:00
|
|
|
.. function:: get_completion_type()
|
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Get the type of completion being attempted. This returns the
|
|
|
|
:c:data:`rl_completion_type` variable in the underlying library as
|
|
|
|
an integer.
|
2007-09-04 10:13:14 -03:00
|
|
|
|
|
|
|
.. versionadded:: 2.6
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
.. function:: get_begidx()
|
2016-04-05 04:37:22 -03:00
|
|
|
get_endidx()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Get the beginning or ending index of the completion scope.
|
|
|
|
These indexes are the *start* and *end* arguments passed to the
|
|
|
|
:c:data:`rl_attempted_completion_function` callback of the
|
|
|
|
underlying library.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: set_completer_delims(string)
|
2016-04-05 04:37:22 -03:00
|
|
|
get_completer_delims()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2016-04-05 04:37:22 -03:00
|
|
|
Set or get the word delimiters for completion. These determine the
|
|
|
|
start of the word to be considered for completion (the completion scope).
|
|
|
|
These functions access the :c:data:`rl_completer_word_break_characters`
|
|
|
|
variable in the underlying library.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2007-09-04 10:13:14 -03:00
|
|
|
.. function:: set_completion_display_matches_hook([function])
|
|
|
|
|
|
|
|
Set or remove the completion display function. If *function* is
|
|
|
|
specified, it will be used as the new completion display function;
|
|
|
|
if omitted or ``None``, any completion display function already
|
2016-04-05 04:37:22 -03:00
|
|
|
installed is removed. This sets or clears the
|
|
|
|
:c:data:`rl_completion_display_matches_hook` callback in the
|
|
|
|
underlying library. The completion display function is called as
|
2007-09-04 10:13:14 -03:00
|
|
|
``function(substitution, [matches], longest_match_length)`` once
|
|
|
|
each time matches need to be displayed.
|
|
|
|
|
|
|
|
.. versionadded:: 2.6
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. _readline-example:
|
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
The following example demonstrates how to use the :mod:`readline` module's
|
|
|
|
history reading and writing functions to automatically load and save a history
|
|
|
|
file named :file:`.pyhist` from the user's home directory. The code below would
|
|
|
|
normally be executed automatically during interactive sessions from the user's
|
|
|
|
:envvar:`PYTHONSTARTUP` file. ::
|
|
|
|
|
|
|
|
import os
|
2010-10-06 06:33:55 -03:00
|
|
|
import readline
|
2011-03-25 21:24:47 -03:00
|
|
|
histfile = os.path.join(os.path.expanduser("~"), ".pyhist")
|
2007-08-15 11:28:01 -03:00
|
|
|
try:
|
|
|
|
readline.read_history_file(histfile)
|
2016-01-11 17:30:15 -04:00
|
|
|
# default history len is -1 (infinite), which may grow unruly
|
|
|
|
readline.set_history_length(1000)
|
2007-08-15 11:28:01 -03:00
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
import atexit
|
|
|
|
atexit.register(readline.write_history_file, histfile)
|
|
|
|
del os, histfile
|
|
|
|
|
|
|
|
The following example extends the :class:`code.InteractiveConsole` class to
|
|
|
|
support history save/restore. ::
|
|
|
|
|
|
|
|
import code
|
|
|
|
import readline
|
|
|
|
import atexit
|
|
|
|
import os
|
|
|
|
|
|
|
|
class HistoryConsole(code.InteractiveConsole):
|
|
|
|
def __init__(self, locals=None, filename="<console>",
|
|
|
|
histfile=os.path.expanduser("~/.console-history")):
|
2009-09-16 06:24:57 -03:00
|
|
|
code.InteractiveConsole.__init__(self, locals, filename)
|
2007-08-15 11:28:01 -03:00
|
|
|
self.init_history(histfile)
|
|
|
|
|
|
|
|
def init_history(self, histfile):
|
|
|
|
readline.parse_and_bind("tab: complete")
|
|
|
|
if hasattr(readline, "read_history_file"):
|
|
|
|
try:
|
|
|
|
readline.read_history_file(histfile)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
atexit.register(self.save_history, histfile)
|
|
|
|
|
|
|
|
def save_history(self, histfile):
|
2016-01-11 17:30:15 -04:00
|
|
|
readline.set_history_length(1000)
|
2007-08-15 11:28:01 -03:00
|
|
|
readline.write_history_file(histfile)
|
|
|
|
|