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