Show More
@@ -53,7 +53,9 b' def all_cells():' | |||||
53 |
|
53 | |||
54 |
|
54 | |||
55 | class TrivialLeoWorkbook: |
|
55 | class TrivialLeoWorkbook: | |
56 |
""" class to find cells |
|
56 | """ class to find cells with simple syntax | |
|
57 | ||||
|
58 | """ | |||
57 | def __getattr__(self, key): |
|
59 | def __getattr__(self, key): | |
58 | cells = all_cells() |
|
60 | cells = all_cells() | |
59 | p = cells[key] |
|
61 | p = cells[key] | |
@@ -80,21 +82,29 b' class LeoNode(object):' | |||||
80 | def get_h(self): return self.p.headString() |
|
82 | def get_h(self): return self.p.headString() | |
81 | def set_h(self,val): |
|
83 | def set_h(self,val): | |
82 |
print "set head",val |
|
84 | print "set head",val | |
|
85 | c.beginUpdate() | |||
|
86 | try: | |||
83 | c.setHeadString(self.p,val) |
|
87 | c.setHeadString(self.p,val) | |
|
88 | finally: | |||
|
89 | c.endUpdate() | |||
84 |
|
90 | |||
85 | h = property( get_h, set_h) |
|
91 | h = property( get_h, set_h) | |
86 |
|
92 | |||
87 | def get_b(self): return self.p.bodyString() |
|
93 | def get_b(self): return self.p.bodyString() | |
88 | def set_b(self,val): |
|
94 | def set_b(self,val): | |
89 |
print "set body",val |
|
95 | print "set body",val | |
|
96 | c.beginUpdate() | |||
|
97 | try: | |||
90 | c.setBodyString(self.p, val) |
|
98 | c.setBodyString(self.p, val) | |
|
99 | finally: | |||
|
100 | c.endUpdate() | |||
91 |
|
101 | |||
92 | b = property(get_b, set_b) |
|
102 | b = property(get_b, set_b) | |
93 |
|
103 | |||
94 | def set_val(self, val): |
|
104 | def set_val(self, val): | |
95 | self.b = pprint.pformat(val) |
|
105 | self.b = pprint.pformat(val) | |
96 |
|
106 | |||
97 |
v |
|
107 | v = property(lambda self: ip.ev(self.b.strip()), set_val) | |
98 |
|
108 | |||
99 | def set_l(self,val): |
|
109 | def set_l(self,val): | |
100 | self.b = '\n'.join(val ) |
|
110 | self.b = '\n'.join(val ) | |
@@ -130,6 +140,9 b' def workbook_complete(obj, prev):' | |||||
130 |
|
140 | |||
131 |
|
141 | |||
132 | def add_var(varname, value = _dummyval): |
|
142 | def add_var(varname, value = _dummyval): | |
|
143 | c.beginUpdate() | |||
|
144 | try: | |||
|
145 | ||||
133 | nodename = '@ipy-var ' + varname |
|
146 | nodename = '@ipy-var ' + varname | |
134 | p2 = g.findNodeAnywhere(c,nodename) |
|
147 | p2 = g.findNodeAnywhere(c,nodename) | |
135 | if not c.positionExists(p2): |
|
148 | if not c.positionExists(p2): | |
@@ -145,11 +158,15 b' def add_var(varname, value = _dummyval):' | |||||
145 | formatted = format_for_leo(val) |
|
158 | formatted = format_for_leo(val) | |
146 | c.setBodyString(p2,formatted) |
|
159 | c.setBodyString(p2,formatted) | |
147 | return p2 |
|
160 | return p2 | |
|
161 | finally: | |||
|
162 | c.endUpdate() | |||
148 |
|
163 | |||
149 | def add_file(self,fname): |
|
164 | def add_file(self,fname): | |
150 | p2 = c.currentPosition().insertAfter() |
|
165 | p2 = c.currentPosition().insertAfter() | |
151 |
|
166 | |||
152 | def push_script(p): |
|
167 | def push_script(p): | |
|
168 | c.beginUpdate() | |||
|
169 | try: | |||
153 | ohist = ip.IP.output_hist |
|
170 | ohist = ip.IP.output_hist | |
154 | hstart = len(ip.IP.input_hist) |
|
171 | hstart = len(ip.IP.input_hist) | |
155 | script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=False,useSentinels=False) |
|
172 | script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=False,useSentinels=False) | |
@@ -173,6 +190,8 b' def push_script(p):' | |||||
173 |
|
190 | |||
174 | if not has_output: |
|
191 | if not has_output: | |
175 | g.es('ipy run: %s' %( p.headString(),), tabName = 'IPython') |
|
192 | g.es('ipy run: %s' %( p.headString(),), tabName = 'IPython') | |
|
193 | finally: | |||
|
194 | c.endUpdate() | |||
176 |
|
195 | |||
177 |
|
196 | |||
178 | def eval_body(body): |
|
197 | def eval_body(body): | |
@@ -190,10 +209,6 b' def push_variable(p,varname):' | |||||
190 | g.es('ipy var: %s' % (varname,), tabName = "IPython") |
|
209 | g.es('ipy var: %s' % (varname,), tabName = "IPython") | |
191 |
|
210 | |||
192 | def push_from_leo(p): |
|
211 | def push_from_leo(p): | |
193 | # headstring without @ are just scripts |
|
|||
194 | if not p.headString().startswith('@'): |
|
|||
195 | push_script(p) |
|
|||
196 | return |
|
|||
197 | tup = p.headString().split(None,1) |
|
212 | tup = p.headString().split(None,1) | |
198 | # @ipy foo is variable foo |
|
213 | # @ipy foo is variable foo | |
199 | if len(tup) == 2 and tup[0] == '@ipy': |
|
214 | if len(tup) == 2 and tup[0] == '@ipy': | |
@@ -201,15 +216,33 b' def push_from_leo(p):' | |||||
201 | push_variable(p,varname) |
|
216 | push_variable(p,varname) | |
202 | return |
|
217 | return | |
203 |
|
218 | |||
|
219 | push_script(p) | |||
|
220 | return | |||
|
221 | ||||
|
222 | ||||
204 | ip.user_ns['leox'].push = push_from_leo |
|
223 | ip.user_ns['leox'].push = push_from_leo | |
205 |
|
224 | |||
206 | def leo_f(self,s): |
|
225 | def leo_f(self,s): | |
207 | ip = self.getapi() |
|
226 | """ open file(s) in Leo | |
208 | s = s.strip() |
|
227 | ||
209 | if s in ip.user_ns: |
|
228 | Takes an mglob pattern, e.g. '%leo *.cpp' or %leo 'rec:*.cpp' | |
210 | add_var(s) |
|
229 | """ | |
211 | elif os.path.isfile(s): |
|
230 | import os | |
212 | # todo open file |
|
231 | from IPython.external import mglob | |
213 | pass |
|
232 | ||
|
233 | files = mglob.expand(s) | |||
|
234 | c.beginUpdate() | |||
|
235 | try: | |||
|
236 | for fname in files: | |||
|
237 | p = g.findNodeAnywhere(c,'@auto ' + fname) | |||
|
238 | if not p: | |||
|
239 | p = c.currentPosition().insertAfter() | |||
|
240 | ||||
|
241 | p.setHeadString('@auto ' + fname) | |||
|
242 | if os.path.isfile(fname): | |||
|
243 | c.setBodyString(p,open(fname).read()) | |||
|
244 | c.selectPosition(p) | |||
|
245 | finally: | |||
|
246 | c.endUpdate() | |||
214 |
|
247 | |||
215 | ip.expose_magic('leo',leo_f) |
|
248 | ip.expose_magic('leo',leo_f) |
General Comments 0
You need to be logged in to leave comments.
Login now