##// END OF EJS Templates
Initial code to expand macro in prefilter. Doesn't quite work right with multi-line macros.
Thomas Kluyver -
Show More
@@ -8,9 +8,8 b''
8 8 #*****************************************************************************
9 9
10 10 import IPython.utils.io
11 from IPython.core.autocall import IPyAutocall
12 11
13 class Macro(IPyAutocall):
12 class Macro(object):
14 13 """Simple class to store the value of macros as strings.
15 14
16 15 Macro is just a callable that executes a string of IPython
@@ -29,11 +28,6 b' class Macro(IPyAutocall):'
29 28 def __repr__(self):
30 29 return 'IPython.macro.Macro(%s)' % repr(self.value)
31 30
32 def __call__(self,*args):
33 IPython.utils.io.Term.cout.flush()
34 self._ip.user_ns['_margv'] = args
35 self._ip.run_cell(self.value)
36
37 31 def __getstate__(self):
38 32 """ needed for safe pickling via %store """
39 33 return {'value': self.value}
@@ -32,6 +32,7 b' import re'
32 32 from IPython.core.alias import AliasManager
33 33 from IPython.core.autocall import IPyAutocall
34 34 from IPython.config.configurable import Configurable
35 from IPython.core.macro import Macro
35 36 from IPython.core.splitinput import split_user_input
36 37 from IPython.core import page
37 38
@@ -598,6 +599,18 b' class ShellEscapeChecker(PrefilterChecker):'
598 599 return self.prefilter_manager.get_handler_by_name('shell')
599 600
600 601
602 class MacroChecker(PrefilterChecker):
603
604 priority = Int(250, config=True)
605
606 def check(self, line_info):
607 obj = self.shell.user_ns.get(line_info.ifun)
608 if isinstance(obj, Macro):
609 return self.prefilter_manager.get_handler_by_name('macro')
610 else:
611 return None
612
613
601 614 class IPyAutocallChecker(PrefilterChecker):
602 615
603 616 priority = Int(300, config=True)
@@ -837,6 +850,16 b' class ShellEscapeHandler(PrefilterHandler):'
837 850 return line_out
838 851
839 852
853 class MacroHandler(PrefilterHandler):
854 handler_name = Str("macro")
855
856 def handle(self, line_info):
857 obj = self.shell.user_ns.get(line_info.ifun)
858 pre_space = line_info.pre_whitespace
859 line_sep = "\n" + pre_space
860 return pre_space + line_sep.join(obj.value.splitlines())
861
862
840 863 class MagicHandler(PrefilterHandler):
841 864
842 865 handler_name = Str('magic')
@@ -979,6 +1002,7 b' _default_transformers = ['
979 1002 _default_checkers = [
980 1003 EmacsChecker,
981 1004 ShellEscapeChecker,
1005 MacroChecker,
982 1006 IPyAutocallChecker,
983 1007 MultiLineMagicChecker,
984 1008 EscCharsChecker,
@@ -993,6 +1017,7 b' _default_handlers = ['
993 1017 PrefilterHandler,
994 1018 AliasHandler,
995 1019 ShellEscapeHandler,
1020 MacroHandler,
996 1021 MagicHandler,
997 1022 AutoHandler,
998 1023 HelpHandler,
General Comments 0
You need to be logged in to leave comments. Login now