##// 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 import IPython.utils.io
10 import IPython.utils.io
11 from IPython.core.autocall import IPyAutocall
12
11
13 class Macro(IPyAutocall):
12 class Macro(object):
14 """Simple class to store the value of macros as strings.
13 """Simple class to store the value of macros as strings.
15
14
16 Macro is just a callable that executes a string of IPython
15 Macro is just a callable that executes a string of IPython
@@ -29,11 +28,6 b' class Macro(IPyAutocall):'
29 def __repr__(self):
28 def __repr__(self):
30 return 'IPython.macro.Macro(%s)' % repr(self.value)
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 def __getstate__(self):
31 def __getstate__(self):
38 """ needed for safe pickling via %store """
32 """ needed for safe pickling via %store """
39 return {'value': self.value}
33 return {'value': self.value}
@@ -32,6 +32,7 b' import re'
32 from IPython.core.alias import AliasManager
32 from IPython.core.alias import AliasManager
33 from IPython.core.autocall import IPyAutocall
33 from IPython.core.autocall import IPyAutocall
34 from IPython.config.configurable import Configurable
34 from IPython.config.configurable import Configurable
35 from IPython.core.macro import Macro
35 from IPython.core.splitinput import split_user_input
36 from IPython.core.splitinput import split_user_input
36 from IPython.core import page
37 from IPython.core import page
37
38
@@ -598,6 +599,18 b' class ShellEscapeChecker(PrefilterChecker):'
598 return self.prefilter_manager.get_handler_by_name('shell')
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 class IPyAutocallChecker(PrefilterChecker):
614 class IPyAutocallChecker(PrefilterChecker):
602
615
603 priority = Int(300, config=True)
616 priority = Int(300, config=True)
@@ -837,6 +850,16 b' class ShellEscapeHandler(PrefilterHandler):'
837 return line_out
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 class MagicHandler(PrefilterHandler):
863 class MagicHandler(PrefilterHandler):
841
864
842 handler_name = Str('magic')
865 handler_name = Str('magic')
@@ -979,6 +1002,7 b' _default_transformers = ['
979 _default_checkers = [
1002 _default_checkers = [
980 EmacsChecker,
1003 EmacsChecker,
981 ShellEscapeChecker,
1004 ShellEscapeChecker,
1005 MacroChecker,
982 IPyAutocallChecker,
1006 IPyAutocallChecker,
983 MultiLineMagicChecker,
1007 MultiLineMagicChecker,
984 EscCharsChecker,
1008 EscCharsChecker,
@@ -993,6 +1017,7 b' _default_handlers = ['
993 PrefilterHandler,
1017 PrefilterHandler,
994 AliasHandler,
1018 AliasHandler,
995 ShellEscapeHandler,
1019 ShellEscapeHandler,
1020 MacroHandler,
996 MagicHandler,
1021 MagicHandler,
997 AutoHandler,
1022 AutoHandler,
998 HelpHandler,
1023 HelpHandler,
General Comments 0
You need to be logged in to leave comments. Login now