annotate .elisp/pycomplete.py @ 175:4741c022c7ae

python-mode: apply patch from the python-mode list to fix triplequotes. This patch is originally by Andreas Roehler <andreas.roehler at online.de>. It was not applied because it works only in GNU Emacs based on list archives.
author Augie Fackler <durin42@gmail.com>
date Wed, 16 Dec 2009 22:59:38 -0600
parents 014e745b2d04
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
174
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
24 # Author: Skip Montanaro
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
25 # Maintainer: skip@pobox.com
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
26 # Created: Oct 2004
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
27 # Keywords: python pymacs emacs
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
28
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
29 # This software is provided as-is, without express or implied warranty.
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
30 # Permission to use, copy, modify, distribute or sell this software, without
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
31 # fee, for any purpose and by any individual or organization, is hereby
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
32 # granted, provided that the above copyright notice and this paragraph
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
33 # appear in all copies.
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
34
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
35 # Along with pycomplete.el this file allows programmers to complete Python
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
36 # symbols within the current buffer.
014e745b2d04 python-mode: updating to current bzr version
Augie Fackler <durin42@gmail.com>
parents: 19
diff changeset
37
19
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
38 import sys
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
39 import os.path
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
40
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
41 try:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
42 x = set
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
43 except NameError:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
44 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
45 else:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
46 del x
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
47
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
48 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
49 """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
50
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
51 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
52 """
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
53 locald = {}
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
54 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
55 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
56 try:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
57 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
58 except TypeError:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
59 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
60
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
61 dots = s.split(".")
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
62 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
63 keys = set()
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
64 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
65 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
66 import __builtin__
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
67 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
68 keys = list(keys)
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
69 keys.sort()
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
70 if s:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
71 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
72 else:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
73 return keys
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 sym = None
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
76 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
77 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
78 try:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
79 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
80 except NameError:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
81 try:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
82 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
83 except ImportError:
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
84 return []
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
85 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
86 s = dots[-1]
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
87 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
88
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
89 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
90 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
91 dots = s.split(".")
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
92 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
93
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
94 if __name__ == "__main__":
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
95 print "<empty> ->", pycomplete("")
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
105 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
106 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
107
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
108 # Local Variables :
b5d75594b356 Add support for the ipython-mode stuff and remove vestigial pymacs code.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
109 # 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
110 # End :