blob: 92633abbc2fbcecb2c0981edb95ce7b4878b9300 [file] [log] [blame]
Guido van Rossuma11cccc1997-10-06 20:19:59 +00001"""Word completion for GNU readline 2.0.
2
3This requires the latest extension to the readline module (the
4set_completer() function). When completing a simple identifier, it
5completes keywords, built-ins and globals in __main__; when completing
6NAME.NAME..., it evaluates (!) the expression up to the last dot and
7completes its attributes.
8
9It's very cool to do "import string" type "string.", hit the
10completion key (twice), and see the list of names defined by the
11string module!
12
13Tip: to use the tab key as the completion key, call
14
15 readline.parse_and_bind("tab: complete")
16
17Notes:
18
19- Exceptions raised by the completer function are *ignored* (and
20generally cause the completion to fail). This is a feature -- since
21readline sets the tty device in raw (or cbreak) mode, printing a
22traceback wouldn't work well without some complicated hoopla to save,
23reset and restore the tty state.
24
25- The evaluation of the NAME.NAME... form may cause arbitrary
26application defined code to be executed if an object with a
27__getattr__ hook is found. Since it is the responsibility of the
28application (or the user) to enable this feature, I consider this an
29acceptable risk. More complicated expressions (e.g. function calls or
30indexing operations) are *not* evaluated.
31
32- GNU readline is also used by the built-in functions input() and
33raw_input(), and thus these also benefit/suffer from the completer
34features. Clearly an interactive application can benefit by
35specifying its own completer function and using raw_input() for all
36its input.
37
38- When the original stdin is not a tty device, GNU readline is never
39used, and this module (and the readline module) are silently inactive.
40
41"""
42
43import readline
Guido van Rossuma11cccc1997-10-06 20:19:59 +000044import __builtin__
45import __main__
Guido van Rossuma11cccc1997-10-06 20:19:59 +000046
47class Completer:
48
49 def complete(self, text, state):
Guido van Rossum548703a1998-03-26 22:14:20 +000050 """Return the next possible completion for 'text'.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000051
Guido van Rossum548703a1998-03-26 22:14:20 +000052 This is called successively with state == 0, 1, 2, ... until it
53 returns None. The completion should begin with 'text'.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000054
Guido van Rossum548703a1998-03-26 22:14:20 +000055 """
56 if state == 0:
57 if "." in text:
58 self.matches = self.attr_matches(text)
59 else:
60 self.matches = self.global_matches(text)
Guido van Rossume03c0501998-08-12 02:38:11 +000061 try:
62 return self.matches[state]
63 except IndexError:
64 return None
Guido van Rossuma11cccc1997-10-06 20:19:59 +000065
66 def global_matches(self, text):
Guido van Rossum548703a1998-03-26 22:14:20 +000067 """Compute matches when text is a simple name.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000068
Guido van Rossum548703a1998-03-26 22:14:20 +000069 Return a list of all keywords, built-in functions and names
70 currently defines in __main__ that match.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000071
Guido van Rossum548703a1998-03-26 22:14:20 +000072 """
73 import keyword
74 matches = []
75 n = len(text)
76 for list in [keyword.kwlist,
77 __builtin__.__dict__.keys(),
78 __main__.__dict__.keys()]:
79 for word in list:
80 if word[:n] == text:
81 matches.append(word)
82 return matches
Guido van Rossuma11cccc1997-10-06 20:19:59 +000083
84 def attr_matches(self, text):
Guido van Rossum548703a1998-03-26 22:14:20 +000085 """Compute matches when text contains a dot.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000086
Guido van Rossum548703a1998-03-26 22:14:20 +000087 Assuming the text is of the form NAME.NAME....[NAME], and is
88 evaluabable in the globals of __main__, it will be evaluated
89 and its attributes (as revealed by dir()) are used as possible
90 completions.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000091
Guido van Rossum548703a1998-03-26 22:14:20 +000092 WARNING: this can still invoke arbitrary C code, if an object
93 with a __getattr__ hook is evaluated.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000094
Guido van Rossum548703a1998-03-26 22:14:20 +000095 """
96 import re
97 m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
98 if not m:
99 return
100 expr, attr = m.group(1, 3)
101 words = dir(eval(expr, __main__.__dict__))
102 matches = []
103 n = len(attr)
104 for word in words:
105 if word[:n] == attr:
106 matches.append("%s.%s" % (expr, word))
107 return matches
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000108
109readline.set_completer(Completer().complete)