Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 1 | """Word completion for GNU readline 2.0. |
| 2 | |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 3 | This requires the latest extension to the readline module. The completer |
| 4 | completes keywords, built-ins and globals in a selectable namespace (which |
| 5 | defaults to __main__); when completing NAME.NAME..., it evaluates (!) the |
| 6 | expression up to the last dot and completes its attributes. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 7 | |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 8 | It's very cool to do "import sys" type "sys.", hit the |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 9 | completion key (twice), and see the list of names defined by the |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 10 | sys module! |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 31 | - When the original stdin is not a tty device, GNU readline is never |
| 32 | used, and this module (and the readline module) are silently inactive. |
| 33 | |
| 34 | """ |
| 35 | |
| 36 | import readline |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 37 | import __builtin__ |
| 38 | import __main__ |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 39 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 40 | __all__ = ["Completer"] |
| 41 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 42 | class Completer: |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 43 | def __init__(self, namespace = None): |
| 44 | """Create a new completer for the command line. |
| 45 | |
| 46 | Completer([namespace]) -> completer instance. |
| 47 | |
| 48 | If unspecified, the default namespace where completions are performed |
| 49 | is __main__ (technically, __main__.__dict__). Namespaces should be |
| 50 | given as dictionaries. |
| 51 | |
| 52 | Completer instances should be used as the completion mechanism of |
| 53 | readline via the set_completer() call: |
| 54 | |
| 55 | readline.set_completer(Completer(my_namespace).complete) |
| 56 | """ |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 57 | |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 58 | if namespace and not isinstance(namespace, dict): |
| 59 | raise TypeError,'namespace must be a dictionary' |
| 60 | |
| 61 | # Don't bind to namespace quite yet, but flag whether the user wants a |
| 62 | # specific namespace or to use __main__.__dict__. This will allow us |
| 63 | # to bind to __main__.__dict__ at completion time, not now. |
| 64 | if namespace is None: |
| 65 | self.use_main_ns = 1 |
| 66 | else: |
| 67 | self.use_main_ns = 0 |
| 68 | self.namespace = namespace |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 69 | |
| 70 | def complete(self, text, state): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 71 | """Return the next possible completion for 'text'. |
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 | This is called successively with state == 0, 1, 2, ... until it |
| 74 | returns None. The completion should begin with 'text'. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 76 | """ |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 77 | if self.use_main_ns: |
| 78 | self.namespace = __main__.__dict__ |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 80 | if state == 0: |
| 81 | if "." in text: |
| 82 | self.matches = self.attr_matches(text) |
| 83 | else: |
| 84 | self.matches = self.global_matches(text) |
Guido van Rossum | d458faa | 1998-06-12 19:42:14 +0000 | [diff] [blame] | 85 | try: |
| 86 | return self.matches[state] |
| 87 | except IndexError: |
| 88 | return None |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 89 | |
| 90 | def global_matches(self, text): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | """Compute matches when text is a simple name. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 92 | |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 93 | Return a list of all keywords, built-in functions and names currently |
| 94 | defined in self.namespace that match. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 96 | """ |
| 97 | import keyword |
| 98 | matches = [] |
| 99 | n = len(text) |
| 100 | for list in [keyword.kwlist, |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 101 | __builtin__.__dict__, |
| 102 | self.namespace]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 103 | for word in list: |
Fred Drake | 46bd9a6 | 2000-05-31 14:31:00 +0000 | [diff] [blame] | 104 | if word[:n] == text and word != "__builtins__": |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 105 | matches.append(word) |
| 106 | return matches |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 107 | |
| 108 | def attr_matches(self, text): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 109 | """Compute matches when text contains a dot. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 111 | Assuming the text is of the form NAME.NAME....[NAME], and is |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 112 | evaluatable in self.namespace, it will be evaluated and its attributes |
| 113 | (as revealed by dir()) are used as possible completions. (For class |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 114 | instances, class members are also considered.) |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 116 | WARNING: this can still invoke arbitrary C code, if an object |
| 117 | with a __getattr__ hook is evaluated. |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 119 | """ |
| 120 | import re |
| 121 | m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) |
| 122 | if not m: |
| 123 | return |
| 124 | expr, attr = m.group(1, 3) |
Neil Schemenauer | dbab3e3 | 2002-03-23 23:44:51 +0000 | [diff] [blame] | 125 | object = eval(expr, self.namespace) |
Guido van Rossum | 4e20de5 | 1999-10-26 13:09:08 +0000 | [diff] [blame] | 126 | words = dir(object) |
| 127 | if hasattr(object,'__class__'): |
| 128 | words.append('__class__') |
| 129 | words = words + get_class_members(object.__class__) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 130 | matches = [] |
| 131 | n = len(attr) |
| 132 | for word in words: |
Fred Drake | 46bd9a6 | 2000-05-31 14:31:00 +0000 | [diff] [blame] | 133 | if word[:n] == attr and word != "__builtins__": |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 134 | matches.append("%s.%s" % (expr, word)) |
| 135 | return matches |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | 4e20de5 | 1999-10-26 13:09:08 +0000 | [diff] [blame] | 137 | def get_class_members(klass): |
| 138 | ret = dir(klass) |
| 139 | if hasattr(klass,'__bases__'): |
| 140 | for base in klass.__bases__: |
| 141 | ret = ret + get_class_members(base) |
| 142 | return ret |
| 143 | |
Guido van Rossum | 2781fbe | 1997-09-26 22:04:56 +0000 | [diff] [blame] | 144 | readline.set_completer(Completer().complete) |