blob: 88c72aca492fc8d6c905b22e6845dad3e491c566 [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"):
Tim Peters182b5ac2004-07-18 06:16:08 +000024 MyFrontWindow = Win.FrontNonFloatingWindow
Just van Rossum40144012002-02-04 12:52:44 +000025else:
Tim Peters182b5ac2004-07-18 06:16:08 +000026 MyFrontWindow = Win.FrontWindow
Just van Rossum40144012002-02-04 12:52:44 +000027
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):
Jack Jansenff773eb2002-03-31 22:01:33 +000038
Tim Peters182b5ac2004-07-18 06:16:08 +000039 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 %r" % (_scriptuntitledcounter,)
47 _scriptuntitledcounter = _scriptuntitledcounter + 1
48 text = ""
49 self._creator = W._signature
50 self._eoln = os.linesep
51 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()
58 self._creator, filetype = MacOS.GetCreatorAndType(path)
59 self.addrecentfile(path)
60 if '\n' in text:
61 if string.find(text, '\r\n') >= 0:
62 self._eoln = '\r\n'
63 else:
64 self._eoln = '\n'
65 text = string.replace(text, self._eoln, '\r')
66 else:
67 self._eoln = '\r'
68 else:
69 raise IOError, "file '%s' does not exist" % path
70 self.path = path
71
72 self.settings = {}
73 if self.path:
74 self.readwindowsettings()
75 if self.settings.has_key("windowbounds"):
76 bounds = self.settings["windowbounds"]
77 else:
78 bounds = defaultwindowsize
79 if self.settings.has_key("fontsettings"):
80 self.fontsettings = self.settings["fontsettings"]
81 else:
82 self.fontsettings = defaultfontsettings
83 if self.settings.has_key("tabsize"):
84 try:
85 self.tabsettings = (tabsize, tabmode) = self.settings["tabsize"]
86 except:
87 self.tabsettings = defaulttabsettings
88 else:
89 self.tabsettings = defaulttabsettings
90
91 W.Window.__init__(self, bounds, self.title, minsize = (330, 120), tabbable = 0)
92 self.setupwidgets(text)
93
94 if self.settings.has_key("selection"):
95 selstart, selend = self.settings["selection"]
96 self.setselection(selstart, selend)
97 self.open()
98 self.setinfotext()
99 self.globals = {}
100 self._buf = "" # for write method
101 self.debugging = 0
102 self.profiling = 0
103 self.run_as_main = self.settings.get("run_as_main", 0)
104 self.run_with_interpreter = self.settings.get("run_with_interpreter", 0)
105 self.run_with_cl_interpreter = self.settings.get("run_with_cl_interpreter", 0)
106
107 def readwindowsettings(self):
108 try:
109 resref = Res.FSpOpenResFile(self.path, 1)
110 except Res.Error:
111 return
112 try:
113 Res.UseResFile(resref)
114 data = Res.Get1Resource('PyWS', 128)
115 self.settings = marshal.loads(data.data)
116 except:
117 pass
118 Res.CloseResFile(resref)
119
120 def writewindowsettings(self):
121 try:
122 resref = Res.FSpOpenResFile(self.path, 3)
123 except Res.Error:
124 Res.FSpCreateResFile(self.path, self._creator, 'TEXT', smAllScripts)
125 resref = Res.FSpOpenResFile(self.path, 3)
126 try:
127 data = Res.Resource(marshal.dumps(self.settings))
128 Res.UseResFile(resref)
129 try:
130 temp = Res.Get1Resource('PyWS', 128)
131 temp.RemoveResource()
132 except Res.Error:
133 pass
134 data.AddResource('PyWS', 128, "window settings")
135 finally:
136 Res.UpdateResFile(resref)
137 Res.CloseResFile(resref)
138
139 def getsettings(self):
140 self.settings = {}
141 self.settings["windowbounds"] = self.getbounds()
142 self.settings["selection"] = self.getselection()
143 self.settings["fontsettings"] = self.editgroup.editor.getfontsettings()
144 self.settings["tabsize"] = self.editgroup.editor.gettabsettings()
145 self.settings["run_as_main"] = self.run_as_main
146 self.settings["run_with_interpreter"] = self.run_with_interpreter
147 self.settings["run_with_cl_interpreter"] = self.run_with_cl_interpreter
148
149 def get(self):
150 return self.editgroup.editor.get()
151
152 def getselection(self):
153 return self.editgroup.editor.ted.WEGetSelection()
154
155 def setselection(self, selstart, selend):
156 self.editgroup.editor.setselection(selstart, selend)
157
158 def getselectedtext(self):
159 return self.editgroup.editor.getselectedtext()
160
161 def getfilename(self):
162 if self.path:
163 return self.path
164 return '<%s>' % self.title
165
166 def setupwidgets(self, text):
167 topbarheight = 24
168 popfieldwidth = 80
169 self.lastlineno = None
170
171 # make an editor
172 self.editgroup = W.Group((0, topbarheight + 1, 0, 0))
173 editor = W.PyEditor((0, 0, -15,-15), text,
174 fontsettings = self.fontsettings,
175 tabsettings = self.tabsettings,
176 file = self.getfilename())
177
178 # make the widgets
179 self.popfield = ClassFinder((popfieldwidth - 17, -15, 16, 16), [], self.popselectline)
180 self.linefield = W.EditText((-1, -15, popfieldwidth - 15, 16), inset = (6, 1))
181 self.editgroup._barx = W.Scrollbar((popfieldwidth - 2, -15, -14, 16), editor.hscroll, max = 32767)
182 self.editgroup._bary = W.Scrollbar((-15, 14, 16, -14), editor.vscroll, max = 32767)
183 self.editgroup.editor = editor # add editor *after* scrollbars
184
185 self.editgroup.optionsmenu = W.PopupMenu((-15, -1, 16, 16), [])
186 self.editgroup.optionsmenu.bind('<click>', self.makeoptionsmenu)
187
188 self.bevelbox = W.BevelBox((0, 0, 0, topbarheight))
189 self.hline = W.HorizontalLine((0, topbarheight, 0, 0))
190 self.infotext = W.TextBox((175, 6, -4, 14), backgroundcolor = (0xe000, 0xe000, 0xe000))
191 self.runbutton = W.BevelButton((6, 4, 80, 16), runButtonLabels[0], self.run)
192 self.runselbutton = W.BevelButton((90, 4, 80, 16), runSelButtonLabels[0], self.runselection)
193
194 # bind some keys
195 editor.bind("cmdr", self.runbutton.push)
196 editor.bind("enter", self.runselbutton.push)
197 editor.bind("cmdj", self.domenu_gotoline)
198 editor.bind("cmdd", self.domenu_toggledebugger)
199 editor.bind("<idle>", self.updateselection)
200
201 editor.bind("cmde", searchengine.setfindstring)
202 editor.bind("cmdf", searchengine.show)
203 editor.bind("cmdg", searchengine.findnext)
204 editor.bind("cmdshiftr", searchengine.replace)
205 editor.bind("cmdt", searchengine.replacefind)
206
207 self.linefield.bind("return", self.dolinefield)
208 self.linefield.bind("enter", self.dolinefield)
209 self.linefield.bind("tab", self.dolinefield)
210
211 # intercept clicks
212 editor.bind("<click>", self.clickeditor)
213 self.linefield.bind("<click>", self.clicklinefield)
214
215 def makeoptionsmenu(self):
216 menuitems = [('Font settings\xc9', self.domenu_fontsettings),
217 ("Save options\xc9", self.domenu_options),
218 '-',
219 ('\0' + chr(self.run_as_main) + 'Run as __main__', self.domenu_toggle_run_as_main),
220 #('\0' + chr(self.run_with_interpreter) + 'Run with Interpreter', self.domenu_dtoggle_run_with_interpreter),
221 ('\0' + chr(self.run_with_cl_interpreter) + 'Run with commandline Python', self.domenu_toggle_run_with_cl_interpreter),
222 '-',
223 ('Modularize', self.domenu_modularize),
224 ('Browse namespace\xc9', self.domenu_browsenamespace),
225 '-']
226 if self.profiling:
227 menuitems = menuitems + [('Disable profiler', self.domenu_toggleprofiler)]
228 else:
229 menuitems = menuitems + [('Enable profiler', self.domenu_toggleprofiler)]
230 if self.editgroup.editor._debugger:
231 menuitems = menuitems + [('Disable debugger', self.domenu_toggledebugger),
232 ('Clear breakpoints', self.domenu_clearbreakpoints),
233 ('Edit breakpoints\xc9', self.domenu_editbreakpoints)]
234 else:
235 menuitems = menuitems + [('Enable debugger', self.domenu_toggledebugger)]
236 self.editgroup.optionsmenu.set(menuitems)
237
238 def domenu_toggle_run_as_main(self):
239 self.run_as_main = not self.run_as_main
240 self.run_with_interpreter = 0
241 self.run_with_cl_interpreter = 0
242 self.editgroup.editor.selectionchanged()
243
244 def XXdomenu_toggle_run_with_interpreter(self):
245 self.run_with_interpreter = not self.run_with_interpreter
246 self.run_as_main = 0
247 self.run_with_cl_interpreter = 0
248 self.editgroup.editor.selectionchanged()
249
250 def domenu_toggle_run_with_cl_interpreter(self):
251 self.run_with_cl_interpreter = not self.run_with_cl_interpreter
252 self.run_as_main = 0
253 self.run_with_interpreter = 0
254 self.editgroup.editor.selectionchanged()
255
256 def showbreakpoints(self, onoff):
257 self.editgroup.editor.showbreakpoints(onoff)
258 self.debugging = onoff
259
260 def domenu_clearbreakpoints(self, *args):
261 self.editgroup.editor.clearbreakpoints()
262
263 def domenu_editbreakpoints(self, *args):
264 self.editgroup.editor.editbreakpoints()
265
266 def domenu_toggledebugger(self, *args):
267 if not self.debugging:
268 W.SetCursor('watch')
269 self.debugging = not self.debugging
270 self.editgroup.editor.togglebreakpoints()
271
272 def domenu_toggleprofiler(self, *args):
273 self.profiling = not self.profiling
274
275 def domenu_browsenamespace(self, *args):
276 import PyBrowser, W
277 W.SetCursor('watch')
278 globals, file, modname = self.getenvironment()
279 if not modname:
280 modname = self.title
281 PyBrowser.Browser(globals, "Object browser: " + modname)
282
283 def domenu_modularize(self, *args):
284 modname = _filename_as_modname(self.title)
285 if not modname:
286 raise W.AlertError, "Can't modularize \"%s\"" % self.title
287 run_as_main = self.run_as_main
288 self.run_as_main = 0
289 self.run()
290 self.run_as_main = run_as_main
291 if self.path:
292 file = self.path
293 else:
294 file = self.title
295
296 if self.globals and not sys.modules.has_key(modname):
297 module = imp.new_module(modname)
298 for attr in self.globals.keys():
299 setattr(module,attr,self.globals[attr])
300 sys.modules[modname] = module
301 self.globals = {}
302
303 def domenu_fontsettings(self, *args):
304 import FontSettings
305 fontsettings = self.editgroup.editor.getfontsettings()
306 tabsettings = self.editgroup.editor.gettabsettings()
307 settings = FontSettings.FontDialog(fontsettings, tabsettings)
308 if settings:
309 fontsettings, tabsettings = settings
310 self.editgroup.editor.setfontsettings(fontsettings)
311 self.editgroup.editor.settabsettings(tabsettings)
312
313 def domenu_options(self, *args):
314 rv = SaveOptions(self._creator, self._eoln)
315 if rv:
316 self.editgroup.editor.selectionchanged() # ouch...
317 self._creator, self._eoln = rv
318
319 def clicklinefield(self):
320 if self._currentwidget <> self.linefield:
321 self.linefield.select(1)
322 self.linefield.selectall()
323 return 1
324
325 def clickeditor(self):
326 if self._currentwidget <> self.editgroup.editor:
327 self.dolinefield()
328 return 1
329
330 def updateselection(self, force = 0):
331 sel = min(self.editgroup.editor.getselection())
332 lineno = self.editgroup.editor.offsettoline(sel)
333 if lineno <> self.lastlineno or force:
334 self.lastlineno = lineno
335 self.linefield.set(str(lineno + 1))
336 self.linefield.selview()
337
338 def dolinefield(self):
339 try:
340 lineno = string.atoi(self.linefield.get()) - 1
341 if lineno <> self.lastlineno:
342 self.editgroup.editor.selectline(lineno)
343 self.updateselection(1)
344 except:
345 self.updateselection(1)
346 self.editgroup.editor.select(1)
347
348 def setinfotext(self):
349 if not hasattr(self, 'infotext'):
350 return
351 if self.path:
352 self.infotext.set(self.path)
353 else:
354 self.infotext.set("")
355
356 def close(self):
357 if self.editgroup.editor.changed:
358 Qd.InitCursor()
359 save = EasyDialogs.AskYesNoCancel('Save window "%s" before closing?' % self.title,
360 default=1, no="Don\xd5t save")
361 if save > 0:
362 if self.domenu_save():
363 return 1
364 elif save < 0:
365 return 1
366 self.globals = None
367 W.Window.close(self)
368
369 def domenu_close(self, *args):
370 return self.close()
371
372 def domenu_save(self, *args):
373 if not self.path:
374 # Will call us recursively
375 return self.domenu_save_as()
376 data = self.editgroup.editor.get()
377 if self._eoln != '\r':
378 data = string.replace(data, '\r', self._eoln)
379 fp = open(self.path, 'wb') # open file in binary mode, data has '\r' line-endings
380 fp.write(data)
381 fp.close()
382 MacOS.SetCreatorAndType(self.path, self._creator, 'TEXT')
383 self.getsettings()
384 self.writewindowsettings()
385 self.editgroup.editor.changed = 0
386 self.editgroup.editor.selchanged = 0
387 import linecache
388 if linecache.cache.has_key(self.path):
389 del linecache.cache[self.path]
390 import macostools
391 macostools.touched(self.path)
392 self.addrecentfile(self.path)
393
394 def can_save(self, menuitem):
395 return self.editgroup.editor.changed or self.editgroup.editor.selchanged
396
397 def domenu_save_as(self, *args):
398 path = EasyDialogs.AskFileForSave(message='Save as:', savedFileName=self.title)
399 if not path:
400 return 1
401 self.showbreakpoints(0)
402 self.path = path
403 self.setinfotext()
404 self.title = os.path.split(self.path)[-1]
405 self.wid.SetWTitle(self.title)
406 self.domenu_save()
407 self.editgroup.editor.setfile(self.getfilename())
408 app = W.getapplication()
409 app.makeopenwindowsmenu()
410 if hasattr(app, 'makescriptsmenu'):
411 app = W.getapplication()
412 fsr, changed = app.scriptsfolder.FSResolveAlias(None)
413 path = fsr.as_pathname()
414 if path == self.path[:len(path)]:
415 W.getapplication().makescriptsmenu()
416
417 def domenu_save_as_applet(self, *args):
418 import buildtools
419
420 buildtools.DEBUG = 0 # ouch.
421
422 if self.title[-3:] == ".py":
423 destname = self.title[:-3]
424 else:
425 destname = self.title + ".applet"
426 destname = EasyDialogs.AskFileForSave(message='Save as Applet:',
427 savedFileName=destname)
428 if not destname:
429 return 1
430 W.SetCursor("watch")
431 if self.path:
432 filename = self.path
433 if filename[-3:] == ".py":
434 rsrcname = filename[:-3] + '.rsrc'
435 else:
436 rsrcname = filename + '.rsrc'
437 else:
438 filename = self.title
439 rsrcname = ""
440
441 pytext = self.editgroup.editor.get()
442 pytext = string.split(pytext, '\r')
443 pytext = string.join(pytext, '\n') + '\n'
444 try:
445 code = compile(pytext, filename, "exec")
446 except (SyntaxError, EOFError):
447 raise buildtools.BuildError, "Syntax error in script %r" % (filename,)
448
449 import tempfile
450 tmpdir = tempfile.mkdtemp()
451
452 if filename[-3:] != ".py":
453 filename = filename + ".py"
454 filename = os.path.join(tmpdir, os.path.split(filename)[1])
455 fp = open(filename, "w")
456 fp.write(pytext)
457 fp.close()
458
459 # Try removing the output file
460 try:
461 os.remove(destname)
462 except os.error:
463 pass
464 template = buildtools.findtemplate()
465 buildtools.process(template, filename, destname, 1, rsrcname=rsrcname, progress=None)
466 try:
467 os.remove(filename)
468 os.rmdir(tmpdir)
469 except os.error:
470 pass
471
472 def domenu_gotoline(self, *args):
473 self.linefield.selectall()
474 self.linefield.select(1)
475 self.linefield.selectall()
476
477 def domenu_selectline(self, *args):
478 self.editgroup.editor.expandselection()
479
480 def domenu_find(self, *args):
481 searchengine.show()
482
483 def domenu_entersearchstring(self, *args):
484 searchengine.setfindstring()
485
486 def domenu_replace(self, *args):
487 searchengine.replace()
488
489 def domenu_findnext(self, *args):
490 searchengine.findnext()
491
492 def domenu_replacefind(self, *args):
493 searchengine.replacefind()
494
495 def domenu_run(self, *args):
496 self.runbutton.push()
497
498 def domenu_runselection(self, *args):
499 self.runselbutton.push()
500
501 def run(self):
502 self._run()
503
504 def _run(self):
505 if self.run_with_interpreter:
506 if self.editgroup.editor.changed:
507 Qd.InitCursor()
508 save = EasyDialogs.AskYesNoCancel('Save "%s" before running?' % self.title, 1)
509 if save > 0:
510 if self.domenu_save():
511 return
512 elif save < 0:
513 return
514 if not self.path:
515 raise W.AlertError, "Can't run unsaved file"
516 self._run_with_interpreter()
517 elif self.run_with_cl_interpreter:
518 if self.editgroup.editor.changed:
519 Qd.InitCursor()
520 save = EasyDialogs.AskYesNoCancel('Save "%s" before running?' % self.title, 1)
521 if save > 0:
522 if self.domenu_save():
523 return
524 elif save < 0:
525 return
526 if not self.path:
527 raise W.AlertError, "Can't run unsaved file"
528 self._run_with_cl_interpreter()
529 else:
530 pytext = self.editgroup.editor.get()
531 globals, file, modname = self.getenvironment()
532 self.execstring(pytext, globals, globals, file, modname)
533
534 def _run_with_interpreter(self):
535 interp_path = os.path.join(sys.exec_prefix, "PythonInterpreter")
536 if not os.path.exists(interp_path):
537 raise W.AlertError, "Can't find interpreter"
538 import findertools
539 XXX
540
541 def _run_with_cl_interpreter(self):
542 import Terminal
543 interp_path = os.path.join(sys.exec_prefix,
544 "Resources", "Python.app", "Contents", "MacOS", "Python")
545 if not os.path.exists(interp_path):
546 interp_path = os.path.join(sys.exec_prefix, "bin", "python")
547 file_path = self.path
548 if not os.path.exists(interp_path):
549 # This "can happen" if we are running IDE under MacPython-OS9.
550 raise W.AlertError, "Can't find command-line Python"
551 cmd = '"%s" "%s" ; exit' % (interp_path, file_path)
552 t = Terminal.Terminal()
553 t.do_script(cmd)
554
555 def runselection(self):
556 self._runselection()
557
558 def _runselection(self):
559 if self.run_with_interpreter or self.run_with_cl_interpreter:
560 raise W.AlertError, "Can't run selection with Interpreter"
561 globals, file, modname = self.getenvironment()
562 locals = globals
563 # select whole lines
564 self.editgroup.editor.expandselection()
565
566 # get lineno of first selected line
567 selstart, selend = self.editgroup.editor.getselection()
568 selstart, selend = min(selstart, selend), max(selstart, selend)
569 selfirstline = self.editgroup.editor.offsettoline(selstart)
570 alltext = self.editgroup.editor.get()
571 pytext = alltext[selstart:selend]
572 lines = string.split(pytext, '\r')
573 indent = getminindent(lines)
574 if indent == 1:
575 classname = ''
576 alllines = string.split(alltext, '\r')
577 for i in range(selfirstline - 1, -1, -1):
578 line = alllines[i]
579 if line[:6] == 'class ':
580 classname = string.split(string.strip(line[6:]))[0]
581 classend = identifieRE_match(classname)
582 if classend < 1:
583 raise W.AlertError, "Can't find a class."
584 classname = classname[:classend]
585 break
586 elif line and line[0] not in '\t#':
587 raise W.AlertError, "Can't find a class."
588 else:
589 raise W.AlertError, "Can't find a class."
590 if globals.has_key(classname):
591 klass = globals[classname]
592 else:
593 raise W.AlertError, "Can't find class \"%s\"." % classname
594 # add class def
595 pytext = ("class %s:\n" % classname) + pytext
596 selfirstline = selfirstline - 1
597 elif indent > 0:
598 raise W.AlertError, "Can't run indented code."
599
600 # add "newlines" to fool compile/exec:
601 # now a traceback will give the right line number
602 pytext = selfirstline * '\r' + pytext
603 self.execstring(pytext, globals, locals, file, modname)
604 if indent == 1 and globals[classname] is not klass:
605 # update the class in place
606 klass.__dict__.update(globals[classname].__dict__)
607 globals[classname] = klass
608
609 def execstring(self, pytext, globals, locals, file, modname):
610 tracebackwindow.hide()
611 # update windows
612 W.getapplication().refreshwindows()
613 if self.run_as_main:
614 modname = "__main__"
615 if self.path:
616 dir = os.path.dirname(self.path)
617 savedir = os.getcwd()
618 os.chdir(dir)
619 sys.path.insert(0, dir)
620 self._scriptDone = False
621 if sys.platform == "darwin":
622 # On MacOSX, MacPython doesn't poll for command-period
623 # (cancel), so to enable the user to cancel a running
624 # script, we have to spawn a thread which does the
625 # polling. It will send a SIGINT to the main thread
626 # (in which the script is running) when the user types
627 # command-period.
628 from threading import Thread
629 t = Thread(target=self._userCancelledMonitor,
630 name="UserCancelledMonitor")
631 t.start()
632 try:
633 execstring(pytext, globals, locals, file, self.debugging,
634 modname, self.profiling)
635 finally:
636 self._scriptDone = True
637 if self.path:
638 os.chdir(savedir)
639 del sys.path[0]
640
641 def _userCancelledMonitor(self):
642 import time
643 from signal import SIGINT
644 while not self._scriptDone:
645 if Evt.CheckEventQueueForUserCancel():
646 # Send a SIGINT signal to ourselves.
647 # This gets delivered to the main thread,
648 # cancelling the running script.
649 os.kill(os.getpid(), SIGINT)
650 break
651 time.sleep(0.25)
652
653 def getenvironment(self):
654 if self.path:
655 file = self.path
656 dir = os.path.dirname(file)
657 # check if we're part of a package
658 modname = ""
659 while os.path.exists(os.path.join(dir, "__init__.py")):
660 dir, dirname = os.path.split(dir)
661 modname = dirname + '.' + modname
662 subname = _filename_as_modname(self.title)
663 if subname is None:
664 return self.globals, file, None
665 if modname:
666 if subname == "__init__":
667 # strip trailing period
668 modname = modname[:-1]
669 else:
670 modname = modname + subname
671 else:
672 modname = subname
673 if sys.modules.has_key(modname):
674 globals = sys.modules[modname].__dict__
675 self.globals = {}
676 else:
677 globals = self.globals
678 modname = subname
679 else:
680 file = '<%s>' % self.title
681 globals = self.globals
682 modname = file
683 return globals, file, modname
684
685 def write(self, stuff):
686 """for use as stdout"""
687 self._buf = self._buf + stuff
688 if '\n' in self._buf:
689 self.flush()
690
691 def flush(self):
692 stuff = string.split(self._buf, '\n')
693 stuff = string.join(stuff, '\r')
694 end = self.editgroup.editor.ted.WEGetTextLength()
695 self.editgroup.editor.ted.WESetSelection(end, end)
696 self.editgroup.editor.ted.WEInsert(stuff, None, None)
697 self.editgroup.editor.updatescrollbars()
698 self._buf = ""
699 # ? optional:
700 #self.wid.SelectWindow()
701
702 def getclasslist(self):
703 from string import find, strip
704 methodRE = re.compile(r"\r[ \t]+def ")
705 findMethod = methodRE.search
706 editor = self.editgroup.editor
707 text = editor.get()
708 list = []
709 append = list.append
710 functag = "func"
711 classtag = "class"
712 methodtag = "method"
713 pos = -1
714 if text[:4] == 'def ':
715 append((pos + 4, functag))
716 pos = 4
717 while 1:
718 pos = find(text, '\rdef ', pos + 1)
719 if pos < 0:
720 break
721 append((pos + 5, functag))
722 pos = -1
723 if text[:6] == 'class ':
724 append((pos + 6, classtag))
725 pos = 6
726 while 1:
727 pos = find(text, '\rclass ', pos + 1)
728 if pos < 0:
729 break
730 append((pos + 7, classtag))
731 pos = 0
732 while 1:
733 m = findMethod(text, pos + 1)
734 if m is None:
735 break
736 pos = m.regs[0][0]
737 #pos = find(text, '\r\tdef ', pos + 1)
738 append((m.regs[0][1], methodtag))
739 list.sort()
740 classlist = []
741 methodlistappend = None
742 offsetToLine = editor.ted.WEOffsetToLine
743 getLineRange = editor.ted.WEGetLineRange
744 append = classlist.append
745 for pos, tag in list:
746 lineno = offsetToLine(pos)
747 lineStart, lineEnd = getLineRange(lineno)
748 line = strip(text[pos:lineEnd])
749 line = line[:identifieRE_match(line)]
750 if tag is functag:
751 append(("def " + line, lineno + 1))
752 methodlistappend = None
753 elif tag is classtag:
754 append(["class " + line])
755 methodlistappend = classlist[-1].append
756 elif methodlistappend and tag is methodtag:
757 methodlistappend(("def " + line, lineno + 1))
758 return classlist
759
760 def popselectline(self, lineno):
761 self.editgroup.editor.selectline(lineno - 1)
762
763 def selectline(self, lineno, charoffset = 0):
764 self.editgroup.editor.selectline(lineno - 1, charoffset)
765
766 def addrecentfile(self, filename):
767 app = W.getapplication()
768 app.addrecentfile(filename)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000769
Just van Rossum12710051999-02-27 17:18:30 +0000770class _saveoptions:
Tim Peters182b5ac2004-07-18 06:16:08 +0000771
772 def __init__(self, creator, eoln):
773 self.rv = None
774 self.eoln = eoln
775 self.w = w = W.ModalDialog((260, 160), 'Save options')
776 radiobuttons = []
777 w.label = W.TextBox((8, 8, 80, 18), "File creator:")
778 w.ide_radio = W.RadioButton((8, 22, 160, 18), "PythonIDE", radiobuttons, self.ide_hit)
779 w.interp_radio = W.RadioButton((8, 42, 160, 18), "MacPython-OS9 Interpreter", radiobuttons, self.interp_hit)
780 w.interpx_radio = W.RadioButton((8, 62, 160, 18), "PythonLauncher", radiobuttons, self.interpx_hit)
781 w.other_radio = W.RadioButton((8, 82, 50, 18), "Other:", radiobuttons)
782 w.other_creator = W.EditText((62, 82, 40, 20), creator, self.otherselect)
783 w.none_radio = W.RadioButton((8, 102, 160, 18), "None", radiobuttons, self.none_hit)
784 w.cancelbutton = W.Button((-180, -30, 80, 16), "Cancel", self.cancelbuttonhit)
785 w.okbutton = W.Button((-90, -30, 80, 16), "Done", self.okbuttonhit)
786 w.setdefaultbutton(w.okbutton)
787 if creator == 'Pyth':
788 w.interp_radio.set(1)
789 elif creator == W._signature:
790 w.ide_radio.set(1)
791 elif creator == 'PytX':
792 w.interpx_radio.set(1)
793 elif creator == '\0\0\0\0':
794 w.none_radio.set(1)
795 else:
796 w.other_radio.set(1)
797
798 w.eolnlabel = W.TextBox((168, 8, 80, 18), "Newline style:")
799 radiobuttons = []
800 w.unix_radio = W.RadioButton((168, 22, 80, 18), "Unix", radiobuttons, self.unix_hit)
801 w.mac_radio = W.RadioButton((168, 42, 80, 18), "Macintosh", radiobuttons, self.mac_hit)
802 w.win_radio = W.RadioButton((168, 62, 80, 18), "Windows", radiobuttons, self.win_hit)
803 if self.eoln == '\n':
804 w.unix_radio.set(1)
805 elif self.eoln == '\r\n':
806 w.win_radio.set(1)
807 else:
808 w.mac_radio.set(1)
809
810 w.bind("cmd.", w.cancelbutton.push)
811 w.open()
812
813 def ide_hit(self):
814 self.w.other_creator.set(W._signature)
815
816 def interp_hit(self):
817 self.w.other_creator.set("Pyth")
818
819 def interpx_hit(self):
820 self.w.other_creator.set("PytX")
821
822 def none_hit(self):
823 self.w.other_creator.set("\0\0\0\0")
824
825 def otherselect(self, *args):
826 sel_from, sel_to = self.w.other_creator.getselection()
827 creator = self.w.other_creator.get()[:4]
828 creator = creator + " " * (4 - len(creator))
829 self.w.other_creator.set(creator)
830 self.w.other_creator.setselection(sel_from, sel_to)
831 self.w.other_radio.set(1)
832
833 def mac_hit(self):
834 self.eoln = '\r'
835
836 def unix_hit(self):
837 self.eoln = '\n'
838
839 def win_hit(self):
840 self.eoln = '\r\n'
841
842 def cancelbuttonhit(self):
843 self.w.close()
844
845 def okbuttonhit(self):
846 self.rv = (self.w.other_creator.get()[:4], self.eoln)
847 self.w.close()
Just van Rossum12710051999-02-27 17:18:30 +0000848
849
Jack Jansen9a389472002-03-29 21:26:04 +0000850def SaveOptions(creator, eoln):
Tim Peters182b5ac2004-07-18 06:16:08 +0000851 s = _saveoptions(creator, eoln)
852 return s.rv
Just van Rossum12710051999-02-27 17:18:30 +0000853
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000854
Tim Peters182b5ac2004-07-18 06:16:08 +0000855def _escape(where, what) :
856 return string.join(string.split(where, what), '\\' + what)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000857
858def _makewholewordpattern(word):
Tim Peters182b5ac2004-07-18 06:16:08 +0000859 # first, escape special regex chars
860 for esc in "\\[]()|.*^+$?":
861 word = _escape(word, esc)
862 notwordcharspat = '[^' + _wordchars + ']'
863 pattern = '(' + word + ')'
864 if word[0] in _wordchars:
865 pattern = notwordcharspat + pattern
866 if word[-1] in _wordchars:
867 pattern = pattern + notwordcharspat
868 return re.compile(pattern)
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000869
Just van Rossumf376ef02001-11-18 14:12:43 +0000870
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000871class SearchEngine:
Tim Peters182b5ac2004-07-18 06:16:08 +0000872
873 def __init__(self):
874 self.visible = 0
875 self.w = None
876 self.parms = { "find": "",
877 "replace": "",
878 "wrap": 1,
879 "casesens": 1,
880 "wholeword": 1
881 }
882 import MacPrefs
883 prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
884 if prefs.searchengine:
885 self.parms["casesens"] = prefs.searchengine.casesens
886 self.parms["wrap"] = prefs.searchengine.wrap
887 self.parms["wholeword"] = prefs.searchengine.wholeword
888
889 def show(self):
890 self.visible = 1
891 if self.w:
892 self.w.wid.ShowWindow()
893 self.w.wid.SelectWindow()
894 self.w.find.edit.select(1)
895 self.w.find.edit.selectall()
896 return
897 self.w = W.Dialog((420, 150), "Find")
898
899 self.w.find = TitledEditText((10, 4, 300, 36), "Search for:")
900 self.w.replace = TitledEditText((10, 100, 300, 36), "Replace with:")
901
902 self.w.boxes = W.Group((10, 50, 300, 40))
903 self.w.boxes.casesens = W.CheckBox((0, 0, 100, 16), "Case sensitive")
904 self.w.boxes.wholeword = W.CheckBox((0, 20, 100, 16), "Whole word")
905 self.w.boxes.wrap = W.CheckBox((110, 0, 100, 16), "Wrap around")
906
907 self.buttons = [ ("Find", "cmdf", self.find),
908 ("Replace", "cmdr", self.replace),
909 ("Replace all", None, self.replaceall),
910 ("Don't find", "cmdd", self.dont),
911 ("Cancel", "cmd.", self.cancel)
912 ]
913 for i in range(len(self.buttons)):
914 bounds = -90, 22 + i * 24, 80, 16
915 title, shortcut, callback = self.buttons[i]
916 self.w[title] = W.Button(bounds, title, callback)
917 if shortcut:
918 self.w.bind(shortcut, self.w[title].push)
919 self.w.setdefaultbutton(self.w["Don't find"])
920 self.w.find.edit.bind("<key>", self.key)
921 self.w.bind("<activate>", self.activate)
922 self.w.bind("<close>", self.close)
923 self.w.open()
924 self.setparms()
925 self.w.find.edit.select(1)
926 self.w.find.edit.selectall()
927 self.checkbuttons()
928
929 def close(self):
930 self.hide()
931 return -1
932
933 def key(self, char, modifiers):
934 self.w.find.edit.key(char, modifiers)
935 self.checkbuttons()
936 return 1
937
938 def activate(self, onoff):
939 if onoff:
940 self.checkbuttons()
941
942 def checkbuttons(self):
943 editor = findeditor(self)
944 if editor:
945 if self.w.find.get():
946 for title, cmd, call in self.buttons[:-2]:
947 self.w[title].enable(1)
948 self.w.setdefaultbutton(self.w["Find"])
949 else:
950 for title, cmd, call in self.buttons[:-2]:
951 self.w[title].enable(0)
952 self.w.setdefaultbutton(self.w["Don't find"])
953 else:
954 for title, cmd, call in self.buttons[:-2]:
955 self.w[title].enable(0)
956 self.w.setdefaultbutton(self.w["Don't find"])
957
958 def find(self):
959 self.getparmsfromwindow()
960 if self.findnext():
961 self.hide()
962
963 def replace(self):
964 editor = findeditor(self)
965 if not editor:
966 return
967 if self.visible:
968 self.getparmsfromwindow()
969 text = editor.getselectedtext()
970 find = self.parms["find"]
971 if not self.parms["casesens"]:
972 find = string.lower(find)
973 text = string.lower(text)
974 if text == find:
975 self.hide()
976 editor.insert(self.parms["replace"])
977
978 def replaceall(self):
979 editor = findeditor(self)
980 if not editor:
981 return
982 if self.visible:
983 self.getparmsfromwindow()
984 W.SetCursor("watch")
985 find = self.parms["find"]
986 if not find:
987 return
988 findlen = len(find)
989 replace = self.parms["replace"]
990 replacelen = len(replace)
991 Text = editor.get()
992 if not self.parms["casesens"]:
993 find = string.lower(find)
994 text = string.lower(Text)
995 else:
996 text = Text
997 newtext = ""
998 pos = 0
999 counter = 0
1000 while 1:
1001 if self.parms["wholeword"]:
1002 wholewordRE = _makewholewordpattern(find)
1003 match = wholewordRE.search(text, pos)
1004 if match:
1005 pos = match.start(1)
1006 else:
1007 pos = -1
1008 else:
1009 pos = string.find(text, find, pos)
1010 if pos < 0:
1011 break
1012 counter = counter + 1
1013 text = text[:pos] + replace + text[pos + findlen:]
1014 Text = Text[:pos] + replace + Text[pos + findlen:]
1015 pos = pos + replacelen
1016 W.SetCursor("arrow")
1017 if counter:
1018 self.hide()
1019 from Carbon import Res
1020 editor.textchanged()
1021 editor.selectionchanged()
1022 editor.set(Text)
1023 EasyDialogs.Message("Replaced %d occurrences" % counter)
1024
1025 def dont(self):
1026 self.getparmsfromwindow()
1027 self.hide()
1028
1029 def replacefind(self):
1030 self.replace()
1031 self.findnext()
1032
1033 def setfindstring(self):
1034 editor = findeditor(self)
1035 if not editor:
1036 return
1037 find = editor.getselectedtext()
1038 if not find:
1039 return
1040 self.parms["find"] = find
1041 if self.w:
1042 self.w.find.edit.set(self.parms["find"])
1043 self.w.find.edit.selectall()
1044
1045 def findnext(self):
1046 editor = findeditor(self)
1047 if not editor:
1048 return
1049 find = self.parms["find"]
1050 if not find:
1051 return
1052 text = editor.get()
1053 if not self.parms["casesens"]:
1054 find = string.lower(find)
1055 text = string.lower(text)
1056 selstart, selend = editor.getselection()
1057 selstart, selend = min(selstart, selend), max(selstart, selend)
1058 if self.parms["wholeword"]:
1059 wholewordRE = _makewholewordpattern(find)
1060 match = wholewordRE.search(text, selend)
1061 if match:
1062 pos = match.start(1)
1063 else:
1064 pos = -1
1065 else:
1066 pos = string.find(text, find, selend)
1067 if pos >= 0:
1068 editor.setselection(pos, pos + len(find))
1069 return 1
1070 elif self.parms["wrap"]:
1071 if self.parms["wholeword"]:
1072 match = wholewordRE.search(text, 0)
1073 if match:
1074 pos = match.start(1)
1075 else:
1076 pos = -1
1077 else:
1078 pos = string.find(text, find)
1079 if selstart > pos >= 0:
1080 editor.setselection(pos, pos + len(find))
1081 return 1
1082
1083 def setparms(self):
1084 for key, value in self.parms.items():
1085 try:
1086 self.w[key].set(value)
1087 except KeyError:
1088 self.w.boxes[key].set(value)
1089
1090 def getparmsfromwindow(self):
1091 if not self.w:
1092 return
1093 for key, value in self.parms.items():
1094 try:
1095 value = self.w[key].get()
1096 except KeyError:
1097 value = self.w.boxes[key].get()
1098 self.parms[key] = value
1099
1100 def cancel(self):
1101 self.hide()
1102 self.setparms()
1103
1104 def hide(self):
1105 if self.w:
1106 self.w.wid.HideWindow()
1107 self.visible = 0
1108
1109 def writeprefs(self):
1110 import MacPrefs
1111 self.getparmsfromwindow()
1112 prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
1113 prefs.searchengine.casesens = self.parms["casesens"]
1114 prefs.searchengine.wrap = self.parms["wrap"]
1115 prefs.searchengine.wholeword = self.parms["wholeword"]
1116 prefs.save()
1117
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001118
1119class TitledEditText(W.Group):
Tim Peters182b5ac2004-07-18 06:16:08 +00001120
1121 def __init__(self, possize, title, text = ""):
1122 W.Group.__init__(self, possize)
1123 self.title = W.TextBox((0, 0, 0, 16), title)
1124 self.edit = W.EditText((0, 16, 0, 0), text)
1125
1126 def set(self, value):
1127 self.edit.set(value)
1128
1129 def get(self):
1130 return self.edit.get()
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001131
1132
1133class ClassFinder(W.PopupWidget):
Tim Peters182b5ac2004-07-18 06:16:08 +00001134
1135 def click(self, point, modifiers):
1136 W.SetCursor("watch")
1137 self.set(self._parentwindow.getclasslist())
1138 W.PopupWidget.click(self, point, modifiers)
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001139
1140
1141def getminindent(lines):
Tim Peters182b5ac2004-07-18 06:16:08 +00001142 indent = -1
1143 for line in lines:
1144 stripped = string.strip(line)
1145 if not stripped or stripped[0] == '#':
1146 continue
1147 if indent < 0 or line[:indent] <> indent * '\t':
1148 indent = 0
1149 for c in line:
1150 if c <> '\t':
1151 break
1152 indent = indent + 1
1153 return indent
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001154
1155
1156def getoptionkey():
Tim Peters182b5ac2004-07-18 06:16:08 +00001157 return not not ord(Evt.GetKeys()[7]) & 0x04
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001158
1159
Tim Peters182b5ac2004-07-18 06:16:08 +00001160def execstring(pytext, globals, locals, filename="<string>", debugging=0,
1161 modname="__main__", profiling=0):
1162 if debugging:
1163 import PyDebugger, bdb
1164 BdbQuit = bdb.BdbQuit
1165 else:
1166 BdbQuit = 'BdbQuitDummyException'
1167 pytext = string.split(pytext, '\r')
1168 pytext = string.join(pytext, '\n') + '\n'
1169 W.SetCursor("watch")
1170 globals['__name__'] = modname
1171 globals['__file__'] = filename
1172 sys.argv = [filename]
1173 try:
1174 code = compile(pytext, filename, "exec")
1175 except:
1176 # XXXX BAAAADDD.... We let tracebackwindow decide to treat SyntaxError
1177 # special. That's wrong because THIS case is special (could be literal
1178 # overflow!) and SyntaxError could mean we need a traceback (syntax error
1179 # in imported module!!!
1180 tracebackwindow.traceback(1, filename)
1181 return
1182 try:
1183 if debugging:
1184 PyDebugger.startfromhere()
1185 else:
1186 if hasattr(MacOS, 'EnableAppswitch'):
1187 MacOS.EnableAppswitch(0)
1188 try:
1189 if profiling:
1190 import profile, ProfileBrowser
1191 p = profile.Profile()
1192 p.set_cmd(filename)
1193 try:
1194 p.runctx(code, globals, locals)
1195 finally:
1196 import pstats
1197
1198 stats = pstats.Stats(p)
1199 ProfileBrowser.ProfileBrowser(stats)
1200 else:
1201 exec code in globals, locals
1202 finally:
1203 if hasattr(MacOS, 'EnableAppswitch'):
1204 MacOS.EnableAppswitch(-1)
1205 except W.AlertError, detail:
1206 raise W.AlertError, detail
1207 except (KeyboardInterrupt, BdbQuit):
1208 pass
1209 except SystemExit, arg:
1210 if arg.code:
1211 sys.stderr.write("Script exited with status code: %s\n" % repr(arg.code))
1212 except:
1213 if debugging:
1214 sys.settrace(None)
1215 PyDebugger.postmortem(sys.exc_type, sys.exc_value, sys.exc_traceback)
1216 return
1217 else:
1218 tracebackwindow.traceback(1, filename)
1219 if debugging:
1220 sys.settrace(None)
1221 PyDebugger.stop()
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001222
1223
Just van Rossum3eec7622001-07-10 19:25:40 +00001224_identifieRE = re.compile(r"[A-Za-z_][A-Za-z_0-9]*")
Jack Jansen9ad27522001-02-21 13:54:31 +00001225
1226def identifieRE_match(str):
Tim Peters182b5ac2004-07-18 06:16:08 +00001227 match = _identifieRE.match(str)
1228 if not match:
1229 return -1
1230 return match.end()
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001231
1232def _filename_as_modname(fname):
Tim Peters182b5ac2004-07-18 06:16:08 +00001233 if fname[-3:] == '.py':
1234 modname = fname[:-3]
1235 match = _identifieRE.match(modname)
1236 if match and match.start() == 0 and match.end() == len(modname):
1237 return string.join(string.split(modname, '.'), '_')
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001238
1239def findeditor(topwindow, fromtop = 0):
Tim Peters182b5ac2004-07-18 06:16:08 +00001240 wid = MyFrontWindow()
1241 if not fromtop:
1242 if topwindow.w and wid == topwindow.w.wid:
1243 wid = topwindow.w.wid.GetNextWindow()
1244 if not wid:
1245 return
1246 app = W.getapplication()
1247 if app._windows.has_key(wid): # KeyError otherwise can happen in RoboFog :-(
1248 window = W.getapplication()._windows[wid]
1249 else:
1250 return
1251 if not isinstance(window, Editor):
1252 return
1253 return window.editgroup.editor
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001254
1255
1256class _EditorDefaultSettings:
Tim Peters182b5ac2004-07-18 06:16:08 +00001257
1258 def __init__(self):
1259 self.template = "%s, %d point"
1260 self.fontsettings, self.tabsettings, self.windowsize = geteditorprefs()
1261 self.w = W.Dialog((328, 120), "Editor default settings")
1262 self.w.setfontbutton = W.Button((8, 8, 80, 16), "Set font\xc9", self.dofont)
1263 self.w.fonttext = W.TextBox((98, 10, -8, 14), self.template % (self.fontsettings[0], self.fontsettings[2]))
1264
1265 self.w.picksizebutton = W.Button((8, 50, 80, 16), "Front window", self.picksize)
1266 self.w.xsizelabel = W.TextBox((98, 32, 40, 14), "Width:")
1267 self.w.ysizelabel = W.TextBox((148, 32, 40, 14), "Height:")
1268 self.w.xsize = W.EditText((98, 48, 40, 20), repr(self.windowsize[0]))
1269 self.w.ysize = W.EditText((148, 48, 40, 20), repr(self.windowsize[1]))
1270
1271 self.w.cancelbutton = W.Button((-180, -26, 80, 16), "Cancel", self.cancel)
1272 self.w.okbutton = W.Button((-90, -26, 80, 16), "Done", self.ok)
1273 self.w.setdefaultbutton(self.w.okbutton)
1274 self.w.bind('cmd.', self.w.cancelbutton.push)
1275 self.w.open()
1276
1277 def picksize(self):
1278 app = W.getapplication()
1279 editor = findeditor(self)
1280 if editor is not None:
1281 width, height = editor._parentwindow._bounds[2:]
1282 self.w.xsize.set(repr(width))
1283 self.w.ysize.set(repr(height))
1284 else:
1285 raise W.AlertError, "No edit window found"
1286
1287 def dofont(self):
1288 import FontSettings
1289 settings = FontSettings.FontDialog(self.fontsettings, self.tabsettings)
1290 if settings:
1291 self.fontsettings, self.tabsettings = settings
1292 sys.exc_traceback = None
1293 self.w.fonttext.set(self.template % (self.fontsettings[0], self.fontsettings[2]))
1294
1295 def close(self):
1296 self.w.close()
1297 del self.w
1298
1299 def cancel(self):
1300 self.close()
1301
1302 def ok(self):
1303 try:
1304 width = string.atoi(self.w.xsize.get())
1305 except:
1306 self.w.xsize.select(1)
1307 self.w.xsize.selectall()
1308 raise W.AlertError, "Bad number for window width"
1309 try:
1310 height = string.atoi(self.w.ysize.get())
1311 except:
1312 self.w.ysize.select(1)
1313 self.w.ysize.selectall()
1314 raise W.AlertError, "Bad number for window height"
1315 self.windowsize = width, height
1316 seteditorprefs(self.fontsettings, self.tabsettings, self.windowsize)
1317 self.close()
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001318
1319def geteditorprefs():
Tim Peters182b5ac2004-07-18 06:16:08 +00001320 import MacPrefs
1321 prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
1322 try:
1323 fontsettings = prefs.pyedit.fontsettings
1324 tabsettings = prefs.pyedit.tabsettings
1325 windowsize = prefs.pyedit.windowsize
1326 except:
1327 fontsettings = prefs.pyedit.fontsettings = ("Geneva", 0, 10, (0, 0, 0))
1328 tabsettings = prefs.pyedit.tabsettings = (8, 1)
1329 windowsize = prefs.pyedit.windowsize = (500, 250)
1330 sys.exc_traceback = None
1331 return fontsettings, tabsettings, windowsize
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001332
1333def seteditorprefs(fontsettings, tabsettings, windowsize):
Tim Peters182b5ac2004-07-18 06:16:08 +00001334 import MacPrefs
1335 prefs = MacPrefs.GetPrefs(W.getapplication().preffilepath)
1336 prefs.pyedit.fontsettings = fontsettings
1337 prefs.pyedit.tabsettings = tabsettings
1338 prefs.pyedit.windowsize = windowsize
1339 prefs.save()
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001340
1341_defaultSettingsEditor = None
1342
1343def EditorDefaultSettings():
Tim Peters182b5ac2004-07-18 06:16:08 +00001344 global _defaultSettingsEditor
1345 if _defaultSettingsEditor is None or not hasattr(_defaultSettingsEditor, "w"):
1346 _defaultSettingsEditor = _EditorDefaultSettings()
1347 else:
1348 _defaultSettingsEditor.w.select()
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001349
1350def resolvealiases(path):
Tim Peters182b5ac2004-07-18 06:16:08 +00001351 try:
1352 fsr, d1, d2 = File.FSResolveAliasFile(path, 1)
1353 path = fsr.as_pathname()
1354 return path
1355 except (File.Error, ValueError), (error, str):
1356 if error <> -120:
1357 raise
1358 dir, file = os.path.split(path)
1359 return os.path.join(resolvealiases(dir), file)
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001360
1361searchengine = SearchEngine()
1362tracebackwindow = Wtraceback.TraceBack()