Show More
@@ -10,14 +10,48 b' import re' | |||||
10 | import UserDict |
|
10 | import UserDict | |
11 | from IPython.ipapi import TryNext |
|
11 | from IPython.ipapi import TryNext | |
12 |
|
12 | |||
13 | ip = IPython.ipapi.get() |
|
13 | ||
14 | leo = ip.user_ns['leox'] |
|
14 | def init_ipython(ipy): | |
15 | c,g = leo.c, leo.g |
|
15 | """ This will be run by _ip.load('ipy_leo') | |
|
16 | ||||
|
17 | Leo still needs to run update_commander() after this. | |||
|
18 | ||||
|
19 | """ | |||
|
20 | global ip | |||
|
21 | ip = ipy | |||
|
22 | ip.set_hook('complete_command', mb_completer, str_key = 'mb') | |||
|
23 | ip.expose_magic('mb',mb_f) | |||
|
24 | ip.expose_magic('leo',leo_f) | |||
|
25 | ip.expose_magic('leoref',leoref_f) | |||
|
26 | expose_ileo_push(push_cl_node,100) | |||
|
27 | # this should be the LAST one that will be executed, and it will never raise TryNext | |||
|
28 | expose_ileo_push(push_ipython_script, 1000) | |||
|
29 | expose_ileo_push(push_plain_python, 100) | |||
|
30 | ip.user_ns['wb'] = LeoWorkbook() | |||
|
31 | ||||
|
32 | show_welcome() | |||
|
33 | ||||
|
34 | ||||
|
35 | def update_commander(new_leox): | |||
|
36 | """ Set the Leo commander to use | |||
|
37 | ||||
|
38 | This will be run every time Leo does ipython-launch; basically, | |||
|
39 | when the user switches the document he is focusing on, he should do | |||
|
40 | ipython-launch to tell ILeo what document the commands apply to. | |||
|
41 | ||||
|
42 | """ | |||
|
43 | ||||
|
44 | global c,g | |||
|
45 | c,g = new_leox.c, new_leox.g | |||
|
46 | print "Set Leo Commander:",c.frame.getTitle() | |||
16 |
|
47 | |||
17 | # will probably be overwritten by user, but handy for experimentation early on |
|
48 | # will probably be overwritten by user, but handy for experimentation early on | |
18 | ip.user_ns['c'] = c |
|
49 | ip.user_ns['c'] = c | |
19 | ip.user_ns['g'] = g |
|
50 | ip.user_ns['g'] = g | |
|
51 | ip.user_ns['_leo'] = new_leox | |||
20 |
|
52 | |||
|
53 | new_leox.push = push_position_from_leo | |||
|
54 | run_leo_startup_node() | |||
21 |
|
55 | |||
22 | from IPython.external.simplegeneric import generic |
|
56 | from IPython.external.simplegeneric import generic | |
23 | import pprint |
|
57 | import pprint | |
@@ -50,8 +84,6 b' def all_cells():' | |||||
50 | d[h] = p.copy() |
|
84 | d[h] = p.copy() | |
51 | return d |
|
85 | return d | |
52 |
|
86 | |||
53 |
|
||||
54 |
|
||||
55 | def eval_node(n): |
|
87 | def eval_node(n): | |
56 | body = n.b |
|
88 | body = n.b | |
57 | if not body.startswith('@cl'): |
|
89 | if not body.startswith('@cl'): | |
@@ -92,6 +124,10 b' class LeoNode(object, UserDict.DictMixin):' | |||||
92 |
|
124 | |||
93 | .ipush() - run push-to-ipython |
|
125 | .ipush() - run push-to-ipython | |
94 |
|
126 | |||
|
127 | Minibuffer command access (tab completion works): | |||
|
128 | ||||
|
129 | mb save-to-file | |||
|
130 | ||||
95 | """ |
|
131 | """ | |
96 | def __init__(self,p): |
|
132 | def __init__(self,p): | |
97 | self.p = p.copy() |
|
133 | self.p = p.copy() | |
@@ -260,10 +296,6 b' class LeoWorkbook:' | |||||
260 | yield node |
|
296 | yield node | |
261 | return |
|
297 | return | |
262 |
|
298 | |||
263 | ip.user_ns['wb'] = LeoWorkbook() |
|
|||
264 |
|
||||
265 |
|
||||
266 |
|
||||
267 | @IPython.generics.complete_object.when_type(LeoWorkbook) |
|
299 | @IPython.generics.complete_object.when_type(LeoWorkbook) | |
268 | def workbook_complete(obj, prev): |
|
300 | def workbook_complete(obj, prev): | |
269 | return all_cells().keys() + [s for s in prev if not s.startswith('_')] |
|
301 | return all_cells().keys() + [s for s in prev if not s.startswith('_')] | |
@@ -322,8 +354,6 b' def push_ipython_script(node):' | |||||
322 | finally: |
|
354 | finally: | |
323 | c.endUpdate() |
|
355 | c.endUpdate() | |
324 |
|
356 | |||
325 | # this should be the LAST one that will be executed, and it will never raise TryNext |
|
|||
326 | expose_ileo_push(push_ipython_script, 1000) |
|
|||
327 |
|
357 | |||
328 | def eval_body(body): |
|
358 | def eval_body(body): | |
329 | try: |
|
359 | try: | |
@@ -345,7 +375,6 b' def push_plain_python(node):' | |||||
345 | raise |
|
375 | raise | |
346 | es('ipy plain: %s (%d LL)' % (node.h,lines)) |
|
376 | es('ipy plain: %s (%d LL)' % (node.h,lines)) | |
347 |
|
377 | |||
348 | expose_ileo_push(push_plain_python, 100) |
|
|||
349 |
|
378 | |||
350 | def push_cl_node(node): |
|
379 | def push_cl_node(node): | |
351 | """ If node starts with @cl, eval it |
|
380 | """ If node starts with @cl, eval it | |
@@ -362,13 +391,11 b' def push_cl_node(node):' | |||||
362 | LeoNode(p2).v = val |
|
391 | LeoNode(p2).v = val | |
363 | es(val) |
|
392 | es(val) | |
364 |
|
393 | |||
365 | expose_ileo_push(push_cl_node,100) |
|
394 | ||
366 |
|
395 | |||
367 | def push_position_from_leo(p): |
|
396 | def push_position_from_leo(p): | |
368 | push_from_leo(LeoNode(p)) |
|
397 | push_from_leo(LeoNode(p)) | |
369 |
|
398 | |||
370 | ip.user_ns['leox'].push = push_position_from_leo |
|
|||
371 |
|
||||
372 | def leo_f(self,s): |
|
399 | def leo_f(self,s): | |
373 | """ open file(s) in Leo |
|
400 | """ open file(s) in Leo | |
374 |
|
401 | |||
@@ -392,7 +419,7 b' def leo_f(self,s):' | |||||
392 | finally: |
|
419 | finally: | |
393 | c.endUpdate() |
|
420 | c.endUpdate() | |
394 |
|
421 | |||
395 | ip.expose_magic('leo',leo_f) |
|
422 | ||
396 |
|
423 | |||
397 | def leoref_f(self,s): |
|
424 | def leoref_f(self,s): | |
398 | """ Quick reference for ILeo """ |
|
425 | """ Quick reference for ILeo """ | |
@@ -409,14 +436,15 b' def leoref_f(self,s):' | |||||
409 |
|
436 | |||
410 | """ |
|
437 | """ | |
411 | ) |
|
438 | ) | |
412 | ip.expose_magic('leoref',leoref_f) |
|
|||
413 |
|
439 | |||
414 | from ipy_leo import * |
|
|||
415 |
|
440 | |||
416 | ip = IPython.ipapi.get() |
|
|||
417 |
|
441 | |||
418 | def mb_f(self, arg): |
|
442 | def mb_f(self, arg): | |
419 |
""" Execute leo minibuffer commands |
|
443 | """ Execute leo minibuffer commands | |
|
444 | ||||
|
445 | Example: | |||
|
446 | mb save-to-file | |||
|
447 | """ | |||
420 | c.executeMinibufferCommand(arg) |
|
448 | c.executeMinibufferCommand(arg) | |
421 |
|
449 | |||
422 | def mb_completer(self,event): |
|
450 | def mb_completer(self,event): | |
@@ -430,11 +458,6 b' def mb_completer(self,event):' | |||||
430 | cmds.sort() |
|
458 | cmds.sort() | |
431 | return cmds |
|
459 | return cmds | |
432 |
|
460 | |||
433 | pass |
|
|||
434 | ip.set_hook('complete_command', mb_completer, str_key = 'mb') |
|
|||
435 | ip.expose_magic('mb',mb_f) |
|
|||
436 |
|
||||
437 |
|
||||
438 | def show_welcome(): |
|
461 | def show_welcome(): | |
439 | print "------------------" |
|
462 | print "------------------" | |
440 | print "Welcome to Leo-enabled IPython session!" |
|
463 | print "Welcome to Leo-enabled IPython session!" | |
@@ -449,7 +472,3 b' def run_leo_startup_node():' | |||||
449 | print "Running @ipy-startup nodes" |
|
472 | print "Running @ipy-startup nodes" | |
450 | for n in LeoNode(p): |
|
473 | for n in LeoNode(p): | |
451 | push_from_leo(n) |
|
474 | push_from_leo(n) | |
452 |
|
||||
453 | run_leo_startup_node() |
|
|||
454 | show_welcome() |
|
|||
455 |
|
General Comments 0
You need to be logged in to leave comments.
Login now