Mercurial > dotfiles
annotate .elisp/pycomplete.py @ 67:9f8bd97da0d4
hg-svn-rebase-all: improved rebasing so that merges no longer abort rebasing other branches.
author | Augie Fackler <durin42@gmail.com> |
---|---|
date | Wed, 01 Apr 2009 14:50:38 -0500 |
parents | b5d75594b356 |
children | 014e745b2d04 |
rev | line source |
---|---|
19
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
1 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
2 """ |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
3 Python dot expression completion using Pymacs. |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
4 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
5 This almost certainly needs work, but if you add |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
6 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
7 (require 'pycomplete) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
8 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
9 to your .xemacs/init.el file (untried w/ GNU Emacs so far) and have Pymacs |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
10 installed, when you hit M-TAB it will try to complete the dot expression |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
11 before point. For example, given this import at the top of the file: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
12 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
13 import time |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
14 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
15 typing "time.cl" then hitting M-TAB should complete "time.clock". |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
16 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
17 This is unlikely to be done the way Emacs completion ought to be done, but |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
18 it's a start. Perhaps someone with more Emacs mojo can take this stuff and |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
19 do it right. |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
20 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
21 See pycomplete.el for the Emacs Lisp side of things. |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
22 """ |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
23 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
24 import sys |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
25 import os.path |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
26 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
27 try: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
28 x = set |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
29 except NameError: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
30 from sets import Set as set |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
31 else: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
32 del x |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
33 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
34 def get_all_completions(s, imports=None): |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
35 """Return contextual completion of s (string of >= zero chars). |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
36 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
37 If given, imports is a list of import statements to be executed first. |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
38 """ |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
39 locald = {} |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
40 if imports is not None: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
41 for stmt in imports: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
42 try: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
43 exec stmt in globals(), locald |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
44 except TypeError: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
45 raise TypeError, "invalid type: %s" % stmt |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
46 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
47 dots = s.split(".") |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
48 if not s or len(dots) == 1: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
49 keys = set() |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
50 keys.update(locald.keys()) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
51 keys.update(globals().keys()) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
52 import __builtin__ |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
53 keys.update(dir(__builtin__)) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
54 keys = list(keys) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
55 keys.sort() |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
56 if s: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
57 return [k for k in keys if k.startswith(s)] |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
58 else: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
59 return keys |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
60 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
61 sym = None |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
62 for i in range(1, len(dots)): |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
63 s = ".".join(dots[:i]) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
64 try: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
65 sym = eval(s, globals(), locald) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
66 except NameError: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
67 try: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
68 sym = __import__(s, globals(), locald, []) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
69 except ImportError: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
70 return [] |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
71 if sym is not None: |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
72 s = dots[-1] |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
73 return [k for k in dir(sym) if k.startswith(s)] |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
74 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
75 def pycomplete(s, imports=None): |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
76 completions = get_all_completions(s, imports) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
77 dots = s.split(".") |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
78 return os.path.commonprefix([k[len(dots[-1]):] for k in completions]) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
79 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
80 if __name__ == "__main__": |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
81 print "<empty> ->", pycomplete("") |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
82 print "sys.get ->", pycomplete("sys.get") |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
83 print "sy ->", pycomplete("sy") |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
84 print "sy (sys in context) ->", pycomplete("sy", imports=["import sys"]) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
85 print "foo. ->", pycomplete("foo.") |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
86 print "Enc (email * imported) ->", |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
87 print pycomplete("Enc", imports=["from email import *"]) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
88 print "E (email * imported) ->", |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
89 print pycomplete("E", imports=["from email import *"]) |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
90 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
91 print "Enc ->", pycomplete("Enc") |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
92 print "E ->", pycomplete("E") |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
93 |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
94 # Local Variables : |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
95 # pymacs-auto-reload : t |
b5d75594b356
Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff
changeset
|
96 # End : |