comparison .elisp/pycomplete.py @ 19:b5d75594b356

Add support for the ipython-mode stuff and remove vestigial pymacs code.
author Augie Fackler <durin42@gmail.com>
date Mon, 08 Dec 2008 10:58:06 -0600
parents
children 014e745b2d04
comparison
equal deleted inserted replaced
18:30467b2328cb 19:b5d75594b356
1
2 """
3 Python dot expression completion using Pymacs.
4
5 This almost certainly needs work, but if you add
6
7 (require 'pycomplete)
8
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:
12
13 import time
14
15 typing "time.cl" then hitting M-TAB should complete "time.clock".
16
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
19 do it right.
20
21 See pycomplete.el for the Emacs Lisp side of things.
22 """
23
24 import sys
25 import os.path
26
27 try:
28 x = set
29 except NameError:
30 from sets import Set as set
31 else:
32 del x
33
34 def get_all_completions(s, imports=None):
35 """Return contextual completion of s (string of >= zero chars).
36
37 If given, imports is a list of import statements to be executed first.
38 """
39 locald = {}
40 if imports is not None:
41 for stmt in imports:
42 try:
43 exec stmt in globals(), locald
44 except TypeError:
45 raise TypeError, "invalid type: %s" % stmt
46
47 dots = s.split(".")
48 if not s or len(dots) == 1:
49 keys = set()
50 keys.update(locald.keys())
51 keys.update(globals().keys())
52 import __builtin__
53 keys.update(dir(__builtin__))
54 keys = list(keys)
55 keys.sort()
56 if s:
57 return [k for k in keys if k.startswith(s)]
58 else:
59 return keys
60
61 sym = None
62 for i in range(1, len(dots)):
63 s = ".".join(dots[:i])
64 try:
65 sym = eval(s, globals(), locald)
66 except NameError:
67 try:
68 sym = __import__(s, globals(), locald, [])
69 except ImportError:
70 return []
71 if sym is not None:
72 s = dots[-1]
73 return [k for k in dir(sym) if k.startswith(s)]
74
75 def pycomplete(s, imports=None):
76 completions = get_all_completions(s, imports)
77 dots = s.split(".")
78 return os.path.commonprefix([k[len(dots[-1]):] for k in completions])
79
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 *"])
90
91 print "Enc ->", pycomplete("Enc")
92 print "E ->", pycomplete("E")
93
94 # Local Variables :
95 # pymacs-auto-reload : t
96 # End :