blob: e0ae72c9aca3f2b94ed37428cc1d26d8253705d7 [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)
61 return self.matches[state]
Guido van Rossuma11cccc1997-10-06 20:19:59 +000062
63 def global_matches(self, text):
Guido van Rossum548703a1998-03-26 22:14:20 +000064 """Compute matches when text is a simple name.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000065
Guido van Rossum548703a1998-03-26 22:14:20 +000066 Return a list of all keywords, built-in functions and names
67 currently defines in __main__ that match.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000068
Guido van Rossum548703a1998-03-26 22:14:20 +000069 """
70 import keyword
71 matches = []
72 n = len(text)
73 for list in [keyword.kwlist,
74 __builtin__.__dict__.keys(),
75 __main__.__dict__.keys()]:
76 for word in list:
77 if word[:n] == text:
78 matches.append(word)
79 return matches
Guido van Rossuma11cccc1997-10-06 20:19:59 +000080
81 def attr_matches(self, text):
Guido van Rossum548703a1998-03-26 22:14:20 +000082 """Compute matches when text contains a dot.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000083
Guido van Rossum548703a1998-03-26 22:14:20 +000084 Assuming the text is of the form NAME.NAME....[NAME], and is
85 evaluabable in the globals of __main__, it will be evaluated
86 and its attributes (as revealed by dir()) are used as possible
87 completions.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000088
Guido van Rossum548703a1998-03-26 22:14:20 +000089 WARNING: this can still invoke arbitrary C code, if an object
90 with a __getattr__ hook is evaluated.
Guido van Rossuma11cccc1997-10-06 20:19:59 +000091
Guido van Rossum548703a1998-03-26 22:14:20 +000092 """
93 import re
94 m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
95 if not m:
96 return
97 expr, attr = m.group(1, 3)
98 words = dir(eval(expr, __main__.__dict__))
99 matches = []
100 n = len(attr)
101 for word in words:
102 if word[:n] == attr:
103 matches.append("%s.%s" % (expr, word))
104 return matches
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000105
106readline.set_completer(Completer().complete)