Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 8 | **Source code:** :source:`Lib/rlcompleter.py` |
| 9 | |
| 10 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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> |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +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 | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | >>> readline. |
| 29 | |
Antoine Pitrou | 1a6cb30 | 2013-05-04 20:08:35 +0200 | [diff] [blame] | 30 | The :mod:`rlcompleter` module is designed for use with Python's |
| 31 | :ref:`interactive mode <tut-interactive>`. Unless Python is run with the |
| 32 | :option:`-S` option, the module is automatically imported and configured |
| 33 | (see :ref:`rlcompleter-config`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
| 35 | On platforms without :mod:`readline`, the :class:`Completer` class defined by |
| 36 | this module can still be used for custom purposes. |
| 37 | |
| 38 | |
| 39 | .. _completer-objects: |
| 40 | |
| 41 | Completer Objects |
| 42 | ----------------- |
| 43 | |
| 44 | Completer objects have the following method: |
| 45 | |
| 46 | |
| 47 | .. method:: Completer.complete(text, state) |
| 48 | |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 49 | Return the *state*\ th completion for *text*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
| 51 | If called for *text* that doesn't include a period character (``'.'``), it will |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 52 | complete from names currently defined in :mod:`__main__`, :mod:`builtins` and |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | keywords (as defined by the :mod:`keyword` module). |
| 54 | |
| 55 | If called for a dotted name, it will try to evaluate anything without obvious |
| 56 | side-effects (functions will not be evaluated, but it can generate calls to |
| 57 | :meth:`__getattr__`) up to the last part, and find matches for the rest via the |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 58 | :func:`dir` function. Any exception raised during the evaluation of the |
Alexandre Vassalotti | 8ae3e05 | 2008-05-16 00:41:41 +0000 | [diff] [blame] | 59 | expression is caught, silenced and :const:`None` is returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |