blob: 64463c669a48fbd6b48f9142aa2660c7d11d8688 [file] [log] [blame]
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001"""A (less & less) simple Python editor"""
2
3import W
4import Wtraceback
5from Wkeys import *
6
Just van Rossum40f9b7b1999-01-30 22:39:17 +00007import MacOS
Jack Jansenfd0b00e2003-01-26 22:15:48 +00008import EasyDialogs
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00009from Carbon import Win
10from Carbon import Res
11from Carbon import Evt
Just van Rossum2ad94192002-07-12 12:06:17 +000012from Carbon import Qd
Jack Jansene7ee17c2003-02-06 22:32:35 +000013from Carbon import File
Just van Rossum40f9b7b1999-01-30 22:39:17 +000014import os
15import imp
16import sys
17import string
18import marshal
Jack Jansen9ad27522001-02-21 13:54:31 +000019import re
Just van Rossum40f9b7b1999-01-30 22:39:17 +000020
Jack Jansene7ee17c2003-02-06 22:32:35 +000021smAllScripts = -3
22
Just van Rossum40144012002-02-04 12:52:44 +000023if hasattr(Win, "FrontNonFloatingWindow"):
24 MyFrontWindow = Win.FrontNonFloatingWindow
25else:
26 MyFrontWindow = Win.FrontWindow
27
28
Just van Rossum40f9b7b1999-01-30 22:39:17 +000029_scriptuntitledcounter = 1
Fred Drake79e75e12001-07-20 19:05:50 +000030_wordchars = string.ascii_letters + string.digits + "_"
Just van Rossum40f9b7b1999-01-30 22:39:17 +000031
32
Just van Rossum73efed22000-04-09 19:45:22 +000033runButtonLabels = ["Run all", "Stop!"]
34runSelButtonLabels = ["Run selection", "Pause!", "Resume"]
35
36
Just van Rossum40f9b7b1999-01-30 22:39:17 +000037class Editor(W.Window):
38
39 def __init__(self, path = "", title = ""):
40 defaultfontsettings, defaulttabsettings, defaultwindowsize = geteditorprefs()
41 global _scriptuntitledcounter
42 if not path:
43 if title:
44 self.title = title
45 else:
46 self.title = "Untitled Script " + `_scriptuntitledcounter`
47 _scriptuntitledcounter = _scriptuntitledcounter + 1
48 text = ""
49 self._creator = W._signature
Jack Jansen9a389472002-03-29 21:26:04 +000050 self._eoln = os.linesep
Just van Rossum40f9b7b1999-01-30 22:39:17 +000051 elif os.path.exists(path):
52 path = resolvealiases(path)
53 dir, name = os.path.split(path)
54 self.title = name
55 f = open(path, "rb")
56 text = f.read()
57 f.close()
Jack Jansene7ee17c2003-02-06 22:32:35 +000058 self._creator, filetype = MacOS.GetCreatorAndType(path)
Jack Jansenc00b6d72003-02-25 15:08:02 +000059 self.addrecentfile(path)
Just van Rossum40f9b7b1999-01-30 22:39:17 +000060 else:
61 raise IOError, "file '%s' does not exist" % path
62 self.path = path
63
Just van Rossumc7ba0801999-05-21 21:42:27 +000064 if '\n' in text:
Just van Rossumc7ba0801999-05-21 21:42:27 +000065 if string.find(text, '\r\n') >= 0:
Jack Jansen9a389472002-03-29 21:26:04 +000066 self._eoln = '\r\n'
Just van Rossumc7ba0801999-05-21 21:42:27 +000067 else:
Jack Jansen9a389472002-03-29 21:26:04 +000068 self._eoln = '\n'
69 text = string.replace(text, self._eoln, '\r')
70 change = 0
Just van Rossumc7ba0801999-05-21 21:42:27 +000071 else:
72 change = 0
Jack Jansen9a389472002-03-29 21:26:04 +000073 self._eoln = '\r'
Just van Rossumc7ba0801999-05-21 21:42:27 +000074
Just van Rossum40f9b7b1999-01-30 22:39:17 +000075 self.settings = {}
76 if self.path:
77 self.readwindowsettings()
78 if self.settings.has_key("windowbounds"):
79 bounds = self.settings["windowbounds"]
80 else:
81 bounds = defaultwindowsize
82 if self.settings.has_key("fontsettings"):
83 self.fontsettings = self.settings["fontsettings"]
84 else:
85 self.fontsettings = defaultfontsettings
86 if self.settings.has_key("tabsize"):
87 try:
88 self.tabsettings = (tabsize, tabmode) = self.settings["tabsize"]
89 except:
90 self.tabsettings = defaulttabsettings
91 else:
92 self.tabsettings = defaulttabsettings
Just van Rossum40f9b7b1999-01-30 22:39:17 +000093
Just van Rossumc7ba0801999-05-21 21:42:27 +000094 W.Window.__init__(self, bounds, self.title, minsize = (330, 120), tabbable = 0)
Just van Rossum40f9b7b1999-01-30 22:39:17 +000095 self.setupwidgets(text)
Just van Rossumc7ba0801999-05-21 21:42:27 +000096 if change > 0:
Just van Rossumf7f93882001-11-02 19:24:41 +000097 self.editgroup.editor.textchanged()
Just van Rossumc7ba0801999-05-21 21:42:27 +000098
Just van Rossum40f9b7b1999-01-30 22:39:17 +000099 if self.settings.has_key("selection"):
100 selstart, selend = self.settings["selection"]
101 self.setselection(selstart, selend)
102 self.open()
103 self.setinfotext()
104 self.globals = {}
105 self._buf = "" # for write method
106 self.debugging = 0
107 self.profiling = 0
Jack Jansenff773eb2002-03-31 22:01:33 +0000108 self.run_as_main = self.settings.get("run_as_main", 0)
109 self.run_with_interpreter = self.settings.get("run_with_interpreter", 0)
110 self.run_with_cl_interpreter = self.settings.get("run_with_cl_interpreter", 0)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000111
112 def readwindowsettings(self):
113 try:
Jack Jansend13c3852000-06-20 21:59:25 +0000114 resref = Res.FSpOpenResFile(self.path, 1)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000115 except Res.Error:
116 return
117 try:
118 Res.UseResFile(resref)
119 data = Res.Get1Resource('PyWS', 128)
120 self.settings = marshal.loads(data.data)
121 except:
122 pass
123 Res.CloseResFile(resref)
124
125 def writewindowsettings(self):
126 try:
Jack Jansend13c3852000-06-20 21:59:25 +0000127 resref = Res.FSpOpenResFile(self.path, 3)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000128 except Res.Error:
Jack Jansene7ee17c2003-02-06 22:32:35 +0000129 Res.FSpCreateResFile(self.path, self._creator, 'TEXT', smAllScripts)
Jack Jansend13c3852000-06-20 21:59:25 +0000130 resref = Res.FSpOpenResFile(self.path, 3)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000131 try:
132 data = Res.Resource(marshal.dumps(self.settings))
133 Res.UseResFile(resref)
134 try:
135 temp = Res.Get1Resource('PyWS', 128)
136 temp.RemoveResource()
137 except Res.Error:
138 pass
139 data.AddResource('PyWS', 128, "window settings")
140 finally:
141 Res.UpdateResFile(resref)
142 Res.CloseResFile(resref)
143
144 def getsettings(self):
145 self.settings = {}
146 self.settings["windowbounds"] = self.getbounds()
147 self.settings["selection"] = self.getselection()
148 self.settings["fontsettings"] = self.editgroup.editor.getfontsettings()
149 self.settings["tabsize"] = self.editgroup.editor.gettabsettings()
150 self.settings["run_as_main"] = self.run_as_main
Just van Rossum0f2fd162000-10-20 06:36:30 +0000151 self.settings["run_with_interpreter"] = self.run_with_interpreter
Jack Jansenff773eb2002-03-31 22:01:33 +0000152 self.settings["run_with_cl_interpreter"] = self.run_with_cl_interpreter
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000153
154 def get(self):
155 return self.editgroup.editor.get()
156
157 def getselection(self):
158 return self.editgroup.editor.ted.WEGetSelection()
159
160 def setselection(self, selstart, selend):
161 self.editgroup.editor.setselection(selstart, selend)
Jack Jansen9a791822003-05-06 14:28:31 +0000162
163 def getselectedtext(self):
164 return self.editgroup.editor.getselectedtext()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000165
166 def getfilename(self):
167 if self.path:
168 return self.path
169 return '<%s>' % self.title
170
171 def setupwidgets(self, text):
Just van Rossumf376ef02001-11-18 14:12:43 +0000172 topbarheight = 24
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000173 popfieldwidth = 80
174 self.lastlineno = None
175
176 # make an editor
177 self.editgroup = W.Group((0, topbarheight + 1, 0, 0))
178 editor = W.PyEditor((0, 0, -15,-15), text,
179 fontsettings = self.fontsettings,
180 tabsettings = self.tabsettings,
181 file = self.getfilename())
182
183 # make the widgets
184 self.popfield = ClassFinder((popfieldwidth - 17, -15, 16, 16), [], self.popselectline)
185 self.linefield = W.EditText((-1, -15, popfieldwidth - 15, 16), inset = (6, 1))
186 self.editgroup._barx = W.Scrollbar((popfieldwidth - 2, -15, -14, 16), editor.hscroll, max = 32767)
187 self.editgroup._bary = W.Scrollbar((-15, 14, 16, -14), editor.vscroll, max = 32767)
188 self.editgroup.editor = editor # add editor *after* scrollbars
189
190 self.editgroup.optionsmenu = W.PopupMenu((-15, -1, 16, 16), [])
191 self.editgroup.optionsmenu.bind('<click>', self.makeoptionsmenu)
192
193 self.bevelbox = W.BevelBox((0, 0, 0, topbarheight))
194 self.hline = W.HorizontalLine((0, topbarheight, 0, 0))
Just van Rossumf376ef02001-11-18 14:12:43 +0000195 self.infotext = W.TextBox((175, 6, -4, 14), backgroundcolor = (0xe000, 0xe000, 0xe000))
196 self.runbutton = W.BevelButton((6, 4, 80, 16), runButtonLabels[0], self.run)
197 self.runselbutton = W.BevelButton((90, 4, 80, 16), runSelButtonLabels[0], self.runselection)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000198
199 # bind some keys
200 editor.bind("cmdr", self.runbutton.push)
201 editor.bind("enter", self.runselbutton.push)
202 editor.bind("cmdj", self.domenu_gotoline)
203 editor.bind("cmdd", self.domenu_toggledebugger)
204 editor.bind("<idle>", self.updateselection)
205
206 editor.bind("cmde", searchengine.setfindstring)
207 editor.bind("cmdf", searchengine.show)
208 editor.bind("cmdg", searchengine.findnext)
209 editor.bind("cmdshiftr", searchengine.replace)
210 editor.bind("cmdt", searchengine.replacefind)
211
212 self.linefield.bind("return", self.dolinefield)
213 self.linefield.bind("enter", self.dolinefield)
214 self.linefield.bind("tab", self.dolinefield)
215
216 # intercept clicks
217 editor.bind("<click>", self.clickeditor)
218 self.linefield.bind("<click>", self.clicklinefield)
219
220 def makeoptionsmenu(self):
Just van Rossumdc3c6172001-06-19 21:37:33 +0000221 menuitems = [('Font settings\xc9', self.domenu_fontsettings),
222 ("Save options\xc9", self.domenu_options),
Just van Rossum12710051999-02-27 17:18:30 +0000223 '-',
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000224 ('\0' + chr(self.run_as_main) + 'Run as __main__', self.domenu_toggle_run_as_main),
Jack Jansenff773eb2002-03-31 22:01:33 +0000225 #('\0' + chr(self.run_with_interpreter) + 'Run with Interpreter', self.domenu_dtoggle_run_with_interpreter),
226 ('\0' + chr(self.run_with_cl_interpreter) + 'Run with commandline Python', self.domenu_toggle_run_with_cl_interpreter),
227 '-',
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000228 ('Modularize', self.domenu_modularize),
Just van Rossumdc3c6172001-06-19 21:37:33 +0000229 ('Browse namespace\xc9', self.domenu_browsenamespace),
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000230 '-']
231 if self.profiling:
232 menuitems = menuitems + [('Disable profiler', self.domenu_toggleprofiler)]
233 else:
234 menuitems = menuitems + [('Enable profiler', self.domenu_toggleprofiler)]
235 if self.editgroup.editor._debugger:
236 menuitems = menuitems + [('Disable debugger', self.domenu_toggledebugger),
237 ('Clear breakpoints', self.domenu_clearbreakpoints),
Just van Rossumdc3c6172001-06-19 21:37:33 +0000238 ('Edit breakpoints\xc9', self.domenu_editbreakpoints)]
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000239 else:
240 menuitems = menuitems + [('Enable debugger', self.domenu_toggledebugger)]
241 self.editgroup.optionsmenu.set(menuitems)
242
243 def domenu_toggle_run_as_main(self):
244 self.run_as_main = not self.run_as_main
Just van Rossum0f2fd162000-10-20 06:36:30 +0000245 self.run_with_interpreter = 0
Jack Jansenff773eb2002-03-31 22:01:33 +0000246 self.run_with_cl_interpreter = 0
Just van Rossumf7f93882001-11-02 19:24:41 +0000247 self.editgroup.editor.selectionchanged()
Just van Rossum0f2fd162000-10-20 06:36:30 +0000248
Jack Jansenff773eb2002-03-31 22:01:33 +0000249 def XXdomenu_toggle_run_with_interpreter(self):
Just van Rossum0f2fd162000-10-20 06:36:30 +0000250 self.run_with_interpreter = not self.run_with_interpreter
251 self.run_as_main = 0
Jack Jansenff773eb2002-03-31 22:01:33 +0000252 self.run_with_cl_interpreter = 0
253 self.editgroup.editor.selectionchanged()
254
255 def domenu_toggle_run_with_cl_interpreter(self):
256 self.run_with_cl_interpreter = not self.run_with_cl_interpreter
257 self.run_as_main = 0
258 self.run_with_interpreter = 0
Just van Rossumf7f93882001-11-02 19:24:41 +0000259 self.editgroup.editor.selectionchanged()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000260
261 def showbreakpoints(self, onoff):
262 self.editgroup.editor.showbreakpoints(onoff)
263 self.debugging = onoff
264
265 def domenu_clearbreakpoints(self, *args):
266 self.editgroup.editor.clearbreakpoints()
267
268 def domenu_editbreakpoints(self, *args):
269 self.editgroup.editor.editbreakpoints()
270
271 def domenu_toggledebugger(self, *args):
272 if not self.debugging:
273 W.SetCursor('watch')
274 self.debugging = not self.debugging
275 self.editgroup.editor.togglebreakpoints()
276
277 def domenu_toggleprofiler(self, *args):
278 self.profiling = not self.profiling
279
280 def domenu_browsenamespace(self, *args):
281 import PyBrowser, W
282 W.SetCursor('watch')
283 globals, file, modname = self.getenvironment()
284 if not modname:
285 modname = self.title
286 PyBrowser.Browser(globals, "Object browser: " + modname)
287
288 def domenu_modularize(self, *args):
289 modname = _filename_as_modname(self.title)
290 if not modname:
Just van Rossumdc3c6172001-06-19 21:37:33 +0000291 raise W.AlertError, "Can't modularize \"%s\"" % self.title
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000292 run_as_main = self.run_as_main
293 self.run_as_main = 0
294 self.run()
295 self.run_as_main = run_as_main
296 if self.path:
297 file = self.path
298 else:
299 file = self.title
300
301 if self.globals and not sys.modules.has_key(modname):
302 module = imp.new_module(modname)
303 for attr in self.globals.keys():
304 setattr(module,attr,self.globals[attr])
305 sys.modules[modname] = module
306 self.globals = {}
307
308 def domenu_fontsettings(self, *args):
309 import FontSettings
310 fontsettings = self.editgroup.editor.getfontsettings()
311 tabsettings = self.editgroup.editor.gettabsettings()
312 settings = FontSettings.FontDialog(fontsettings, tabsettings)
313 if settings:
314 fontsettings, tabsettings = settings
315 self.editgroup.editor.setfontsettings(fontsettings)
316 self.editgroup.editor.settabsettings(tabsettings)
317
Just van Rossum12710051999-02-27 17:18:30 +0000318 def domenu_options(self, *args):
Just van Rossumca3d3072002-03-29 21:48:42 +0000319 rv = SaveOptions(self._creator, self._eoln)
320 if rv:
Just van Rossumf7f93882001-11-02 19:24:41 +0000321 self.editgroup.editor.selectionchanged() # ouch...
Just van Rossumca3d3072002-03-29 21:48:42 +0000322 self._creator, self._eoln = rv
Just van Rossum12710051999-02-27 17:18:30 +0000323
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000324 def clicklinefield(self):
325 if self._currentwidget <> self.linefield:
326 self.linefield.select(1)
327 self.linefield.selectall()
328 return 1
329
330 def clickeditor(self):
331 if self._currentwidget <> self.editgroup.editor:
332 self.dolinefield()
333 return 1
334
335 def updateselection(self, force = 0):
336 sel = min(self.editgroup.editor.getselection())
337 lineno = self.editgroup.editor.offsettoline(sel)
338 if lineno <> self.lastlineno or force:
339 self.lastlineno = lineno
340 self.linefield.set(str(lineno + 1))
341 self.linefield.selview()
342
343 def dolinefield(self):
344 try:
345 lineno = string.atoi(self.linefield.get()) - 1
346 if lineno <> self.lastlineno:
347 self.editgroup.editor.selectline(lineno)
348 self.updateselection(1)
349 except:
350 self.updateselection(1)
351 self.editgroup.editor.select(1)
352
353 def setinfotext(self):
354 if not hasattr(self, 'infotext'):
355 return
356 if self.path:
357 self.infotext.set(self.path)
358 else:
359 self.infotext.set("")
360
361 def close(self):
362 if self.editgroup.editor.changed:
Just van Rossum25ddc632001-07-05 07:06:26 +0000363 Qd.InitCursor()
364 save = EasyDialogs.AskYesNoCancel('Save window "%s" before closing?' % self.title,
365 default=1, no="Don\xd5t save")
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000366 if save > 0:
367 if self.domenu_save():
368 return 1
369 elif save < 0:
370 return 1
Just van Rossum25ddc632001-07-05 07:06:26 +0000371 self.globals = None
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000372 W.Window.close(self)
373
374 def domenu_close(self, *args):
375 return self.close()
376
377 def domenu_save(self, *args):
378 if not self.path:
379 # Will call us recursively
380 return self.domenu_save_as()
381 data = self.editgroup.editor.get()
Jack Jansen9a389472002-03-29 21:26:04 +0000382 if self._eoln != '\r':
383 data = string.replace(data, '\r', self._eoln)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000384 fp = open(self.path, 'wb') # open file in binary mode, data has '\r' line-endings
385 fp.write(data)
386 fp.close()
Jack Jansene7ee17c2003-02-06 22:32:35 +0000387 MacOS.SetCreatorAndType(self.path, self._creator, 'TEXT')
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000388 self.getsettings()
389 self.writewindowsettings()
390 self.editgroup.editor.changed = 0
391 self.editgroup.editor.selchanged = 0
392 import linecache
393 if linecache.cache.has_key(self.path):
394 del linecache.cache[self.path]
395 import macostools
396 macostools.touched(self.path)
Jack Jansenc00b6d72003-02-25 15:08:02 +0000397 self.addrecentfile(self.path)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000398
399 def can_save(self, menuitem):
400 return self.editgroup.editor.changed or self.editgroup.editor.selchanged
401
402 def domenu_save_as(self, *args):
Jack Jansenfd0b00e2003-01-26 22:15:48 +0000403 path = EasyDialogs.AskFileForSave(message='Save as:', savedFileName=self.title)
404 if not path:
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000405 return 1
406 self.showbreakpoints(0)
Jack Jansenfd0b00e2003-01-26 22:15:48 +0000407 self.path = path
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000408 self.setinfotext()
409 self.title = os.path.split(self.path)[-1]
410 self.wid.SetWTitle(self.title)
411 self.domenu_save()
412 self.editgroup.editor.setfile(self.getfilename())
413 app = W.getapplication()
414 app.makeopenwindowsmenu()
415 if hasattr(app, 'makescriptsmenu'):
416 app = W.getapplication()
Jack Jansene7ee17c2003-02-06 22:32:35 +0000417 fsr, changed = app.scriptsfolder.FSResolveAlias(None)
418 path = fsr.as_pathname()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000419 if path == self.path[:len(path)]:
420 W.getapplication().makescriptsmenu()
421
422 def domenu_save_as_applet(self, *args):
Just van Rossumdc3c6172001-06-19 21:37:33 +0000423 import buildtools
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000424
425 buildtools.DEBUG = 0 # ouch.
426
427 if self.title[-3:] == ".py":
428 destname = self.title[:-3]
429 else:
430 destname = self.title + ".applet"
Jack Jansenfd0b00e2003-01-26 22:15:48 +0000431 destname = EasyDialogs.AskFileForSave(message='Save as Applet:',
432 savedFileName=destname)
433 if not destname:
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000434 return 1
435 W.SetCursor("watch")
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000436 if self.path:
437 filename = self.path
438 if filename[-3:] == ".py":
439 rsrcname = filename[:-3] + '.rsrc'
440 else:
441 rsrcname = filename + '.rsrc'
442 else:
443 filename = self.title
444 rsrcname = ""
445
446 pytext = self.editgroup.editor.get()
447 pytext = string.split(pytext, '\r')
448 pytext = string.join(pytext, '\n') + '\n'
449 try:
450 code = compile(pytext, filename, "exec")
451 except (SyntaxError, EOFError):
452 raise buildtools.BuildError, "Syntax error in script %s" % `filename`
Jack Jansenc0452da2003-02-12 15:38:37 +0000453
454 import tempfile
455 tmpdir = tempfile.mkdtemp()
456
457 if filename[-3:] != ".py":
458 filename = filename + ".py"
459 filename = os.path.join(tmpdir, os.path.split(filename)[1])
460 fp = open(filename, "w")
461 fp.write(pytext)
462 fp.close()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000463
464 # Try removing the output file
465 try:
466 os.remove(destname)
467 except os.error:
468 pass
469 template = buildtools.findtemplate()
Jack Jansen5bb97e62003-02-21 22:33:55 +0000470 buildtools.process(template, filename, destname, 1, rsrcname=rsrcname, progress=None)
Jack Jansenfd3e54c2003-02-16 21:28:51 +0000471 try:
472 os.remove(filename)
473 os.rmdir(tmpdir)
474 except os.error:
475 pass
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000476
477 def domenu_gotoline(self, *args):
478 self.linefield.selectall()
479 self.linefield.select(1)
480 self.linefield.selectall()
481
482 def domenu_selectline(self, *args):
483 self.editgroup.editor.expandselection()
484
485 def domenu_find(self, *args):
486 searchengine.show()
487
488 def domenu_entersearchstring(self, *args):
489 searchengine.setfindstring()
490
491 def domenu_replace(self, *args):
492 searchengine.replace()
493
494 def domenu_findnext(self, *args):
495 searchengine.findnext()
496
497 def domenu_replacefind(self, *args):
498 searchengine.replacefind()
499
500 def domenu_run(self, *args):
501 self.runbutton.push()
502
503 def domenu_runselection(self, *args):
504 self.runselbutton.push()
505
506 def run(self):
Just van Rossum476736e2003-05-09 08:33:58 +0000507 self._run()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000508
509 def _run(self):
Just van Rossum0f2fd162000-10-20 06:36:30 +0000510 if self.run_with_interpreter:
511 if self.editgroup.editor.changed:
Just van Rossum2ad94192002-07-12 12:06:17 +0000512 Qd.InitCursor()
Just van Rossumdc3c6172001-06-19 21:37:33 +0000513 save = EasyDialogs.AskYesNoCancel('Save "%s" before running?' % self.title, 1)
Just van Rossum0f2fd162000-10-20 06:36:30 +0000514 if save > 0:
515 if self.domenu_save():
516 return
517 elif save < 0:
518 return
519 if not self.path:
520 raise W.AlertError, "Can't run unsaved file"
521 self._run_with_interpreter()
Jack Jansenff773eb2002-03-31 22:01:33 +0000522 elif self.run_with_cl_interpreter:
Jack Jansenff773eb2002-03-31 22:01:33 +0000523 if self.editgroup.editor.changed:
Just van Rossum2ad94192002-07-12 12:06:17 +0000524 Qd.InitCursor()
Jack Jansenff773eb2002-03-31 22:01:33 +0000525 save = EasyDialogs.AskYesNoCancel('Save "%s" before running?' % self.title, 1)
526 if save > 0:
527 if self.domenu_save():
528 return
529 elif save < 0:
530 return
531 if not self.path:
532 raise W.AlertError, "Can't run unsaved file"
533 self._run_with_cl_interpreter()
Just van Rossum0f2fd162000-10-20 06:36:30 +0000534 else:
535 pytext = self.editgroup.editor.get()
536 globals, file, modname = self.getenvironment()
537 self.execstring(pytext, globals, globals, file, modname)
538
539 def _run_with_interpreter(self):
540 interp_path = os.path.join(sys.exec_prefix, "PythonInterpreter")
541 if not os.path.exists(interp_path):
542 raise W.AlertError, "Can't find interpreter"
543 import findertools
544 XXX
Jack Jansenff773eb2002-03-31 22:01:33 +0000545
546 def _run_with_cl_interpreter(self):
547 import Terminal
548 interp_path = os.path.join(sys.exec_prefix, "bin", "python")
549 file_path = self.path
550 if not os.path.exists(interp_path):
Jack Jansene7ee17c2003-02-06 22:32:35 +0000551 # This "can happen" if we are running IDE under MacPython-OS9.
552 raise W.AlertError, "Can't find command-line Python"
Jack Jansenff773eb2002-03-31 22:01:33 +0000553 cmd = '"%s" "%s" ; exit' % (interp_path, file_path)
554 t = Terminal.Terminal()
555 t.do_script(with_command=cmd)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000556
557 def runselection(self):
Just van Rossum476736e2003-05-09 08:33:58 +0000558 self._runselection()
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000559
560 def _runselection(self):
Jack Jansenff773eb2002-03-31 22:01:33 +0000561 if self.run_with_interpreter or self.run_with_cl_interpreter:
Just van Rossum0f2fd162000-10-20 06:36:30 +0000562 raise W.AlertError, "Can't run selection with Interpreter"
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000563 globals, file, modname = self.getenvironment()
564 locals = globals
565 # select whole lines
566 self.editgroup.editor.expandselection()
567
568 # get lineno of first selected line
569 selstart, selend = self.editgroup.editor.getselection()
570 selstart, selend = min(selstart, selend), max(selstart, selend)
571 selfirstline = self.editgroup.editor.offsettoline(selstart)
572 alltext = self.editgroup.editor.get()
573 pytext = alltext[selstart:selend]
574 lines = string.split(pytext, '\r')
575 indent = getminindent(lines)
576 if indent == 1:
577 classname = ''
578 alllines = string.split(alltext, '\r')
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000579 for i in range(selfirstline - 1, -1, -1):
580 line = alllines[i]
581 if line[:6] == 'class ':
582 classname = string.split(string.strip(line[6:]))[0]
583 classend = identifieRE_match(classname)
584 if classend < 1:
Just van Rossumdc3c6172001-06-19 21:37:33 +0000585 raise W.AlertError, "Can't find a class."
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000586 classname = classname[:classend]
587 break
588 elif line and line[0] not in '\t#':
Just van Rossumdc3c6172001-06-19 21:37:33 +0000589 raise W.AlertError, "Can't find a class."
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000590 else:
Just van Rossumdc3c6172001-06-19 21:37:33 +0000591 raise W.AlertError, "Can't find a class."
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000592 if globals.has_key(classname):
Just van Rossum25ddc632001-07-05 07:06:26 +0000593 klass = globals[classname]
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000594 else:
Just van Rossumdc3c6172001-06-19 21:37:33 +0000595 raise W.AlertError, "Can't find class \"%s\"." % classname
Just van Rossum25ddc632001-07-05 07:06:26 +0000596 # add class def
597 pytext = ("class %s:\n" % classname) + pytext
598 selfirstline = selfirstline - 1
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000599 elif indent > 0:
Just van Rossumdc3c6172001-06-19 21:37:33 +0000600 raise W.AlertError, "Can't run indented code."
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000601
602 # add "newlines" to fool compile/exec:
603 # now a traceback will give the right line number
604 pytext = selfirstline * '\r' + pytext
605 self.execstring(pytext, globals, locals, file, modname)
Just van Rossum25ddc632001-07-05 07:06:26 +0000606 if indent == 1 and globals[classname] is not klass:
607 # update the class in place
608 klass.__dict__.update(globals[classname].__dict__)
609 globals[classname] = klass
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000610
611 def execstring(self, pytext, globals, locals, file, modname):
612 tracebackwindow.hide()
613 # update windows
614 W.getapplication().refreshwindows()
615 if self.run_as_main:
616 modname = "__main__"
617 if self.path:
618 dir = os.path.dirname(self.path)
619 savedir = os.getcwd()
620 os.chdir(dir)
Just van Rossuma61f4ac1999-02-01 16:34:08 +0000621 sys.path.insert(0, dir)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000622 else:
623 cwdindex = None
624 try:
Just van Rossum5ef0e7c2003-05-09 08:27:33 +0000625 execstring(pytext, globals, locals, file, self.debugging,
626 modname, self.profiling)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000627 finally:
628 if self.path:
629 os.chdir(savedir)
Just van Rossuma61f4ac1999-02-01 16:34:08 +0000630 del sys.path[0]
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000631
632 def getenvironment(self):
633 if self.path:
634 file = self.path
635 dir = os.path.dirname(file)
636 # check if we're part of a package
637 modname = ""
638 while os.path.exists(os.path.join(dir, "__init__.py")):
639 dir, dirname = os.path.split(dir)
Just van Rossum2aaeb521999-02-05 21:58:25 +0000640 modname = dirname + '.' + modname
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000641 subname = _filename_as_modname(self.title)
Just van Rossumf7f93882001-11-02 19:24:41 +0000642 if subname is None:
643 return self.globals, file, None
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000644 if modname:
645 if subname == "__init__":
Just van Rossum2aaeb521999-02-05 21:58:25 +0000646 # strip trailing period
647 modname = modname[:-1]
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000648 else:
Just van Rossum2aaeb521999-02-05 21:58:25 +0000649 modname = modname + subname
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000650 else:
651 modname = subname
652 if sys.modules.has_key(modname):
653 globals = sys.modules[modname].__dict__
654 self.globals = {}
655 else:
656 globals = self.globals
Just van Rossum73efed22000-04-09 19:45:22 +0000657 modname = subname
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000658 else:
659 file = '<%s>' % self.title
660 globals = self.globals
661 modname = file
662 return globals, file, modname
663
664 def write(self, stuff):
665 """for use as stdout"""
666 self._buf = self._buf + stuff
667 if '\n' in self._buf:
668 self.flush()
669
670 def flush(self):
671 stuff = string.split(self._buf, '\n')
672 stuff = string.join(stuff, '\r')
673 end = self.editgroup.editor.ted.WEGetTextLength()
674 self.editgroup.editor.ted.WESetSelection(end, end)
675 self.editgroup.editor.ted.WEInsert(stuff, None, None)
676 self.editgroup.editor.updatescrollbars()
677 self._buf = ""
678 # ? optional:
679 #self.wid.SelectWindow()
680
681 def getclasslist(self):
682 from string import find, strip
Just van Rossum24073ea1999-12-23 15:46:57 +0000683 methodRE = re.compile(r"\r[ \t]+def ")
684 findMethod = methodRE.search
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000685 editor = self.editgroup.editor
686 text = editor.get()
687 list = []
688 append = list.append
689 functag = "func"
690 classtag = "class"
691 methodtag = "method"
692 pos = -1
693 if text[:4] == 'def ':
694 append((pos + 4, functag))
695 pos = 4
696 while 1:
697 pos = find(text, '\rdef ', pos + 1)
698 if pos < 0:
699 break
700 append((pos + 5, functag))
701 pos = -1
702 if text[:6] == 'class ':
703 append((pos + 6, classtag))
704 pos = 6
705 while 1:
706 pos = find(text, '\rclass ', pos + 1)
707 if pos < 0:
708 break
709 append((pos + 7, classtag))
710 pos = 0
711 while 1:
Just van Rossum24073ea1999-12-23 15:46:57 +0000712 m = findMethod(text, pos + 1)
713 if m is None:
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000714 break
Just van Rossum24073ea1999-12-23 15:46:57 +0000715 pos = m.regs[0][0]
716 #pos = find(text, '\r\tdef ', pos + 1)
717 append((m.regs[0][1], methodtag))
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000718 list.sort()
719 classlist = []
720 methodlistappend = None
721 offsetToLine = editor.ted.WEOffsetToLine
722 getLineRange = editor.ted.WEGetLineRange
723 append = classlist.append
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000724 for pos, tag in list:
725 lineno = offsetToLine(pos)
726 lineStart, lineEnd = getLineRange(lineno)
727 line = strip(text[pos:lineEnd])
728 line = line[:identifieRE_match(line)]
729 if tag is functag:
730 append(("def " + line, lineno + 1))
731 methodlistappend = None
732 elif tag is classtag:
733 append(["class " + line])
734 methodlistappend = classlist[-1].append
735 elif methodlistappend and tag is methodtag:
736 methodlistappend(("def " + line, lineno + 1))
737 return classlist
738
739 def popselectline(self, lineno):
740 self.editgroup.editor.selectline(lineno - 1)
741
742 def selectline(self, lineno, charoffset = 0):
743 self.editgroup.editor.selectline(lineno - 1, charoffset)
Jack Jansenc00b6d72003-02-25 15:08:02 +0000744
745 def addrecentfile(self, filename):
746 app = W.getapplication()
747 app.addrecentfile(filename)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000748
Just van Rossum12710051999-02-27 17:18:30 +0000749class _saveoptions:
750
Jack Jansen9a389472002-03-29 21:26:04 +0000751 def __init__(self, creator, eoln):
Just van Rossum12710051999-02-27 17:18:30 +0000752 self.rv = None
Jack Jansen9a389472002-03-29 21:26:04 +0000753 self.eoln = eoln
754 self.w = w = W.ModalDialog((260, 160), 'Save options')
Just van Rossum12710051999-02-27 17:18:30 +0000755 radiobuttons = []
756 w.label = W.TextBox((8, 8, 80, 18), "File creator:")
Just van Rossum3af507d1999-04-22 22:23:46 +0000757 w.ide_radio = W.RadioButton((8, 22, 160, 18), "This application", radiobuttons, self.ide_hit)
Jack Jansen9a389472002-03-29 21:26:04 +0000758 w.interp_radio = W.RadioButton((8, 42, 160, 18), "MacPython Interpreter", radiobuttons, self.interp_hit)
759 w.interpx_radio = W.RadioButton((8, 62, 160, 18), "OSX PythonW Interpreter", radiobuttons, self.interpx_hit)
760 w.other_radio = W.RadioButton((8, 82, 50, 18), "Other:", radiobuttons)
761 w.other_creator = W.EditText((62, 82, 40, 20), creator, self.otherselect)
762 w.none_radio = W.RadioButton((8, 102, 160, 18), "None", radiobuttons, self.none_hit)
Just van Rossum12710051999-02-27 17:18:30 +0000763 w.cancelbutton = W.Button((-180, -30, 80, 16), "Cancel", self.cancelbuttonhit)
764 w.okbutton = W.Button((-90, -30, 80, 16), "Done", self.okbuttonhit)
765 w.setdefaultbutton(w.okbutton)
766 if creator == 'Pyth':
767 w.interp_radio.set(1)
Just van Rossum3af507d1999-04-22 22:23:46 +0000768 elif creator == W._signature:
Just van Rossum12710051999-02-27 17:18:30 +0000769 w.ide_radio.set(1)
Jack Jansen9a389472002-03-29 21:26:04 +0000770 elif creator == 'PytX':
771 w.interpx_radio.set(1)
772 elif creator == '\0\0\0\0':
773 w.none_radio.set(1)
Just van Rossum12710051999-02-27 17:18:30 +0000774 else:
775 w.other_radio.set(1)
Jack Jansen9a389472002-03-29 21:26:04 +0000776
777 w.eolnlabel = W.TextBox((168, 8, 80, 18), "Newline style:")
778 radiobuttons = []
779 w.unix_radio = W.RadioButton((168, 22, 80, 18), "Unix", radiobuttons, self.unix_hit)
780 w.mac_radio = W.RadioButton((168, 42, 80, 18), "Macintosh", radiobuttons, self.mac_hit)
781 w.win_radio = W.RadioButton((168, 62, 80, 18), "Windows", radiobuttons, self.win_hit)
782 if self.eoln == '\n':
783 w.unix_radio.set(1)
784 elif self.eoln == '\r\n':
785 w.win_radio.set(1)
786 else:
787 w.mac_radio.set(1)
788
Just van Rossum12710051999-02-27 17:18:30 +0000789 w.bind("cmd.", w.cancelbutton.push)
790 w.open()
791
792 def ide_hit(self):
Just van Rossum3af507d1999-04-22 22:23:46 +0000793 self.w.other_creator.set(W._signature)
Just van Rossum12710051999-02-27 17:18:30 +0000794
795 def interp_hit(self):
796 self.w.other_creator.set("Pyth")
797
Jack Jansen9a389472002-03-29 21:26:04 +0000798 def interpx_hit(self):
799 self.w.other_creator.set("PytX")
800
801 def none_hit(self):
802 self.w.other_creator.set("\0\0\0\0")
803
Just van Rossum12710051999-02-27 17:18:30 +0000804 def otherselect(self, *args):
805 sel_from, sel_to = self.w.other_creator.getselection()
806 creator = self.w.other_creator.get()[:4]
807 creator = creator + " " * (4 - len(creator))
808 self.w.other_creator.set(creator)
809 self.w.other_creator.setselection(sel_from, sel_to)
810 self.w.other_radio.set(1)
811
Jack Jansen9a389472002-03-29 21:26:04 +0000812 def mac_hit(self):
813 self.eoln = '\r'
814
815 def unix_hit(self):
816 self.eoln = '\n'
817
818 def win_hit(self):
819 self.eoln = '\r\n'
820
Just van Rossum12710051999-02-27 17:18:30 +0000821 def cancelbuttonhit(self):
822 self.w.close()
823
824 def okbuttonhit(self):
Jack Jansen9a389472002-03-29 21:26:04 +0000825 self.rv = (self.w.other_creator.get()[:4], self.eoln)
Just van Rossum12710051999-02-27 17:18:30 +0000826 self.w.close()
827
828
Jack Jansen9a389472002-03-29 21:26:04 +0000829def SaveOptions(creator, eoln):
830 s = _saveoptions(creator, eoln)
Just van Rossum12710051999-02-27 17:18:30 +0000831 return s.rv
832
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000833
834def _escape(where, what) :
835 return string.join(string.split(where, what), '\\' + what)
836
837def _makewholewordpattern(word):
838 # first, escape special regex chars
Just van Rossum3eec7622001-07-10 19:25:40 +0000839 for esc in "\\[]()|.*^+$?":
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000840 word = _escape(word, esc)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000841 notwordcharspat = '[^' + _wordchars + ']'
Jack Jansen9ad27522001-02-21 13:54:31 +0000842 pattern = '(' + word + ')'
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000843 if word[0] in _wordchars:
844 pattern = notwordcharspat + pattern
845 if word[-1] in _wordchars:
846 pattern = pattern + notwordcharspat
Jack Jansen9ad27522001-02-21 13:54:31 +0000847 return re.compile(pattern)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000848
Just van Rossumf376ef02001-11-18 14:12:43 +0000849
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000850class SearchEngine:
851
852 def __init__(self):
853 self.visible = 0
854 self.w = None
855 self.parms = { "find": "",
856 "replace": "",
857 "wrap": 1,
858 "casesens": 1,
859 "wholeword": 1
860 }
861 import MacPrefs
862 prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
863 if prefs.searchengine:
864 self.parms["casesens"] = prefs.searchengine.casesens
865 self.parms["wrap"] = prefs.searchengine.wrap
866 self.parms["wholeword"] = prefs.searchengine.wholeword
867
868 def show(self):
869 self.visible = 1
870 if self.w:
871 self.w.wid.ShowWindow()
872 self.w.wid.SelectWindow()
873 self.w.find.edit.select(1)
874 self.w.find.edit.selectall()
875 return
876 self.w = W.Dialog((420, 150), "Find")
877
878 self.w.find = TitledEditText((10, 4, 300, 36), "Search for:")
879 self.w.replace = TitledEditText((10, 100, 300, 36), "Replace with:")
880
881 self.w.boxes = W.Group((10, 50, 300, 40))
882 self.w.boxes.casesens = W.CheckBox((0, 0, 100, 16), "Case sensitive")
883 self.w.boxes.wholeword = W.CheckBox((0, 20, 100, 16), "Whole word")
884 self.w.boxes.wrap = W.CheckBox((110, 0, 100, 16), "Wrap around")
885
886 self.buttons = [ ("Find", "cmdf", self.find),
887 ("Replace", "cmdr", self.replace),
888 ("Replace all", None, self.replaceall),
Just van Rossumdc3c6172001-06-19 21:37:33 +0000889 ("Don't find", "cmdd", self.dont),
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000890 ("Cancel", "cmd.", self.cancel)
891 ]
892 for i in range(len(self.buttons)):
893 bounds = -90, 22 + i * 24, 80, 16
894 title, shortcut, callback = self.buttons[i]
895 self.w[title] = W.Button(bounds, title, callback)
896 if shortcut:
897 self.w.bind(shortcut, self.w[title].push)
Just van Rossumdc3c6172001-06-19 21:37:33 +0000898 self.w.setdefaultbutton(self.w["Don't find"])
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000899 self.w.find.edit.bind("<key>", self.key)
900 self.w.bind("<activate>", self.activate)
901 self.w.bind("<close>", self.close)
902 self.w.open()
903 self.setparms()
904 self.w.find.edit.select(1)
905 self.w.find.edit.selectall()
906 self.checkbuttons()
907
908 def close(self):
909 self.hide()
910 return -1
911
912 def key(self, char, modifiers):
913 self.w.find.edit.key(char, modifiers)
914 self.checkbuttons()
915 return 1
916
917 def activate(self, onoff):
918 if onoff:
919 self.checkbuttons()
920
921 def checkbuttons(self):
922 editor = findeditor(self)
923 if editor:
924 if self.w.find.get():
925 for title, cmd, call in self.buttons[:-2]:
926 self.w[title].enable(1)
927 self.w.setdefaultbutton(self.w["Find"])
928 else:
929 for title, cmd, call in self.buttons[:-2]:
930 self.w[title].enable(0)
Just van Rossumdc3c6172001-06-19 21:37:33 +0000931 self.w.setdefaultbutton(self.w["Don't find"])
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000932 else:
933 for title, cmd, call in self.buttons[:-2]:
934 self.w[title].enable(0)
Just van Rossumdc3c6172001-06-19 21:37:33 +0000935 self.w.setdefaultbutton(self.w["Don't find"])
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000936
937 def find(self):
938 self.getparmsfromwindow()
939 if self.findnext():
940 self.hide()
941
942 def replace(self):
943 editor = findeditor(self)
944 if not editor:
945 return
946 if self.visible:
947 self.getparmsfromwindow()
948 text = editor.getselectedtext()
949 find = self.parms["find"]
950 if not self.parms["casesens"]:
951 find = string.lower(find)
952 text = string.lower(text)
953 if text == find:
954 self.hide()
955 editor.insert(self.parms["replace"])
956
957 def replaceall(self):
958 editor = findeditor(self)
959 if not editor:
960 return
961 if self.visible:
962 self.getparmsfromwindow()
963 W.SetCursor("watch")
964 find = self.parms["find"]
965 if not find:
966 return
967 findlen = len(find)
968 replace = self.parms["replace"]
969 replacelen = len(replace)
970 Text = editor.get()
971 if not self.parms["casesens"]:
972 find = string.lower(find)
973 text = string.lower(Text)
974 else:
975 text = Text
976 newtext = ""
977 pos = 0
978 counter = 0
979 while 1:
980 if self.parms["wholeword"]:
981 wholewordRE = _makewholewordpattern(find)
Jack Jansen9ad27522001-02-21 13:54:31 +0000982 match = wholewordRE.search(text, pos)
983 if match:
984 pos = match.start(1)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000985 else:
986 pos = -1
987 else:
988 pos = string.find(text, find, pos)
989 if pos < 0:
990 break
991 counter = counter + 1
992 text = text[:pos] + replace + text[pos + findlen:]
993 Text = Text[:pos] + replace + Text[pos + findlen:]
994 pos = pos + replacelen
995 W.SetCursor("arrow")
996 if counter:
997 self.hide()
Jack Jansen5a6fdcd2001-08-25 12:15:04 +0000998 from Carbon import Res
Just van Rossumf7f93882001-11-02 19:24:41 +0000999 editor.textchanged()
1000 editor.selectionchanged()
Just van Rossum7b025512002-10-24 20:03:29 +00001001 editor.set(Text)
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001002 EasyDialogs.Message("Replaced %d occurrences" % counter)
1003
1004 def dont(self):
1005 self.getparmsfromwindow()
1006 self.hide()
1007
1008 def replacefind(self):
1009 self.replace()
1010 self.findnext()
1011
1012 def setfindstring(self):
1013 editor = findeditor(self)
1014 if not editor:
1015 return
1016 find = editor.getselectedtext()
1017 if not find:
1018 return
1019 self.parms["find"] = find
1020 if self.w:
1021 self.w.find.edit.set(self.parms["find"])
1022 self.w.find.edit.selectall()
1023
1024 def findnext(self):
1025 editor = findeditor(self)
1026 if not editor:
1027 return
1028 find = self.parms["find"]
1029 if not find:
1030 return
1031 text = editor.get()
1032 if not self.parms["casesens"]:
1033 find = string.lower(find)
1034 text = string.lower(text)
1035 selstart, selend = editor.getselection()
1036 selstart, selend = min(selstart, selend), max(selstart, selend)
1037 if self.parms["wholeword"]:
1038 wholewordRE = _makewholewordpattern(find)
Jack Jansen9ad27522001-02-21 13:54:31 +00001039 match = wholewordRE.search(text, selend)
1040 if match:
1041 pos = match.start(1)
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001042 else:
1043 pos = -1
1044 else:
1045 pos = string.find(text, find, selend)
1046 if pos >= 0:
1047 editor.setselection(pos, pos + len(find))
1048 return 1
1049 elif self.parms["wrap"]:
1050 if self.parms["wholeword"]:
Jack Jansen9ad27522001-02-21 13:54:31 +00001051 match = wholewordRE.search(text, 0)
1052 if match:
1053 pos = match.start(1)
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001054 else:
1055 pos = -1
1056 else:
1057 pos = string.find(text, find)
1058 if selstart > pos >= 0:
1059 editor.setselection(pos, pos + len(find))
1060 return 1
1061
1062 def setparms(self):
1063 for key, value in self.parms.items():
1064 try:
1065 self.w[key].set(value)
1066 except KeyError:
1067 self.w.boxes[key].set(value)
1068
1069 def getparmsfromwindow(self):
1070 if not self.w:
1071 return
1072 for key, value in self.parms.items():
1073 try:
1074 value = self.w[key].get()
1075 except KeyError:
1076 value = self.w.boxes[key].get()
1077 self.parms[key] = value
1078
1079 def cancel(self):
1080 self.hide()
1081 self.setparms()
1082
1083 def hide(self):
1084 if self.w:
1085 self.w.wid.HideWindow()
1086 self.visible = 0
1087
1088 def writeprefs(self):
1089 import MacPrefs
1090 self.getparmsfromwindow()
1091 prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
1092 prefs.searchengine.casesens = self.parms["casesens"]
1093 prefs.searchengine.wrap = self.parms["wrap"]
1094 prefs.searchengine.wholeword = self.parms["wholeword"]
1095 prefs.save()
1096
1097
1098class TitledEditText(W.Group):
1099
1100 def __init__(self, possize, title, text = ""):
1101 W.Group.__init__(self, possize)
1102 self.title = W.TextBox((0, 0, 0, 16), title)
1103 self.edit = W.EditText((0, 16, 0, 0), text)
1104
1105 def set(self, value):
1106 self.edit.set(value)
1107
1108 def get(self):
1109 return self.edit.get()
1110
1111
1112class ClassFinder(W.PopupWidget):
1113
1114 def click(self, point, modifiers):
1115 W.SetCursor("watch")
1116 self.set(self._parentwindow.getclasslist())
1117 W.PopupWidget.click(self, point, modifiers)
1118
1119
1120def getminindent(lines):
1121 indent = -1
1122 for line in lines:
1123 stripped = string.strip(line)
1124 if not stripped or stripped[0] == '#':
1125 continue
1126 if indent < 0 or line[:indent] <> indent * '\t':
1127 indent = 0
1128 for c in line:
1129 if c <> '\t':
1130 break
1131 indent = indent + 1
1132 return indent
1133
1134
1135def getoptionkey():
1136 return not not ord(Evt.GetKeys()[7]) & 0x04
1137
1138
1139def execstring(pytext, globals, locals, filename="<string>", debugging=0,
1140 modname="__main__", profiling=0):
1141 if debugging:
1142 import PyDebugger, bdb
1143 BdbQuit = bdb.BdbQuit
1144 else:
1145 BdbQuit = 'BdbQuitDummyException'
1146 pytext = string.split(pytext, '\r')
1147 pytext = string.join(pytext, '\n') + '\n'
1148 W.SetCursor("watch")
1149 globals['__name__'] = modname
1150 globals['__file__'] = filename
1151 sys.argv = [filename]
1152 try:
1153 code = compile(pytext, filename, "exec")
1154 except:
1155 # XXXX BAAAADDD.... We let tracebackwindow decide to treat SyntaxError
1156 # special. That's wrong because THIS case is special (could be literal
1157 # overflow!) and SyntaxError could mean we need a traceback (syntax error
1158 # in imported module!!!
1159 tracebackwindow.traceback(1, filename)
1160 return
1161 try:
1162 if debugging:
Just van Rossum5ef0e7c2003-05-09 08:27:33 +00001163 PyDebugger.startfromhere()
1164 else:
Jack Jansen815d2bf2002-01-21 23:00:52 +00001165 if hasattr(MacOS, 'EnableAppswitch'):
1166 MacOS.EnableAppswitch(0)
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001167 try:
1168 if profiling:
1169 import profile, ProfileBrowser
1170 p = profile.Profile()
1171 p.set_cmd(filename)
1172 try:
1173 p.runctx(code, globals, locals)
1174 finally:
1175 import pstats
1176
1177 stats = pstats.Stats(p)
1178 ProfileBrowser.ProfileBrowser(stats)
1179 else:
1180 exec code in globals, locals
1181 finally:
Just van Rossum5ef0e7c2003-05-09 08:27:33 +00001182 if hasattr(MacOS, 'EnableAppswitch'):
1183 MacOS.EnableAppswitch(-1)
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001184 except W.AlertError, detail:
1185 raise W.AlertError, detail
1186 except (KeyboardInterrupt, BdbQuit):
1187 pass
Just van Rossumf7f93882001-11-02 19:24:41 +00001188 except SystemExit, arg:
1189 if arg.code:
1190 sys.stderr.write("Script exited with status code: %s\n" % repr(arg.code))
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001191 except:
1192 if debugging:
1193 sys.settrace(None)
1194 PyDebugger.postmortem(sys.exc_type, sys.exc_value, sys.exc_traceback)
1195 return
1196 else:
1197 tracebackwindow.traceback(1, filename)
1198 if debugging:
1199 sys.settrace(None)
1200 PyDebugger.stop()
1201
1202
Just van Rossum3eec7622001-07-10 19:25:40 +00001203_identifieRE = re.compile(r"[A-Za-z_][A-Za-z_0-9]*")
Jack Jansen9ad27522001-02-21 13:54:31 +00001204
1205def identifieRE_match(str):
1206 match = _identifieRE.match(str)
1207 if not match:
1208 return -1
1209 return match.end()
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001210
1211def _filename_as_modname(fname):
1212 if fname[-3:] == '.py':
1213 modname = fname[:-3]
Jack Jansen9ad27522001-02-21 13:54:31 +00001214 match = _identifieRE.match(modname)
1215 if match and match.start() == 0 and match.end() == len(modname):
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001216 return string.join(string.split(modname, '.'), '_')
1217
1218def findeditor(topwindow, fromtop = 0):
Just van Rossum40144012002-02-04 12:52:44 +00001219 wid = MyFrontWindow()
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001220 if not fromtop:
1221 if topwindow.w and wid == topwindow.w.wid:
1222 wid = topwindow.w.wid.GetNextWindow()
1223 if not wid:
1224 return
1225 app = W.getapplication()
1226 if app._windows.has_key(wid): # KeyError otherwise can happen in RoboFog :-(
1227 window = W.getapplication()._windows[wid]
1228 else:
1229 return
1230 if not isinstance(window, Editor):
1231 return
1232 return window.editgroup.editor
1233
1234
1235class _EditorDefaultSettings:
1236
1237 def __init__(self):
1238 self.template = "%s, %d point"
1239 self.fontsettings, self.tabsettings, self.windowsize = geteditorprefs()
1240 self.w = W.Dialog((328, 120), "Editor default settings")
Just van Rossumdc3c6172001-06-19 21:37:33 +00001241 self.w.setfontbutton = W.Button((8, 8, 80, 16), "Set font\xc9", self.dofont)
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001242 self.w.fonttext = W.TextBox((98, 10, -8, 14), self.template % (self.fontsettings[0], self.fontsettings[2]))
1243
1244 self.w.picksizebutton = W.Button((8, 50, 80, 16), "Front window", self.picksize)
1245 self.w.xsizelabel = W.TextBox((98, 32, 40, 14), "Width:")
1246 self.w.ysizelabel = W.TextBox((148, 32, 40, 14), "Height:")
1247 self.w.xsize = W.EditText((98, 48, 40, 20), `self.windowsize[0]`)
1248 self.w.ysize = W.EditText((148, 48, 40, 20), `self.windowsize[1]`)
1249
1250 self.w.cancelbutton = W.Button((-180, -26, 80, 16), "Cancel", self.cancel)
1251 self.w.okbutton = W.Button((-90, -26, 80, 16), "Done", self.ok)
1252 self.w.setdefaultbutton(self.w.okbutton)
1253 self.w.bind('cmd.', self.w.cancelbutton.push)
1254 self.w.open()
1255
1256 def picksize(self):
1257 app = W.getapplication()
1258 editor = findeditor(self)
1259 if editor is not None:
1260 width, height = editor._parentwindow._bounds[2:]
1261 self.w.xsize.set(`width`)
1262 self.w.ysize.set(`height`)
1263 else:
1264 raise W.AlertError, "No edit window found"
1265
1266 def dofont(self):
1267 import FontSettings
1268 settings = FontSettings.FontDialog(self.fontsettings, self.tabsettings)
1269 if settings:
1270 self.fontsettings, self.tabsettings = settings
1271 sys.exc_traceback = None
1272 self.w.fonttext.set(self.template % (self.fontsettings[0], self.fontsettings[2]))
1273
1274 def close(self):
1275 self.w.close()
1276 del self.w
1277
1278 def cancel(self):
1279 self.close()
1280
1281 def ok(self):
1282 try:
1283 width = string.atoi(self.w.xsize.get())
1284 except:
1285 self.w.xsize.select(1)
1286 self.w.xsize.selectall()
1287 raise W.AlertError, "Bad number for window width"
1288 try:
1289 height = string.atoi(self.w.ysize.get())
1290 except:
1291 self.w.ysize.select(1)
1292 self.w.ysize.selectall()
1293 raise W.AlertError, "Bad number for window height"
1294 self.windowsize = width, height
1295 seteditorprefs(self.fontsettings, self.tabsettings, self.windowsize)
1296 self.close()
1297
1298def geteditorprefs():
1299 import MacPrefs
1300 prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
1301 try:
1302 fontsettings = prefs.pyedit.fontsettings
1303 tabsettings = prefs.pyedit.tabsettings
1304 windowsize = prefs.pyedit.windowsize
1305 except:
Just van Rossumf7f93882001-11-02 19:24:41 +00001306 fontsettings = prefs.pyedit.fontsettings = ("Geneva", 0, 10, (0, 0, 0))
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001307 tabsettings = prefs.pyedit.tabsettings = (8, 1)
1308 windowsize = prefs.pyedit.windowsize = (500, 250)
1309 sys.exc_traceback = None
1310 return fontsettings, tabsettings, windowsize
1311
1312def seteditorprefs(fontsettings, tabsettings, windowsize):
1313 import MacPrefs
1314 prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
1315 prefs.pyedit.fontsettings = fontsettings
1316 prefs.pyedit.tabsettings = tabsettings
1317 prefs.pyedit.windowsize = windowsize
1318 prefs.save()
1319
1320_defaultSettingsEditor = None
1321
1322def EditorDefaultSettings():
1323 global _defaultSettingsEditor
1324 if _defaultSettingsEditor is None or not hasattr(_defaultSettingsEditor, "w"):
1325 _defaultSettingsEditor = _EditorDefaultSettings()
1326 else:
1327 _defaultSettingsEditor.w.select()
1328
1329def resolvealiases(path):
1330 try:
Jack Jansene7ee17c2003-02-06 22:32:35 +00001331 fsr, d1, d2 = File.FSResolveAliasFile(path, 1)
1332 path = fsr.as_pathname()
1333 return path
1334 except (File.Error, ValueError), (error, str):
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001335 if error <> -120:
1336 raise
1337 dir, file = os.path.split(path)
1338 return os.path.join(resolvealiases(dir), file)
1339
1340searchengine = SearchEngine()
1341tracebackwindow = Wtraceback.TraceBack()