3
Python dot expression completion using Pymacs.
5
This almost certainly needs work, but if you add
9
to your .xemacs/init.el file (untried w/ GNU Emacs so far) and have Pymacs
10
installed, when you hit M-TAB it will try to complete the dot expression
11
before point. For example, given this import at the top of the file:
15
typing "time.cl" then hitting M-TAB should complete "time.clock".
17
This is unlikely to be done the way Emacs completion ought to be done, but
18
it's a start. Perhaps someone with more Emacs mojo can take this stuff and
21
See pycomplete.el for the Emacs Lisp side of things.
30
from sets import Set as set
34
def get_all_completions(s, imports=None):
35
"""Return contextual completion of s (string of >= zero chars).
37
If given, imports is a list of import statements to be executed first.
40
if imports is not None:
43
exec stmt in globals(), locald
45
raise TypeError, "invalid type: %s" % stmt
48
if not s or len(dots) == 1:
50
keys.update(locald.keys())
51
keys.update(globals().keys())
53
keys.update(dir(__builtin__))
57
return [k for k in keys if k.startswith(s)]
62
for i in range(1, len(dots)):
63
s = ".".join(dots[:i])
65
sym = eval(s, globals(), locald)
68
sym = __import__(s, globals(), locald, [])
73
return [k for k in dir(sym) if k.startswith(s)]
75
def pycomplete(s, imports=None):
76
completions = get_all_completions(s, imports)
78
return os.path.commonprefix([k[len(dots[-1]):] for k in completions])
80
if __name__ == "__main__":
81
print "<empty> ->", pycomplete("")
82
print "sys.get ->", pycomplete("sys.get")
83
print "sy ->", pycomplete("sy")
84
print "sy (sys in context) ->", pycomplete("sy", imports=["import sys"])
85
print "foo. ->", pycomplete("foo.")
86
print "Enc (email * imported) ->",
87
print pycomplete("Enc", imports=["from email import *"])
88
print "E (email * imported) ->",
89
print pycomplete("E", imports=["from email import *"])
91
print "Enc ->", pycomplete("Enc")
92
print "E ->", pycomplete("E")
95
# pymacs-auto-reload : t