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