Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :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 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 8 | **Source code:** :source:`Lib/rlcompleter.py` |
| 9 | |
| 10 | -------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 11 | |
| 12 | The :mod:`rlcompleter` module defines a completion function suitable for the |
| 13 | :mod:`readline` module by completing valid Python identifiers and keywords. |
| 14 | |
| 15 | When this module is imported on a Unix platform with the :mod:`readline` module |
| 16 | available, an instance of the :class:`Completer` class is automatically created |
| 17 | and its :meth:`complete` method is set as the :mod:`readline` completer. |
| 18 | |
| 19 | Example:: |
| 20 | |
| 21 | >>> import rlcompleter |
| 22 | >>> import readline |
| 23 | >>> readline.parse_and_bind("tab: complete") |
| 24 | >>> readline. <TAB PRESSED> |
Facundo Batista | 66c5277 | 2008-07-02 16:52:55 +0000 | [diff] [blame] | 25 | 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 Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 28 | >>> readline. |
| 29 | |
| 30 | The :mod:`rlcompleter` module is designed for use with Python's interactive |
| 31 | mode. A user can add the following lines to his or her initialization file |
| 32 | (identified by the :envvar:`PYTHONSTARTUP` environment variable) to get |
| 33 | automatic :kbd:`Tab` completion:: |
| 34 | |
| 35 | try: |
| 36 | import readline |
| 37 | except ImportError: |
| 38 | print "Module readline not available." |
| 39 | else: |
| 40 | import rlcompleter |
| 41 | readline.parse_and_bind("tab: complete") |
| 42 | |
| 43 | On platforms without :mod:`readline`, the :class:`Completer` class defined by |
| 44 | this module can still be used for custom purposes. |
| 45 | |
| 46 | |
| 47 | .. _completer-objects: |
| 48 | |
| 49 | Completer Objects |
| 50 | ----------------- |
| 51 | |
| 52 | Completer objects have the following method: |
| 53 | |
| 54 | |
| 55 | .. method:: Completer.complete(text, state) |
| 56 | |
Georg Brandl | ccbb47b | 2009-05-30 07:26:04 +0000 | [diff] [blame] | 57 | Return the *state*\ th completion for *text*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 58 | |
| 59 | If called for *text* that doesn't include a period character (``'.'``), it will |
| 60 | complete from names currently defined in :mod:`__main__`, :mod:`__builtin__` and |
| 61 | 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 Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 66 | :func:`dir` function. Any exception raised during the evaluation of the |
Georg Brandl | 627a666 | 2008-05-11 21:03:42 +0000 | [diff] [blame] | 67 | expression is caught, silenced and :const:`None` is returned. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 68 | |