1 """A (less & less) simple Python editor"""
10 from Carbon
import Res
11 from Carbon
import Evt
13 from Carbon
import File
23 if hasattr(Win
, "FrontNonFloatingWindow"):
24 MyFrontWindow
= Win
.FrontNonFloatingWindow
26 MyFrontWindow
= Win
.FrontWindow
29 _scriptuntitledcounter
= 1
30 _wordchars
= string
.ascii_letters
+ string
.digits
+ "_"
33 runButtonLabels
= ["Run all", "Stop!"]
34 runSelButtonLabels
= ["Run selection", "Pause!", "Resume"]
37 class Editor(W
.Window
):
39 def __init__(self
, path
= "", title
= ""):
40 defaultfontsettings
, defaulttabsettings
, defaultwindowsize
= geteditorprefs()
41 global _scriptuntitledcounter
46 self
.title
= "Untitled Script " + `_scriptuntitledcounter`
47 _scriptuntitledcounter
= _scriptuntitledcounter
+ 1
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
)
58 self
._creator
, filetype
= MacOS
.GetCreatorAndType(path
)
59 self
.addrecentfile(path
)
61 if string
.find(text
, '\r\n') >= 0:
65 text
= string
.replace(text
, self
._eoln
, '\r')
69 raise IOError, "file '%s' does not exist" % path
74 self
.readwindowsettings()
75 if self
.settings
.has_key("windowbounds"):
76 bounds
= self
.settings
["windowbounds"]
78 bounds
= defaultwindowsize
79 if self
.settings
.has_key("fontsettings"):
80 self
.fontsettings
= self
.settings
["fontsettings"]
82 self
.fontsettings
= defaultfontsettings
83 if self
.settings
.has_key("tabsize"):
85 self
.tabsettings
= (tabsize
, tabmode
) = self
.settings
["tabsize"]
87 self
.tabsettings
= defaulttabsettings
89 self
.tabsettings
= defaulttabsettings
91 W
.Window
.__init
__(self
, bounds
, self
.title
, minsize
= (330, 120), tabbable
= 0)
92 self
.setupwidgets(text
)
94 if self
.settings
.has_key("selection"):
95 selstart
, selend
= self
.settings
["selection"]
96 self
.setselection(selstart
, selend
)
100 self
._buf
= "" # for write method
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)
107 def readwindowsettings(self
):
109 resref
= Res
.FSpOpenResFile(self
.path
, 1)
113 Res
.UseResFile(resref
)
114 data
= Res
.Get1Resource('PyWS', 128)
115 self
.settings
= marshal
.loads(data
.data
)
118 Res
.CloseResFile(resref
)
120 def writewindowsettings(self
):
122 resref
= Res
.FSpOpenResFile(self
.path
, 3)
124 Res
.FSpCreateResFile(self
.path
, self
._creator
, 'TEXT', smAllScripts
)
125 resref
= Res
.FSpOpenResFile(self
.path
, 3)
127 data
= Res
.Resource(marshal
.dumps(self
.settings
))
128 Res
.UseResFile(resref
)
130 temp
= Res
.Get1Resource('PyWS', 128)
131 temp
.RemoveResource()
134 data
.AddResource('PyWS', 128, "window settings")
136 Res
.UpdateResFile(resref
)
137 Res
.CloseResFile(resref
)
139 def getsettings(self
):
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
150 return self
.editgroup
.editor
.get()
152 def getselection(self
):
153 return self
.editgroup
.editor
.ted
.WEGetSelection()
155 def setselection(self
, selstart
, selend
):
156 self
.editgroup
.editor
.setselection(selstart
, selend
)
158 def getselectedtext(self
):
159 return self
.editgroup
.editor
.getselectedtext()
161 def getfilename(self
):
164 return '<%s>' % self
.title
166 def setupwidgets(self
, text
):
169 self
.lastlineno
= None
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())
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
185 self
.editgroup
.optionsmenu
= W
.PopupMenu((-15, -1, 16, 16), [])
186 self
.editgroup
.optionsmenu
.bind('<click>', self
.makeoptionsmenu
)
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
)
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
)
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
)
207 self
.linefield
.bind("return", self
.dolinefield
)
208 self
.linefield
.bind("enter", self
.dolinefield
)
209 self
.linefield
.bind("tab", self
.dolinefield
)
212 editor
.bind("<click>", self
.clickeditor
)
213 self
.linefield
.bind("<click>", self
.clicklinefield
)
215 def makeoptionsmenu(self
):
216 menuitems
= [('Font settings\xc9', self
.domenu_fontsettings
),
217 ("Save options\xc9", self
.domenu_options
),
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
),
223 ('Modularize', self
.domenu_modularize
),
224 ('Browse namespace\xc9', self
.domenu_browsenamespace
),
227 menuitems
= menuitems
+ [('Disable profiler', self
.domenu_toggleprofiler
)]
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
)]
235 menuitems
= menuitems
+ [('Enable debugger', self
.domenu_toggledebugger
)]
236 self
.editgroup
.optionsmenu
.set(menuitems
)
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()
244 def XXdomenu_toggle_run_with_interpreter(self
):
245 self
.run_with_interpreter
= not self
.run_with_interpreter
247 self
.run_with_cl_interpreter
= 0
248 self
.editgroup
.editor
.selectionchanged()
250 def domenu_toggle_run_with_cl_interpreter(self
):
251 self
.run_with_cl_interpreter
= not self
.run_with_cl_interpreter
253 self
.run_with_interpreter
= 0
254 self
.editgroup
.editor
.selectionchanged()
256 def showbreakpoints(self
, onoff
):
257 self
.editgroup
.editor
.showbreakpoints(onoff
)
258 self
.debugging
= onoff
260 def domenu_clearbreakpoints(self
, *args
):
261 self
.editgroup
.editor
.clearbreakpoints()
263 def domenu_editbreakpoints(self
, *args
):
264 self
.editgroup
.editor
.editbreakpoints()
266 def domenu_toggledebugger(self
, *args
):
267 if not self
.debugging
:
269 self
.debugging
= not self
.debugging
270 self
.editgroup
.editor
.togglebreakpoints()
272 def domenu_toggleprofiler(self
, *args
):
273 self
.profiling
= not self
.profiling
275 def domenu_browsenamespace(self
, *args
):
278 globals, file, modname
= self
.getenvironment()
281 PyBrowser
.Browser(globals, "Object browser: " + modname
)
283 def domenu_modularize(self
, *args
):
284 modname
= _filename_as_modname(self
.title
)
286 raise W
.AlertError
, "Can't modularize \"%s\"" % self
.title
287 run_as_main
= self
.run_as_main
290 self
.run_as_main
= run_as_main
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
303 def domenu_fontsettings(self
, *args
):
305 fontsettings
= self
.editgroup
.editor
.getfontsettings()
306 tabsettings
= self
.editgroup
.editor
.gettabsettings()
307 settings
= FontSettings
.FontDialog(fontsettings
, tabsettings
)
309 fontsettings
, tabsettings
= settings
310 self
.editgroup
.editor
.setfontsettings(fontsettings
)
311 self
.editgroup
.editor
.settabsettings(tabsettings
)
313 def domenu_options(self
, *args
):
314 rv
= SaveOptions(self
._creator
, self
._eoln
)
316 self
.editgroup
.editor
.selectionchanged() # ouch...
317 self
._creator
, self
._eoln
= rv
319 def clicklinefield(self
):
320 if self
._currentwidget
<> self
.linefield
:
321 self
.linefield
.select(1)
322 self
.linefield
.selectall()
325 def clickeditor(self
):
326 if self
._currentwidget
<> self
.editgroup
.editor
:
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()
338 def dolinefield(self
):
340 lineno
= string
.atoi(self
.linefield
.get()) - 1
341 if lineno
<> self
.lastlineno
:
342 self
.editgroup
.editor
.selectline(lineno
)
343 self
.updateselection(1)
345 self
.updateselection(1)
346 self
.editgroup
.editor
.select(1)
348 def setinfotext(self
):
349 if not hasattr(self
, 'infotext'):
352 self
.infotext
.set(self
.path
)
354 self
.infotext
.set("")
357 if self
.editgroup
.editor
.changed
:
359 save
= EasyDialogs
.AskYesNoCancel('Save window "%s" before closing?' % self
.title
,
360 default
=1, no
="Don\xd5t save")
362 if self
.domenu_save():
369 def domenu_close(self
, *args
):
372 def domenu_save(self
, *args
):
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
382 MacOS
.SetCreatorAndType(self
.path
, self
._creator
, 'TEXT')
384 self
.writewindowsettings()
385 self
.editgroup
.editor
.changed
= 0
386 self
.editgroup
.editor
.selchanged
= 0
388 if linecache
.cache
.has_key(self
.path
):
389 del linecache
.cache
[self
.path
]
391 macostools
.touched(self
.path
)
392 self
.addrecentfile(self
.path
)
394 def can_save(self
, menuitem
):
395 return self
.editgroup
.editor
.changed
or self
.editgroup
.editor
.selchanged
397 def domenu_save_as(self
, *args
):
398 path
= EasyDialogs
.AskFileForSave(message
='Save as:', savedFileName
=self
.title
)
401 self
.showbreakpoints(0)
404 self
.title
= os
.path
.split(self
.path
)[-1]
405 self
.wid
.SetWTitle(self
.title
)
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()
417 def domenu_save_as_applet(self
, *args
):
420 buildtools
.DEBUG
= 0 # ouch.
422 if self
.title
[-3:] == ".py":
423 destname
= self
.title
[:-3]
425 destname
= self
.title
+ ".applet"
426 destname
= EasyDialogs
.AskFileForSave(message
='Save as Applet:',
427 savedFileName
=destname
)
433 if filename
[-3:] == ".py":
434 rsrcname
= filename
[:-3] + '.rsrc'
436 rsrcname
= filename
+ '.rsrc'
438 filename
= self
.title
441 pytext
= self
.editgroup
.editor
.get()
442 pytext
= string
.split(pytext
, '\r')
443 pytext
= string
.join(pytext
, '\n') + '\n'
445 code
= compile(pytext
, filename
, "exec")
446 except (SyntaxError, EOFError):
447 raise buildtools
.BuildError
, "Syntax error in script %s" % `filename`
450 tmpdir
= tempfile
.mkdtemp()
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")
459 # Try removing the output file
464 template
= buildtools
.findtemplate()
465 buildtools
.process(template
, filename
, destname
, 1, rsrcname
=rsrcname
, progress
=None)
472 def domenu_gotoline(self
, *args
):
473 self
.linefield
.selectall()
474 self
.linefield
.select(1)
475 self
.linefield
.selectall()
477 def domenu_selectline(self
, *args
):
478 self
.editgroup
.editor
.expandselection()
480 def domenu_find(self
, *args
):
483 def domenu_entersearchstring(self
, *args
):
484 searchengine
.setfindstring()
486 def domenu_replace(self
, *args
):
487 searchengine
.replace()
489 def domenu_findnext(self
, *args
):
490 searchengine
.findnext()
492 def domenu_replacefind(self
, *args
):
493 searchengine
.replacefind()
495 def domenu_run(self
, *args
):
496 self
.runbutton
.push()
498 def domenu_runselection(self
, *args
):
499 self
.runselbutton
.push()
505 if self
.run_with_interpreter
:
506 if self
.editgroup
.editor
.changed
:
508 save
= EasyDialogs
.AskYesNoCancel('Save "%s" before running?' % self
.title
, 1)
510 if self
.domenu_save():
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
:
520 save
= EasyDialogs
.AskYesNoCancel('Save "%s" before running?' % self
.title
, 1)
522 if self
.domenu_save():
527 raise W
.AlertError
, "Can't run unsaved file"
528 self
._run
_with
_cl
_interpreter
()
530 pytext
= self
.editgroup
.editor
.get()
531 globals, file, modname
= self
.getenvironment()
532 self
.execstring(pytext
, globals, globals, file, modname
)
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"
541 def _run_with_cl_interpreter(self
):
543 interp_path
= os
.path
.join(sys
.exec_prefix
, "bin", "python")
544 file_path
= self
.path
545 if not os
.path
.exists(interp_path
):
546 # This "can happen" if we are running IDE under MacPython-OS9.
547 raise W
.AlertError
, "Can't find command-line Python"
548 cmd
= '"%s" "%s" ; exit' % (interp_path
, file_path
)
549 t
= Terminal
.Terminal()
550 t
.do_script(with_command
=cmd
)
552 def runselection(self
):
555 def _runselection(self
):
556 if self
.run_with_interpreter
or self
.run_with_cl_interpreter
:
557 raise W
.AlertError
, "Can't run selection with Interpreter"
558 globals, file, modname
= self
.getenvironment()
561 self
.editgroup
.editor
.expandselection()
563 # get lineno of first selected line
564 selstart
, selend
= self
.editgroup
.editor
.getselection()
565 selstart
, selend
= min(selstart
, selend
), max(selstart
, selend
)
566 selfirstline
= self
.editgroup
.editor
.offsettoline(selstart
)
567 alltext
= self
.editgroup
.editor
.get()
568 pytext
= alltext
[selstart
:selend
]
569 lines
= string
.split(pytext
, '\r')
570 indent
= getminindent(lines
)
573 alllines
= string
.split(alltext
, '\r')
574 for i
in range(selfirstline
- 1, -1, -1):
576 if line
[:6] == 'class ':
577 classname
= string
.split(string
.strip(line
[6:]))[0]
578 classend
= identifieRE_match(classname
)
580 raise W
.AlertError
, "Can't find a class."
581 classname
= classname
[:classend
]
583 elif line
and line
[0] not in '\t#':
584 raise W
.AlertError
, "Can't find a class."
586 raise W
.AlertError
, "Can't find a class."
587 if globals.has_key(classname
):
588 klass
= globals[classname
]
590 raise W
.AlertError
, "Can't find class \"%s\"." % classname
592 pytext
= ("class %s:\n" % classname
) + pytext
593 selfirstline
= selfirstline
- 1
595 raise W
.AlertError
, "Can't run indented code."
597 # add "newlines" to fool compile/exec:
598 # now a traceback will give the right line number
599 pytext
= selfirstline
* '\r' + pytext
600 self
.execstring(pytext
, globals, locals, file, modname
)
601 if indent
== 1 and globals[classname
] is not klass
:
602 # update the class in place
603 klass
.__dict
__.update(globals[classname
].__dict
__)
604 globals[classname
] = klass
606 def execstring(self
, pytext
, globals, locals, file, modname
):
607 tracebackwindow
.hide()
609 W
.getapplication().refreshwindows()
613 dir = os
.path
.dirname(self
.path
)
614 savedir
= os
.getcwd()
616 sys
.path
.insert(0, dir)
617 self
._scriptDone
= False
618 if sys
.platform
== "darwin":
619 # On MacOSX, MacPython doesn't poll for command-period
620 # (cancel), so to enable the user to cancel a running
621 # script, we have to spawn a thread which does the
622 # polling. It will send a SIGINT to the main thread
623 # (in which the script is running) when the user types
625 from threading
import Thread
626 t
= Thread(target
=self
._userCancelledMonitor
,
627 name
="UserCancelledMonitor")
630 execstring(pytext
, globals, locals, file, self
.debugging
,
631 modname
, self
.profiling
)
633 self
._scriptDone
= True
638 def _userCancelledMonitor(self
):
640 from signal
import SIGINT
641 while not self
._scriptDone
:
642 if Evt
.CheckEventQueueForUserCancel():
643 # Send a SIGINT signal to ourselves.
644 # This gets delivered to the main thread,
645 # cancelling the running script.
646 os
.kill(os
.getpid(), SIGINT
)
650 def getenvironment(self
):
653 dir = os
.path
.dirname(file)
654 # check if we're part of a package
656 while os
.path
.exists(os
.path
.join(dir, "__init__.py")):
657 dir, dirname
= os
.path
.split(dir)
658 modname
= dirname
+ '.' + modname
659 subname
= _filename_as_modname(self
.title
)
661 return self
.globals, file, None
663 if subname
== "__init__":
664 # strip trailing period
665 modname
= modname
[:-1]
667 modname
= modname
+ subname
670 if sys
.modules
.has_key(modname
):
671 globals = sys
.modules
[modname
].__dict
__
674 globals = self
.globals
677 file = '<%s>' % self
.title
678 globals = self
.globals
680 return globals, file, modname
682 def write(self
, stuff
):
683 """for use as stdout"""
684 self
._buf
= self
._buf
+ stuff
685 if '\n' in self
._buf
:
689 stuff
= string
.split(self
._buf
, '\n')
690 stuff
= string
.join(stuff
, '\r')
691 end
= self
.editgroup
.editor
.ted
.WEGetTextLength()
692 self
.editgroup
.editor
.ted
.WESetSelection(end
, end
)
693 self
.editgroup
.editor
.ted
.WEInsert(stuff
, None, None)
694 self
.editgroup
.editor
.updatescrollbars()
697 #self.wid.SelectWindow()
699 def getclasslist(self
):
700 from string
import find
, strip
701 methodRE
= re
.compile(r
"\r[ \t]+def ")
702 findMethod
= methodRE
.search
703 editor
= self
.editgroup
.editor
711 if text
[:4] == 'def ':
712 append((pos
+ 4, functag
))
715 pos
= find(text
, '\rdef ', pos
+ 1)
718 append((pos
+ 5, functag
))
720 if text
[:6] == 'class ':
721 append((pos
+ 6, classtag
))
724 pos
= find(text
, '\rclass ', pos
+ 1)
727 append((pos
+ 7, classtag
))
730 m
= findMethod(text
, pos
+ 1)
734 #pos = find(text, '\r\tdef ', pos + 1)
735 append((m
.regs
[0][1], methodtag
))
738 methodlistappend
= None
739 offsetToLine
= editor
.ted
.WEOffsetToLine
740 getLineRange
= editor
.ted
.WEGetLineRange
741 append
= classlist
.append
742 for pos
, tag
in list:
743 lineno
= offsetToLine(pos
)
744 lineStart
, lineEnd
= getLineRange(lineno
)
745 line
= strip(text
[pos
:lineEnd
])
746 line
= line
[:identifieRE_match(line
)]
748 append(("def " + line
, lineno
+ 1))
749 methodlistappend
= None
750 elif tag
is classtag
:
751 append(["class " + line
])
752 methodlistappend
= classlist
[-1].append
753 elif methodlistappend
and tag
is methodtag
:
754 methodlistappend(("def " + line
, lineno
+ 1))
757 def popselectline(self
, lineno
):
758 self
.editgroup
.editor
.selectline(lineno
- 1)
760 def selectline(self
, lineno
, charoffset
= 0):
761 self
.editgroup
.editor
.selectline(lineno
- 1, charoffset
)
763 def addrecentfile(self
, filename
):
764 app
= W
.getapplication()
765 app
.addrecentfile(filename
)
769 def __init__(self
, creator
, eoln
):
772 self
.w
= w
= W
.ModalDialog((260, 160), 'Save options')
774 w
.label
= W
.TextBox((8, 8, 80, 18), "File creator:")
775 w
.ide_radio
= W
.RadioButton((8, 22, 160, 18), "This application", radiobuttons
, self
.ide_hit
)
776 w
.interp_radio
= W
.RadioButton((8, 42, 160, 18), "MacPython Interpreter", radiobuttons
, self
.interp_hit
)
777 w
.interpx_radio
= W
.RadioButton((8, 62, 160, 18), "OSX PythonW Interpreter", radiobuttons
, self
.interpx_hit
)
778 w
.other_radio
= W
.RadioButton((8, 82, 50, 18), "Other:", radiobuttons
)
779 w
.other_creator
= W
.EditText((62, 82, 40, 20), creator
, self
.otherselect
)
780 w
.none_radio
= W
.RadioButton((8, 102, 160, 18), "None", radiobuttons
, self
.none_hit
)
781 w
.cancelbutton
= W
.Button((-180, -30, 80, 16), "Cancel", self
.cancelbuttonhit
)
782 w
.okbutton
= W
.Button((-90, -30, 80, 16), "Done", self
.okbuttonhit
)
783 w
.setdefaultbutton(w
.okbutton
)
784 if creator
== 'Pyth':
785 w
.interp_radio
.set(1)
786 elif creator
== W
._signature
:
788 elif creator
== 'PytX':
789 w
.interpx_radio
.set(1)
790 elif creator
== '\0\0\0\0':
795 w
.eolnlabel
= W
.TextBox((168, 8, 80, 18), "Newline style:")
797 w
.unix_radio
= W
.RadioButton((168, 22, 80, 18), "Unix", radiobuttons
, self
.unix_hit
)
798 w
.mac_radio
= W
.RadioButton((168, 42, 80, 18), "Macintosh", radiobuttons
, self
.mac_hit
)
799 w
.win_radio
= W
.RadioButton((168, 62, 80, 18), "Windows", radiobuttons
, self
.win_hit
)
800 if self
.eoln
== '\n':
802 elif self
.eoln
== '\r\n':
807 w
.bind("cmd.", w
.cancelbutton
.push
)
811 self
.w
.other_creator
.set(W
._signature
)
813 def interp_hit(self
):
814 self
.w
.other_creator
.set("Pyth")
816 def interpx_hit(self
):
817 self
.w
.other_creator
.set("PytX")
820 self
.w
.other_creator
.set("\0\0\0\0")
822 def otherselect(self
, *args
):
823 sel_from
, sel_to
= self
.w
.other_creator
.getselection()
824 creator
= self
.w
.other_creator
.get()[:4]
825 creator
= creator
+ " " * (4 - len(creator
))
826 self
.w
.other_creator
.set(creator
)
827 self
.w
.other_creator
.setselection(sel_from
, sel_to
)
828 self
.w
.other_radio
.set(1)
839 def cancelbuttonhit(self
):
842 def okbuttonhit(self
):
843 self
.rv
= (self
.w
.other_creator
.get()[:4], self
.eoln
)
847 def SaveOptions(creator
, eoln
):
848 s
= _saveoptions(creator
, eoln
)
852 def _escape(where
, what
) :
853 return string
.join(string
.split(where
, what
), '\\' + what
)
855 def _makewholewordpattern(word
):
856 # first, escape special regex chars
857 for esc
in "\\[]()|.*^+$?":
858 word
= _escape(word
, esc
)
859 notwordcharspat
= '[^' + _wordchars
+ ']'
860 pattern
= '(' + word
+ ')'
861 if word
[0] in _wordchars
:
862 pattern
= notwordcharspat
+ pattern
863 if word
[-1] in _wordchars
:
864 pattern
= pattern
+ notwordcharspat
865 return re
.compile(pattern
)
873 self
.parms
= { "find": "",
880 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
881 if prefs
.searchengine
:
882 self
.parms
["casesens"] = prefs
.searchengine
.casesens
883 self
.parms
["wrap"] = prefs
.searchengine
.wrap
884 self
.parms
["wholeword"] = prefs
.searchengine
.wholeword
889 self
.w
.wid
.ShowWindow()
890 self
.w
.wid
.SelectWindow()
891 self
.w
.find
.edit
.select(1)
892 self
.w
.find
.edit
.selectall()
894 self
.w
= W
.Dialog((420, 150), "Find")
896 self
.w
.find
= TitledEditText((10, 4, 300, 36), "Search for:")
897 self
.w
.replace
= TitledEditText((10, 100, 300, 36), "Replace with:")
899 self
.w
.boxes
= W
.Group((10, 50, 300, 40))
900 self
.w
.boxes
.casesens
= W
.CheckBox((0, 0, 100, 16), "Case sensitive")
901 self
.w
.boxes
.wholeword
= W
.CheckBox((0, 20, 100, 16), "Whole word")
902 self
.w
.boxes
.wrap
= W
.CheckBox((110, 0, 100, 16), "Wrap around")
904 self
.buttons
= [ ("Find", "cmdf", self
.find
),
905 ("Replace", "cmdr", self
.replace
),
906 ("Replace all", None, self
.replaceall
),
907 ("Don't find", "cmdd", self
.dont
),
908 ("Cancel", "cmd.", self
.cancel
)
910 for i
in range(len(self
.buttons
)):
911 bounds
= -90, 22 + i
* 24, 80, 16
912 title
, shortcut
, callback
= self
.buttons
[i
]
913 self
.w
[title
] = W
.Button(bounds
, title
, callback
)
915 self
.w
.bind(shortcut
, self
.w
[title
].push
)
916 self
.w
.setdefaultbutton(self
.w
["Don't find"])
917 self
.w
.find
.edit
.bind("<key>", self
.key
)
918 self
.w
.bind("<activate>", self
.activate
)
919 self
.w
.bind("<close>", self
.close
)
922 self
.w
.find
.edit
.select(1)
923 self
.w
.find
.edit
.selectall()
930 def key(self
, char
, modifiers
):
931 self
.w
.find
.edit
.key(char
, modifiers
)
935 def activate(self
, onoff
):
939 def checkbuttons(self
):
940 editor
= findeditor(self
)
942 if self
.w
.find
.get():
943 for title
, cmd
, call
in self
.buttons
[:-2]:
944 self
.w
[title
].enable(1)
945 self
.w
.setdefaultbutton(self
.w
["Find"])
947 for title
, cmd
, call
in self
.buttons
[:-2]:
948 self
.w
[title
].enable(0)
949 self
.w
.setdefaultbutton(self
.w
["Don't find"])
951 for title
, cmd
, call
in self
.buttons
[:-2]:
952 self
.w
[title
].enable(0)
953 self
.w
.setdefaultbutton(self
.w
["Don't find"])
956 self
.getparmsfromwindow()
961 editor
= findeditor(self
)
965 self
.getparmsfromwindow()
966 text
= editor
.getselectedtext()
967 find
= self
.parms
["find"]
968 if not self
.parms
["casesens"]:
969 find
= string
.lower(find
)
970 text
= string
.lower(text
)
973 editor
.insert(self
.parms
["replace"])
975 def replaceall(self
):
976 editor
= findeditor(self
)
980 self
.getparmsfromwindow()
982 find
= self
.parms
["find"]
986 replace
= self
.parms
["replace"]
987 replacelen
= len(replace
)
989 if not self
.parms
["casesens"]:
990 find
= string
.lower(find
)
991 text
= string
.lower(Text
)
998 if self
.parms
["wholeword"]:
999 wholewordRE
= _makewholewordpattern(find
)
1000 match
= wholewordRE
.search(text
, pos
)
1002 pos
= match
.start(1)
1006 pos
= string
.find(text
, find
, pos
)
1009 counter
= counter
+ 1
1010 text
= text
[:pos
] + replace
+ text
[pos
+ findlen
:]
1011 Text
= Text
[:pos
] + replace
+ Text
[pos
+ findlen
:]
1012 pos
= pos
+ replacelen
1013 W
.SetCursor("arrow")
1016 from Carbon
import Res
1017 editor
.textchanged()
1018 editor
.selectionchanged()
1020 EasyDialogs
.Message("Replaced %d occurrences" % counter
)
1023 self
.getparmsfromwindow()
1026 def replacefind(self
):
1030 def setfindstring(self
):
1031 editor
= findeditor(self
)
1034 find
= editor
.getselectedtext()
1037 self
.parms
["find"] = find
1039 self
.w
.find
.edit
.set(self
.parms
["find"])
1040 self
.w
.find
.edit
.selectall()
1043 editor
= findeditor(self
)
1046 find
= self
.parms
["find"]
1050 if not self
.parms
["casesens"]:
1051 find
= string
.lower(find
)
1052 text
= string
.lower(text
)
1053 selstart
, selend
= editor
.getselection()
1054 selstart
, selend
= min(selstart
, selend
), max(selstart
, selend
)
1055 if self
.parms
["wholeword"]:
1056 wholewordRE
= _makewholewordpattern(find
)
1057 match
= wholewordRE
.search(text
, selend
)
1059 pos
= match
.start(1)
1063 pos
= string
.find(text
, find
, selend
)
1065 editor
.setselection(pos
, pos
+ len(find
))
1067 elif self
.parms
["wrap"]:
1068 if self
.parms
["wholeword"]:
1069 match
= wholewordRE
.search(text
, 0)
1071 pos
= match
.start(1)
1075 pos
= string
.find(text
, find
)
1076 if selstart
> pos
>= 0:
1077 editor
.setselection(pos
, pos
+ len(find
))
1081 for key
, value
in self
.parms
.items():
1083 self
.w
[key
].set(value
)
1085 self
.w
.boxes
[key
].set(value
)
1087 def getparmsfromwindow(self
):
1090 for key
, value
in self
.parms
.items():
1092 value
= self
.w
[key
].get()
1094 value
= self
.w
.boxes
[key
].get()
1095 self
.parms
[key
] = value
1103 self
.w
.wid
.HideWindow()
1106 def writeprefs(self
):
1108 self
.getparmsfromwindow()
1109 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
1110 prefs
.searchengine
.casesens
= self
.parms
["casesens"]
1111 prefs
.searchengine
.wrap
= self
.parms
["wrap"]
1112 prefs
.searchengine
.wholeword
= self
.parms
["wholeword"]
1116 class TitledEditText(W
.Group
):
1118 def __init__(self
, possize
, title
, text
= ""):
1119 W
.Group
.__init
__(self
, possize
)
1120 self
.title
= W
.TextBox((0, 0, 0, 16), title
)
1121 self
.edit
= W
.EditText((0, 16, 0, 0), text
)
1123 def set(self
, value
):
1124 self
.edit
.set(value
)
1127 return self
.edit
.get()
1130 class ClassFinder(W
.PopupWidget
):
1132 def click(self
, point
, modifiers
):
1133 W
.SetCursor("watch")
1134 self
.set(self
._parentwindow
.getclasslist())
1135 W
.PopupWidget
.click(self
, point
, modifiers
)
1138 def getminindent(lines
):
1141 stripped
= string
.strip(line
)
1142 if not stripped
or stripped
[0] == '#':
1144 if indent
< 0 or line
[:indent
] <> indent
* '\t':
1154 return not not ord(Evt
.GetKeys()[7]) & 0x04
1157 def execstring(pytext
, globals, locals, filename
="<string>", debugging
=0,
1158 modname
="__main__", profiling
=0):
1160 import PyDebugger
, bdb
1161 BdbQuit
= bdb
.BdbQuit
1163 BdbQuit
= 'BdbQuitDummyException'
1164 pytext
= string
.split(pytext
, '\r')
1165 pytext
= string
.join(pytext
, '\n') + '\n'
1166 W
.SetCursor("watch")
1167 globals['__name__'] = modname
1168 globals['__file__'] = filename
1169 sys
.argv
= [filename
]
1171 code
= compile(pytext
, filename
, "exec")
1173 # XXXX BAAAADDD.... We let tracebackwindow decide to treat SyntaxError
1174 # special. That's wrong because THIS case is special (could be literal
1175 # overflow!) and SyntaxError could mean we need a traceback (syntax error
1176 # in imported module!!!
1177 tracebackwindow
.traceback(1, filename
)
1181 PyDebugger
.startfromhere()
1183 if hasattr(MacOS
, 'EnableAppswitch'):
1184 MacOS
.EnableAppswitch(0)
1187 import profile
, ProfileBrowser
1188 p
= profile
.Profile()
1191 p
.runctx(code
, globals, locals)
1195 stats
= pstats
.Stats(p
)
1196 ProfileBrowser
.ProfileBrowser(stats
)
1198 exec code
in globals, locals
1200 if hasattr(MacOS
, 'EnableAppswitch'):
1201 MacOS
.EnableAppswitch(-1)
1202 except W
.AlertError
, detail
:
1203 raise W
.AlertError
, detail
1204 except (KeyboardInterrupt, BdbQuit
):
1206 except SystemExit, arg
:
1208 sys
.stderr
.write("Script exited with status code: %s\n" % repr(arg
.code
))
1212 PyDebugger
.postmortem(sys
.exc_type
, sys
.exc_value
, sys
.exc_traceback
)
1215 tracebackwindow
.traceback(1, filename
)
1221 _identifieRE
= re
.compile(r
"[A-Za-z_][A-Za-z_0-9]*")
1223 def identifieRE_match(str):
1224 match
= _identifieRE
.match(str)
1229 def _filename_as_modname(fname
):
1230 if fname
[-3:] == '.py':
1231 modname
= fname
[:-3]
1232 match
= _identifieRE
.match(modname
)
1233 if match
and match
.start() == 0 and match
.end() == len(modname
):
1234 return string
.join(string
.split(modname
, '.'), '_')
1236 def findeditor(topwindow
, fromtop
= 0):
1237 wid
= MyFrontWindow()
1239 if topwindow
.w
and wid
== topwindow
.w
.wid
:
1240 wid
= topwindow
.w
.wid
.GetNextWindow()
1243 app
= W
.getapplication()
1244 if app
._windows
.has_key(wid
): # KeyError otherwise can happen in RoboFog :-(
1245 window
= W
.getapplication()._windows
[wid
]
1248 if not isinstance(window
, Editor
):
1250 return window
.editgroup
.editor
1253 class _EditorDefaultSettings
:
1256 self
.template
= "%s, %d point"
1257 self
.fontsettings
, self
.tabsettings
, self
.windowsize
= geteditorprefs()
1258 self
.w
= W
.Dialog((328, 120), "Editor default settings")
1259 self
.w
.setfontbutton
= W
.Button((8, 8, 80, 16), "Set font\xc9", self
.dofont
)
1260 self
.w
.fonttext
= W
.TextBox((98, 10, -8, 14), self
.template
% (self
.fontsettings
[0], self
.fontsettings
[2]))
1262 self
.w
.picksizebutton
= W
.Button((8, 50, 80, 16), "Front window", self
.picksize
)
1263 self
.w
.xsizelabel
= W
.TextBox((98, 32, 40, 14), "Width:")
1264 self
.w
.ysizelabel
= W
.TextBox((148, 32, 40, 14), "Height:")
1265 self
.w
.xsize
= W
.EditText((98, 48, 40, 20), `self
.windowsize
[0]`
)
1266 self
.w
.ysize
= W
.EditText((148, 48, 40, 20), `self
.windowsize
[1]`
)
1268 self
.w
.cancelbutton
= W
.Button((-180, -26, 80, 16), "Cancel", self
.cancel
)
1269 self
.w
.okbutton
= W
.Button((-90, -26, 80, 16), "Done", self
.ok
)
1270 self
.w
.setdefaultbutton(self
.w
.okbutton
)
1271 self
.w
.bind('cmd.', self
.w
.cancelbutton
.push
)
1275 app
= W
.getapplication()
1276 editor
= findeditor(self
)
1277 if editor
is not None:
1278 width
, height
= editor
._parentwindow
._bounds
[2:]
1279 self
.w
.xsize
.set(`width`
)
1280 self
.w
.ysize
.set(`height`
)
1282 raise W
.AlertError
, "No edit window found"
1286 settings
= FontSettings
.FontDialog(self
.fontsettings
, self
.tabsettings
)
1288 self
.fontsettings
, self
.tabsettings
= settings
1289 sys
.exc_traceback
= None
1290 self
.w
.fonttext
.set(self
.template
% (self
.fontsettings
[0], self
.fontsettings
[2]))
1301 width
= string
.atoi(self
.w
.xsize
.get())
1303 self
.w
.xsize
.select(1)
1304 self
.w
.xsize
.selectall()
1305 raise W
.AlertError
, "Bad number for window width"
1307 height
= string
.atoi(self
.w
.ysize
.get())
1309 self
.w
.ysize
.select(1)
1310 self
.w
.ysize
.selectall()
1311 raise W
.AlertError
, "Bad number for window height"
1312 self
.windowsize
= width
, height
1313 seteditorprefs(self
.fontsettings
, self
.tabsettings
, self
.windowsize
)
1316 def geteditorprefs():
1318 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
1320 fontsettings
= prefs
.pyedit
.fontsettings
1321 tabsettings
= prefs
.pyedit
.tabsettings
1322 windowsize
= prefs
.pyedit
.windowsize
1324 fontsettings
= prefs
.pyedit
.fontsettings
= ("Geneva", 0, 10, (0, 0, 0))
1325 tabsettings
= prefs
.pyedit
.tabsettings
= (8, 1)
1326 windowsize
= prefs
.pyedit
.windowsize
= (500, 250)
1327 sys
.exc_traceback
= None
1328 return fontsettings
, tabsettings
, windowsize
1330 def seteditorprefs(fontsettings
, tabsettings
, windowsize
):
1332 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
1333 prefs
.pyedit
.fontsettings
= fontsettings
1334 prefs
.pyedit
.tabsettings
= tabsettings
1335 prefs
.pyedit
.windowsize
= windowsize
1338 _defaultSettingsEditor
= None
1340 def EditorDefaultSettings():
1341 global _defaultSettingsEditor
1342 if _defaultSettingsEditor
is None or not hasattr(_defaultSettingsEditor
, "w"):
1343 _defaultSettingsEditor
= _EditorDefaultSettings()
1345 _defaultSettingsEditor
.w
.select()
1347 def resolvealiases(path
):
1349 fsr
, d1
, d2
= File
.FSResolveAliasFile(path
, 1)
1350 path
= fsr
.as_pathname()
1352 except (File
.Error
, ValueError), (error
, str):
1355 dir, file = os
.path
.split(path
)
1356 return os
.path
.join(resolvealiases(dir), file)
1358 searchengine
= SearchEngine()
1359 tracebackwindow
= Wtraceback
.TraceBack()