##// END OF EJS Templates
Backport PR #2816: in %edit, don't save "last_call" unless last call succeeded...
MinRK -
Show More
@@ -1,521 +1,522 b''
1 """Implementation of code management magic functions.
1 """Implementation of code management magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import inspect
16 import inspect
17 import io
17 import io
18 import json
18 import json
19 import os
19 import os
20 import sys
20 import sys
21 from urllib2 import urlopen
21 from urllib2 import urlopen
22
22
23 # Our own packages
23 # Our own packages
24 from IPython.core.error import TryNext, StdinNotImplementedError
24 from IPython.core.error import TryNext, StdinNotImplementedError
25 from IPython.core.macro import Macro
25 from IPython.core.macro import Macro
26 from IPython.core.magic import Magics, magics_class, line_magic
26 from IPython.core.magic import Magics, magics_class, line_magic
27 from IPython.core.oinspect import find_file, find_source_lines
27 from IPython.core.oinspect import find_file, find_source_lines
28 from IPython.testing.skipdoctest import skip_doctest
28 from IPython.testing.skipdoctest import skip_doctest
29 from IPython.utils import openpy
29 from IPython.utils import openpy
30 from IPython.utils import py3compat
30 from IPython.utils import py3compat
31 from IPython.utils.io import file_read
31 from IPython.utils.io import file_read
32 from IPython.utils.path import get_py_filename, unquote_filename
32 from IPython.utils.path import get_py_filename, unquote_filename
33 from IPython.utils.warn import warn
33 from IPython.utils.warn import warn
34
34
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36 # Magic implementation classes
36 # Magic implementation classes
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38
38
39 # Used for exception handling in magic_edit
39 # Used for exception handling in magic_edit
40 class MacroToEdit(ValueError): pass
40 class MacroToEdit(ValueError): pass
41
41
42
42
43 @magics_class
43 @magics_class
44 class CodeMagics(Magics):
44 class CodeMagics(Magics):
45 """Magics related to code management (loading, saving, editing, ...)."""
45 """Magics related to code management (loading, saving, editing, ...)."""
46
46
47 @line_magic
47 @line_magic
48 def save(self, parameter_s=''):
48 def save(self, parameter_s=''):
49 """Save a set of lines or a macro to a given filename.
49 """Save a set of lines or a macro to a given filename.
50
50
51 Usage:\\
51 Usage:\\
52 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
52 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
53
53
54 Options:
54 Options:
55
55
56 -r: use 'raw' input. By default, the 'processed' history is used,
56 -r: use 'raw' input. By default, the 'processed' history is used,
57 so that magics are loaded in their transformed version to valid
57 so that magics are loaded in their transformed version to valid
58 Python. If this option is given, the raw input as typed as the
58 Python. If this option is given, the raw input as typed as the
59 command line is used instead.
59 command line is used instead.
60
60
61 -f: force overwrite. If file exists, %save will prompt for overwrite
61 -f: force overwrite. If file exists, %save will prompt for overwrite
62 unless -f is given.
62 unless -f is given.
63
63
64 This function uses the same syntax as %history for input ranges,
64 This function uses the same syntax as %history for input ranges,
65 then saves the lines to the filename you specify.
65 then saves the lines to the filename you specify.
66
66
67 It adds a '.py' extension to the file if you don't do so yourself, and
67 It adds a '.py' extension to the file if you don't do so yourself, and
68 it asks for confirmation before overwriting existing files.
68 it asks for confirmation before overwriting existing files.
69
69
70 If `-r` option is used, the default extension is `.ipy`.
70 If `-r` option is used, the default extension is `.ipy`.
71 """
71 """
72
72
73 opts,args = self.parse_options(parameter_s,'fr',mode='list')
73 opts,args = self.parse_options(parameter_s,'fr',mode='list')
74 raw = 'r' in opts
74 raw = 'r' in opts
75 force = 'f' in opts
75 force = 'f' in opts
76 ext = u'.ipy' if raw else u'.py'
76 ext = u'.ipy' if raw else u'.py'
77 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
77 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
78 if not fname.endswith((u'.py',u'.ipy')):
78 if not fname.endswith((u'.py',u'.ipy')):
79 fname += ext
79 fname += ext
80 if os.path.isfile(fname) and not force:
80 if os.path.isfile(fname) and not force:
81 try:
81 try:
82 overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
82 overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
83 except StdinNotImplementedError:
83 except StdinNotImplementedError:
84 print "File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s)
84 print "File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s)
85 return
85 return
86 if not overwrite :
86 if not overwrite :
87 print 'Operation cancelled.'
87 print 'Operation cancelled.'
88 return
88 return
89 try:
89 try:
90 cmds = self.shell.find_user_code(codefrom,raw)
90 cmds = self.shell.find_user_code(codefrom,raw)
91 except (TypeError, ValueError) as e:
91 except (TypeError, ValueError) as e:
92 print e.args[0]
92 print e.args[0]
93 return
93 return
94 with io.open(fname,'w', encoding="utf-8") as f:
94 with io.open(fname,'w', encoding="utf-8") as f:
95 f.write(u"# coding: utf-8\n")
95 f.write(u"# coding: utf-8\n")
96 f.write(py3compat.cast_unicode(cmds))
96 f.write(py3compat.cast_unicode(cmds))
97 print 'The following commands were written to file `%s`:' % fname
97 print 'The following commands were written to file `%s`:' % fname
98 print cmds
98 print cmds
99
99
100 @line_magic
100 @line_magic
101 def pastebin(self, parameter_s=''):
101 def pastebin(self, parameter_s=''):
102 """Upload code to Github's Gist paste bin, returning the URL.
102 """Upload code to Github's Gist paste bin, returning the URL.
103
103
104 Usage:\\
104 Usage:\\
105 %pastebin [-d "Custom description"] 1-7
105 %pastebin [-d "Custom description"] 1-7
106
106
107 The argument can be an input history range, a filename, or the name of a
107 The argument can be an input history range, a filename, or the name of a
108 string or macro.
108 string or macro.
109
109
110 Options:
110 Options:
111
111
112 -d: Pass a custom description for the gist. The default will say
112 -d: Pass a custom description for the gist. The default will say
113 "Pasted from IPython".
113 "Pasted from IPython".
114 """
114 """
115 opts, args = self.parse_options(parameter_s, 'd:')
115 opts, args = self.parse_options(parameter_s, 'd:')
116
116
117 try:
117 try:
118 code = self.shell.find_user_code(args)
118 code = self.shell.find_user_code(args)
119 except (ValueError, TypeError) as e:
119 except (ValueError, TypeError) as e:
120 print e.args[0]
120 print e.args[0]
121 return
121 return
122
122
123 post_data = json.dumps({
123 post_data = json.dumps({
124 "description": opts.get('d', "Pasted from IPython"),
124 "description": opts.get('d', "Pasted from IPython"),
125 "public": True,
125 "public": True,
126 "files": {
126 "files": {
127 "file1.py": {
127 "file1.py": {
128 "content": code
128 "content": code
129 }
129 }
130 }
130 }
131 }).encode('utf-8')
131 }).encode('utf-8')
132
132
133 response = urlopen("https://api.github.com/gists", post_data)
133 response = urlopen("https://api.github.com/gists", post_data)
134 response_data = json.loads(response.read().decode('utf-8'))
134 response_data = json.loads(response.read().decode('utf-8'))
135 return response_data['html_url']
135 return response_data['html_url']
136
136
137 @line_magic
137 @line_magic
138 def loadpy(self, arg_s):
138 def loadpy(self, arg_s):
139 """Alias of `%load`
139 """Alias of `%load`
140
140
141 `%loadpy` has gained some flexibility and droped the requirement of a `.py`
141 `%loadpy` has gained some flexibility and droped the requirement of a `.py`
142 extension. So it has been renamed simply into %load. You can look at
142 extension. So it has been renamed simply into %load. You can look at
143 `%load`'s docstring for more info.
143 `%load`'s docstring for more info.
144 """
144 """
145 self.load(arg_s)
145 self.load(arg_s)
146
146
147 @line_magic
147 @line_magic
148 def load(self, arg_s):
148 def load(self, arg_s):
149 """Load code into the current frontend.
149 """Load code into the current frontend.
150
150
151 Usage:\\
151 Usage:\\
152 %load [options] source
152 %load [options] source
153
153
154 where source can be a filename, URL, input history range or macro
154 where source can be a filename, URL, input history range or macro
155
155
156 Options:
156 Options:
157 --------
157 --------
158 -y : Don't ask confirmation for loading source above 200 000 characters.
158 -y : Don't ask confirmation for loading source above 200 000 characters.
159
159
160 This magic command can either take a local filename, a URL, an history
160 This magic command can either take a local filename, a URL, an history
161 range (see %history) or a macro as argument, it will prompt for
161 range (see %history) or a macro as argument, it will prompt for
162 confirmation before loading source with more than 200 000 characters, unless
162 confirmation before loading source with more than 200 000 characters, unless
163 -y flag is passed or if the frontend does not support raw_input::
163 -y flag is passed or if the frontend does not support raw_input::
164
164
165 %load myscript.py
165 %load myscript.py
166 %load 7-27
166 %load 7-27
167 %load myMacro
167 %load myMacro
168 %load http://www.example.com/myscript.py
168 %load http://www.example.com/myscript.py
169 """
169 """
170 opts,args = self.parse_options(arg_s,'y')
170 opts,args = self.parse_options(arg_s,'y')
171
171
172 contents = self.shell.find_user_code(args)
172 contents = self.shell.find_user_code(args)
173 l = len(contents)
173 l = len(contents)
174
174
175 # 200 000 is ~ 2500 full 80 caracter lines
175 # 200 000 is ~ 2500 full 80 caracter lines
176 # so in average, more than 5000 lines
176 # so in average, more than 5000 lines
177 if l > 200000 and 'y' not in opts:
177 if l > 200000 and 'y' not in opts:
178 try:
178 try:
179 ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
179 ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
180 " (%d characters). Continue (y/[N]) ?" % l), default='n' )
180 " (%d characters). Continue (y/[N]) ?" % l), default='n' )
181 except StdinNotImplementedError:
181 except StdinNotImplementedError:
182 #asume yes if raw input not implemented
182 #asume yes if raw input not implemented
183 ans = True
183 ans = True
184
184
185 if ans is False :
185 if ans is False :
186 print 'Operation cancelled.'
186 print 'Operation cancelled.'
187 return
187 return
188
188
189 self.shell.set_next_input(contents)
189 self.shell.set_next_input(contents)
190
190
191 @staticmethod
191 @staticmethod
192 def _find_edit_target(shell, args, opts, last_call):
192 def _find_edit_target(shell, args, opts, last_call):
193 """Utility method used by magic_edit to find what to edit."""
193 """Utility method used by magic_edit to find what to edit."""
194
194
195 def make_filename(arg):
195 def make_filename(arg):
196 "Make a filename from the given args"
196 "Make a filename from the given args"
197 arg = unquote_filename(arg)
197 arg = unquote_filename(arg)
198 try:
198 try:
199 filename = get_py_filename(arg)
199 filename = get_py_filename(arg)
200 except IOError:
200 except IOError:
201 # If it ends with .py but doesn't already exist, assume we want
201 # If it ends with .py but doesn't already exist, assume we want
202 # a new file.
202 # a new file.
203 if arg.endswith('.py'):
203 if arg.endswith('.py'):
204 filename = arg
204 filename = arg
205 else:
205 else:
206 filename = None
206 filename = None
207 return filename
207 return filename
208
208
209 # Set a few locals from the options for convenience:
209 # Set a few locals from the options for convenience:
210 opts_prev = 'p' in opts
210 opts_prev = 'p' in opts
211 opts_raw = 'r' in opts
211 opts_raw = 'r' in opts
212
212
213 # custom exceptions
213 # custom exceptions
214 class DataIsObject(Exception): pass
214 class DataIsObject(Exception): pass
215
215
216 # Default line number value
216 # Default line number value
217 lineno = opts.get('n',None)
217 lineno = opts.get('n',None)
218
218
219 if opts_prev:
219 if opts_prev:
220 args = '_%s' % last_call[0]
220 args = '_%s' % last_call[0]
221 if not shell.user_ns.has_key(args):
221 if not shell.user_ns.has_key(args):
222 args = last_call[1]
222 args = last_call[1]
223
223
224 # use last_call to remember the state of the previous call, but don't
225 # let it be clobbered by successive '-p' calls.
226 try:
227 last_call[0] = shell.displayhook.prompt_count
228 if not opts_prev:
229 last_call[1] = args
230 except:
231 pass
232
233 # by default this is done with temp files, except when the given
224 # by default this is done with temp files, except when the given
234 # arg is a filename
225 # arg is a filename
235 use_temp = True
226 use_temp = True
236
227
237 data = ''
228 data = ''
238
229
239 # First, see if the arguments should be a filename.
230 # First, see if the arguments should be a filename.
240 filename = make_filename(args)
231 filename = make_filename(args)
241 if filename:
232 if filename:
242 use_temp = False
233 use_temp = False
243 elif args:
234 elif args:
244 # Mode where user specifies ranges of lines, like in %macro.
235 # Mode where user specifies ranges of lines, like in %macro.
245 data = shell.extract_input_lines(args, opts_raw)
236 data = shell.extract_input_lines(args, opts_raw)
246 if not data:
237 if not data:
247 try:
238 try:
248 # Load the parameter given as a variable. If not a string,
239 # Load the parameter given as a variable. If not a string,
249 # process it as an object instead (below)
240 # process it as an object instead (below)
250
241
251 #print '*** args',args,'type',type(args) # dbg
242 #print '*** args',args,'type',type(args) # dbg
252 data = eval(args, shell.user_ns)
243 data = eval(args, shell.user_ns)
253 if not isinstance(data, basestring):
244 if not isinstance(data, basestring):
254 raise DataIsObject
245 raise DataIsObject
255
246
256 except (NameError,SyntaxError):
247 except (NameError,SyntaxError):
257 # given argument is not a variable, try as a filename
248 # given argument is not a variable, try as a filename
258 filename = make_filename(args)
249 filename = make_filename(args)
259 if filename is None:
250 if filename is None:
260 warn("Argument given (%s) can't be found as a variable "
251 warn("Argument given (%s) can't be found as a variable "
261 "or as a filename." % args)
252 "or as a filename." % args)
262 return
253 return
263 use_temp = False
254 use_temp = False
264
255
265 except DataIsObject:
256 except DataIsObject:
266 # macros have a special edit function
257 # macros have a special edit function
267 if isinstance(data, Macro):
258 if isinstance(data, Macro):
268 raise MacroToEdit(data)
259 raise MacroToEdit(data)
269
260
270 # For objects, try to edit the file where they are defined
261 # For objects, try to edit the file where they are defined
271 filename = find_file(data)
262 filename = find_file(data)
272 if filename:
263 if filename:
273 if 'fakemodule' in filename.lower() and \
264 if 'fakemodule' in filename.lower() and \
274 inspect.isclass(data):
265 inspect.isclass(data):
275 # class created by %edit? Try to find source
266 # class created by %edit? Try to find source
276 # by looking for method definitions instead, the
267 # by looking for method definitions instead, the
277 # __module__ in those classes is FakeModule.
268 # __module__ in those classes is FakeModule.
278 attrs = [getattr(data, aname) for aname in dir(data)]
269 attrs = [getattr(data, aname) for aname in dir(data)]
279 for attr in attrs:
270 for attr in attrs:
280 if not inspect.ismethod(attr):
271 if not inspect.ismethod(attr):
281 continue
272 continue
282 filename = find_file(attr)
273 filename = find_file(attr)
283 if filename and \
274 if filename and \
284 'fakemodule' not in filename.lower():
275 'fakemodule' not in filename.lower():
285 # change the attribute to be the edit
276 # change the attribute to be the edit
286 # target instead
277 # target instead
287 data = attr
278 data = attr
288 break
279 break
289
280
290 datafile = 1
281 datafile = 1
291 if filename is None:
282 if filename is None:
292 filename = make_filename(args)
283 filename = make_filename(args)
293 datafile = 1
284 datafile = 1
294 warn('Could not find file where `%s` is defined.\n'
285 warn('Could not find file where `%s` is defined.\n'
295 'Opening a file named `%s`' % (args, filename))
286 'Opening a file named `%s`' % (args, filename))
296 # Now, make sure we can actually read the source (if it was
287 # Now, make sure we can actually read the source (if it was
297 # in a temp file it's gone by now).
288 # in a temp file it's gone by now).
298 if datafile:
289 if datafile:
299 if lineno is None:
290 if lineno is None:
300 lineno = find_source_lines(data)
291 lineno = find_source_lines(data)
301 if lineno is None:
292 if lineno is None:
302 filename = make_filename(args)
293 filename = make_filename(args)
303 if filename is None:
294 if filename is None:
304 warn('The file `%s` where `%s` was defined '
295 warn('The file `%s` where `%s` was defined '
305 'cannot be read.' % (filename, data))
296 'cannot be read.' % (filename, data))
306 return
297 return
307 use_temp = False
298 use_temp = False
308
299
309 if use_temp:
300 if use_temp:
310 filename = shell.mktempfile(data)
301 filename = shell.mktempfile(data)
311 print 'IPython will make a temporary file named:',filename
302 print 'IPython will make a temporary file named:',filename
312
303
304 # use last_call to remember the state of the previous call, but don't
305 # let it be clobbered by successive '-p' calls.
306 try:
307 last_call[0] = shell.displayhook.prompt_count
308 if not opts_prev:
309 last_call[1] = args
310 except:
311 pass
312
313
313 return filename, lineno, use_temp
314 return filename, lineno, use_temp
314
315
315 def _edit_macro(self,mname,macro):
316 def _edit_macro(self,mname,macro):
316 """open an editor with the macro data in a file"""
317 """open an editor with the macro data in a file"""
317 filename = self.shell.mktempfile(macro.value)
318 filename = self.shell.mktempfile(macro.value)
318 self.shell.hooks.editor(filename)
319 self.shell.hooks.editor(filename)
319
320
320 # and make a new macro object, to replace the old one
321 # and make a new macro object, to replace the old one
321 mfile = open(filename)
322 mfile = open(filename)
322 mvalue = mfile.read()
323 mvalue = mfile.read()
323 mfile.close()
324 mfile.close()
324 self.shell.user_ns[mname] = Macro(mvalue)
325 self.shell.user_ns[mname] = Macro(mvalue)
325
326
326 @line_magic
327 @line_magic
327 def ed(self, parameter_s=''):
328 def ed(self, parameter_s=''):
328 """Alias to %edit."""
329 """Alias to %edit."""
329 return self.edit(parameter_s)
330 return self.edit(parameter_s)
330
331
331 @skip_doctest
332 @skip_doctest
332 @line_magic
333 @line_magic
333 def edit(self, parameter_s='',last_call=['','']):
334 def edit(self, parameter_s='',last_call=['','']):
334 """Bring up an editor and execute the resulting code.
335 """Bring up an editor and execute the resulting code.
335
336
336 Usage:
337 Usage:
337 %edit [options] [args]
338 %edit [options] [args]
338
339
339 %edit runs IPython's editor hook. The default version of this hook is
340 %edit runs IPython's editor hook. The default version of this hook is
340 set to call the editor specified by your $EDITOR environment variable.
341 set to call the editor specified by your $EDITOR environment variable.
341 If this isn't found, it will default to vi under Linux/Unix and to
342 If this isn't found, it will default to vi under Linux/Unix and to
342 notepad under Windows. See the end of this docstring for how to change
343 notepad under Windows. See the end of this docstring for how to change
343 the editor hook.
344 the editor hook.
344
345
345 You can also set the value of this editor via the
346 You can also set the value of this editor via the
346 ``TerminalInteractiveShell.editor`` option in your configuration file.
347 ``TerminalInteractiveShell.editor`` option in your configuration file.
347 This is useful if you wish to use a different editor from your typical
348 This is useful if you wish to use a different editor from your typical
348 default with IPython (and for Windows users who typically don't set
349 default with IPython (and for Windows users who typically don't set
349 environment variables).
350 environment variables).
350
351
351 This command allows you to conveniently edit multi-line code right in
352 This command allows you to conveniently edit multi-line code right in
352 your IPython session.
353 your IPython session.
353
354
354 If called without arguments, %edit opens up an empty editor with a
355 If called without arguments, %edit opens up an empty editor with a
355 temporary file and will execute the contents of this file when you
356 temporary file and will execute the contents of this file when you
356 close it (don't forget to save it!).
357 close it (don't forget to save it!).
357
358
358
359
359 Options:
360 Options:
360
361
361 -n <number>: open the editor at a specified line number. By default,
362 -n <number>: open the editor at a specified line number. By default,
362 the IPython editor hook uses the unix syntax 'editor +N filename', but
363 the IPython editor hook uses the unix syntax 'editor +N filename', but
363 you can configure this by providing your own modified hook if your
364 you can configure this by providing your own modified hook if your
364 favorite editor supports line-number specifications with a different
365 favorite editor supports line-number specifications with a different
365 syntax.
366 syntax.
366
367
367 -p: this will call the editor with the same data as the previous time
368 -p: this will call the editor with the same data as the previous time
368 it was used, regardless of how long ago (in your current session) it
369 it was used, regardless of how long ago (in your current session) it
369 was.
370 was.
370
371
371 -r: use 'raw' input. This option only applies to input taken from the
372 -r: use 'raw' input. This option only applies to input taken from the
372 user's history. By default, the 'processed' history is used, so that
373 user's history. By default, the 'processed' history is used, so that
373 magics are loaded in their transformed version to valid Python. If
374 magics are loaded in their transformed version to valid Python. If
374 this option is given, the raw input as typed as the command line is
375 this option is given, the raw input as typed as the command line is
375 used instead. When you exit the editor, it will be executed by
376 used instead. When you exit the editor, it will be executed by
376 IPython's own processor.
377 IPython's own processor.
377
378
378 -x: do not execute the edited code immediately upon exit. This is
379 -x: do not execute the edited code immediately upon exit. This is
379 mainly useful if you are editing programs which need to be called with
380 mainly useful if you are editing programs which need to be called with
380 command line arguments, which you can then do using %run.
381 command line arguments, which you can then do using %run.
381
382
382
383
383 Arguments:
384 Arguments:
384
385
385 If arguments are given, the following possibilities exist:
386 If arguments are given, the following possibilities exist:
386
387
387 - If the argument is a filename, IPython will load that into the
388 - If the argument is a filename, IPython will load that into the
388 editor. It will execute its contents with execfile() when you exit,
389 editor. It will execute its contents with execfile() when you exit,
389 loading any code in the file into your interactive namespace.
390 loading any code in the file into your interactive namespace.
390
391
391 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
392 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
392 The syntax is the same as in the %history magic.
393 The syntax is the same as in the %history magic.
393
394
394 - If the argument is a string variable, its contents are loaded
395 - If the argument is a string variable, its contents are loaded
395 into the editor. You can thus edit any string which contains
396 into the editor. You can thus edit any string which contains
396 python code (including the result of previous edits).
397 python code (including the result of previous edits).
397
398
398 - If the argument is the name of an object (other than a string),
399 - If the argument is the name of an object (other than a string),
399 IPython will try to locate the file where it was defined and open the
400 IPython will try to locate the file where it was defined and open the
400 editor at the point where it is defined. You can use `%edit function`
401 editor at the point where it is defined. You can use `%edit function`
401 to load an editor exactly at the point where 'function' is defined,
402 to load an editor exactly at the point where 'function' is defined,
402 edit it and have the file be executed automatically.
403 edit it and have the file be executed automatically.
403
404
404 - If the object is a macro (see %macro for details), this opens up your
405 - If the object is a macro (see %macro for details), this opens up your
405 specified editor with a temporary file containing the macro's data.
406 specified editor with a temporary file containing the macro's data.
406 Upon exit, the macro is reloaded with the contents of the file.
407 Upon exit, the macro is reloaded with the contents of the file.
407
408
408 Note: opening at an exact line is only supported under Unix, and some
409 Note: opening at an exact line is only supported under Unix, and some
409 editors (like kedit and gedit up to Gnome 2.8) do not understand the
410 editors (like kedit and gedit up to Gnome 2.8) do not understand the
410 '+NUMBER' parameter necessary for this feature. Good editors like
411 '+NUMBER' parameter necessary for this feature. Good editors like
411 (X)Emacs, vi, jed, pico and joe all do.
412 (X)Emacs, vi, jed, pico and joe all do.
412
413
413 After executing your code, %edit will return as output the code you
414 After executing your code, %edit will return as output the code you
414 typed in the editor (except when it was an existing file). This way
415 typed in the editor (except when it was an existing file). This way
415 you can reload the code in further invocations of %edit as a variable,
416 you can reload the code in further invocations of %edit as a variable,
416 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
417 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
417 the output.
418 the output.
418
419
419 Note that %edit is also available through the alias %ed.
420 Note that %edit is also available through the alias %ed.
420
421
421 This is an example of creating a simple function inside the editor and
422 This is an example of creating a simple function inside the editor and
422 then modifying it. First, start up the editor::
423 then modifying it. First, start up the editor::
423
424
424 In [1]: ed
425 In [1]: ed
425 Editing... done. Executing edited code...
426 Editing... done. Executing edited code...
426 Out[1]: 'def foo():\\n print "foo() was defined in an editing
427 Out[1]: 'def foo():\\n print "foo() was defined in an editing
427 session"\\n'
428 session"\\n'
428
429
429 We can then call the function foo()::
430 We can then call the function foo()::
430
431
431 In [2]: foo()
432 In [2]: foo()
432 foo() was defined in an editing session
433 foo() was defined in an editing session
433
434
434 Now we edit foo. IPython automatically loads the editor with the
435 Now we edit foo. IPython automatically loads the editor with the
435 (temporary) file where foo() was previously defined::
436 (temporary) file where foo() was previously defined::
436
437
437 In [3]: ed foo
438 In [3]: ed foo
438 Editing... done. Executing edited code...
439 Editing... done. Executing edited code...
439
440
440 And if we call foo() again we get the modified version::
441 And if we call foo() again we get the modified version::
441
442
442 In [4]: foo()
443 In [4]: foo()
443 foo() has now been changed!
444 foo() has now been changed!
444
445
445 Here is an example of how to edit a code snippet successive
446 Here is an example of how to edit a code snippet successive
446 times. First we call the editor::
447 times. First we call the editor::
447
448
448 In [5]: ed
449 In [5]: ed
449 Editing... done. Executing edited code...
450 Editing... done. Executing edited code...
450 hello
451 hello
451 Out[5]: "print 'hello'\\n"
452 Out[5]: "print 'hello'\\n"
452
453
453 Now we call it again with the previous output (stored in _)::
454 Now we call it again with the previous output (stored in _)::
454
455
455 In [6]: ed _
456 In [6]: ed _
456 Editing... done. Executing edited code...
457 Editing... done. Executing edited code...
457 hello world
458 hello world
458 Out[6]: "print 'hello world'\\n"
459 Out[6]: "print 'hello world'\\n"
459
460
460 Now we call it with the output #8 (stored in _8, also as Out[8])::
461 Now we call it with the output #8 (stored in _8, also as Out[8])::
461
462
462 In [7]: ed _8
463 In [7]: ed _8
463 Editing... done. Executing edited code...
464 Editing... done. Executing edited code...
464 hello again
465 hello again
465 Out[7]: "print 'hello again'\\n"
466 Out[7]: "print 'hello again'\\n"
466
467
467
468
468 Changing the default editor hook:
469 Changing the default editor hook:
469
470
470 If you wish to write your own editor hook, you can put it in a
471 If you wish to write your own editor hook, you can put it in a
471 configuration file which you load at startup time. The default hook
472 configuration file which you load at startup time. The default hook
472 is defined in the IPython.core.hooks module, and you can use that as a
473 is defined in the IPython.core.hooks module, and you can use that as a
473 starting example for further modifications. That file also has
474 starting example for further modifications. That file also has
474 general instructions on how to set a new hook for use once you've
475 general instructions on how to set a new hook for use once you've
475 defined it."""
476 defined it."""
476 opts,args = self.parse_options(parameter_s,'prxn:')
477 opts,args = self.parse_options(parameter_s,'prxn:')
477
478
478 try:
479 try:
479 filename, lineno, is_temp = self._find_edit_target(self.shell,
480 filename, lineno, is_temp = self._find_edit_target(self.shell,
480 args, opts, last_call)
481 args, opts, last_call)
481 except MacroToEdit as e:
482 except MacroToEdit as e:
482 self._edit_macro(args, e.args[0])
483 self._edit_macro(args, e.args[0])
483 return
484 return
484
485
485 # do actual editing here
486 # do actual editing here
486 print 'Editing...',
487 print 'Editing...',
487 sys.stdout.flush()
488 sys.stdout.flush()
488 try:
489 try:
489 # Quote filenames that may have spaces in them
490 # Quote filenames that may have spaces in them
490 if ' ' in filename:
491 if ' ' in filename:
491 filename = "'%s'" % filename
492 filename = "'%s'" % filename
492 self.shell.hooks.editor(filename,lineno)
493 self.shell.hooks.editor(filename,lineno)
493 except TryNext:
494 except TryNext:
494 warn('Could not open editor')
495 warn('Could not open editor')
495 return
496 return
496
497
497 # XXX TODO: should this be generalized for all string vars?
498 # XXX TODO: should this be generalized for all string vars?
498 # For now, this is special-cased to blocks created by cpaste
499 # For now, this is special-cased to blocks created by cpaste
499 if args.strip() == 'pasted_block':
500 if args.strip() == 'pasted_block':
500 self.shell.user_ns['pasted_block'] = file_read(filename)
501 self.shell.user_ns['pasted_block'] = file_read(filename)
501
502
502 if 'x' in opts: # -x prevents actual execution
503 if 'x' in opts: # -x prevents actual execution
503 print
504 print
504 else:
505 else:
505 print 'done. Executing edited code...'
506 print 'done. Executing edited code...'
506 if 'r' in opts: # Untranslated IPython code
507 if 'r' in opts: # Untranslated IPython code
507 self.shell.run_cell(file_read(filename),
508 self.shell.run_cell(file_read(filename),
508 store_history=False)
509 store_history=False)
509 else:
510 else:
510 self.shell.safe_execfile(filename, self.shell.user_ns,
511 self.shell.safe_execfile(filename, self.shell.user_ns,
511 self.shell.user_ns)
512 self.shell.user_ns)
512
513
513 if is_temp:
514 if is_temp:
514 try:
515 try:
515 return open(filename).read()
516 return open(filename).read()
516 except IOError,msg:
517 except IOError,msg:
517 if msg.filename == filename:
518 if msg.filename == filename:
518 warn('File not found. Did you forget to save?')
519 warn('File not found. Did you forget to save?')
519 return
520 return
520 else:
521 else:
521 self.shell.showtraceback()
522 self.shell.showtraceback()
General Comments 0
You need to be logged in to leave comments. Login now