Fred Drake | 4755e7d | 1999-06-21 18:25:49 +0000 | [diff] [blame] | 1 | \section{\module{rlcompleter} --- |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 2 | Completion function for GNU readline} |
Fred Drake | 4755e7d | 1999-06-21 18:25:49 +0000 | [diff] [blame] | 3 | |
| 4 | \declaremodule{standard}{rlcompleter} |
Fred Drake | 57657bc | 2000-12-01 15:25:23 +0000 | [diff] [blame] | 5 | \sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il} |
Georg Brandl | 3583cff | 2006-04-30 18:14:54 +0000 | [diff] [blame] | 6 | \modulesynopsis{Python identifier completion, suitable for the GNU readline library.} |
Fred Drake | 4755e7d | 1999-06-21 18:25:49 +0000 | [diff] [blame] | 7 | |
Georg Brandl | 3583cff | 2006-04-30 18:14:54 +0000 | [diff] [blame] | 8 | The \module{rlcompleter} module defines a completion function suitable for |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 9 | the \refmodule{readline} module by completing valid Python identifiers |
| 10 | and keywords. |
| 11 | |
Georg Brandl | 3583cff | 2006-04-30 18:14:54 +0000 | [diff] [blame] | 12 | When this module is imported on a \UNIX\ platform with the \module{readline} |
| 13 | module available, an instance of the \class{Completer} class is automatically |
| 14 | created and its \method{complete} method is set as the \module{readline} |
| 15 | completer. |
Fred Drake | 4755e7d | 1999-06-21 18:25:49 +0000 | [diff] [blame] | 16 | |
| 17 | Example: |
| 18 | |
| 19 | \begin{verbatim} |
| 20 | >>> import rlcompleter |
| 21 | >>> import readline |
| 22 | >>> readline.parse_and_bind("tab: complete") |
| 23 | >>> readline. <TAB PRESSED> |
| 24 | readline.__doc__ readline.get_line_buffer readline.read_init_file |
| 25 | readline.__file__ readline.insert_text readline.set_completer |
| 26 | readline.__name__ readline.parse_and_bind |
| 27 | >>> readline. |
| 28 | \end{verbatim} |
| 29 | |
| 30 | The \module{rlcompleter} module is designed for use with Python's |
| 31 | interactive mode. A user can add the following lines to his or her |
| 32 | initialization file (identified by the \envvar{PYTHONSTARTUP} |
| 33 | environment variable) to get automatic \kbd{Tab} completion: |
| 34 | |
| 35 | \begin{verbatim} |
| 36 | try: |
| 37 | import readline |
| 38 | except ImportError: |
| 39 | print "Module readline not available." |
| 40 | else: |
| 41 | import rlcompleter |
| 42 | readline.parse_and_bind("tab: complete") |
| 43 | \end{verbatim} |
| 44 | |
| 45 | |
Georg Brandl | 3583cff | 2006-04-30 18:14:54 +0000 | [diff] [blame] | 46 | On platforms without \module{readline}, the \class{Completer} class defined |
| 47 | by this module can still be used for custom purposes. |
| 48 | |
Fred Drake | 4755e7d | 1999-06-21 18:25:49 +0000 | [diff] [blame] | 49 | \subsection{Completer Objects \label{completer-objects}} |
| 50 | |
| 51 | Completer objects have the following method: |
| 52 | |
| 53 | \begin{methoddesc}[Completer]{complete}{text, state} |
| 54 | Return the \var{state}th completion for \var{text}. |
| 55 | |
Fred Drake | 3c62d9e | 2000-07-06 04:51:04 +0000 | [diff] [blame] | 56 | If called for \var{text} that doesn't include a period character |
Fred Drake | 4755e7d | 1999-06-21 18:25:49 +0000 | [diff] [blame] | 57 | (\character{.}), it will complete from names currently defined in |
Fred Drake | ce20112 | 1999-08-02 14:13:09 +0000 | [diff] [blame] | 58 | \refmodule[main]{__main__}, \refmodule[builtin]{__builtin__} and |
| 59 | keywords (as defined by the \refmodule{keyword} module). |
Fred Drake | 4755e7d | 1999-06-21 18:25:49 +0000 | [diff] [blame] | 60 | |
| 61 | If called for a dotted name, it will try to evaluate anything without |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 62 | obvious side-effects (functions will not be evaluated, but it |
Andrew M. Kuchling | 44c62ef | 2003-04-14 15:32:18 +0000 | [diff] [blame] | 63 | can generate calls to \method{__getattr__()}) up to the last part, and |
Fred Drake | 4755e7d | 1999-06-21 18:25:49 +0000 | [diff] [blame] | 64 | find matches for the rest via the \function{dir()} function. |
| 65 | \end{methoddesc} |