##// END OF EJS Templates
Update terminal-only magics to new API.
Fernando Perez -
Show More
@@ -14,22 +14,18 b''
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import __builtin__
18 import bdb
17 import bdb
19 import os
18 import os
20 import re
19 import re
21 import sys
20 import sys
22 import textwrap
21 import textwrap
23
22
24 try:
23 from contextlib import nested
25 from contextlib import nested
26 except:
27 from IPython.utils.nested_context import nested
28
24
29 from IPython.core.error import TryNext, UsageError
25 from IPython.core.error import TryNext, UsageError
30 from IPython.core.usage import interactive_usage, default_banner
26 from IPython.core.usage import interactive_usage, default_banner
31 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
27 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
32 from IPython.core.pylabtools import pylab_activate
28 from IPython.core.magic import Magics, register_magics, line_magic
33 from IPython.testing.skipdoctest import skip_doctest
29 from IPython.testing.skipdoctest import skip_doctest
34 from IPython.utils.encoding import get_stream_enc
30 from IPython.utils.encoding import get_stream_enc
35 from IPython.utils import py3compat
31 from IPython.utils import py3compat
@@ -124,128 +120,133 b" def rerun_pasted(shell, name='pasted_block'):"
124 # Terminal-specific magics
120 # Terminal-specific magics
125 #------------------------------------------------------------------------
121 #------------------------------------------------------------------------
126
122
127 def magic_autoindent(self, parameter_s = ''):
123 @register_magics
128 """Toggle autoindent on/off (if available)."""
124 class TerminalMagics(Magics):
129
125
130 self.shell.set_autoindent()
126 @line_magic
131 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
127 def autoindent(self, parameter_s = ''):
128 """Toggle autoindent on/off (if available)."""
132
129
133 @skip_doctest
130 self.shell.set_autoindent()
134 def magic_cpaste(self, parameter_s=''):
131 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
135 """Paste & execute a pre-formatted code block from clipboard.
136
132
137 You must terminate the block with '--' (two minus-signs) or Ctrl-D
133 @skip_doctest
138 alone on the line. You can also provide your own sentinel with '%paste
134 @line_magic
139 -s %%' ('%%' is the new sentinel for this operation)
135 def cpaste(self, parameter_s=''):
136 """Paste & execute a pre-formatted code block from clipboard.
140
137
141 The block is dedented prior to execution to enable execution of method
138 You must terminate the block with '--' (two minus-signs) or Ctrl-D
142 definitions. '>' and '+' characters at the beginning of a line are
139 alone on the line. You can also provide your own sentinel with '%paste
143 ignored, to allow pasting directly from e-mails, diff files and
140 -s %%' ('%%' is the new sentinel for this operation)
144 doctests (the '...' continuation prompt is also stripped). The
145 executed block is also assigned to variable named 'pasted_block' for
146 later editing with '%edit pasted_block'.
147
141
148 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
142 The block is dedented prior to execution to enable execution of method
149 This assigns the pasted block to variable 'foo' as string, without
143 definitions. '>' and '+' characters at the beginning of a line are
150 dedenting or executing it (preceding >>> and + is still stripped)
144 ignored, to allow pasting directly from e-mails, diff files and
145 doctests (the '...' continuation prompt is also stripped). The
146 executed block is also assigned to variable named 'pasted_block' for
147 later editing with '%edit pasted_block'.
151
148
152 '%cpaste -r' re-executes the block previously entered by cpaste.
149 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
150 This assigns the pasted block to variable 'foo' as string, without
151 dedenting or executing it (preceding >>> and + is still stripped)
153
152
154 Do not be alarmed by garbled output on Windows (it's a readline bug).
153 '%cpaste -r' re-executes the block previously entered by cpaste.
155 Just press enter and type -- (and press enter again) and the block
156 will be what was just pasted.
157
154
158 IPython statements (magics, shell escapes) are not supported (yet).
155 Do not be alarmed by garbled output on Windows (it's a readline bug).
156 Just press enter and type -- (and press enter again) and the block
157 will be what was just pasted.
159
158
160 See also
159 IPython statements (magics, shell escapes) are not supported (yet).
161 --------
162 paste: automatically pull code from clipboard.
163
160
164 Examples
161 See also
165 --------
162 --------
166 ::
163 paste: automatically pull code from clipboard.
167
164
168 In [8]: %cpaste
165 Examples
169 Pasting code; enter '--' alone on the line to stop.
166 --------
170 :>>> a = ["world!", "Hello"]
167 ::
171 :>>> print " ".join(sorted(a))
172 :--
173 Hello world!
174 """
175
176 opts, name = self.parse_options(parameter_s, 'rs:', mode='string')
177 if 'r' in opts:
178 rerun_pasted(self.shell)
179 return
180
168
181 sentinel = opts.get('s', '--')
169 In [8]: %cpaste
182 block = strip_email_quotes(get_pasted_lines(sentinel))
170 Pasting code; enter '--' alone on the line to stop.
183 store_or_execute(self.shell, block, name)
171 :>>> a = ["world!", "Hello"]
172 :>>> print " ".join(sorted(a))
173 :--
174 Hello world!
175 """
184
176
177 opts, name = self.parse_options(parameter_s, 'rs:', mode='string')
178 if 'r' in opts:
179 rerun_pasted(self.shell)
180 return
185
181
186 def magic_paste(self, parameter_s=''):
182 sentinel = opts.get('s', '--')
187 """Paste & execute a pre-formatted code block from clipboard.
183 block = strip_email_quotes(get_pasted_lines(sentinel))
184 store_or_execute(self.shell, block, name)
188
185
189 The text is pulled directly from the clipboard without user
186 @line_magic
190 intervention and printed back on the screen before execution (unless
187 def paste(self, parameter_s=''):
191 the -q flag is given to force quiet mode).
188 """Paste & execute a pre-formatted code block from clipboard.
192
189
193 The block is dedented prior to execution to enable execution of method
190 The text is pulled directly from the clipboard without user
194 definitions. '>' and '+' characters at the beginning of a line are
191 intervention and printed back on the screen before execution (unless
195 ignored, to allow pasting directly from e-mails, diff files and
192 the -q flag is given to force quiet mode).
196 doctests (the '...' continuation prompt is also stripped). The
197 executed block is also assigned to variable named 'pasted_block' for
198 later editing with '%edit pasted_block'.
199
193
200 You can also pass a variable name as an argument, e.g. '%paste foo'.
194 The block is dedented prior to execution to enable execution of method
201 This assigns the pasted block to variable 'foo' as string, without
195 definitions. '>' and '+' characters at the beginning of a line are
202 dedenting or executing it (preceding >>> and + is still stripped)
196 ignored, to allow pasting directly from e-mails, diff files and
197 doctests (the '...' continuation prompt is also stripped). The
198 executed block is also assigned to variable named 'pasted_block' for
199 later editing with '%edit pasted_block'.
203
200
204 Options
201 You can also pass a variable name as an argument, e.g. '%paste foo'.
205 -------
202 This assigns the pasted block to variable 'foo' as string, without
203 dedenting or executing it (preceding >>> and + is still stripped)
206
204
207 -r: re-executes the block previously entered by cpaste.
205 Options
206 -------
208
207
209 -q: quiet mode: do not echo the pasted text back to the terminal.
208 -r: re-executes the block previously entered by cpaste.
210
209
211 IPython statements (magics, shell escapes) are not supported (yet).
210 -q: quiet mode: do not echo the pasted text back to the terminal.
212
211
213 See also
212 IPython statements (magics, shell escapes) are not supported (yet).
214 --------
215 cpaste: manually paste code into terminal until you mark its end.
216 """
217 opts, name = self.parse_options(parameter_s, 'rq', mode='string')
218 if 'r' in opts:
219 rerun_pasted(self.shell)
220 return
221 try:
222 text = self.shell.hooks.clipboard_get()
223 block = strip_email_quotes(text.splitlines())
224 except TryNext as clipboard_exc:
225 message = getattr(clipboard_exc, 'args')
226 if message:
227 error(message[0])
228 else:
229 error('Could not get text from the clipboard.')
230 return
231
213
232 # By default, echo back to terminal unless quiet mode is requested
214 See also
233 if 'q' not in opts:
215 --------
234 write = self.shell.write
216 cpaste: manually paste code into terminal until you mark its end.
235 write(self.shell.pycolorize(block))
217 """
236 if not block.endswith('\n'):
218 opts, name = self.parse_options(parameter_s, 'rq', mode='string')
237 write('\n')
219 if 'r' in opts:
238 write("## -- End pasted text --\n")
220 rerun_pasted(self.shell)
221 return
222 try:
223 text = self.shell.hooks.clipboard_get()
224 block = strip_email_quotes(text.splitlines())
225 except TryNext as clipboard_exc:
226 message = getattr(clipboard_exc, 'args')
227 if message:
228 error(message[0])
229 else:
230 error('Could not get text from the clipboard.')
231 return
239
232
240 store_or_execute(self.shell, block, name)
233 # By default, echo back to terminal unless quiet mode is requested
234 if 'q' not in opts:
235 write = self.shell.write
236 write(self.shell.pycolorize(block))
237 if not block.endswith('\n'):
238 write('\n')
239 write("## -- End pasted text --\n")
241
240
241 store_or_execute(self.shell, block, name)
242
242
243 # Class-level: add a '%cls' magic only on Windows
243 # Class-level: add a '%cls' magic only on Windows
244 if sys.platform == 'win32':
244 if sys.platform == 'win32':
245 def magic_cls(self, s):
245 @line_magic
246 """Clear screen.
246 def cls(self, s):
247 """
247 """Clear screen.
248 os.system("cls")
248 """
249 os.system("cls")
249
250
250 #-----------------------------------------------------------------------------
251 #-----------------------------------------------------------------------------
251 # Main class
252 # Main class
@@ -664,15 +665,7 b' class TerminalInteractiveShell(InteractiveShell):'
664
665
665 def init_magics(self):
666 def init_magics(self):
666 super(TerminalInteractiveShell, self).init_magics()
667 super(TerminalInteractiveShell, self).init_magics()
667 self.define_magic('autoindent', magic_autoindent)
668 self.register_magics(TerminalMagics)
668 self.define_magic('cpaste', magic_cpaste)
669 self.define_magic('paste', magic_paste)
670 try:
671 magic_cls
672 except NameError:
673 pass
674 else:
675 self.define_magic('cls', magic_cls)
676
669
677 def showindentationerror(self):
670 def showindentationerror(self):
678 super(TerminalInteractiveShell, self).showindentationerror()
671 super(TerminalInteractiveShell, self).showindentationerror()
General Comments 0
You need to be logged in to leave comments. Login now