##// END OF EJS Templates
Add %alias_magic for creating magic aliases.
Bradley M. Froehle -
Show More
@@ -19,6 +19,7 b' import sys'
19 from pprint import pformat
19 from pprint import pformat
20
20
21 # Our own packages
21 # Our own packages
22 from IPython.core import magic_arguments
22 from IPython.core.error import UsageError
23 from IPython.core.error import UsageError
23 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
24 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
24 from IPython.utils.text import format_screen
25 from IPython.utils.text import format_screen
@@ -39,6 +40,67 b' class BasicMagics(Magics):'
39 These are various magics that don't fit into specific categories but that
40 These are various magics that don't fit into specific categories but that
40 are all part of the base 'IPython experience'."""
41 are all part of the base 'IPython experience'."""
41
42
43 @magic_arguments.magic_arguments()
44 @magic_arguments.argument(
45 '-l', '--line', action='store_true',
46 help="""Create a line magic alias."""
47 )
48 @magic_arguments.argument(
49 '-c', '--cell', action='store_true',
50 help="""Create a cell magic alias."""
51 )
52 @magic_arguments.argument(
53 'name',
54 help="""Name of the magic to be created."""
55 )
56 @magic_arguments.argument(
57 'target',
58 help="""Name of the existing line or cell magic."""
59 )
60 @line_magic
61 def alias_magic(self, line=''):
62 """Create an alias for an existing line or cell magic."""
63 args = magic_arguments.parse_argstring(self.alias_magic, line)
64 shell = self.shell
65 escs = ''.join(magic_escapes.values())
66
67 target = args.target.lstrip(escs)
68 name = args.name.lstrip(escs)
69
70 # Find the requested magics.
71 m_line = shell.find_magic(target, 'line')
72 m_cell = shell.find_magic(target, 'cell')
73 if args.line and m_line is None:
74 raise UsageError('Line magic function `%s%s` not found.' %
75 (magic_escapes['line'], target))
76 if args.cell and m_cell is None:
77 raise UsageError('Cell magic function `%s%s` not found.' %
78 (magic_escapes['cell'], target))
79
80 # If --line and --cell are not specified, default to the ones
81 # that are available.
82 if not args.line and not args.cell:
83 if not m_line and not m_cell:
84 raise UsageError(
85 'No line or cell magic with name `%s` found.' % target
86 )
87 args.line = bool(m_line)
88 args.cell = bool(m_cell)
89
90 if args.line:
91 def wrapper(line): return m_line(line)
92 wrapper.__name__ = str(name)
93 wrapper.__doc__ = "Alias for `%s%s`." % \
94 (magic_escapes['line'], target)
95 shell.register_magic_function(wrapper, 'line', name)
96
97 if args.cell:
98 def wrapper(line, cell): return m_cell(line, cell)
99 wrapper.__name__ = str(name)
100 wrapper.__doc__ = "Alias for `%s%s`." % \
101 (magic_escapes['cell'], target)
102 shell.register_magic_function(wrapper, 'cell', name)
103
42 def _lsmagic(self):
104 def _lsmagic(self):
43 mesc = magic_escapes['line']
105 mesc = magic_escapes['line']
44 cesc = magic_escapes['cell']
106 cesc = magic_escapes['cell']
General Comments 0
You need to be logged in to leave comments. Login now