blob: 285faedb72b25fc09022f8835e3ef5bee4d1567a [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
44import keyword
45import __builtin__
46import __main__
47import string
48import re
49import traceback
50
51class Completer:
52
53 def complete(self, text, state):
54 """Return the next possible completion for 'text'.
55
56 This is called successively with state == 0, 1, 2, ... until it
57 returns None. The completion should begin with 'text'.
58
59 """
60 if state == 0:
61 if "." in text:
62 self.matches = self.attr_matches(text)
63 else:
64 self.matches = self.global_matches(text)
65 return self.matches[state]
66
67 def global_matches(self, text):
68 """Compute matches when text is a simple name.
69
70 Return a list of all keywords, built-in functions and names
71 currently defines in __main__ that match.
72
73 """
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
83
84 def attr_matches(self, text):
85 """Compute matches when text contains a dot.
86
87 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.
91
92 WARNING: this can still invoke arbitrary C code, if an object
93 with a __getattr__ hook is evaluated.
94
95 """
96 m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
97 if not m:
98 return
99 expr, attr = m.group(1, 3)
100 words = dir(eval(expr, __main__.__dict__))
101 matches = []
102 n = len(attr)
103 for word in words:
104 if word[:n] == attr:
105 matches.append("%s.%s" % (expr, word))
106 return matches
107
108readline.set_completer(Completer().complete)