33This adds the following commands:
44
55- Check module does a full syntax check of the current module.
6- It also runs the tabnanny to catch any inconsistent tabs.
6+ It also runs the tabnanny to catch any inconsistent tabs.
77
88- Run module executes the module's code in the __main__ namespace. The window
9- must have been saved previously. The module is added to sys.modules, and is
10- also added to the __main__ namespace.
9+ must have been saved previously. The module is added to sys.modules, and is
10+ also added to the __main__ namespace.
1111
12- XXX Redesign this interface (yet again) as follows:
12+ XXX GvR Redesign this interface (yet again) as follows:
1313
1414- Present a dialog box for ``Run script''
1515
1616- Allow specify command line arguments in the dialog box
1717
1818"""
1919
20+ import re
21+ import string
22+ import tabnanny
23+ import tokenize
2024import tkMessageBox
2125
26+ IDENTCHARS = string .ascii_letters + string .digits + "_"
27+
2228indent_message = """Error: Inconsistent indentation detected!
2329
2430This means that either:
3238 by Untabify Region (both in the Edit menu)."""
3339
3440
35- # XXX TBD Implement stop-execution KBK 11Jun02
41+ # XXX 11Jun02 KBK TBD Implement stop-execution
42+
3643class ScriptBinding :
3744
3845 menudefs = [
3946 ('run' , [None ,
40- ('Check module ' , '<<check-module>>' ),
41- ('Run script ' , '<<run-script>>' ), ]), ]
47+ ('Check Module ' , '<<check-module>>' ),
48+ ('Run Script ' , '<<run-script>>' ), ]), ]
4249
4350 def __init__ (self , editwin ):
4451 self .editwin = editwin
@@ -53,12 +60,9 @@ def check_module_event(self, event):
5360 return
5461 if not self .tabnanny (filename ):
5562 return
56- if not self .checksyntax (filename ):
57- return
63+ self .checksyntax (filename )
5864
5965 def tabnanny (self , filename ):
60- import tabnanny
61- import tokenize
6266 f = open (filename , 'r' )
6367 try :
6468 tabnanny .process_tokens (tokenize .generate_tokens (f .readline ))
@@ -77,31 +81,46 @@ def checksyntax(self, filename):
7781 source = f .read ()
7882 f .close ()
7983 if '\r ' in source :
80- import re
8184 source = re .sub (r"\r\n" , "\n " , source )
8285 if source and source [- 1 ] != '\n ' :
8386 source = source + '\n '
8487 try :
85- compile (source , filename , "exec" )
88+ # If successful, return the compiled code
89+ return compile (source , filename , "exec" )
8690 except (SyntaxError , OverflowError ), err :
8791 try :
8892 msg , (errorfilename , lineno , offset , line ) = err
8993 if not errorfilename :
9094 err .args = msg , (filename , lineno , offset , line )
9195 err .filename = filename
96+ self .colorize_syntax_error (msg , lineno , offset )
9297 except :
93- lineno = None
9498 msg = "*** " + str (err )
95- if lineno :
96- self .editwin .gotoline (lineno )
9799 self .errorbox ("Syntax error" ,
98100 "There's an error in your program:\n " + msg )
99- return True
100-
101+ return False
102+
103+ def colorize_syntax_error (self , msg , lineno , offset ):
104+ text = self .editwin .text
105+ pos = "0.0 + %d lines + %d chars" % (lineno - 1 , offset - 1 )
106+ text .tag_add ("ERROR" , pos )
107+ char = text .get (pos )
108+ if char and char in IDENTCHARS :
109+ text .tag_add ("ERROR" , pos + " wordstart" , pos )
110+ if '\n ' == text .get (pos ): # error at line end
111+ text .mark_set ("insert" , pos )
112+ else :
113+ text .mark_set ("insert" , pos + "+1c" )
114+ text .see (pos )
115+
101116 def run_script_event (self , event ):
117+ "Check syntax, if ok run the script in the shell top level"
102118 filename = self .getfilename ()
103119 if not filename :
104120 return
121+ code = self .checksyntax (filename )
122+ if not code :
123+ return
105124 flist = self .editwin .flist
106125 shell = flist .open_shell ()
107126 interp = shell .interp
@@ -116,11 +135,10 @@ def run_script_event(self, event):
116135 from os.path import basename as _basename
117136 if (not _sys.argv or
118137 _basename(_sys.argv[0]) != _basename(_filename)):
119- # XXX 25 July 2002 KBK should this be sys.argv not _sys.argv?
120138 _sys.argv = [_filename]
121- del _filename, _sys, _basename
139+ del _filename, _sys, _basename
122140 \n """ % `filename` )
123- interp .execfile ( filename )
141+ interp .runcode ( code )
124142
125143 def getfilename (self ):
126144 # Logic to make sure we have a saved filename
0 commit comments