##// END OF EJS Templates
Added support for automatically reopening the editor if the file had a...
fperez -
Show More
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 958 2005-12-27 23:17:51Z fperez $"""
4 $Id: Magic.py 960 2005-12-28 06:51:01Z fperez $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -1875,7 +1875,7 b' Currently the magic system has the following functions:\\n"""'
1875 1875 else:
1876 1876 print 'done. Executing edited code...'
1877 1877 try:
1878 execfile(filename,self.shell.user_ns)
1878 self.shell.safe_execfile(filename,self.shell.user_ns)
1879 1879 except IOError,msg:
1880 1880 if msg.filename == filename:
1881 1881 warn('File not found. Did you forget to save?')
@@ -2,7 +2,7 b''
2 2 """
3 3 Classes for handling input/output prompts.
4 4
5 $Id: Prompts.py 958 2005-12-27 23:17:51Z fperez $"""
5 $Id: Prompts.py 960 2005-12-28 06:51:01Z fperez $"""
6 6
7 7 #*****************************************************************************
8 8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
@@ -494,6 +494,8 b' class CachedOutput:'
494 494 if arg is not None:
495 495 cout_write = Term.cout.write # fast lookup
496 496 # first handle the cache and counters
497 # but avoid recursive reference when displaying _oh/Out
498 if arg is not self.user_ns['_oh']:
497 499 self.update(arg)
498 500 # do not print output if input ends in ';'
499 501 if self.input_hist[self.prompt_count].endswith(';\n'):
@@ -5,7 +5,7 b' General purpose utilities.'
5 5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 6 these things are also convenient when working at the command line.
7 7
8 $Id: genutils.py 958 2005-12-27 23:17:51Z fperez $"""
8 $Id: genutils.py 960 2005-12-28 06:51:01Z fperez $"""
9 9
10 10 #*****************************************************************************
11 11 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
@@ -948,7 +948,7 b' def ask_yes_no(prompt,default=None):'
948 948
949 949 Valid answers are: y/yes/n/no (match is not case sensitive)."""
950 950
951 answers = {'y':1,'n':0,'yes':1,'no':0}
951 answers = {'y':True,'n':False,'yes':True,'no':False}
952 952 ans = None
953 953 eofs, max_eofs = 0, 20
954 954 while ans not in answers.keys():
@@ -32,7 +32,7 b" ip_set_hook('editor',myiphooks.calljed)"
32 32 The ip_set_hook function is put by IPython into the builtin namespace, so it
33 33 is always available from all running code.
34 34
35 $Id: hooks.py 535 2005-03-02 08:42:25Z fperez $"""
35 $Id: hooks.py 960 2005-12-28 06:51:01Z fperez $"""
36 36
37 37 #*****************************************************************************
38 38 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
@@ -48,9 +48,9 b' __version__ = Release.version'
48 48
49 49 import os
50 50
51 # List here all the default hooks. For now it's just the editor, but over
52 # time we'll move here all the public API for user-accessible things.
53 __all__ = ['editor']
51 # List here all the default hooks. For now it's just the editor functions
52 # but over time we'll move here all the public API for user-accessible things.
53 __all__ = ['editor', 'fix_error_editor']
54 54
55 55 def editor(self,filename, linenum):
56 56 """Open the default editor at the given filename and linenumber.
@@ -70,3 +70,26 b' def editor(self,filename, linenum):'
70 70 linemark = '+%d' % linenum
71 71 # Call the actual editor
72 72 os.system('%s %s %s' % (editor,linemark,filename))
73
74 import tempfile
75 def fix_error_editor(self,filename,linenum,column,msg):
76 """Open the editor at the given filename, linenumber, column and
77 show an error message. This is used for correcting syntax errors.
78 The current implementation only has special support for the VIM editor,
79 and falls back on the 'editor' hook if VIM is not used.
80
81 Call ip_set_hook('fix_error_editor',youfunc) to use your own function,
82 """
83 def vim_quickfix_file():
84 t = tempfile.NamedTemporaryFile()
85 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
86 t.flush()
87 return t
88 if os.path.basename(self.rc.editor) != 'vim':
89 self.hooks.editor(filename,linenum)
90 return
91 t = vim_quickfix_file()
92 try:
93 os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name)
94 finally:
95 t.close()
@@ -6,7 +6,7 b' Requires Python 2.1 or newer.'
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 959 2005-12-28 02:04:41Z fperez $
9 $Id: iplib.py 960 2005-12-28 06:51:01Z fperez $
10 10 """
11 11
12 12 #*****************************************************************************
@@ -212,6 +212,23 b' class InputList(list):'
212 212 def __getslice__(self,i,j):
213 213 return ''.join(list.__getslice__(self,i,j))
214 214
215 class SyntaxTB(ultraTB.ListTB):
216 """Extension which holds some state: the last exception value"""
217
218 def __init__(self,color_scheme = 'NoColor'):
219 ultraTB.ListTB.__init__(self,color_scheme)
220 self.last_syntax_error = None
221
222 def __call__(self, etype, value, elist):
223 self.last_syntax_error = value
224 ultraTB.ListTB.__call__(self,etype,value,elist)
225
226 def clear_err_state(self):
227 """Return the current error state and clear it"""
228 e = self.last_syntax_error
229 self.last_syntax_error = None
230 return e
231
215 232 #****************************************************************************
216 233 # Main IPython class
217 234 class InteractiveShell(Logger, Magic):
@@ -544,10 +561,10 b' class InteractiveShell(Logger, Magic):'
544 561
545 562 # TraceBack handlers:
546 563 # Need two, one for syntax errors and one for other exceptions.
547 self.SyntaxTB = ultraTB.ListTB(color_scheme='NoColor')
548 # This one is initialized with an offset, meaning we always want to
549 # remove the topmost item in the traceback, which is our own internal
550 # code. Valid modes: ['Plain','Context','Verbose']
564 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
565 # The interactive one is initialized with an offset, meaning we always
566 # want to remove the topmost item in the traceback, which is our own
567 # internal code. Valid modes: ['Plain','Context','Verbose']
551 568 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
552 569 color_scheme='NoColor',
553 570 tb_offset = 1)
@@ -1022,6 +1039,44 b' want to merge them back into the new files.""" % locals()'
1022 1039 # Configure auto-indent for all platforms
1023 1040 self.set_autoindent(self.rc.autoindent)
1024 1041
1042 def _should_recompile(self,e):
1043 """Utility routine for edit_syntax_error"""
1044
1045 if e.filename in ('<ipython console>','<input>','<string>',
1046 '<console>'):
1047 return False
1048 try:
1049 if not ask_yes_no('Return to editor to correct syntax error? '
1050 '[Y/n] ','y'):
1051 return False
1052 except EOFError:
1053 return False
1054 self.hooks.fix_error_editor(e.filename,e.lineno,e.offset,e.msg)
1055 return True
1056
1057 def edit_syntax_error(self):
1058 """The bottom half of the syntax error handler called in the main loop.
1059
1060 Loop until syntax error is fixed or user cancels.
1061 """
1062
1063 while self.SyntaxTB.last_syntax_error:
1064 # copy and clear last_syntax_error
1065 err = self.SyntaxTB.clear_err_state()
1066 if not self._should_recompile(err):
1067 return
1068 try:
1069 # may set last_syntax_error again if a SyntaxError is raised
1070 self.safe_execfile(err.filename,self.shell.user_ns)
1071 except:
1072 self.showtraceback()
1073 else:
1074 f = file(err.filename)
1075 try:
1076 sys.displayhook(f.read())
1077 finally:
1078 f.close()
1079
1025 1080 def showsyntaxerror(self, filename=None):
1026 1081 """Display the syntax error that just occurred.
1027 1082
@@ -1221,10 +1276,15 b' want to merge them back into the new files.""" % locals()'
1221 1276 self.indent_current_nsp -= 4
1222 1277 else:
1223 1278 self.indent_current_nsp = 0
1279
1224 1280 # indent_current is the actual string to be inserted
1225 1281 # by the readline hooks for indentation
1226 1282 self.indent_current = ' '* self.indent_current_nsp
1227 1283
1284 if (self.SyntaxTB.last_syntax_error and
1285 self.rc.autoedit_syntax):
1286 self.edit_syntax_error()
1287
1228 1288 except KeyboardInterrupt:
1229 1289 self.write("\nKeyboardInterrupt\n")
1230 1290 self.resetbuffer()
@@ -6,7 +6,7 b' Requires Python 2.1 or better.'
6 6
7 7 This file contains the main make_IPython() starter function.
8 8
9 $Id: ipmaker.py 958 2005-12-27 23:17:51Z fperez $"""
9 $Id: ipmaker.py 960 2005-12-28 06:51:01Z fperez $"""
10 10
11 11 #*****************************************************************************
12 12 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
@@ -161,7 +161,8 b" object? -> Details about 'object'. ?object also works, ?? prints more."
161 161 'rcfile=s separate_in|si=s separate_out|so=s '
162 162 'separate_out2|so2=s xmode=s wildcards_case_sensitive! '
163 163 'magic_docstrings system_verbose! '
164 'multi_line_specials!')
164 'multi_line_specials! '
165 'autoedit_syntax!')
165 166
166 167 # Options that can *only* appear at the cmd line (not in rcfiles).
167 168
@@ -224,6 +225,7 b" object? -> Details about 'object'. ?object also works, ?? prints more."
224 225 xmode = 'Verbose',
225 226 wildcards_case_sensitive = 1,
226 227 magic_docstrings = 0, # undocumented, for doc generation
228 autoedit_syntax = 0,
227 229 )
228 230
229 231 # Things that will *only* appear in rcfiles (not at the command line).
@@ -6,7 +6,7 b''
6 6 # the file COPYING, distributed as part of this software.
7 7 #*****************************************************************************
8 8
9 # $Id: usage.py 926 2005-12-01 18:14:21Z fperez $
9 # $Id: usage.py 960 2005-12-28 06:51:01Z fperez $
10 10
11 11 from IPython import Release
12 12 __author__ = '%s <%s>' % Release.authors['Fernando']
@@ -154,13 +154,12 b' REGULAR OPTIONS'
154 154
155 155 -[no]automagic
156 156 Make magic commands automatic (without needing their first char-
157 acter to be @). Type @magic at the IPython prompt for more
157 acter to be %). Type %magic at the IPython prompt for more
158 158 information.
159 159
160 -[no]autoparens
161 Make IPython automatically call any callable object even if you
162 didn’t type explicit parentheses. For example, ’str 43’ becomes
163 ’str(43)’ automatically.
160 -[no]autoedit_syntax
161 When a syntax error occurs after editing a file, automatically
162 open the file to the trouble causing line for convenient fixing.
164 163
165 164 -[no]banner
166 165 Print the intial information banner (default on).
@@ -4,6 +4,11 b''
4 4 input with emtpy lines. This fixes
5 5 http://www.scipy.net/roundup/ipython/issue43 and a similar
6 6 discussion on the user list.
7 (edit_syntax_error): added support for automatically reopening the
8 editor if the file had a syntax error in it. Thanks to scottt who
9 provided the patch at:
10 http://www.scipy.net/roundup/ipython/issue36 (slightly modified
11 version committed).
7 12
8 13 WARNING: a behavior change is necessarily introduced to support
9 14 blank lines: now a single blank line with whitespace does NOT
@@ -124,12 +124,11 b' Turn automatic indentation on/off.'
124 124 .TP
125 125 .B \-[no]automagic
126 126 Make magic commands automatic (without needing their first character
127 to be @). Type @magic at the IPython prompt for more information.
127 to be %). Type %magic at the IPython prompt for more information.
128 128 .TP
129 .B \-[no]autoparens
130 Make IPython automatically call any callable object even if you didn't
131 type explicit parentheses. For example, 'str 43' becomes 'str(43)'
132 automatically.
129 .B \-[no]autoedit_syntax
130 When a syntax error occurs after editing a file, automatically open the file
131 to the trouble causing line for convenient fixing.
133 132 .TP
134 133 .B \-[no]banner
135 134 Print the intial information banner (default on).
@@ -2707,6 +2707,28 b' show()'
2707 2707
2708 2708 \family typewriter
2709 2709 \series bold
2710 -[no]autocall:
2711 \family default
2712 \series default
2713 Make IPython automatically call any callable object even if you didn't
2714 type explicit parentheses.
2715 For example, `str 43' becomes `str(43)' automatically.
2716 \layout List
2717 \labelwidthstring 00.00.0000
2718
2719
2720 \family typewriter
2721 \series bold
2722 -[no]autoindent:
2723 \family default
2724 \series default
2725 Turn automatic indentation on/off.
2726 \layout List
2727 \labelwidthstring 00.00.0000
2728
2729
2730 \family typewriter
2731 \series bold
2710 2732 -[no]automagic
2711 2733 \series default
2712 2734 :
@@ -2728,6 +2750,18 b' show()'
2728 2750
2729 2751 \family typewriter
2730 2752 \series bold
2753 -[no]autoedit_syntax:
2754 \family default
2755 \series default
2756 When a syntax error occurs after editing a file, automatically open the
2757 file to the trouble causing line for convenient fixing.
2758
2759 \layout List
2760 \labelwidthstring 00.00.0000
2761
2762
2763 \family typewriter
2764 \series bold
2731 2765 -[no]banner
2732 2766 \series default
2733 2767 :
@@ -9121,4 +9155,14 b' Smedt'
9121 9155 \family default
9122 9156 Debugger enhancements, so that when pdb is activated from within IPython,
9123 9157 coloring, tab completion and other features continue to work seamlessly.
9158 \layout List
9159 \labelwidthstring 00.00.0000
9160
9161 Scott (email unknown) Support for automatic editor invocation on syntax
9162 errors (see
9163 \begin_inset LatexCommand \htmlurl{http://www.scipy.net/roundup/ipython/issue36}
9164
9165 \end_inset
9166
9167 ).
9124 9168 \the_end
General Comments 0
You need to be logged in to leave comments. Login now