blob: 633088d897645f34bf1ccf48618c8e827a52fe07 [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
Raymond Hettinger469271d2011-01-27 20:38:46 +00008**Source code:** :source:`Lib/rlcompleter.py`
9
10--------------
Georg Brandl116aa622007-08-15 14:28:22 +000011
12The :mod:`rlcompleter` module defines a completion function suitable for the
13:mod:`readline` module by completing valid Python identifiers and keywords.
14
15When this module is imported on a Unix platform with the :mod:`readline` module
16available, an instance of the :class:`Completer` class is automatically created
17and its :meth:`complete` method is set as the :mod:`readline` completer.
18
19Example::
20
21 >>> import rlcompleter
22 >>> import readline
23 >>> readline.parse_and_bind("tab: complete")
24 >>> readline. <TAB PRESSED>
Benjamin Peterson41181742008-07-02 20:22:54 +000025 readline.__doc__ readline.get_line_buffer( readline.read_init_file(
26 readline.__file__ readline.insert_text( readline.set_completer(
27 readline.__name__ readline.parse_and_bind(
Georg Brandl116aa622007-08-15 14:28:22 +000028 >>> readline.
29
30The :mod:`rlcompleter` module is designed for use with Python's interactive
31mode. A user can add the following lines to his or her initialization file
32(identified by the :envvar:`PYTHONSTARTUP` environment variable) to get
33automatic :kbd:`Tab` completion::
34
35 try:
36 import readline
37 except ImportError:
Georg Brandl6911e3c2007-09-04 07:15:32 +000038 print("Module readline not available.")
Georg Brandl116aa622007-08-15 14:28:22 +000039 else:
40 import rlcompleter
41 readline.parse_and_bind("tab: complete")
42
43On platforms without :mod:`readline`, the :class:`Completer` class defined by
44this module can still be used for custom purposes.
45
46
47.. _completer-objects:
48
49Completer Objects
50-----------------
51
52Completer objects have the following method:
53
54
55.. method:: Completer.complete(text, state)
56
Benjamin Petersonfa0d7032009-06-01 22:42:33 +000057 Return the *state*\ th completion for *text*.
Georg Brandl116aa622007-08-15 14:28:22 +000058
59 If called for *text* that doesn't include a period character (``'.'``), it will
Georg Brandl1a3284e2007-12-02 09:40:06 +000060 complete from names currently defined in :mod:`__main__`, :mod:`builtins` and
Georg Brandl116aa622007-08-15 14:28:22 +000061 keywords (as defined by the :mod:`keyword` module).
62
63 If called for a dotted name, it will try to evaluate anything without obvious
64 side-effects (functions will not be evaluated, but it can generate calls to
65 :meth:`__getattr__`) up to the last part, and find matches for the rest via the
Georg Brandl48310cd2009-01-03 21:18:54 +000066 :func:`dir` function. Any exception raised during the evaluation of the
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +000067 expression is caught, silenced and :const:`None` is returned.
Georg Brandl116aa622007-08-15 14:28:22 +000068