David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | # changes by dscherer@cmu.edu |
| 2 | # - IOBinding.open() replaces the current window with the opened file, |
| 3 | # if the current window is both unmodified and unnamed |
| 4 | # - IOBinding.loadfile() interprets Windows, UNIX, and Macintosh |
| 5 | # end-of-line conventions, instead of relying on the standard library, |
| 6 | # which will only understand the local convention. |
| 7 | |
| 8 | import os |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 9 | import types |
| 10 | import sys |
| 11 | import codecs |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 12 | import tempfile |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 13 | import tkFileDialog |
| 14 | import tkMessageBox |
| 15 | import re |
Kurt B. Kaiser | a053f33 | 2003-05-10 00:49:56 +0000 | [diff] [blame] | 16 | from Tkinter import * |
| 17 | from SimpleDialog import SimpleDialog |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 18 | |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 19 | from configHandler import idleConf |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 20 | |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 21 | try: |
| 22 | from codecs import BOM_UTF8 |
| 23 | except ImportError: |
| 24 | # only available since Python 2.3 |
| 25 | BOM_UTF8 = '\xef\xbb\xbf' |
| 26 | |
| 27 | # Try setting the locale, so that we can find out |
| 28 | # what encoding to use |
| 29 | try: |
| 30 | import locale |
| 31 | locale.setlocale(locale.LC_CTYPE, "") |
| 32 | except ImportError: |
| 33 | pass |
| 34 | |
| 35 | encoding = "ascii" |
| 36 | if sys.platform == 'win32': |
| 37 | # On Windows, we could use "mbcs". However, to give the user |
| 38 | # a portable encoding name, we need to find the code page |
| 39 | try: |
| 40 | encoding = locale.getdefaultlocale()[1] |
| 41 | codecs.lookup(encoding) |
| 42 | except LookupError: |
| 43 | pass |
| 44 | else: |
| 45 | try: |
| 46 | # Different things can fail here: the locale module may not be |
| 47 | # loaded, it may not offer nl_langinfo, or CODESET, or the |
| 48 | # resulting codeset may be unknown to Python. We ignore all |
| 49 | # these problems, falling back to ASCII |
| 50 | encoding = locale.nl_langinfo(locale.CODESET) |
Tony Lownds | e555fc7 | 2002-09-23 01:01:20 +0000 | [diff] [blame] | 51 | if encoding is None: |
| 52 | # situation occurs on Mac OS X |
| 53 | encoding = 'ascii' |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 54 | codecs.lookup(encoding) |
| 55 | except (NameError, AttributeError, LookupError): |
| 56 | # Try getdefaultlocale well: it parses environment variables, |
| 57 | # which may give a clue. Unfortunately, getdefaultlocale has |
| 58 | # bugs that can cause ValueError. |
| 59 | try: |
| 60 | encoding = locale.getdefaultlocale()[1] |
Tony Lownds | e555fc7 | 2002-09-23 01:01:20 +0000 | [diff] [blame] | 61 | if encoding is None: |
| 62 | # situation occurs on Mac OS X |
| 63 | encoding = 'ascii' |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 64 | codecs.lookup(encoding) |
| 65 | except (ValueError, LookupError): |
| 66 | pass |
| 67 | |
| 68 | encoding = encoding.lower() |
| 69 | |
| 70 | coding_re = re.compile("coding[:=]\s*([-\w_.]+)") |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 71 | |
Kurt B. Kaiser | a053f33 | 2003-05-10 00:49:56 +0000 | [diff] [blame] | 72 | class EncodingMessage(SimpleDialog): |
| 73 | "Inform user that an encoding declaration is needed." |
| 74 | def __init__(self, master, enc): |
| 75 | self.should_edit = False |
Kurt B. Kaiser | 4767401 | 2003-05-18 02:24:32 +0000 | [diff] [blame^] | 76 | |
Kurt B. Kaiser | a053f33 | 2003-05-10 00:49:56 +0000 | [diff] [blame] | 77 | self.root = top = Toplevel(master) |
| 78 | top.bind("<Return>", self.return_event) |
| 79 | top.bind("<Escape>", self.do_ok) |
| 80 | top.protocol("WM_DELETE_WINDOW", self.wm_delete_window) |
| 81 | top.wm_title("I/O Warning") |
| 82 | top.wm_iconname("I/O Warning") |
| 83 | self.top = top |
| 84 | |
| 85 | l1 = Label(top, |
| 86 | text="Non-ASCII found, yet no encoding declared. Add a line like") |
| 87 | l1.pack(side=TOP, anchor=W) |
| 88 | l2 = Entry(top, font="courier") |
| 89 | l2.insert(0, "# -*- coding: %s -*-" % enc) |
| 90 | # For some reason, the text is not selectable anymore if the |
| 91 | # widget is disabled. |
| 92 | # l2['state'] = DISABLED |
| 93 | l2.pack(side=TOP, anchor = W, fill=X) |
| 94 | l3 = Label(top, text="to your file\n" |
| 95 | "Choose OK to save this file as %s\n" |
| 96 | "Edit your general options to silence this warning" % enc) |
| 97 | l3.pack(side=TOP, anchor = W) |
| 98 | |
| 99 | buttons = Frame(top) |
| 100 | buttons.pack(side=TOP, fill=X) |
| 101 | # Both return and cancel mean the same thing: do nothing |
| 102 | self.default = self.cancel = 0 |
| 103 | b1 = Button(buttons, text="Ok", default="active", |
| 104 | command=self.do_ok) |
| 105 | b1.pack(side=LEFT, fill=BOTH, expand=1) |
| 106 | b2 = Button(buttons, text="Edit my file", |
| 107 | command=self.do_edit) |
| 108 | b2.pack(side=LEFT, fill=BOTH, expand=1) |
Kurt B. Kaiser | 4767401 | 2003-05-18 02:24:32 +0000 | [diff] [blame^] | 109 | |
Kurt B. Kaiser | a053f33 | 2003-05-10 00:49:56 +0000 | [diff] [blame] | 110 | self._set_transient(master) |
| 111 | |
| 112 | def do_ok(self): |
| 113 | self.done(0) |
| 114 | |
| 115 | def do_edit(self): |
| 116 | self.done(1) |
| 117 | |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 118 | def coding_spec(str): |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 119 | """Return the encoding declaration according to PEP 263. |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 120 | |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 121 | Raise LookupError if the encoding is declared but unknown. |
| 122 | """ |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 123 | # Only consider the first two lines |
| 124 | str = str.split("\n")[:2] |
| 125 | str = "\n".join(str) |
| 126 | |
| 127 | match = coding_re.search(str) |
| 128 | if not match: |
| 129 | return None |
| 130 | name = match.group(1) |
| 131 | # Check whether the encoding is known |
| 132 | import codecs |
| 133 | try: |
| 134 | codecs.lookup(name) |
| 135 | except LookupError: |
| 136 | # The standard encoding error does not indicate the encoding |
| 137 | raise LookupError, "Unknown encoding "+name |
| 138 | return name |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 139 | |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 140 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 141 | class IOBinding: |
| 142 | |
| 143 | def __init__(self, editwin): |
| 144 | self.editwin = editwin |
| 145 | self.text = editwin.text |
| 146 | self.__id_open = self.text.bind("<<open-window-from-file>>", self.open) |
| 147 | self.__id_save = self.text.bind("<<save-window>>", self.save) |
| 148 | self.__id_saveas = self.text.bind("<<save-window-as-file>>", |
| 149 | self.save_as) |
| 150 | self.__id_savecopy = self.text.bind("<<save-copy-of-window-as-file>>", |
| 151 | self.save_a_copy) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 152 | self.fileencoding = None |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 153 | self.__id_print = self.text.bind("<<print-window>>", self.print_window) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 154 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 155 | def close(self): |
| 156 | # Undo command bindings |
| 157 | self.text.unbind("<<open-window-from-file>>", self.__id_open) |
| 158 | self.text.unbind("<<save-window>>", self.__id_save) |
| 159 | self.text.unbind("<<save-window-as-file>>",self.__id_saveas) |
| 160 | self.text.unbind("<<save-copy-of-window-as-file>>", self.__id_savecopy) |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 161 | self.text.unbind("<<print-window>>", self.__id_print) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 162 | # Break cycles |
| 163 | self.editwin = None |
| 164 | self.text = None |
| 165 | self.filename_change_hook = None |
| 166 | |
| 167 | def get_saved(self): |
| 168 | return self.editwin.get_saved() |
| 169 | |
| 170 | def set_saved(self, flag): |
| 171 | self.editwin.set_saved(flag) |
| 172 | |
| 173 | def reset_undo(self): |
| 174 | self.editwin.reset_undo() |
| 175 | |
| 176 | filename_change_hook = None |
| 177 | |
| 178 | def set_filename_change_hook(self, hook): |
| 179 | self.filename_change_hook = hook |
| 180 | |
| 181 | filename = None |
| 182 | |
| 183 | def set_filename(self, filename): |
| 184 | self.filename = filename |
| 185 | self.set_saved(1) |
| 186 | if self.filename_change_hook: |
| 187 | self.filename_change_hook() |
| 188 | |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 189 | def open(self, event=None, editFile=None): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 190 | if self.editwin.flist: |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 191 | if not editFile: |
| 192 | filename = self.askopenfile() |
| 193 | else: |
| 194 | filename=editFile |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 195 | if filename: |
Kurt B. Kaiser | 1bf4c2d | 2002-07-21 01:24:28 +0000 | [diff] [blame] | 196 | # If the current window has no filename and hasn't been |
| 197 | # modified, we replace its contents (no loss). Otherwise |
| 198 | # we open a new window. But we won't replace the |
| 199 | # shell window (which has an interp(reter) attribute), which |
| 200 | # gets set to "not modified" at every new prompt. |
| 201 | try: |
| 202 | interp = self.editwin.interp |
| 203 | except: |
| 204 | interp = None |
| 205 | if not self.filename and self.get_saved() and not interp: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 206 | self.editwin.flist.open(filename, self.loadfile) |
| 207 | else: |
| 208 | self.editwin.flist.open(filename) |
| 209 | else: |
| 210 | self.text.focus_set() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 211 | return "break" |
Kurt B. Kaiser | 1bf4c2d | 2002-07-21 01:24:28 +0000 | [diff] [blame] | 212 | # |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 213 | # Code for use outside IDLE: |
| 214 | if self.get_saved(): |
| 215 | reply = self.maybesave() |
| 216 | if reply == "cancel": |
| 217 | self.text.focus_set() |
| 218 | return "break" |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 219 | if not editFile: |
| 220 | filename = self.askopenfile() |
| 221 | else: |
| 222 | filename=editFile |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 223 | if filename: |
| 224 | self.loadfile(filename) |
| 225 | else: |
| 226 | self.text.focus_set() |
| 227 | return "break" |
| 228 | |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 229 | eol = r"(\r\n)|\n|\r" # \r\n (Windows), \n (UNIX), or \r (Mac) |
| 230 | eol_re = re.compile(eol) |
| 231 | eol_convention = os.linesep # Default |
| 232 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 233 | def loadfile(self, filename): |
| 234 | try: |
| 235 | # open the file in binary mode so that we can handle |
| 236 | # end-of-line convention ourselves. |
| 237 | f = open(filename,'rb') |
| 238 | chars = f.read() |
| 239 | f.close() |
| 240 | except IOError, msg: |
| 241 | tkMessageBox.showerror("I/O Error", str(msg), master=self.text) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 242 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 243 | |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 244 | chars = self.decode(chars) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 245 | # We now convert all end-of-lines to '\n's |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 246 | firsteol = self.eol_re.search(chars) |
| 247 | if firsteol: |
| 248 | self.eol_convention = firsteol.group(0) |
| 249 | chars = self.eol_re.sub(r"\n", chars) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 250 | |
| 251 | self.text.delete("1.0", "end") |
| 252 | self.set_filename(None) |
| 253 | self.text.insert("1.0", chars) |
| 254 | self.reset_undo() |
| 255 | self.set_filename(filename) |
| 256 | self.text.mark_set("insert", "1.0") |
| 257 | self.text.see("insert") |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 258 | self.updaterecentfileslist(filename) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 259 | return True |
| 260 | |
| 261 | def decode(self, chars): |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 262 | """Create a Unicode string |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 263 | |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 264 | If that fails, let Tcl try its best |
| 265 | """ |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 266 | # Check presence of a UTF-8 signature first |
| 267 | if chars.startswith(BOM_UTF8): |
| 268 | try: |
| 269 | chars = chars[3:].decode("utf-8") |
| 270 | except UnicodeError: |
| 271 | # has UTF-8 signature, but fails to decode... |
| 272 | return chars |
| 273 | else: |
| 274 | # Indicates that this file originally had a BOM |
| 275 | self.fileencoding = BOM_UTF8 |
| 276 | return chars |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 277 | # Next look for coding specification |
| 278 | try: |
| 279 | enc = coding_spec(chars) |
| 280 | except LookupError, name: |
| 281 | tkMessageBox.showerror( |
| 282 | title="Error loading the file", |
| 283 | message="The encoding '%s' is not known to this Python "\ |
| 284 | "installation. The file may not display correctly" % name, |
| 285 | master = self.text) |
| 286 | enc = None |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 287 | if enc: |
| 288 | try: |
| 289 | return unicode(chars, enc) |
| 290 | except UnicodeError: |
| 291 | pass |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 292 | # If it is ASCII, we need not to record anything |
| 293 | try: |
| 294 | return unicode(chars, 'ascii') |
| 295 | except UnicodeError: |
| 296 | pass |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 297 | # Finally, try the locale's encoding. This is deprecated; |
| 298 | # the user should declare a non-ASCII encoding |
| 299 | try: |
| 300 | chars = unicode(chars, encoding) |
| 301 | self.fileencoding = encoding |
| 302 | except UnicodeError: |
| 303 | pass |
| 304 | return chars |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 305 | |
| 306 | def maybesave(self): |
| 307 | if self.get_saved(): |
| 308 | return "yes" |
| 309 | message = "Do you want to save %s before closing?" % ( |
| 310 | self.filename or "this untitled document") |
| 311 | m = tkMessageBox.Message( |
| 312 | title="Save On Close", |
| 313 | message=message, |
| 314 | icon=tkMessageBox.QUESTION, |
| 315 | type=tkMessageBox.YESNOCANCEL, |
| 316 | master=self.text) |
| 317 | reply = m.show() |
| 318 | if reply == "yes": |
| 319 | self.save(None) |
| 320 | if not self.get_saved(): |
| 321 | reply = "cancel" |
| 322 | self.text.focus_set() |
| 323 | return reply |
| 324 | |
| 325 | def save(self, event): |
| 326 | if not self.filename: |
| 327 | self.save_as(event) |
| 328 | else: |
| 329 | if self.writefile(self.filename): |
| 330 | self.set_saved(1) |
Kurt B. Kaiser | ddeaf11 | 2003-03-04 04:42:04 +0000 | [diff] [blame] | 331 | try: |
| 332 | self.editwin.store_file_breaks() |
| 333 | except AttributeError: # may be a PyShell |
| 334 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 335 | self.text.focus_set() |
| 336 | return "break" |
| 337 | |
| 338 | def save_as(self, event): |
| 339 | filename = self.asksavefile() |
| 340 | if filename: |
| 341 | if self.writefile(filename): |
| 342 | self.set_filename(filename) |
| 343 | self.set_saved(1) |
Kurt B. Kaiser | ddeaf11 | 2003-03-04 04:42:04 +0000 | [diff] [blame] | 344 | try: |
| 345 | self.editwin.store_file_breaks() |
| 346 | except AttributeError: |
| 347 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 348 | self.text.focus_set() |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 349 | self.updaterecentfileslist(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 350 | return "break" |
| 351 | |
| 352 | def save_a_copy(self, event): |
| 353 | filename = self.asksavefile() |
| 354 | if filename: |
| 355 | self.writefile(filename) |
| 356 | self.text.focus_set() |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 357 | self.updaterecentfileslist(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 358 | return "break" |
| 359 | |
| 360 | def writefile(self, filename): |
| 361 | self.fixlastline() |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 362 | chars = self.encode(self.text.get("1.0", "end-1c")) |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 363 | if self.eol_convention != "\n": |
| 364 | chars = chars.replace("\n", self.eol_convention) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 365 | try: |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 366 | f = open(filename, "wb") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 367 | f.write(chars) |
| 368 | f.close() |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 369 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 370 | except IOError, msg: |
| 371 | tkMessageBox.showerror("I/O Error", str(msg), |
| 372 | master=self.text) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 373 | return False |
| 374 | |
| 375 | def encode(self, chars): |
| 376 | if isinstance(chars, types.StringType): |
| 377 | # This is either plain ASCII, or Tk was returning mixed-encoding |
| 378 | # text to us. Don't try to guess further. |
| 379 | return chars |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 380 | # See whether there is anything non-ASCII in it. |
| 381 | # If not, no need to figure out the encoding. |
| 382 | try: |
| 383 | return chars.encode('ascii') |
| 384 | except UnicodeError: |
| 385 | pass |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 386 | # If there is an encoding declared, try this first. |
| 387 | try: |
| 388 | enc = coding_spec(chars) |
| 389 | failed = None |
| 390 | except LookupError, msg: |
| 391 | failed = msg |
| 392 | enc = None |
| 393 | if enc: |
| 394 | try: |
| 395 | return chars.encode(enc) |
| 396 | except UnicodeError: |
| 397 | failed = "Invalid encoding '%s'" % enc |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 398 | if failed: |
| 399 | tkMessageBox.showerror( |
| 400 | "I/O Error", |
| 401 | "%s. Saving as UTF-8" % failed, |
| 402 | master = self.text) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 403 | # If there was a UTF-8 signature, use that. This should not fail |
| 404 | if self.fileencoding == BOM_UTF8 or failed: |
| 405 | return BOM_UTF8 + chars.encode("utf-8") |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 406 | # Try the original file encoding next, if any |
| 407 | if self.fileencoding: |
| 408 | try: |
| 409 | return chars.encode(self.fileencoding) |
| 410 | except UnicodeError: |
| 411 | tkMessageBox.showerror( |
| 412 | "I/O Error", |
| 413 | "Cannot save this as '%s' anymore. Saving as UTF-8" \ |
| 414 | % self.fileencoding, |
| 415 | master = self.text) |
| 416 | return BOM_UTF8 + chars.encode("utf-8") |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 417 | # Nothing was declared, and we had not determined an encoding |
| 418 | # on loading. Recommend an encoding line. |
Kurt B. Kaiser | 4767401 | 2003-05-18 02:24:32 +0000 | [diff] [blame^] | 419 | config_encoding = idleConf.GetOption("main","EditorWindow", |
| 420 | "encoding") |
| 421 | if config_encoding == 'utf-8': |
| 422 | # User has requested that we save files as UTF-8 |
| 423 | return BOM_UTF8 + chars.encode("utf-8") |
| 424 | ask_user = True |
| 425 | try: |
| 426 | chars = chars.encode(encoding) |
| 427 | enc = encoding |
| 428 | if config_encoding == 'locale': |
Kurt B. Kaiser | a053f33 | 2003-05-10 00:49:56 +0000 | [diff] [blame] | 429 | ask_user = False |
Kurt B. Kaiser | 4767401 | 2003-05-18 02:24:32 +0000 | [diff] [blame^] | 430 | except UnicodeError: |
| 431 | chars = BOM_UTF8 + chars.encode("utf-8") |
| 432 | enc = "utf-8" |
| 433 | if not ask_user: |
Kurt B. Kaiser | a053f33 | 2003-05-10 00:49:56 +0000 | [diff] [blame] | 434 | return chars |
Kurt B. Kaiser | 4767401 | 2003-05-18 02:24:32 +0000 | [diff] [blame^] | 435 | dialog = EncodingMessage(self.editwin.top, enc) |
| 436 | dialog.go() |
| 437 | if dialog.num == 1: |
| 438 | # User asked us to edit the file |
| 439 | encline = "# -*- coding: %s -*-\n" % enc |
| 440 | firstline = self.text.get("1.0", "2.0") |
| 441 | if firstline.startswith("#!"): |
| 442 | # Insert encoding after #! line |
| 443 | self.text.insert("2.0", encline) |
| 444 | else: |
| 445 | self.text.insert("1.0", encline) |
| 446 | return self.encode(self.text.get("1.0", "end-1c")) |
| 447 | return chars |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 448 | |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 449 | def fixlastline(self): |
| 450 | c = self.text.get("end-2c") |
| 451 | if c != '\n': |
| 452 | self.text.insert("end-1c", "\n") |
| 453 | |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 454 | def print_window(self, event): |
| 455 | tempfilename = None |
| 456 | if self.get_saved(): |
| 457 | filename = self.filename |
| 458 | else: |
| 459 | filename = tempfilename = tempfile.mktemp() |
| 460 | if not self.writefile(filename): |
| 461 | os.unlink(tempfilename) |
| 462 | return "break" |
| 463 | platform=os.name |
| 464 | printPlatform=1 |
| 465 | if platform == 'posix': #posix platform |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 466 | command = idleConf.GetOption('main','General', |
| 467 | 'print-command-posix') |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 468 | command = command + " 2>&1" |
| 469 | elif platform == 'nt': #win32 platform |
| 470 | command = idleConf.GetOption('main','General','print-command-win') |
| 471 | else: #no printing for this platform |
| 472 | printPlatform=0 |
| 473 | if printPlatform: #we can try to print for this platform |
| 474 | command = command % filename |
| 475 | pipe = os.popen(command, "r") |
| 476 | output = pipe.read().strip() |
| 477 | status = pipe.close() |
| 478 | if status: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 479 | output = "Printing failed (exit status 0x%x)\n" % \ |
| 480 | status + output |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 481 | if output: |
| 482 | output = "Printing command: %s\n" % repr(command) + output |
| 483 | tkMessageBox.showerror("Print status", output, master=self.text) |
| 484 | else: #no printing for this platform |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 485 | message="Printing is not enabled for this platform: %s" % platform |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 486 | tkMessageBox.showinfo("Print status", message, master=self.text) |
| 487 | return "break" |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 488 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 489 | opendialog = None |
| 490 | savedialog = None |
| 491 | |
| 492 | filetypes = [ |
| 493 | ("Python and text files", "*.py *.pyw *.txt", "TEXT"), |
| 494 | ("All text files", "*", "TEXT"), |
| 495 | ("All files", "*"), |
| 496 | ] |
| 497 | |
| 498 | def askopenfile(self): |
| 499 | dir, base = self.defaultfilename("open") |
| 500 | if not self.opendialog: |
| 501 | self.opendialog = tkFileDialog.Open(master=self.text, |
| 502 | filetypes=self.filetypes) |
| 503 | return self.opendialog.show(initialdir=dir, initialfile=base) |
| 504 | |
| 505 | def defaultfilename(self, mode="open"): |
| 506 | if self.filename: |
| 507 | return os.path.split(self.filename) |
| 508 | else: |
| 509 | try: |
| 510 | pwd = os.getcwd() |
| 511 | except os.error: |
| 512 | pwd = "" |
| 513 | return pwd, "" |
| 514 | |
| 515 | def asksavefile(self): |
| 516 | dir, base = self.defaultfilename("save") |
| 517 | if not self.savedialog: |
| 518 | self.savedialog = tkFileDialog.SaveAs(master=self.text, |
| 519 | filetypes=self.filetypes) |
| 520 | return self.savedialog.show(initialdir=dir, initialfile=base) |
| 521 | |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 522 | def updaterecentfileslist(self,filename): |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 523 | "Update recent file list on all editor windows" |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 524 | self.editwin.UpdateRecentFilesList(filename) |
| 525 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 526 | def test(): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 527 | root = Tk() |
| 528 | class MyEditWin: |
| 529 | def __init__(self, text): |
| 530 | self.text = text |
| 531 | self.flist = None |
| 532 | self.text.bind("<Control-o>", self.open) |
| 533 | self.text.bind("<Control-s>", self.save) |
| 534 | self.text.bind("<Alt-s>", self.save_as) |
| 535 | self.text.bind("<Alt-z>", self.save_a_copy) |
| 536 | def get_saved(self): return 0 |
| 537 | def set_saved(self, flag): pass |
| 538 | def reset_undo(self): pass |
| 539 | def open(self, event): |
| 540 | self.text.event_generate("<<open-window-from-file>>") |
| 541 | def save(self, event): |
| 542 | self.text.event_generate("<<save-window>>") |
| 543 | def save_as(self, event): |
| 544 | self.text.event_generate("<<save-window-as-file>>") |
| 545 | def save_a_copy(self, event): |
| 546 | self.text.event_generate("<<save-copy-of-window-as-file>>") |
| 547 | text = Text(root) |
| 548 | text.pack() |
| 549 | text.focus_set() |
| 550 | editwin = MyEditWin(text) |
| 551 | io = IOBinding(editwin) |
| 552 | root.mainloop() |
| 553 | |
| 554 | if __name__ == "__main__": |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 555 | test() |