blob: 48ca50ebaaff500b8da0cbb07b36e989e068a6d8 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`rlcompleter` --- Completion function for GNU readline
2===========================================================
3
4.. module:: rlcompleter
5 :synopsis: Python identifier completion, suitable for the GNU readline library.
6.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
7
8
9The :mod:`rlcompleter` module defines a completion function suitable for the
10:mod:`readline` module by completing valid Python identifiers and keywords.
11
12When this module is imported on a Unix platform with the :mod:`readline` module
13available, an instance of the :class:`Completer` class is automatically created
14and its :meth:`complete` method is set as the :mod:`readline` completer.
15
16Example::
17
18 >>> import rlcompleter
19 >>> import readline
20 >>> readline.parse_and_bind("tab: complete")
21 >>> readline. <TAB PRESSED>
Benjamin Peterson41181742008-07-02 20:22:54 +000022 readline.__doc__ readline.get_line_buffer( readline.read_init_file(
23 readline.__file__ readline.insert_text( readline.set_completer(
24 readline.__name__ readline.parse_and_bind(
Georg Brandl116aa622007-08-15 14:28:22 +000025 >>> readline.
26
27The :mod:`rlcompleter` module is designed for use with Python's interactive
28mode. A user can add the following lines to his or her initialization file
29(identified by the :envvar:`PYTHONSTARTUP` environment variable) to get
30automatic :kbd:`Tab` completion::
31
32 try:
33 import readline
34 except ImportError:
Georg Brandl6911e3c2007-09-04 07:15:32 +000035 print("Module readline not available.")
Georg Brandl116aa622007-08-15 14:28:22 +000036 else:
37 import rlcompleter
38 readline.parse_and_bind("tab: complete")
39
40On platforms without :mod:`readline`, the :class:`Completer` class defined by
41this module can still be used for custom purposes.
42
43
44.. _completer-objects:
45
46Completer Objects
47-----------------
48
49Completer objects have the following method:
50
51
52.. method:: Completer.complete(text, state)
53
Benjamin Petersonfa0d7032009-06-01 22:42:33 +000054 Return the *state*\ th completion for *text*.
Georg Brandl116aa622007-08-15 14:28:22 +000055
56 If called for *text* that doesn't include a period character (``'.'``), it will
Georg Brandl1a3284e2007-12-02 09:40:06 +000057 complete from names currently defined in :mod:`__main__`, :mod:`builtins` and
Georg Brandl116aa622007-08-15 14:28:22 +000058 keywords (as defined by the :mod:`keyword` module).
59
60 If called for a dotted name, it will try to evaluate anything without obvious
61 side-effects (functions will not be evaluated, but it can generate calls to
62 :meth:`__getattr__`) up to the last part, and find matches for the rest via the
Georg Brandl48310cd2009-01-03 21:18:54 +000063 :func:`dir` function. Any exception raised during the evaluation of the
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000064 expression is caught, silenced and :const:`None` is returned.
Georg Brandl116aa622007-08-15 14:28:22 +000065