From 66f66203f99b8f23609c4fda8a0f1d09ee95983b 2011-03-13 12:51:50 From: Thomas Kluyver Date: 2011-03-13 12:51:50 Subject: [PATCH] Initial code to expand macro in prefilter. Doesn't quite work right with multi-line macros. --- diff --git a/IPython/core/macro.py b/IPython/core/macro.py index c7fa44b..f6af9f1 100644 --- a/IPython/core/macro.py +++ b/IPython/core/macro.py @@ -8,9 +8,8 @@ #***************************************************************************** import IPython.utils.io -from IPython.core.autocall import IPyAutocall -class Macro(IPyAutocall): +class Macro(object): """Simple class to store the value of macros as strings. Macro is just a callable that executes a string of IPython @@ -29,11 +28,6 @@ class Macro(IPyAutocall): def __repr__(self): return 'IPython.macro.Macro(%s)' % repr(self.value) - def __call__(self,*args): - IPython.utils.io.Term.cout.flush() - self._ip.user_ns['_margv'] = args - self._ip.run_cell(self.value) - def __getstate__(self): """ needed for safe pickling via %store """ return {'value': self.value} diff --git a/IPython/core/prefilter.py b/IPython/core/prefilter.py index a29bfe6..7bae693 100755 --- a/IPython/core/prefilter.py +++ b/IPython/core/prefilter.py @@ -32,6 +32,7 @@ import re from IPython.core.alias import AliasManager from IPython.core.autocall import IPyAutocall from IPython.config.configurable import Configurable +from IPython.core.macro import Macro from IPython.core.splitinput import split_user_input from IPython.core import page @@ -598,6 +599,18 @@ class ShellEscapeChecker(PrefilterChecker): return self.prefilter_manager.get_handler_by_name('shell') +class MacroChecker(PrefilterChecker): + + priority = Int(250, config=True) + + def check(self, line_info): + obj = self.shell.user_ns.get(line_info.ifun) + if isinstance(obj, Macro): + return self.prefilter_manager.get_handler_by_name('macro') + else: + return None + + class IPyAutocallChecker(PrefilterChecker): priority = Int(300, config=True) @@ -837,6 +850,16 @@ class ShellEscapeHandler(PrefilterHandler): return line_out +class MacroHandler(PrefilterHandler): + handler_name = Str("macro") + + def handle(self, line_info): + obj = self.shell.user_ns.get(line_info.ifun) + pre_space = line_info.pre_whitespace + line_sep = "\n" + pre_space + return pre_space + line_sep.join(obj.value.splitlines()) + + class MagicHandler(PrefilterHandler): handler_name = Str('magic') @@ -979,6 +1002,7 @@ _default_transformers = [ _default_checkers = [ EmacsChecker, ShellEscapeChecker, + MacroChecker, IPyAutocallChecker, MultiLineMagicChecker, EscCharsChecker, @@ -993,6 +1017,7 @@ _default_handlers = [ PrefilterHandler, AliasHandler, ShellEscapeHandler, + MacroHandler, MagicHandler, AutoHandler, HelpHandler,