##// END OF EJS Templates
ipy_leo: rename %leo to %lee, make it more useful (edit macros and other objects)
Ville M. Vainio -
Show More
@@ -9,7 +9,7 b' from IPython.hooks import CommandChainDispatcher'
9 import re
9 import re
10 import UserDict
10 import UserDict
11 from IPython.ipapi import TryNext
11 from IPython.ipapi import TryNext
12
12 import IPython.macro
13
13
14 def init_ipython(ipy):
14 def init_ipython(ipy):
15 """ This will be run by _ip.load('ipy_leo')
15 """ This will be run by _ip.load('ipy_leo')
@@ -21,7 +21,7 b' def init_ipython(ipy):'
21 ip = ipy
21 ip = ipy
22 ip.set_hook('complete_command', mb_completer, str_key = 'mb')
22 ip.set_hook('complete_command', mb_completer, str_key = 'mb')
23 ip.expose_magic('mb',mb_f)
23 ip.expose_magic('mb',mb_f)
24 ip.expose_magic('leo',leo_f)
24 ip.expose_magic('lee',lee_f)
25 ip.expose_magic('leoref',leoref_f)
25 ip.expose_magic('leoref',leoref_f)
26 expose_ileo_push(push_cl_node,100)
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
27 # this should be the LAST one that will be executed, and it will never raise TryNext
@@ -69,6 +69,7 b' def format_for_leo(obj):'
69 @format_for_leo.when_type(list)
69 @format_for_leo.when_type(list)
70 def format_list(obj):
70 def format_list(obj):
71 return "\n".join(str(s) for s in obj)
71 return "\n".join(str(s) for s in obj)
72
72
73
73 attribute_re = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')
74 attribute_re = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')
74 def valid_attribute(s):
75 def valid_attribute(s):
@@ -272,7 +273,7 b' class LeoWorkbook:'
272 cells = all_cells()
273 cells = all_cells()
273 p = cells.get(key, None)
274 p = cells.get(key, None)
274 if p is None:
275 if p is None:
275 p = add_var(key)
276 return add_var(key)
276
277
277 return LeoNode(p)
278 return LeoNode(p)
278
279
@@ -307,14 +308,14 b' def add_var(varname):'
307 try:
308 try:
308 p2 = g.findNodeAnywhere(c,varname)
309 p2 = g.findNodeAnywhere(c,varname)
309 if p2:
310 if p2:
310 return
311 return LeoNode(p2)
311
312
312 rootpos = g.findNodeAnywhere(c,'@ipy-results')
313 rootpos = g.findNodeAnywhere(c,'@ipy-results')
313 if not rootpos:
314 if not rootpos:
314 rootpos = c.currentPosition()
315 rootpos = c.currentPosition()
315 p2 = rootpos.insertAsLastChild()
316 p2 = rootpos.insertAsLastChild()
316 c.setHeadString(p2,varname)
317 c.setHeadString(p2,varname)
317 return p2
318 return LeoNode(p2)
318 finally:
319 finally:
319 c.endUpdate()
320 c.endUpdate()
320
321
@@ -403,22 +404,43 b' def push_ev_node(node):'
403 node.v = res
404 node.v = res
404
405
405
406
406
407
408 def push_position_from_leo(p):
407 def push_position_from_leo(p):
409 push_from_leo(LeoNode(p))
408 push_from_leo(LeoNode(p))
410
409
411 def leo_f(self,s):
410 @generic
412 """ open file(s) in Leo
411 def edit_object_in_leo(obj, varname):
413
412 """ Make it @cl node so it can be pushed back directly by alt+I """
414 Takes an mglob pattern, e.g. '%leo *.cpp' or %leo 'rec:*.cpp'
413 node = add_var(varname)
414 node.b = '@cl\n' + format_for_leo(obj)
415 node.go()
416
417 @edit_object_in_leo.when_type(IPython.macro.Macro)
418 def edit_macro(obj,varname):
419 bod = '_ip.defmacro("""\\\n' + obj.value + '""")'
420 node = add_var('Macro_' + varname)
421 node.b = bod
422 node.go()
423
424 def lee_f(self,s):
425 """ Open file(s)/objects in Leo
426
427 Takes an mglob pattern, e.g. '%lee *.cpp' or %leo 'rec:*.cpp'
415 """
428 """
416 import os
429 import os
417 from IPython.external import mglob
418
430
419 files = mglob.expand(s)
420 c.beginUpdate()
431 c.beginUpdate()
421 try:
432 try:
433
434 # try editing the object directly
435 obj = ip.user_ns.get(s, None)
436 if obj is not None:
437 edit_object_in_leo(obj,s)
438 return
439
440 # if it's not object, it's a file name / mglob pattern
441 from IPython.external import mglob
442
443 files = mglob.expand(s)
422 for fname in files:
444 for fname in files:
423 p = g.findNodeAnywhere(c,'@auto ' + fname)
445 p = g.findNodeAnywhere(c,'@auto ' + fname)
424 if not p:
446 if not p:
@@ -437,7 +459,7 b' def leoref_f(self,s):'
437 """ Quick reference for ILeo """
459 """ Quick reference for ILeo """
438 import textwrap
460 import textwrap
439 print textwrap.dedent("""\
461 print textwrap.dedent("""\
440 %leo file - open file in leo
462 %leoe file/object - open file / object in leo
441 wb.foo.v - eval node foo (i.e. headstring is 'foo' or '@ipy foo')
463 wb.foo.v - eval node foo (i.e. headstring is 'foo' or '@ipy foo')
442 wb.foo.v = 12 - assign to body of node foo
464 wb.foo.v = 12 - assign to body of node foo
443 wb.foo.b - read or write the body of node foo
465 wb.foo.b - read or write the body of node foo
General Comments 0
You need to be logged in to leave comments. Login now