annotate etc/emacs3.py @ 84967:90cae4d53a9c

(server-delete-client): Only delete the terminal if it is non-nil.
author Dan Nicolaescu <dann@ics.uci.edu>
date Fri, 28 Sep 2007 16:55:00 +0000
parents 69c173eabff4
children 66dd209de97f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84822
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
1 """
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
2 Warning: This file is automatically generated from emacs2.py with the
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
3 2to3 script. Do not hand edit.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
4 """
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
5
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
6 """Definitions used by commands sent to inferior Python in python.el."""
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
7
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
8 # Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
9 # Author: Dave Love <fx@gnu.org>
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
10
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
11 # This file is part of GNU Emacs.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
12
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
13 # GNU Emacs is free software; you can redistribute it and/or modify
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
14 # it under the terms of the GNU General Public License as published by
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
15 # the Free Software Foundation; either version 3, or (at your option)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
16 # any later version.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
17
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
18 # GNU Emacs is distributed in the hope that it will be useful,
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
21 # GNU General Public License for more details.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
22
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
23 # You should have received a copy of the GNU General Public License
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
24 # along with GNU Emacs; see the file COPYING. If not, write to the
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
25 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
26 # Boston, MA 02110-1301, USA.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
27
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
28 import os, sys, traceback, inspect, __main__
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
29
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
30 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
31 set
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
32 except:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
33 from sets import Set as set
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
34
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
35 __all__ = ["eexecfile", "eargs", "complete", "ehelp", "eimport", "modpath"]
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
36
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
37 def format_exception (filename, should_remove_self):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
38 type, value, tb = sys.exc_info ()
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
39 sys.last_type = type
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
40 sys.last_value = value
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
41 sys.last_traceback = tb
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
42 if type is SyntaxError:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
43 try: # parse the error message
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
44 msg, (dummy_filename, lineno, offset, line) = value
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
45 except:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
46 pass # Not the format we expect; leave it alone
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
47 else:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
48 # Stuff in the right filename
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
49 value = SyntaxError(msg, (filename, lineno, offset, line))
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
50 sys.last_value = value
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
51 res = traceback.format_exception_only (type, value)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
52 # There are some compilation errors which do not provide traceback so we
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
53 # should not massage it.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
54 if should_remove_self:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
55 tblist = traceback.extract_tb (tb)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
56 del tblist[:1]
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
57 res = traceback.format_list (tblist)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
58 if res:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
59 res.insert(0, "Traceback (most recent call last):\n")
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
60 res[len(res):] = traceback.format_exception_only (type, value)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
61 # traceback.print_exception(type, value, tb)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
62 for line in res: print(line, end=' ')
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
63
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
64 def eexecfile (file):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
65 """Execute FILE and then remove it.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
66 Execute the file within the __main__ namespace.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
67 If we get an exception, print a traceback with the top frame
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
68 (ourselves) excluded."""
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
69 # We cannot use real execfile since it has a bug where the file stays
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
70 # locked forever (under w32) if SyntaxError occurs.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
71 # --- code based on code.py and PyShell.py.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
72 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
73 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
74 source = open (file, "r").read()
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
75 code = compile (source, file, "exec")
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
76 # Other exceptions (shouldn't be any...) will (correctly) fall
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
77 # through to "final".
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
78 except (OverflowError, SyntaxError, ValueError):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
79 # FIXME: When can compile() raise anything else than
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
80 # SyntaxError ????
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
81 format_exception (file, False)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
82 return
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
83 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
84 exec(code, __main__.__dict__)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
85 except:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
86 format_exception (file, True)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
87 finally:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
88 os.remove (file)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
89
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
90 def eargs (name, imports):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
91 "Get arglist of NAME for Eldoc &c."
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
92 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
93 if imports: exec(imports)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
94 parts = name.split ('.')
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
95 if len (parts) > 1:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
96 exec('import ' + parts[0]) # might fail
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
97 func = eval (name)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
98 if inspect.isbuiltin (func) or type(func) is type:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
99 doc = func.__doc__
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
100 if doc.find (' ->') != -1:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
101 print('_emacs_out', doc.split (' ->')[0])
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
102 else:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
103 print('_emacs_out', doc.split ('\n')[0])
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
104 return
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
105 if inspect.ismethod (func):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
106 func = func.im_func
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
107 if not inspect.isfunction (func):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
108 print('_emacs_out ')
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
109 return
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
110 (args, varargs, varkw, defaults) = inspect.getargspec (func)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
111 # No space between name and arglist for consistency with builtins.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
112 print('_emacs_out', \
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
113 func.__name__ + inspect.formatargspec (args, varargs, varkw,
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
114 defaults))
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
115 except:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
116 print("_emacs_out ")
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
117
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
118 def all_names (object):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
119 """Return (an approximation to) a list of all possible attribute
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
120 names reachable via the attributes of OBJECT, i.e. roughly the
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
121 leaves of the dictionary tree under it."""
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
122
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
123 def do_object (object, names):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
124 if inspect.ismodule (object):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
125 do_module (object, names)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
126 elif inspect.isclass (object):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
127 do_class (object, names)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
128 # Might have an object without its class in scope.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
129 elif hasattr (object, '__class__'):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
130 names.add ('__class__')
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
131 do_class (object.__class__, names)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
132 # Probably not a good idea to try to enumerate arbitrary
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
133 # dictionaries...
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
134 return names
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
135
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
136 def do_module (module, names):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
137 if hasattr (module, '__all__'): # limited export list
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
138 names.update(module.__all__)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
139 for i in module.__all__:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
140 do_object (getattr (module, i), names)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
141 else: # use all names
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
142 names.update(dir (module))
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
143 for i in dir (module):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
144 do_object (getattr (module, i), names)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
145 return names
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
146
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
147 def do_class (object, names):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
148 ns = dir (object)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
149 names.update(ns)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
150 if hasattr (object, '__bases__'): # superclasses
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
151 for i in object.__bases__: do_object (i, names)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
152 return names
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
153
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
154 return do_object (object, set([]))
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
155
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
156 def complete (name, imports):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
157 """Complete TEXT in NAMESPACE and print a Lisp list of completions.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
158 Exec IMPORTS first."""
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
159 import __main__, keyword
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
160
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
161 def class_members(object):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
162 names = dir (object)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
163 if hasattr (object, '__bases__'):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
164 for super in object.__bases__:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
165 names = class_members (super)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
166 return names
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
167
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
168 names = set([])
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
169 base = None
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
170 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
171 dict = __main__.__dict__.copy()
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
172 if imports: exec(imports, dict)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
173 l = len (name)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
174 if not "." in name:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
175 for src in [dir (__builtins__), keyword.kwlist, list(dict.keys())]:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
176 for elt in src:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
177 if elt[:l] == name: names.add(elt)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
178 else:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
179 base = name[:name.rfind ('.')]
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
180 name = name[name.rfind('.')+1:]
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
181 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
182 object = eval (base, dict)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
183 names = set(dir (object))
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
184 if hasattr (object, '__class__'):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
185 names.add('__class__')
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
186 names.update(class_members (object))
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
187 except: names = all_names (dict)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
188 except:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
189 print(sys.exc_info())
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
190 names = []
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
191
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
192 l = len(name)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
193 print('_emacs_out (', end=' ')
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
194 for n in names:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
195 if name == n[:l]:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
196 if base: print('"%s.%s"' % (base, n), end=' ')
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
197 else: print('"%s"' % n, end=' ')
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
198 print(')')
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
199
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
200 def ehelp (name, imports):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
201 """Get help on string NAME.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
202 First try to eval name for, e.g. user definitions where we need
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
203 the object. Otherwise try the string form."""
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
204 locls = {}
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
205 if imports:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
206 try: exec(imports, locls)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
207 except: pass
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
208 try: help (eval (name, globals(), locls))
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
209 except: help (name)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
210
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
211 def eimport (mod, dir):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
212 """Import module MOD with directory DIR at the head of the search path.
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
213 NB doesn't load from DIR if MOD shadows a system module."""
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
214 from __main__ import __dict__
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
215
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
216 path0 = sys.path[0]
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
217 sys.path[0] = dir
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
218 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
219 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
220 if mod in __dict__ and inspect.ismodule (__dict__[mod]):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
221 reload (__dict__[mod])
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
222 else:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
223 __dict__[mod] = __import__ (mod)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
224 except:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
225 (type, value, tb) = sys.exc_info ()
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
226 print("Traceback (most recent call last):")
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
227 traceback.print_exception (type, value, tb.tb_next)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
228 finally:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
229 sys.path[0] = path0
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
230
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
231 def modpath (module):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
232 """Return the source file for the given MODULE (or None).
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
233 Assumes that MODULE.py and MODULE.pyc are in the same directory."""
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
234 try:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
235 path = __import__ (module).__file__
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
236 if path[-4:] == '.pyc' and os.path.exists (path[0:-1]):
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
237 path = path[:-1]
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
238 print("_emacs_out", path)
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
239 except:
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
240 print("_emacs_out ()")
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
241
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
242 # print '_emacs_ok' # ready for input and can call continuation
82a29542ff1d Adam Hupp <adam at hupp.org>
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
243
84885
69c173eabff4 Add arch tagline
Miles Bader <miles@gnu.org>
parents: 84822
diff changeset
244 # arch-tag: 37bfed38-5f4a-4027-a2bf-d5f41819dd89