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