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
34 haveThreading
= Wthreading
.haveThreading
36 _scriptuntitledcounter
= 1
37 _wordchars
= string
.ascii_letters
+ string
.digits
+ "_"
40 runButtonLabels
= ["Run all", "Stop!"]
41 runSelButtonLabels
= ["Run selection", "Pause!", "Resume"]
44 class Editor(W
.Window
):
46 def __init__(self
, path
= "", title
= ""):
47 defaultfontsettings
, defaulttabsettings
, defaultwindowsize
= geteditorprefs()
48 global _scriptuntitledcounter
53 self
.title
= "Untitled Script " + `_scriptuntitledcounter`
54 _scriptuntitledcounter
= _scriptuntitledcounter
+ 1
56 self
._creator
= W
._signature
57 self
._eoln
= os
.linesep
58 elif os
.path
.exists(path
):
59 path
= resolvealiases(path
)
60 dir, name
= os
.path
.split(path
)
65 self
._creator
, filetype
= MacOS
.GetCreatorAndType(path
)
66 self
.addrecentfile(path
)
68 raise IOError, "file '%s' does not exist" % path
72 if string
.find(text
, '\r\n') >= 0:
76 text
= string
.replace(text
, self
._eoln
, '\r')
84 self
.readwindowsettings()
85 if self
.settings
.has_key("windowbounds"):
86 bounds
= self
.settings
["windowbounds"]
88 bounds
= defaultwindowsize
89 if self
.settings
.has_key("fontsettings"):
90 self
.fontsettings
= self
.settings
["fontsettings"]
92 self
.fontsettings
= defaultfontsettings
93 if self
.settings
.has_key("tabsize"):
95 self
.tabsettings
= (tabsize
, tabmode
) = self
.settings
["tabsize"]
97 self
.tabsettings
= defaulttabsettings
99 self
.tabsettings
= defaulttabsettings
101 W
.Window
.__init
__(self
, bounds
, self
.title
, minsize
= (330, 120), tabbable
= 0)
102 self
.setupwidgets(text
)
104 self
.editgroup
.editor
.textchanged()
106 if self
.settings
.has_key("selection"):
107 selstart
, selend
= self
.settings
["selection"]
108 self
.setselection(selstart
, selend
)
112 self
._buf
= "" # for write method
115 self
.run_as_main
= self
.settings
.get("run_as_main", 0)
116 self
.run_with_interpreter
= self
.settings
.get("run_with_interpreter", 0)
117 self
.run_with_cl_interpreter
= self
.settings
.get("run_with_cl_interpreter", 0)
118 self
._threadstate
= (0, 0)
121 def readwindowsettings(self
):
123 resref
= Res
.FSpOpenResFile(self
.path
, 1)
127 Res
.UseResFile(resref
)
128 data
= Res
.Get1Resource('PyWS', 128)
129 self
.settings
= marshal
.loads(data
.data
)
132 Res
.CloseResFile(resref
)
134 def writewindowsettings(self
):
136 resref
= Res
.FSpOpenResFile(self
.path
, 3)
138 Res
.FSpCreateResFile(self
.path
, self
._creator
, 'TEXT', smAllScripts
)
139 resref
= Res
.FSpOpenResFile(self
.path
, 3)
141 data
= Res
.Resource(marshal
.dumps(self
.settings
))
142 Res
.UseResFile(resref
)
144 temp
= Res
.Get1Resource('PyWS', 128)
145 temp
.RemoveResource()
148 data
.AddResource('PyWS', 128, "window settings")
150 Res
.UpdateResFile(resref
)
151 Res
.CloseResFile(resref
)
153 def getsettings(self
):
155 self
.settings
["windowbounds"] = self
.getbounds()
156 self
.settings
["selection"] = self
.getselection()
157 self
.settings
["fontsettings"] = self
.editgroup
.editor
.getfontsettings()
158 self
.settings
["tabsize"] = self
.editgroup
.editor
.gettabsettings()
159 self
.settings
["run_as_main"] = self
.run_as_main
160 self
.settings
["run_with_interpreter"] = self
.run_with_interpreter
161 self
.settings
["run_with_cl_interpreter"] = self
.run_with_cl_interpreter
164 return self
.editgroup
.editor
.get()
166 def getselection(self
):
167 return self
.editgroup
.editor
.ted
.WEGetSelection()
169 def setselection(self
, selstart
, selend
):
170 self
.editgroup
.editor
.setselection(selstart
, selend
)
172 def getfilename(self
):
175 return '<%s>' % self
.title
177 def setupwidgets(self
, text
):
180 self
.lastlineno
= None
183 self
.editgroup
= W
.Group((0, topbarheight
+ 1, 0, 0))
184 editor
= W
.PyEditor((0, 0, -15,-15), text
,
185 fontsettings
= self
.fontsettings
,
186 tabsettings
= self
.tabsettings
,
187 file = self
.getfilename())
190 self
.popfield
= ClassFinder((popfieldwidth
- 17, -15, 16, 16), [], self
.popselectline
)
191 self
.linefield
= W
.EditText((-1, -15, popfieldwidth
- 15, 16), inset
= (6, 1))
192 self
.editgroup
._barx
= W
.Scrollbar((popfieldwidth
- 2, -15, -14, 16), editor
.hscroll
, max = 32767)
193 self
.editgroup
._bary
= W
.Scrollbar((-15, 14, 16, -14), editor
.vscroll
, max = 32767)
194 self
.editgroup
.editor
= editor
# add editor *after* scrollbars
196 self
.editgroup
.optionsmenu
= W
.PopupMenu((-15, -1, 16, 16), [])
197 self
.editgroup
.optionsmenu
.bind('<click>', self
.makeoptionsmenu
)
199 self
.bevelbox
= W
.BevelBox((0, 0, 0, topbarheight
))
200 self
.hline
= W
.HorizontalLine((0, topbarheight
, 0, 0))
201 self
.infotext
= W
.TextBox((175, 6, -4, 14), backgroundcolor
= (0xe000, 0xe000, 0xe000))
202 self
.runbutton
= W
.BevelButton((6, 4, 80, 16), runButtonLabels
[0], self
.run
)
203 self
.runselbutton
= W
.BevelButton((90, 4, 80, 16), runSelButtonLabels
[0], self
.runselection
)
206 editor
.bind("cmdr", self
.runbutton
.push
)
207 editor
.bind("enter", self
.runselbutton
.push
)
208 editor
.bind("cmdj", self
.domenu_gotoline
)
209 editor
.bind("cmdd", self
.domenu_toggledebugger
)
210 editor
.bind("<idle>", self
.updateselection
)
212 editor
.bind("cmde", searchengine
.setfindstring
)
213 editor
.bind("cmdf", searchengine
.show
)
214 editor
.bind("cmdg", searchengine
.findnext
)
215 editor
.bind("cmdshiftr", searchengine
.replace
)
216 editor
.bind("cmdt", searchengine
.replacefind
)
218 self
.linefield
.bind("return", self
.dolinefield
)
219 self
.linefield
.bind("enter", self
.dolinefield
)
220 self
.linefield
.bind("tab", self
.dolinefield
)
223 editor
.bind("<click>", self
.clickeditor
)
224 self
.linefield
.bind("<click>", self
.clicklinefield
)
226 def makeoptionsmenu(self
):
227 menuitems
= [('Font settings\xc9', self
.domenu_fontsettings
),
228 ("Save options\xc9", self
.domenu_options
),
230 ('\0' + chr(self
.run_as_main
) + 'Run as __main__', self
.domenu_toggle_run_as_main
),
231 #('\0' + chr(self.run_with_interpreter) + 'Run with Interpreter', self.domenu_dtoggle_run_with_interpreter),
232 ('\0' + chr(self
.run_with_cl_interpreter
) + 'Run with commandline Python', self
.domenu_toggle_run_with_cl_interpreter
),
234 ('Modularize', self
.domenu_modularize
),
235 ('Browse namespace\xc9', self
.domenu_browsenamespace
),
238 menuitems
= menuitems
+ [('Disable profiler', self
.domenu_toggleprofiler
)]
240 menuitems
= menuitems
+ [('Enable profiler', self
.domenu_toggleprofiler
)]
241 if self
.editgroup
.editor
._debugger
:
242 menuitems
= menuitems
+ [('Disable debugger', self
.domenu_toggledebugger
),
243 ('Clear breakpoints', self
.domenu_clearbreakpoints
),
244 ('Edit breakpoints\xc9', self
.domenu_editbreakpoints
)]
246 menuitems
= menuitems
+ [('Enable debugger', self
.domenu_toggledebugger
)]
247 self
.editgroup
.optionsmenu
.set(menuitems
)
249 def domenu_toggle_run_as_main(self
):
250 self
.run_as_main
= not self
.run_as_main
251 self
.run_with_interpreter
= 0
252 self
.run_with_cl_interpreter
= 0
253 self
.editgroup
.editor
.selectionchanged()
255 def XXdomenu_toggle_run_with_interpreter(self
):
256 self
.run_with_interpreter
= not self
.run_with_interpreter
258 self
.run_with_cl_interpreter
= 0
259 self
.editgroup
.editor
.selectionchanged()
261 def domenu_toggle_run_with_cl_interpreter(self
):
262 self
.run_with_cl_interpreter
= not self
.run_with_cl_interpreter
264 self
.run_with_interpreter
= 0
265 self
.editgroup
.editor
.selectionchanged()
267 def showbreakpoints(self
, onoff
):
268 self
.editgroup
.editor
.showbreakpoints(onoff
)
269 self
.debugging
= onoff
271 def domenu_clearbreakpoints(self
, *args
):
272 self
.editgroup
.editor
.clearbreakpoints()
274 def domenu_editbreakpoints(self
, *args
):
275 self
.editgroup
.editor
.editbreakpoints()
277 def domenu_toggledebugger(self
, *args
):
278 if not self
.debugging
:
280 self
.debugging
= not self
.debugging
281 self
.editgroup
.editor
.togglebreakpoints()
283 def domenu_toggleprofiler(self
, *args
):
284 self
.profiling
= not self
.profiling
286 def domenu_browsenamespace(self
, *args
):
289 globals, file, modname
= self
.getenvironment()
292 PyBrowser
.Browser(globals, "Object browser: " + modname
)
294 def domenu_modularize(self
, *args
):
295 modname
= _filename_as_modname(self
.title
)
297 raise W
.AlertError
, "Can't modularize \"%s\"" % self
.title
298 run_as_main
= self
.run_as_main
301 self
.run_as_main
= run_as_main
307 if self
.globals and not sys
.modules
.has_key(modname
):
308 module
= imp
.new_module(modname
)
309 for attr
in self
.globals.keys():
310 setattr(module
,attr
,self
.globals[attr
])
311 sys
.modules
[modname
] = module
314 def domenu_fontsettings(self
, *args
):
316 fontsettings
= self
.editgroup
.editor
.getfontsettings()
317 tabsettings
= self
.editgroup
.editor
.gettabsettings()
318 settings
= FontSettings
.FontDialog(fontsettings
, tabsettings
)
320 fontsettings
, tabsettings
= settings
321 self
.editgroup
.editor
.setfontsettings(fontsettings
)
322 self
.editgroup
.editor
.settabsettings(tabsettings
)
324 def domenu_options(self
, *args
):
325 rv
= SaveOptions(self
._creator
, self
._eoln
)
327 self
.editgroup
.editor
.selectionchanged() # ouch...
328 self
._creator
, self
._eoln
= rv
330 def clicklinefield(self
):
331 if self
._currentwidget
<> self
.linefield
:
332 self
.linefield
.select(1)
333 self
.linefield
.selectall()
336 def clickeditor(self
):
337 if self
._currentwidget
<> self
.editgroup
.editor
:
341 def updateselection(self
, force
= 0):
342 sel
= min(self
.editgroup
.editor
.getselection())
343 lineno
= self
.editgroup
.editor
.offsettoline(sel
)
344 if lineno
<> self
.lastlineno
or force
:
345 self
.lastlineno
= lineno
346 self
.linefield
.set(str(lineno
+ 1))
347 self
.linefield
.selview()
349 def dolinefield(self
):
351 lineno
= string
.atoi(self
.linefield
.get()) - 1
352 if lineno
<> self
.lastlineno
:
353 self
.editgroup
.editor
.selectline(lineno
)
354 self
.updateselection(1)
356 self
.updateselection(1)
357 self
.editgroup
.editor
.select(1)
359 def setinfotext(self
):
360 if not hasattr(self
, 'infotext'):
363 self
.infotext
.set(self
.path
)
365 self
.infotext
.set("")
368 if self
.editgroup
.editor
.changed
:
370 save
= EasyDialogs
.AskYesNoCancel('Save window "%s" before closing?' % self
.title
,
371 default
=1, no
="Don\xd5t save")
373 if self
.domenu_save():
380 def domenu_close(self
, *args
):
383 def domenu_save(self
, *args
):
385 # Will call us recursively
386 return self
.domenu_save_as()
387 data
= self
.editgroup
.editor
.get()
388 if self
._eoln
!= '\r':
389 data
= string
.replace(data
, '\r', self
._eoln
)
390 fp
= open(self
.path
, 'wb') # open file in binary mode, data has '\r' line-endings
393 MacOS
.SetCreatorAndType(self
.path
, self
._creator
, 'TEXT')
395 self
.writewindowsettings()
396 self
.editgroup
.editor
.changed
= 0
397 self
.editgroup
.editor
.selchanged
= 0
399 if linecache
.cache
.has_key(self
.path
):
400 del linecache
.cache
[self
.path
]
402 macostools
.touched(self
.path
)
403 self
.addrecentfile(self
.path
)
405 def can_save(self
, menuitem
):
406 return self
.editgroup
.editor
.changed
or self
.editgroup
.editor
.selchanged
408 def domenu_save_as(self
, *args
):
409 path
= EasyDialogs
.AskFileForSave(message
='Save as:', savedFileName
=self
.title
)
412 self
.showbreakpoints(0)
415 self
.title
= os
.path
.split(self
.path
)[-1]
416 self
.wid
.SetWTitle(self
.title
)
418 self
.editgroup
.editor
.setfile(self
.getfilename())
419 app
= W
.getapplication()
420 app
.makeopenwindowsmenu()
421 if hasattr(app
, 'makescriptsmenu'):
422 app
= W
.getapplication()
423 fsr
, changed
= app
.scriptsfolder
.FSResolveAlias(None)
424 path
= fsr
.as_pathname()
425 if path
== self
.path
[:len(path
)]:
426 W
.getapplication().makescriptsmenu()
428 def domenu_save_as_applet(self
, *args
):
431 buildtools
.DEBUG
= 0 # ouch.
433 if self
.title
[-3:] == ".py":
434 destname
= self
.title
[:-3]
436 destname
= self
.title
+ ".applet"
437 destname
= EasyDialogs
.AskFileForSave(message
='Save as Applet:',
438 savedFileName
=destname
)
444 if filename
[-3:] == ".py":
445 rsrcname
= filename
[:-3] + '.rsrc'
447 rsrcname
= filename
+ '.rsrc'
449 filename
= self
.title
452 pytext
= self
.editgroup
.editor
.get()
453 pytext
= string
.split(pytext
, '\r')
454 pytext
= string
.join(pytext
, '\n') + '\n'
456 code
= compile(pytext
, filename
, "exec")
457 except (SyntaxError, EOFError):
458 raise buildtools
.BuildError
, "Syntax error in script %s" % `filename`
461 tmpdir
= tempfile
.mkdtemp()
463 if filename
[-3:] != ".py":
464 filename
= filename
+ ".py"
465 filename
= os
.path
.join(tmpdir
, os
.path
.split(filename
)[1])
466 fp
= open(filename
, "w")
470 # Try removing the output file
475 template
= buildtools
.findtemplate()
476 buildtools
.process(template
, filename
, destname
, 1, rsrcname
=rsrcname
, progress
=None)
483 def domenu_gotoline(self
, *args
):
484 self
.linefield
.selectall()
485 self
.linefield
.select(1)
486 self
.linefield
.selectall()
488 def domenu_selectline(self
, *args
):
489 self
.editgroup
.editor
.expandselection()
491 def domenu_find(self
, *args
):
494 def domenu_entersearchstring(self
, *args
):
495 searchengine
.setfindstring()
497 def domenu_replace(self
, *args
):
498 searchengine
.replace()
500 def domenu_findnext(self
, *args
):
501 searchengine
.findnext()
503 def domenu_replacefind(self
, *args
):
504 searchengine
.replacefind()
506 def domenu_run(self
, *args
):
507 self
.runbutton
.push()
509 def domenu_runselection(self
, *args
):
510 self
.runselbutton
.push()
513 if self
._threadstate
== (0, 0):
516 lock
= Wthreading
.Lock()
518 self
._thread
.postException(KeyboardInterrupt)
519 if self
._thread
.isBlocked():
524 if self
.run_with_interpreter
:
525 if self
.editgroup
.editor
.changed
:
527 save
= EasyDialogs
.AskYesNoCancel('Save "%s" before running?' % self
.title
, 1)
529 if self
.domenu_save():
534 raise W
.AlertError
, "Can't run unsaved file"
535 self
._run
_with
_interpreter
()
536 elif self
.run_with_cl_interpreter
:
537 if self
.editgroup
.editor
.changed
:
539 save
= EasyDialogs
.AskYesNoCancel('Save "%s" before running?' % self
.title
, 1)
541 if self
.domenu_save():
546 raise W
.AlertError
, "Can't run unsaved file"
547 self
._run
_with
_cl
_interpreter
()
549 pytext
= self
.editgroup
.editor
.get()
550 globals, file, modname
= self
.getenvironment()
551 self
.execstring(pytext
, globals, globals, file, modname
)
553 def _run_with_interpreter(self
):
554 interp_path
= os
.path
.join(sys
.exec_prefix
, "PythonInterpreter")
555 if not os
.path
.exists(interp_path
):
556 raise W
.AlertError
, "Can't find interpreter"
560 def _run_with_cl_interpreter(self
):
562 interp_path
= os
.path
.join(sys
.exec_prefix
, "bin", "python")
563 file_path
= self
.path
564 if not os
.path
.exists(interp_path
):
565 # This "can happen" if we are running IDE under MacPython-OS9.
566 raise W
.AlertError
, "Can't find command-line Python"
567 cmd
= '"%s" "%s" ; exit' % (interp_path
, file_path
)
568 t
= Terminal
.Terminal()
569 t
.do_script(with_command
=cmd
)
571 def runselection(self
):
572 if self
._threadstate
== (0, 0):
574 elif self
._threadstate
== (1, 1):
576 self
.setthreadstate((1, 2))
577 elif self
._threadstate
== (1, 2):
579 self
.setthreadstate((1, 1))
581 def _runselection(self
):
582 if self
.run_with_interpreter
or self
.run_with_cl_interpreter
:
583 raise W
.AlertError
, "Can't run selection with Interpreter"
584 globals, file, modname
= self
.getenvironment()
587 self
.editgroup
.editor
.expandselection()
589 # get lineno of first selected line
590 selstart
, selend
= self
.editgroup
.editor
.getselection()
591 selstart
, selend
= min(selstart
, selend
), max(selstart
, selend
)
592 selfirstline
= self
.editgroup
.editor
.offsettoline(selstart
)
593 alltext
= self
.editgroup
.editor
.get()
594 pytext
= alltext
[selstart
:selend
]
595 lines
= string
.split(pytext
, '\r')
596 indent
= getminindent(lines
)
599 alllines
= string
.split(alltext
, '\r')
600 for i
in range(selfirstline
- 1, -1, -1):
602 if line
[:6] == 'class ':
603 classname
= string
.split(string
.strip(line
[6:]))[0]
604 classend
= identifieRE_match(classname
)
606 raise W
.AlertError
, "Can't find a class."
607 classname
= classname
[:classend
]
609 elif line
and line
[0] not in '\t#':
610 raise W
.AlertError
, "Can't find a class."
612 raise W
.AlertError
, "Can't find a class."
613 if globals.has_key(classname
):
614 klass
= globals[classname
]
616 raise W
.AlertError
, "Can't find class \"%s\"." % classname
618 pytext
= ("class %s:\n" % classname
) + pytext
619 selfirstline
= selfirstline
- 1
621 raise W
.AlertError
, "Can't run indented code."
623 # add "newlines" to fool compile/exec:
624 # now a traceback will give the right line number
625 pytext
= selfirstline
* '\r' + pytext
626 self
.execstring(pytext
, globals, locals, file, modname
)
627 if indent
== 1 and globals[classname
] is not klass
:
628 # update the class in place
629 klass
.__dict
__.update(globals[classname
].__dict
__)
630 globals[classname
] = klass
632 def setthreadstate(self
, state
):
633 oldstate
= self
._threadstate
634 if oldstate
[0] <> state
[0]:
635 self
.runbutton
.settitle(runButtonLabels
[state
[0]])
636 if oldstate
[1] <> state
[1]:
637 self
.runselbutton
.settitle(runSelButtonLabels
[state
[1]])
638 self
._threadstate
= state
640 def _exec_threadwrapper(self
, *args
, **kwargs
):
641 apply(execstring
, args
, kwargs
)
642 self
.setthreadstate((0, 0))
645 def execstring(self
, pytext
, globals, locals, file, modname
):
646 tracebackwindow
.hide()
648 W
.getapplication().refreshwindows()
652 dir = os
.path
.dirname(self
.path
)
653 savedir
= os
.getcwd()
655 sys
.path
.insert(0, dir)
660 self
._thread
= Wthreading
.Thread(os
.path
.basename(file),
661 self
._exec
_threadwrapper
, pytext
, globals, locals, file, self
.debugging
,
662 modname
, self
.profiling
)
663 self
.setthreadstate((1, 1))
666 execstring(pytext
, globals, locals, file, self
.debugging
,
667 modname
, self
.profiling
)
673 def getenvironment(self
):
676 dir = os
.path
.dirname(file)
677 # check if we're part of a package
679 while os
.path
.exists(os
.path
.join(dir, "__init__.py")):
680 dir, dirname
= os
.path
.split(dir)
681 modname
= dirname
+ '.' + modname
682 subname
= _filename_as_modname(self
.title
)
684 return self
.globals, file, None
686 if subname
== "__init__":
687 # strip trailing period
688 modname
= modname
[:-1]
690 modname
= modname
+ subname
693 if sys
.modules
.has_key(modname
):
694 globals = sys
.modules
[modname
].__dict
__
697 globals = self
.globals
700 file = '<%s>' % self
.title
701 globals = self
.globals
703 return globals, file, modname
705 def write(self
, stuff
):
706 """for use as stdout"""
707 self
._buf
= self
._buf
+ stuff
708 if '\n' in self
._buf
:
712 stuff
= string
.split(self
._buf
, '\n')
713 stuff
= string
.join(stuff
, '\r')
714 end
= self
.editgroup
.editor
.ted
.WEGetTextLength()
715 self
.editgroup
.editor
.ted
.WESetSelection(end
, end
)
716 self
.editgroup
.editor
.ted
.WEInsert(stuff
, None, None)
717 self
.editgroup
.editor
.updatescrollbars()
720 #self.wid.SelectWindow()
722 def getclasslist(self
):
723 from string
import find
, strip
724 methodRE
= re
.compile(r
"\r[ \t]+def ")
725 findMethod
= methodRE
.search
726 editor
= self
.editgroup
.editor
734 if text
[:4] == 'def ':
735 append((pos
+ 4, functag
))
738 pos
= find(text
, '\rdef ', pos
+ 1)
741 append((pos
+ 5, functag
))
743 if text
[:6] == 'class ':
744 append((pos
+ 6, classtag
))
747 pos
= find(text
, '\rclass ', pos
+ 1)
750 append((pos
+ 7, classtag
))
753 m
= findMethod(text
, pos
+ 1)
757 #pos = find(text, '\r\tdef ', pos + 1)
758 append((m
.regs
[0][1], methodtag
))
761 methodlistappend
= None
762 offsetToLine
= editor
.ted
.WEOffsetToLine
763 getLineRange
= editor
.ted
.WEGetLineRange
764 append
= classlist
.append
765 for pos
, tag
in list:
766 lineno
= offsetToLine(pos
)
767 lineStart
, lineEnd
= getLineRange(lineno
)
768 line
= strip(text
[pos
:lineEnd
])
769 line
= line
[:identifieRE_match(line
)]
771 append(("def " + line
, lineno
+ 1))
772 methodlistappend
= None
773 elif tag
is classtag
:
774 append(["class " + line
])
775 methodlistappend
= classlist
[-1].append
776 elif methodlistappend
and tag
is methodtag
:
777 methodlistappend(("def " + line
, lineno
+ 1))
780 def popselectline(self
, lineno
):
781 self
.editgroup
.editor
.selectline(lineno
- 1)
783 def selectline(self
, lineno
, charoffset
= 0):
784 self
.editgroup
.editor
.selectline(lineno
- 1, charoffset
)
786 def addrecentfile(self
, filename
):
787 app
= W
.getapplication()
788 app
.addrecentfile(filename
)
792 def __init__(self
, creator
, eoln
):
795 self
.w
= w
= W
.ModalDialog((260, 160), 'Save options')
797 w
.label
= W
.TextBox((8, 8, 80, 18), "File creator:")
798 w
.ide_radio
= W
.RadioButton((8, 22, 160, 18), "This application", radiobuttons
, self
.ide_hit
)
799 w
.interp_radio
= W
.RadioButton((8, 42, 160, 18), "MacPython Interpreter", radiobuttons
, self
.interp_hit
)
800 w
.interpx_radio
= W
.RadioButton((8, 62, 160, 18), "OSX PythonW Interpreter", radiobuttons
, self
.interpx_hit
)
801 w
.other_radio
= W
.RadioButton((8, 82, 50, 18), "Other:", radiobuttons
)
802 w
.other_creator
= W
.EditText((62, 82, 40, 20), creator
, self
.otherselect
)
803 w
.none_radio
= W
.RadioButton((8, 102, 160, 18), "None", radiobuttons
, self
.none_hit
)
804 w
.cancelbutton
= W
.Button((-180, -30, 80, 16), "Cancel", self
.cancelbuttonhit
)
805 w
.okbutton
= W
.Button((-90, -30, 80, 16), "Done", self
.okbuttonhit
)
806 w
.setdefaultbutton(w
.okbutton
)
807 if creator
== 'Pyth':
808 w
.interp_radio
.set(1)
809 elif creator
== W
._signature
:
811 elif creator
== 'PytX':
812 w
.interpx_radio
.set(1)
813 elif creator
== '\0\0\0\0':
818 w
.eolnlabel
= W
.TextBox((168, 8, 80, 18), "Newline style:")
820 w
.unix_radio
= W
.RadioButton((168, 22, 80, 18), "Unix", radiobuttons
, self
.unix_hit
)
821 w
.mac_radio
= W
.RadioButton((168, 42, 80, 18), "Macintosh", radiobuttons
, self
.mac_hit
)
822 w
.win_radio
= W
.RadioButton((168, 62, 80, 18), "Windows", radiobuttons
, self
.win_hit
)
823 if self
.eoln
== '\n':
825 elif self
.eoln
== '\r\n':
830 w
.bind("cmd.", w
.cancelbutton
.push
)
834 self
.w
.other_creator
.set(W
._signature
)
836 def interp_hit(self
):
837 self
.w
.other_creator
.set("Pyth")
839 def interpx_hit(self
):
840 self
.w
.other_creator
.set("PytX")
843 self
.w
.other_creator
.set("\0\0\0\0")
845 def otherselect(self
, *args
):
846 sel_from
, sel_to
= self
.w
.other_creator
.getselection()
847 creator
= self
.w
.other_creator
.get()[:4]
848 creator
= creator
+ " " * (4 - len(creator
))
849 self
.w
.other_creator
.set(creator
)
850 self
.w
.other_creator
.setselection(sel_from
, sel_to
)
851 self
.w
.other_radio
.set(1)
862 def cancelbuttonhit(self
):
865 def okbuttonhit(self
):
866 self
.rv
= (self
.w
.other_creator
.get()[:4], self
.eoln
)
870 def SaveOptions(creator
, eoln
):
871 s
= _saveoptions(creator
, eoln
)
875 def _escape(where
, what
) :
876 return string
.join(string
.split(where
, what
), '\\' + what
)
878 def _makewholewordpattern(word
):
879 # first, escape special regex chars
880 for esc
in "\\[]()|.*^+$?":
881 word
= _escape(word
, esc
)
882 notwordcharspat
= '[^' + _wordchars
+ ']'
883 pattern
= '(' + word
+ ')'
884 if word
[0] in _wordchars
:
885 pattern
= notwordcharspat
+ pattern
886 if word
[-1] in _wordchars
:
887 pattern
= pattern
+ notwordcharspat
888 return re
.compile(pattern
)
896 self
.parms
= { "find": "",
903 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
904 if prefs
.searchengine
:
905 self
.parms
["casesens"] = prefs
.searchengine
.casesens
906 self
.parms
["wrap"] = prefs
.searchengine
.wrap
907 self
.parms
["wholeword"] = prefs
.searchengine
.wholeword
912 self
.w
.wid
.ShowWindow()
913 self
.w
.wid
.SelectWindow()
914 self
.w
.find
.edit
.select(1)
915 self
.w
.find
.edit
.selectall()
917 self
.w
= W
.Dialog((420, 150), "Find")
919 self
.w
.find
= TitledEditText((10, 4, 300, 36), "Search for:")
920 self
.w
.replace
= TitledEditText((10, 100, 300, 36), "Replace with:")
922 self
.w
.boxes
= W
.Group((10, 50, 300, 40))
923 self
.w
.boxes
.casesens
= W
.CheckBox((0, 0, 100, 16), "Case sensitive")
924 self
.w
.boxes
.wholeword
= W
.CheckBox((0, 20, 100, 16), "Whole word")
925 self
.w
.boxes
.wrap
= W
.CheckBox((110, 0, 100, 16), "Wrap around")
927 self
.buttons
= [ ("Find", "cmdf", self
.find
),
928 ("Replace", "cmdr", self
.replace
),
929 ("Replace all", None, self
.replaceall
),
930 ("Don't find", "cmdd", self
.dont
),
931 ("Cancel", "cmd.", self
.cancel
)
933 for i
in range(len(self
.buttons
)):
934 bounds
= -90, 22 + i
* 24, 80, 16
935 title
, shortcut
, callback
= self
.buttons
[i
]
936 self
.w
[title
] = W
.Button(bounds
, title
, callback
)
938 self
.w
.bind(shortcut
, self
.w
[title
].push
)
939 self
.w
.setdefaultbutton(self
.w
["Don't find"])
940 self
.w
.find
.edit
.bind("<key>", self
.key
)
941 self
.w
.bind("<activate>", self
.activate
)
942 self
.w
.bind("<close>", self
.close
)
945 self
.w
.find
.edit
.select(1)
946 self
.w
.find
.edit
.selectall()
953 def key(self
, char
, modifiers
):
954 self
.w
.find
.edit
.key(char
, modifiers
)
958 def activate(self
, onoff
):
962 def checkbuttons(self
):
963 editor
= findeditor(self
)
965 if self
.w
.find
.get():
966 for title
, cmd
, call
in self
.buttons
[:-2]:
967 self
.w
[title
].enable(1)
968 self
.w
.setdefaultbutton(self
.w
["Find"])
970 for title
, cmd
, call
in self
.buttons
[:-2]:
971 self
.w
[title
].enable(0)
972 self
.w
.setdefaultbutton(self
.w
["Don't find"])
974 for title
, cmd
, call
in self
.buttons
[:-2]:
975 self
.w
[title
].enable(0)
976 self
.w
.setdefaultbutton(self
.w
["Don't find"])
979 self
.getparmsfromwindow()
984 editor
= findeditor(self
)
988 self
.getparmsfromwindow()
989 text
= editor
.getselectedtext()
990 find
= self
.parms
["find"]
991 if not self
.parms
["casesens"]:
992 find
= string
.lower(find
)
993 text
= string
.lower(text
)
996 editor
.insert(self
.parms
["replace"])
998 def replaceall(self
):
999 editor
= findeditor(self
)
1003 self
.getparmsfromwindow()
1004 W
.SetCursor("watch")
1005 find
= self
.parms
["find"]
1009 replace
= self
.parms
["replace"]
1010 replacelen
= len(replace
)
1012 if not self
.parms
["casesens"]:
1013 find
= string
.lower(find
)
1014 text
= string
.lower(Text
)
1021 if self
.parms
["wholeword"]:
1022 wholewordRE
= _makewholewordpattern(find
)
1023 match
= wholewordRE
.search(text
, pos
)
1025 pos
= match
.start(1)
1029 pos
= string
.find(text
, find
, pos
)
1032 counter
= counter
+ 1
1033 text
= text
[:pos
] + replace
+ text
[pos
+ findlen
:]
1034 Text
= Text
[:pos
] + replace
+ Text
[pos
+ findlen
:]
1035 pos
= pos
+ replacelen
1036 W
.SetCursor("arrow")
1039 from Carbon
import Res
1040 editor
.textchanged()
1041 editor
.selectionchanged()
1043 EasyDialogs
.Message("Replaced %d occurrences" % counter
)
1046 self
.getparmsfromwindow()
1049 def replacefind(self
):
1053 def setfindstring(self
):
1054 editor
= findeditor(self
)
1057 find
= editor
.getselectedtext()
1060 self
.parms
["find"] = find
1062 self
.w
.find
.edit
.set(self
.parms
["find"])
1063 self
.w
.find
.edit
.selectall()
1066 editor
= findeditor(self
)
1069 find
= self
.parms
["find"]
1073 if not self
.parms
["casesens"]:
1074 find
= string
.lower(find
)
1075 text
= string
.lower(text
)
1076 selstart
, selend
= editor
.getselection()
1077 selstart
, selend
= min(selstart
, selend
), max(selstart
, selend
)
1078 if self
.parms
["wholeword"]:
1079 wholewordRE
= _makewholewordpattern(find
)
1080 match
= wholewordRE
.search(text
, selend
)
1082 pos
= match
.start(1)
1086 pos
= string
.find(text
, find
, selend
)
1088 editor
.setselection(pos
, pos
+ len(find
))
1090 elif self
.parms
["wrap"]:
1091 if self
.parms
["wholeword"]:
1092 match
= wholewordRE
.search(text
, 0)
1094 pos
= match
.start(1)
1098 pos
= string
.find(text
, find
)
1099 if selstart
> pos
>= 0:
1100 editor
.setselection(pos
, pos
+ len(find
))
1104 for key
, value
in self
.parms
.items():
1106 self
.w
[key
].set(value
)
1108 self
.w
.boxes
[key
].set(value
)
1110 def getparmsfromwindow(self
):
1113 for key
, value
in self
.parms
.items():
1115 value
= self
.w
[key
].get()
1117 value
= self
.w
.boxes
[key
].get()
1118 self
.parms
[key
] = value
1126 self
.w
.wid
.HideWindow()
1129 def writeprefs(self
):
1131 self
.getparmsfromwindow()
1132 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
1133 prefs
.searchengine
.casesens
= self
.parms
["casesens"]
1134 prefs
.searchengine
.wrap
= self
.parms
["wrap"]
1135 prefs
.searchengine
.wholeword
= self
.parms
["wholeword"]
1139 class TitledEditText(W
.Group
):
1141 def __init__(self
, possize
, title
, text
= ""):
1142 W
.Group
.__init
__(self
, possize
)
1143 self
.title
= W
.TextBox((0, 0, 0, 16), title
)
1144 self
.edit
= W
.EditText((0, 16, 0, 0), text
)
1146 def set(self
, value
):
1147 self
.edit
.set(value
)
1150 return self
.edit
.get()
1153 class ClassFinder(W
.PopupWidget
):
1155 def click(self
, point
, modifiers
):
1156 W
.SetCursor("watch")
1157 self
.set(self
._parentwindow
.getclasslist())
1158 W
.PopupWidget
.click(self
, point
, modifiers
)
1161 def getminindent(lines
):
1164 stripped
= string
.strip(line
)
1165 if not stripped
or stripped
[0] == '#':
1167 if indent
< 0 or line
[:indent
] <> indent
* '\t':
1177 return not not ord(Evt
.GetKeys()[7]) & 0x04
1180 def execstring(pytext
, globals, locals, filename
="<string>", debugging
=0,
1181 modname
="__main__", profiling
=0):
1183 import PyDebugger
, bdb
1184 BdbQuit
= bdb
.BdbQuit
1186 BdbQuit
= 'BdbQuitDummyException'
1187 pytext
= string
.split(pytext
, '\r')
1188 pytext
= string
.join(pytext
, '\n') + '\n'
1189 W
.SetCursor("watch")
1190 globals['__name__'] = modname
1191 globals['__file__'] = filename
1192 sys
.argv
= [filename
]
1194 code
= compile(pytext
, filename
, "exec")
1196 # XXXX BAAAADDD.... We let tracebackwindow decide to treat SyntaxError
1197 # special. That's wrong because THIS case is special (could be literal
1198 # overflow!) and SyntaxError could mean we need a traceback (syntax error
1199 # in imported module!!!
1200 tracebackwindow
.traceback(1, filename
)
1205 lock
= Wthreading
.Lock()
1207 PyDebugger
.startfromhere()
1210 PyDebugger
.startfromhere()
1211 elif not haveThreading
:
1212 if hasattr(MacOS
, 'EnableAppswitch'):
1213 MacOS
.EnableAppswitch(0)
1216 import profile
, ProfileBrowser
1217 p
= profile
.Profile()
1220 p
.runctx(code
, globals, locals)
1224 stats
= pstats
.Stats(p
)
1225 ProfileBrowser
.ProfileBrowser(stats
)
1227 exec code
in globals, locals
1229 if not haveThreading
:
1230 if hasattr(MacOS
, 'EnableAppswitch'):
1231 MacOS
.EnableAppswitch(-1)
1232 except W
.AlertError
, detail
:
1233 raise W
.AlertError
, detail
1234 except (KeyboardInterrupt, BdbQuit
):
1236 except SystemExit, arg
:
1238 sys
.stderr
.write("Script exited with status code: %s\n" % repr(arg
.code
))
1242 lock
= Wthreading
.Lock()
1246 PyDebugger
.postmortem(sys
.exc_type
, sys
.exc_value
, sys
.exc_traceback
)
1249 tracebackwindow
.traceback(1, filename
)
1257 _identifieRE
= re
.compile(r
"[A-Za-z_][A-Za-z_0-9]*")
1259 def identifieRE_match(str):
1260 match
= _identifieRE
.match(str)
1265 def _filename_as_modname(fname
):
1266 if fname
[-3:] == '.py':
1267 modname
= fname
[:-3]
1268 match
= _identifieRE
.match(modname
)
1269 if match
and match
.start() == 0 and match
.end() == len(modname
):
1270 return string
.join(string
.split(modname
, '.'), '_')
1272 def findeditor(topwindow
, fromtop
= 0):
1273 wid
= MyFrontWindow()
1275 if topwindow
.w
and wid
== topwindow
.w
.wid
:
1276 wid
= topwindow
.w
.wid
.GetNextWindow()
1279 app
= W
.getapplication()
1280 if app
._windows
.has_key(wid
): # KeyError otherwise can happen in RoboFog :-(
1281 window
= W
.getapplication()._windows
[wid
]
1284 if not isinstance(window
, Editor
):
1286 return window
.editgroup
.editor
1289 class _EditorDefaultSettings
:
1292 self
.template
= "%s, %d point"
1293 self
.fontsettings
, self
.tabsettings
, self
.windowsize
= geteditorprefs()
1294 self
.w
= W
.Dialog((328, 120), "Editor default settings")
1295 self
.w
.setfontbutton
= W
.Button((8, 8, 80, 16), "Set font\xc9", self
.dofont
)
1296 self
.w
.fonttext
= W
.TextBox((98, 10, -8, 14), self
.template
% (self
.fontsettings
[0], self
.fontsettings
[2]))
1298 self
.w
.picksizebutton
= W
.Button((8, 50, 80, 16), "Front window", self
.picksize
)
1299 self
.w
.xsizelabel
= W
.TextBox((98, 32, 40, 14), "Width:")
1300 self
.w
.ysizelabel
= W
.TextBox((148, 32, 40, 14), "Height:")
1301 self
.w
.xsize
= W
.EditText((98, 48, 40, 20), `self
.windowsize
[0]`
)
1302 self
.w
.ysize
= W
.EditText((148, 48, 40, 20), `self
.windowsize
[1]`
)
1304 self
.w
.cancelbutton
= W
.Button((-180, -26, 80, 16), "Cancel", self
.cancel
)
1305 self
.w
.okbutton
= W
.Button((-90, -26, 80, 16), "Done", self
.ok
)
1306 self
.w
.setdefaultbutton(self
.w
.okbutton
)
1307 self
.w
.bind('cmd.', self
.w
.cancelbutton
.push
)
1311 app
= W
.getapplication()
1312 editor
= findeditor(self
)
1313 if editor
is not None:
1314 width
, height
= editor
._parentwindow
._bounds
[2:]
1315 self
.w
.xsize
.set(`width`
)
1316 self
.w
.ysize
.set(`height`
)
1318 raise W
.AlertError
, "No edit window found"
1322 settings
= FontSettings
.FontDialog(self
.fontsettings
, self
.tabsettings
)
1324 self
.fontsettings
, self
.tabsettings
= settings
1325 sys
.exc_traceback
= None
1326 self
.w
.fonttext
.set(self
.template
% (self
.fontsettings
[0], self
.fontsettings
[2]))
1337 width
= string
.atoi(self
.w
.xsize
.get())
1339 self
.w
.xsize
.select(1)
1340 self
.w
.xsize
.selectall()
1341 raise W
.AlertError
, "Bad number for window width"
1343 height
= string
.atoi(self
.w
.ysize
.get())
1345 self
.w
.ysize
.select(1)
1346 self
.w
.ysize
.selectall()
1347 raise W
.AlertError
, "Bad number for window height"
1348 self
.windowsize
= width
, height
1349 seteditorprefs(self
.fontsettings
, self
.tabsettings
, self
.windowsize
)
1352 def geteditorprefs():
1354 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
1356 fontsettings
= prefs
.pyedit
.fontsettings
1357 tabsettings
= prefs
.pyedit
.tabsettings
1358 windowsize
= prefs
.pyedit
.windowsize
1360 fontsettings
= prefs
.pyedit
.fontsettings
= ("Geneva", 0, 10, (0, 0, 0))
1361 tabsettings
= prefs
.pyedit
.tabsettings
= (8, 1)
1362 windowsize
= prefs
.pyedit
.windowsize
= (500, 250)
1363 sys
.exc_traceback
= None
1364 return fontsettings
, tabsettings
, windowsize
1366 def seteditorprefs(fontsettings
, tabsettings
, windowsize
):
1368 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
1369 prefs
.pyedit
.fontsettings
= fontsettings
1370 prefs
.pyedit
.tabsettings
= tabsettings
1371 prefs
.pyedit
.windowsize
= windowsize
1374 _defaultSettingsEditor
= None
1376 def EditorDefaultSettings():
1377 global _defaultSettingsEditor
1378 if _defaultSettingsEditor
is None or not hasattr(_defaultSettingsEditor
, "w"):
1379 _defaultSettingsEditor
= _EditorDefaultSettings()
1381 _defaultSettingsEditor
.w
.select()
1383 def resolvealiases(path
):
1385 fsr
, d1
, d2
= File
.FSResolveAliasFile(path
, 1)
1386 path
= fsr
.as_pathname()
1388 except (File
.Error
, ValueError), (error
, str):
1391 dir, file = os
.path
.split(path
)
1392 return os
.path
.join(resolvealiases(dir), file)
1394 searchengine
= SearchEngine()
1395 tracebackwindow
= Wtraceback
.TraceBack()