Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 1 | """Word completion for GNU readline 2.0. |
| 2 | |
| 3 | This requires the latest extension to the readline module (the |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 4 | completes keywords, built-ins and globals in __main__; when completing |
| 5 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
| 6 | completes its attributes. |
| 7 | |
| 8 | It's very cool to do "import string" type "string.", hit the |
| 9 | completion key (twice), and see the list of names defined by the |
| 10 | string module! |
| 11 | |
| 12 | Tip: to use the tab key as the completion key, call |
| 13 | |
| 14 | readline.parse_and_bind("tab: complete") |
| 15 | |
| 16 | Notes: |
| 17 | |
| 18 | - Exceptions raised by the completer function are *ignored* (and |
| 19 | generally cause the completion to fail). This is a feature -- since |
| 20 | readline sets the tty device in raw (or cbreak) mode, printing a |
| 21 | traceback wouldn't work well without some complicated hoopla to save, |
| 22 | reset and restore the tty state. |
| 23 | |
| 24 | - The evaluation of the NAME.NAME... form may cause arbitrary |
| 25 | application defined code to be executed if an object with a |
| 26 | __getattr__ hook is found. Since it is the responsibility of the |
| 27 | application (or the user) to enable this feature, I consider this an |
| 28 | acceptable risk. More complicated expressions (e.g. function calls or |
| 29 | indexing operations) are *not* evaluated. |
| 30 | |
| 31 | - GNU readline is also used by the built-in functions input() and |
| 32 | raw_input(), and thus these also benefit/suffer from the completer |
| 33 | features. Clearly an interactive application can benefit by |
| 34 | specifying its own completer function and using raw_input() for all |
| 35 | its input. |
| 36 | |
| 37 | - When the original stdin is not a tty device, GNU readline is never |
| 38 | used, and this module (and the readline module) are silently inactive. |
| 39 | |
| 40 | """ |
| 41 | |
| 42 | import readline |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 43 | import __builtin__ |
| 44 | import __main__ |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 45 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 46 | __all__ = ["Completer"] |
| 47 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 48 | class Completer: |
| 49 | |
| 50 | def complete(self, text, state): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 51 | """Return the next possible completion for 'text'. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 53 | This is called successively with state == 0, 1, 2, ... until it |
| 54 | returns None. The completion should begin with 'text'. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 56 | """ |
| 57 | if state == 0: |
| 58 | if "." in text: |
| 59 | self.matches = self.attr_matches(text) |
| 60 | else: |
| 61 | self.matches = self.global_matches(text) |
Guido van Rossum | d458faa | 1998-06-12 19:42:14 +0000 | [diff] [blame] | 62 | try: |
| 63 | return self.matches[state] |
| 64 | except IndexError: |
| 65 | return None |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 66 | |
| 67 | def global_matches(self, text): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 68 | """Compute matches when text is a simple name. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 70 | Return a list of all keywords, built-in functions and names |
| 71 | currently defines in __main__ that match. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 73 | """ |
| 74 | import keyword |
| 75 | matches = [] |
| 76 | n = len(text) |
| 77 | for list in [keyword.kwlist, |
| 78 | __builtin__.__dict__.keys(), |
| 79 | __main__.__dict__.keys()]: |
| 80 | for word in list: |
Fred Drake | 46bd9a6 | 2000-05-31 14:31:00 +0000 | [diff] [blame] | 81 | if word[:n] == text and word != "__builtins__": |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 82 | matches.append(word) |
| 83 | return matches |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 84 | |
| 85 | def attr_matches(self, text): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 86 | """Compute matches when text contains a dot. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 88 | Assuming the text is of the form NAME.NAME....[NAME], and is |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 89 | evaluatable in the globals of __main__, it will be evaluated |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 90 | and its attributes (as revealed by dir()) are used as possible |
Guido van Rossum | 4e20de5 | 1999-10-26 13:09:08 +0000 | [diff] [blame] | 91 | completions. (For class instances, class members are are also |
| 92 | considered.) |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 94 | WARNING: this can still invoke arbitrary C code, if an object |
| 95 | with a __getattr__ hook is evaluated. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 97 | """ |
| 98 | import re |
| 99 | m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) |
| 100 | if not m: |
| 101 | return |
| 102 | expr, attr = m.group(1, 3) |
Guido van Rossum | 4e20de5 | 1999-10-26 13:09:08 +0000 | [diff] [blame] | 103 | object = eval(expr, __main__.__dict__) |
| 104 | words = dir(object) |
| 105 | if hasattr(object,'__class__'): |
| 106 | words.append('__class__') |
| 107 | words = words + get_class_members(object.__class__) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 108 | matches = [] |
| 109 | n = len(attr) |
| 110 | for word in words: |
Fred Drake | 46bd9a6 | 2000-05-31 14:31:00 +0000 | [diff] [blame] | 111 | if word[:n] == attr and word != "__builtins__": |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 112 | matches.append("%s.%s" % (expr, word)) |
| 113 | return matches |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 4e20de5 | 1999-10-26 13:09:08 +0000 | [diff] [blame] | 115 | def get_class_members(klass): |
| 116 | ret = dir(klass) |
| 117 | if hasattr(klass,'__bases__'): |
| 118 | for base in klass.__bases__: |
| 119 | ret = ret + get_class_members(base) |
| 120 | return ret |
| 121 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 122 | readline.set_completer(Completer().complete) |