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