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 |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame^] | 182 | dirname = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 183 | |
| 184 | def set_filename(self, filename): |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame^] | 185 | if filename and os.path.isdir(filename): |
| 186 | self.filename = None |
| 187 | self.dirname = filename |
| 188 | else: |
| 189 | self.filename = filename |
| 190 | self.dirname = None |
| 191 | self.set_saved(1) |
| 192 | if self.filename_change_hook: |
| 193 | self.filename_change_hook() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 194 | |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 195 | def open(self, event=None, editFile=None): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 196 | if self.editwin.flist: |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 197 | if not editFile: |
| 198 | filename = self.askopenfile() |
| 199 | else: |
| 200 | filename=editFile |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 201 | if filename: |
Kurt B. Kaiser | 1bf4c2d | 2002-07-21 01:24:28 +0000 | [diff] [blame] | 202 | # If the current window has no filename and hasn't been |
| 203 | # modified, we replace its contents (no loss). Otherwise |
| 204 | # we open a new window. But we won't replace the |
| 205 | # shell window (which has an interp(reter) attribute), which |
| 206 | # gets set to "not modified" at every new prompt. |
| 207 | try: |
| 208 | interp = self.editwin.interp |
| 209 | except: |
| 210 | interp = None |
| 211 | if not self.filename and self.get_saved() and not interp: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 212 | self.editwin.flist.open(filename, self.loadfile) |
| 213 | else: |
| 214 | self.editwin.flist.open(filename) |
| 215 | else: |
| 216 | self.text.focus_set() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 217 | return "break" |
Kurt B. Kaiser | 1bf4c2d | 2002-07-21 01:24:28 +0000 | [diff] [blame] | 218 | # |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 219 | # Code for use outside IDLE: |
| 220 | if self.get_saved(): |
| 221 | reply = self.maybesave() |
| 222 | if reply == "cancel": |
| 223 | self.text.focus_set() |
| 224 | return "break" |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 225 | if not editFile: |
| 226 | filename = self.askopenfile() |
| 227 | else: |
| 228 | filename=editFile |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 229 | if filename: |
| 230 | self.loadfile(filename) |
| 231 | else: |
| 232 | self.text.focus_set() |
| 233 | return "break" |
| 234 | |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 235 | eol = r"(\r\n)|\n|\r" # \r\n (Windows), \n (UNIX), or \r (Mac) |
| 236 | eol_re = re.compile(eol) |
| 237 | eol_convention = os.linesep # Default |
| 238 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 239 | def loadfile(self, filename): |
| 240 | try: |
| 241 | # open the file in binary mode so that we can handle |
| 242 | # end-of-line convention ourselves. |
| 243 | f = open(filename,'rb') |
| 244 | chars = f.read() |
| 245 | f.close() |
| 246 | except IOError, msg: |
| 247 | tkMessageBox.showerror("I/O Error", str(msg), master=self.text) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 248 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 249 | |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 250 | chars = self.decode(chars) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 251 | # We now convert all end-of-lines to '\n's |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 252 | firsteol = self.eol_re.search(chars) |
| 253 | if firsteol: |
| 254 | self.eol_convention = firsteol.group(0) |
| 255 | chars = self.eol_re.sub(r"\n", chars) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 256 | |
| 257 | self.text.delete("1.0", "end") |
| 258 | self.set_filename(None) |
| 259 | self.text.insert("1.0", chars) |
| 260 | self.reset_undo() |
| 261 | self.set_filename(filename) |
| 262 | self.text.mark_set("insert", "1.0") |
| 263 | self.text.see("insert") |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 264 | self.updaterecentfileslist(filename) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 265 | return True |
| 266 | |
| 267 | def decode(self, chars): |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 268 | """Create a Unicode string |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 269 | |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 270 | If that fails, let Tcl try its best |
| 271 | """ |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 272 | # Check presence of a UTF-8 signature first |
| 273 | if chars.startswith(BOM_UTF8): |
| 274 | try: |
| 275 | chars = chars[3:].decode("utf-8") |
| 276 | except UnicodeError: |
| 277 | # has UTF-8 signature, but fails to decode... |
| 278 | return chars |
| 279 | else: |
| 280 | # Indicates that this file originally had a BOM |
| 281 | self.fileencoding = BOM_UTF8 |
| 282 | return chars |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 283 | # Next look for coding specification |
| 284 | try: |
| 285 | enc = coding_spec(chars) |
| 286 | except LookupError, name: |
| 287 | tkMessageBox.showerror( |
| 288 | title="Error loading the file", |
| 289 | message="The encoding '%s' is not known to this Python "\ |
| 290 | "installation. The file may not display correctly" % name, |
| 291 | master = self.text) |
| 292 | enc = None |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 293 | if enc: |
| 294 | try: |
| 295 | return unicode(chars, enc) |
| 296 | except UnicodeError: |
| 297 | pass |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 298 | # If it is ASCII, we need not to record anything |
| 299 | try: |
| 300 | return unicode(chars, 'ascii') |
| 301 | except UnicodeError: |
| 302 | pass |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 303 | # Finally, try the locale's encoding. This is deprecated; |
| 304 | # the user should declare a non-ASCII encoding |
| 305 | try: |
| 306 | chars = unicode(chars, encoding) |
| 307 | self.fileencoding = encoding |
| 308 | except UnicodeError: |
| 309 | pass |
| 310 | return chars |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 311 | |
| 312 | def maybesave(self): |
| 313 | if self.get_saved(): |
| 314 | return "yes" |
| 315 | message = "Do you want to save %s before closing?" % ( |
| 316 | self.filename or "this untitled document") |
| 317 | m = tkMessageBox.Message( |
| 318 | title="Save On Close", |
| 319 | message=message, |
| 320 | icon=tkMessageBox.QUESTION, |
| 321 | type=tkMessageBox.YESNOCANCEL, |
| 322 | master=self.text) |
| 323 | reply = m.show() |
| 324 | if reply == "yes": |
| 325 | self.save(None) |
| 326 | if not self.get_saved(): |
| 327 | reply = "cancel" |
| 328 | self.text.focus_set() |
| 329 | return reply |
| 330 | |
| 331 | def save(self, event): |
| 332 | if not self.filename: |
| 333 | self.save_as(event) |
| 334 | else: |
| 335 | if self.writefile(self.filename): |
| 336 | self.set_saved(1) |
Kurt B. Kaiser | ddeaf11 | 2003-03-04 04:42:04 +0000 | [diff] [blame] | 337 | try: |
| 338 | self.editwin.store_file_breaks() |
| 339 | except AttributeError: # may be a PyShell |
| 340 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 341 | self.text.focus_set() |
| 342 | return "break" |
| 343 | |
| 344 | def save_as(self, event): |
| 345 | filename = self.asksavefile() |
| 346 | if filename: |
| 347 | if self.writefile(filename): |
| 348 | self.set_filename(filename) |
| 349 | self.set_saved(1) |
Kurt B. Kaiser | ddeaf11 | 2003-03-04 04:42:04 +0000 | [diff] [blame] | 350 | try: |
| 351 | self.editwin.store_file_breaks() |
| 352 | except AttributeError: |
| 353 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 354 | self.text.focus_set() |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 355 | self.updaterecentfileslist(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 356 | return "break" |
| 357 | |
| 358 | def save_a_copy(self, event): |
| 359 | filename = self.asksavefile() |
| 360 | if filename: |
| 361 | self.writefile(filename) |
| 362 | self.text.focus_set() |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 363 | self.updaterecentfileslist(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 364 | return "break" |
| 365 | |
| 366 | def writefile(self, filename): |
| 367 | self.fixlastline() |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 368 | chars = self.encode(self.text.get("1.0", "end-1c")) |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 369 | if self.eol_convention != "\n": |
| 370 | chars = chars.replace("\n", self.eol_convention) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 371 | try: |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 372 | f = open(filename, "wb") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 373 | f.write(chars) |
| 374 | f.close() |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 375 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 376 | except IOError, msg: |
| 377 | tkMessageBox.showerror("I/O Error", str(msg), |
| 378 | master=self.text) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 379 | return False |
| 380 | |
| 381 | def encode(self, chars): |
| 382 | if isinstance(chars, types.StringType): |
| 383 | # This is either plain ASCII, or Tk was returning mixed-encoding |
| 384 | # text to us. Don't try to guess further. |
| 385 | return chars |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 386 | # See whether there is anything non-ASCII in it. |
| 387 | # If not, no need to figure out the encoding. |
| 388 | try: |
| 389 | return chars.encode('ascii') |
| 390 | except UnicodeError: |
| 391 | pass |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 392 | # If there is an encoding declared, try this first. |
| 393 | try: |
| 394 | enc = coding_spec(chars) |
| 395 | failed = None |
| 396 | except LookupError, msg: |
| 397 | failed = msg |
| 398 | enc = None |
| 399 | if enc: |
| 400 | try: |
| 401 | return chars.encode(enc) |
| 402 | except UnicodeError: |
| 403 | failed = "Invalid encoding '%s'" % enc |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 404 | if failed: |
| 405 | tkMessageBox.showerror( |
| 406 | "I/O Error", |
| 407 | "%s. Saving as UTF-8" % failed, |
| 408 | master = self.text) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 409 | # If there was a UTF-8 signature, use that. This should not fail |
| 410 | if self.fileencoding == BOM_UTF8 or failed: |
| 411 | return BOM_UTF8 + chars.encode("utf-8") |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 412 | # Try the original file encoding next, if any |
| 413 | if self.fileencoding: |
| 414 | try: |
| 415 | return chars.encode(self.fileencoding) |
| 416 | except UnicodeError: |
| 417 | tkMessageBox.showerror( |
| 418 | "I/O Error", |
| 419 | "Cannot save this as '%s' anymore. Saving as UTF-8" \ |
| 420 | % self.fileencoding, |
| 421 | master = self.text) |
| 422 | return BOM_UTF8 + chars.encode("utf-8") |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 423 | # Nothing was declared, and we had not determined an encoding |
| 424 | # on loading. Recommend an encoding line. |
Kurt B. Kaiser | 4767401 | 2003-05-18 02:24:32 +0000 | [diff] [blame] | 425 | config_encoding = idleConf.GetOption("main","EditorWindow", |
| 426 | "encoding") |
| 427 | if config_encoding == 'utf-8': |
| 428 | # User has requested that we save files as UTF-8 |
| 429 | return BOM_UTF8 + chars.encode("utf-8") |
| 430 | ask_user = True |
| 431 | try: |
| 432 | chars = chars.encode(encoding) |
| 433 | enc = encoding |
| 434 | if config_encoding == 'locale': |
Kurt B. Kaiser | a053f33 | 2003-05-10 00:49:56 +0000 | [diff] [blame] | 435 | ask_user = False |
Kurt B. Kaiser | 4767401 | 2003-05-18 02:24:32 +0000 | [diff] [blame] | 436 | except UnicodeError: |
| 437 | chars = BOM_UTF8 + chars.encode("utf-8") |
| 438 | enc = "utf-8" |
| 439 | if not ask_user: |
Kurt B. Kaiser | a053f33 | 2003-05-10 00:49:56 +0000 | [diff] [blame] | 440 | return chars |
Kurt B. Kaiser | 4767401 | 2003-05-18 02:24:32 +0000 | [diff] [blame] | 441 | dialog = EncodingMessage(self.editwin.top, enc) |
| 442 | dialog.go() |
| 443 | if dialog.num == 1: |
| 444 | # User asked us to edit the file |
| 445 | encline = "# -*- coding: %s -*-\n" % enc |
| 446 | firstline = self.text.get("1.0", "2.0") |
| 447 | if firstline.startswith("#!"): |
| 448 | # Insert encoding after #! line |
| 449 | self.text.insert("2.0", encline) |
| 450 | else: |
| 451 | self.text.insert("1.0", encline) |
| 452 | return self.encode(self.text.get("1.0", "end-1c")) |
| 453 | return chars |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 454 | |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 455 | def fixlastline(self): |
| 456 | c = self.text.get("end-2c") |
| 457 | if c != '\n': |
| 458 | self.text.insert("end-1c", "\n") |
| 459 | |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 460 | def print_window(self, event): |
| 461 | tempfilename = None |
| 462 | if self.get_saved(): |
| 463 | filename = self.filename |
| 464 | else: |
| 465 | filename = tempfilename = tempfile.mktemp() |
| 466 | if not self.writefile(filename): |
| 467 | os.unlink(tempfilename) |
| 468 | return "break" |
| 469 | platform=os.name |
| 470 | printPlatform=1 |
| 471 | if platform == 'posix': #posix platform |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 472 | command = idleConf.GetOption('main','General', |
| 473 | 'print-command-posix') |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 474 | command = command + " 2>&1" |
| 475 | elif platform == 'nt': #win32 platform |
| 476 | command = idleConf.GetOption('main','General','print-command-win') |
| 477 | else: #no printing for this platform |
| 478 | printPlatform=0 |
| 479 | if printPlatform: #we can try to print for this platform |
| 480 | command = command % filename |
| 481 | pipe = os.popen(command, "r") |
| 482 | output = pipe.read().strip() |
| 483 | status = pipe.close() |
| 484 | if status: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 485 | output = "Printing failed (exit status 0x%x)\n" % \ |
| 486 | status + output |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 487 | if output: |
| 488 | output = "Printing command: %s\n" % repr(command) + output |
| 489 | tkMessageBox.showerror("Print status", output, master=self.text) |
| 490 | else: #no printing for this platform |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 491 | message="Printing is not enabled for this platform: %s" % platform |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 492 | tkMessageBox.showinfo("Print status", message, master=self.text) |
| 493 | return "break" |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 494 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 495 | opendialog = None |
| 496 | savedialog = None |
| 497 | |
| 498 | filetypes = [ |
| 499 | ("Python and text files", "*.py *.pyw *.txt", "TEXT"), |
| 500 | ("All text files", "*", "TEXT"), |
| 501 | ("All files", "*"), |
| 502 | ] |
| 503 | |
| 504 | def askopenfile(self): |
| 505 | dir, base = self.defaultfilename("open") |
| 506 | if not self.opendialog: |
| 507 | self.opendialog = tkFileDialog.Open(master=self.text, |
| 508 | filetypes=self.filetypes) |
| 509 | return self.opendialog.show(initialdir=dir, initialfile=base) |
| 510 | |
| 511 | def defaultfilename(self, mode="open"): |
| 512 | if self.filename: |
| 513 | return os.path.split(self.filename) |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame^] | 514 | elif self.dirname: |
| 515 | return self.dirname, "" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 516 | else: |
| 517 | try: |
| 518 | pwd = os.getcwd() |
| 519 | except os.error: |
| 520 | pwd = "" |
| 521 | return pwd, "" |
| 522 | |
| 523 | def asksavefile(self): |
| 524 | dir, base = self.defaultfilename("save") |
| 525 | if not self.savedialog: |
| 526 | self.savedialog = tkFileDialog.SaveAs(master=self.text, |
| 527 | filetypes=self.filetypes) |
| 528 | return self.savedialog.show(initialdir=dir, initialfile=base) |
| 529 | |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 530 | def updaterecentfileslist(self,filename): |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 531 | "Update recent file list on all editor windows" |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 532 | self.editwin.UpdateRecentFilesList(filename) |
| 533 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 534 | def test(): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 535 | root = Tk() |
| 536 | class MyEditWin: |
| 537 | def __init__(self, text): |
| 538 | self.text = text |
| 539 | self.flist = None |
| 540 | self.text.bind("<Control-o>", self.open) |
| 541 | self.text.bind("<Control-s>", self.save) |
| 542 | self.text.bind("<Alt-s>", self.save_as) |
| 543 | self.text.bind("<Alt-z>", self.save_a_copy) |
| 544 | def get_saved(self): return 0 |
| 545 | def set_saved(self, flag): pass |
| 546 | def reset_undo(self): pass |
| 547 | def open(self, event): |
| 548 | self.text.event_generate("<<open-window-from-file>>") |
| 549 | def save(self, event): |
| 550 | self.text.event_generate("<<save-window>>") |
| 551 | def save_as(self, event): |
| 552 | self.text.event_generate("<<save-window-as-file>>") |
| 553 | def save_a_copy(self, event): |
| 554 | self.text.event_generate("<<save-copy-of-window-as-file>>") |
| 555 | text = Text(root) |
| 556 | text.pack() |
| 557 | text.focus_set() |
| 558 | editwin = MyEditWin(text) |
| 559 | io = IOBinding(editwin) |
| 560 | root.mainloop() |
| 561 | |
| 562 | if __name__ == "__main__": |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 563 | test() |