David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | import os |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 2 | import types |
| 3 | import sys |
| 4 | import codecs |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 5 | import tempfile |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 6 | import tkinter.filedialog as tkFileDialog |
| 7 | import tkinter.messagebox as tkMessageBox |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 8 | import re |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 9 | from tkinter import * |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 10 | from tkinter.simpledialog import askstring |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 11 | |
Kurt B. Kaiser | 3a4e24b | 2007-08-22 18:06:14 +0000 | [diff] [blame] | 12 | from idlelib.configHandler import idleConf |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 13 | |
Kurt B. Kaiser | 1963ad3 | 2007-09-01 19:47:39 +0000 | [diff] [blame] | 14 | from codecs import BOM_UTF8 |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 15 | |
| 16 | # Try setting the locale, so that we can find out |
| 17 | # what encoding to use |
| 18 | try: |
| 19 | import locale |
| 20 | locale.setlocale(locale.LC_CTYPE, "") |
Kurt B. Kaiser | 188e25f | 2003-11-25 05:01:00 +0000 | [diff] [blame] | 21 | except (ImportError, locale.Error): |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 22 | pass |
| 23 | |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 24 | # Encoding for file names |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 25 | filesystemencoding = sys.getfilesystemencoding() ### currently unused |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 26 | |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 27 | locale_encoding = 'ascii' |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 28 | if sys.platform == 'win32': |
| 29 | # On Windows, we could use "mbcs". However, to give the user |
| 30 | # a portable encoding name, we need to find the code page |
| 31 | try: |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 32 | locale_encoding = locale.getdefaultlocale()[1] |
| 33 | codecs.lookup(locale_encoding) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 34 | except LookupError: |
| 35 | pass |
| 36 | else: |
| 37 | try: |
| 38 | # Different things can fail here: the locale module may not be |
| 39 | # loaded, it may not offer nl_langinfo, or CODESET, or the |
| 40 | # resulting codeset may be unknown to Python. We ignore all |
| 41 | # these problems, falling back to ASCII |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 42 | locale_encoding = locale.nl_langinfo(locale.CODESET) |
| 43 | if locale_encoding is None or locale_encoding is '': |
Tony Lownds | e555fc7 | 2002-09-23 01:01:20 +0000 | [diff] [blame] | 44 | # situation occurs on Mac OS X |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 45 | locale_encoding = 'ascii' |
| 46 | codecs.lookup(locale_encoding) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 47 | except (NameError, AttributeError, LookupError): |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 48 | # Try getdefaultlocale: it parses environment variables, |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 49 | # which may give a clue. Unfortunately, getdefaultlocale has |
| 50 | # bugs that can cause ValueError. |
| 51 | try: |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 52 | locale_encoding = locale.getdefaultlocale()[1] |
| 53 | if locale_encoding is None or locale_encoding is '': |
Tony Lownds | e555fc7 | 2002-09-23 01:01:20 +0000 | [diff] [blame] | 54 | # situation occurs on Mac OS X |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 55 | locale_encoding = 'ascii' |
| 56 | codecs.lookup(locale_encoding) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 57 | except (ValueError, LookupError): |
| 58 | pass |
| 59 | |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 60 | locale_encoding = locale_encoding.lower() |
| 61 | |
| 62 | encoding = locale_encoding ### KBK 07Sep07 This is used all over IDLE, check! |
| 63 | ### 'encoding' is used below in encode(), check! |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 64 | |
| 65 | coding_re = re.compile("coding[:=]\s*([-\w_.]+)") |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 66 | |
Kurt B. Kaiser | 1963ad3 | 2007-09-01 19:47:39 +0000 | [diff] [blame] | 67 | def coding_spec(data): |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 68 | """Return the encoding declaration according to PEP 263. |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 69 | |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 70 | When checking encoded data, only the first two lines should be passed |
| 71 | in to avoid a UnicodeDecodeError if the rest of the data is not unicode. |
| 72 | The first two lines would contain the encoding specification. |
| 73 | |
| 74 | Raise a LookupError if the encoding is declared but unknown. |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 75 | """ |
Kurt B. Kaiser | 1963ad3 | 2007-09-01 19:47:39 +0000 | [diff] [blame] | 76 | if isinstance(data, bytes): |
Martin v. Löwis | 975a079 | 2009-01-18 20:15:42 +0000 | [diff] [blame] | 77 | # This encoding might be wrong. However, the coding |
| 78 | # spec must be ASCII-only, so any non-ASCII characters |
| 79 | # around here will be ignored. Decoding to Latin-1 should |
| 80 | # never fail (except for memory outage) |
| 81 | lines = data.decode('iso-8859-1') |
Kurt B. Kaiser | 1963ad3 | 2007-09-01 19:47:39 +0000 | [diff] [blame] | 82 | else: |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 83 | lines = data |
| 84 | # consider only the first two lines |
| 85 | if '\n' in lines: |
| 86 | lst = lines.split('\n')[:2] |
| 87 | elif '\r' in lines: |
| 88 | lst = lines.split('\r')[:2] |
| 89 | else: |
| 90 | lst = list(lines) |
| 91 | str = '\n'.join(lst) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 92 | match = coding_re.search(str) |
| 93 | if not match: |
| 94 | return None |
| 95 | name = match.group(1) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 96 | try: |
| 97 | codecs.lookup(name) |
| 98 | except LookupError: |
| 99 | # The standard encoding error does not indicate the encoding |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 100 | raise LookupError("Unknown encoding: "+name) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 101 | return name |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 102 | |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 103 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 104 | class IOBinding: |
| 105 | |
| 106 | def __init__(self, editwin): |
| 107 | self.editwin = editwin |
| 108 | self.text = editwin.text |
| 109 | self.__id_open = self.text.bind("<<open-window-from-file>>", self.open) |
| 110 | self.__id_save = self.text.bind("<<save-window>>", self.save) |
| 111 | self.__id_saveas = self.text.bind("<<save-window-as-file>>", |
| 112 | self.save_as) |
| 113 | self.__id_savecopy = self.text.bind("<<save-copy-of-window-as-file>>", |
| 114 | self.save_a_copy) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 115 | self.fileencoding = None |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 116 | self.__id_print = self.text.bind("<<print-window>>", self.print_window) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 117 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 118 | def close(self): |
| 119 | # Undo command bindings |
| 120 | self.text.unbind("<<open-window-from-file>>", self.__id_open) |
| 121 | self.text.unbind("<<save-window>>", self.__id_save) |
| 122 | self.text.unbind("<<save-window-as-file>>",self.__id_saveas) |
| 123 | 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] | 124 | self.text.unbind("<<print-window>>", self.__id_print) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 125 | # Break cycles |
| 126 | self.editwin = None |
| 127 | self.text = None |
| 128 | self.filename_change_hook = None |
| 129 | |
| 130 | def get_saved(self): |
| 131 | return self.editwin.get_saved() |
| 132 | |
| 133 | def set_saved(self, flag): |
| 134 | self.editwin.set_saved(flag) |
| 135 | |
| 136 | def reset_undo(self): |
| 137 | self.editwin.reset_undo() |
| 138 | |
| 139 | filename_change_hook = None |
| 140 | |
| 141 | def set_filename_change_hook(self, hook): |
| 142 | self.filename_change_hook = hook |
| 143 | |
| 144 | filename = None |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 145 | dirname = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 146 | |
| 147 | def set_filename(self, filename): |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 148 | if filename and os.path.isdir(filename): |
| 149 | self.filename = None |
| 150 | self.dirname = filename |
| 151 | else: |
| 152 | self.filename = filename |
| 153 | self.dirname = None |
| 154 | self.set_saved(1) |
| 155 | if self.filename_change_hook: |
| 156 | self.filename_change_hook() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 157 | |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 158 | def open(self, event=None, editFile=None): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 159 | if self.editwin.flist: |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 160 | if not editFile: |
| 161 | filename = self.askopenfile() |
| 162 | else: |
| 163 | filename=editFile |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 164 | if filename: |
Kurt B. Kaiser | 1bf4c2d | 2002-07-21 01:24:28 +0000 | [diff] [blame] | 165 | # If the current window has no filename and hasn't been |
| 166 | # modified, we replace its contents (no loss). Otherwise |
| 167 | # we open a new window. But we won't replace the |
| 168 | # shell window (which has an interp(reter) attribute), which |
| 169 | # gets set to "not modified" at every new prompt. |
| 170 | try: |
| 171 | interp = self.editwin.interp |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 172 | except AttributeError: |
Kurt B. Kaiser | 1bf4c2d | 2002-07-21 01:24:28 +0000 | [diff] [blame] | 173 | interp = None |
| 174 | if not self.filename and self.get_saved() and not interp: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 175 | self.editwin.flist.open(filename, self.loadfile) |
| 176 | else: |
| 177 | self.editwin.flist.open(filename) |
| 178 | else: |
| 179 | self.text.focus_set() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 180 | return "break" |
Kurt B. Kaiser | 1bf4c2d | 2002-07-21 01:24:28 +0000 | [diff] [blame] | 181 | # |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 182 | # Code for use outside IDLE: |
| 183 | if self.get_saved(): |
| 184 | reply = self.maybesave() |
| 185 | if reply == "cancel": |
| 186 | self.text.focus_set() |
| 187 | return "break" |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 188 | if not editFile: |
| 189 | filename = self.askopenfile() |
| 190 | else: |
| 191 | filename=editFile |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 192 | if filename: |
| 193 | self.loadfile(filename) |
| 194 | else: |
| 195 | self.text.focus_set() |
| 196 | return "break" |
| 197 | |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 198 | eol = r"(\r\n)|\n|\r" # \r\n (Windows), \n (UNIX), or \r (Mac) |
| 199 | eol_re = re.compile(eol) |
Kurt B. Kaiser | 8d5c8b5 | 2007-10-10 00:36:38 +0000 | [diff] [blame] | 200 | eol_convention = os.linesep # default |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 201 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 202 | def loadfile(self, filename): |
| 203 | try: |
| 204 | # open the file in binary mode so that we can handle |
Kurt B. Kaiser | 3a4e24b | 2007-08-22 18:06:14 +0000 | [diff] [blame] | 205 | # end-of-line convention ourselves. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 206 | f = open(filename,'rb') |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 207 | two_lines = f.readline() + f.readline() |
| 208 | f.seek(0) |
Kurt B. Kaiser | 1963ad3 | 2007-09-01 19:47:39 +0000 | [diff] [blame] | 209 | bytes = f.read() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 210 | f.close() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 211 | except IOError as msg: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 212 | tkMessageBox.showerror("I/O Error", str(msg), master=self.text) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 213 | return False |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 214 | chars, converted = self._decode(two_lines, bytes) |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 215 | if chars is None: |
| 216 | tkMessageBox.showerror("Decoding Error", |
| 217 | "File %s\nFailed to Decode" % filename, |
| 218 | parent=self.text) |
| 219 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 220 | # We now convert all end-of-lines to '\n's |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 221 | firsteol = self.eol_re.search(chars) |
| 222 | if firsteol: |
| 223 | self.eol_convention = firsteol.group(0) |
| 224 | chars = self.eol_re.sub(r"\n", chars) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 225 | self.text.delete("1.0", "end") |
| 226 | self.set_filename(None) |
| 227 | self.text.insert("1.0", chars) |
| 228 | self.reset_undo() |
| 229 | self.set_filename(filename) |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 230 | if converted: |
| 231 | # We need to save the conversion results first |
| 232 | # before being able to execute the code |
| 233 | self.set_saved(False) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 234 | self.text.mark_set("insert", "1.0") |
Ned Deily | f25e3d5 | 2011-07-26 18:17:33 -0700 | [diff] [blame] | 235 | self.text.yview("insert") |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 236 | self.updaterecentfileslist(filename) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 237 | return True |
| 238 | |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 239 | def _decode(self, two_lines, bytes): |
| 240 | "Create a Unicode string." |
| 241 | chars = None |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 242 | # Check presence of a UTF-8 signature first |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 243 | if bytes.startswith(BOM_UTF8): |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 244 | try: |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 245 | chars = bytes[3:].decode("utf-8") |
| 246 | except UnicodeDecodeError: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 247 | # has UTF-8 signature, but fails to decode... |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 248 | return None, False |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 249 | else: |
| 250 | # Indicates that this file originally had a BOM |
Kurt B. Kaiser | 1963ad3 | 2007-09-01 19:47:39 +0000 | [diff] [blame] | 251 | self.fileencoding = 'BOM' |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 252 | return chars, False |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 253 | # Next look for coding specification |
| 254 | try: |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 255 | enc = coding_spec(two_lines) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 256 | except LookupError as name: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 257 | tkMessageBox.showerror( |
| 258 | title="Error loading the file", |
| 259 | message="The encoding '%s' is not known to this Python "\ |
| 260 | "installation. The file may not display correctly" % name, |
| 261 | master = self.text) |
| 262 | enc = None |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 263 | except UnicodeDecodeError: |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 264 | return None, False |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 265 | if enc: |
| 266 | try: |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 267 | chars = str(bytes, enc) |
| 268 | self.fileencoding = enc |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 269 | return chars, False |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 270 | except UnicodeDecodeError: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 271 | pass |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 272 | # Try ascii: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 273 | try: |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 274 | chars = str(bytes, 'ascii') |
| 275 | self.fileencoding = None |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 276 | return chars, False |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 277 | except UnicodeDecodeError: |
| 278 | pass |
| 279 | # Try utf-8: |
| 280 | try: |
| 281 | chars = str(bytes, 'utf-8') |
| 282 | self.fileencoding = 'utf-8' |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 283 | return chars, False |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 284 | except UnicodeDecodeError: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 285 | pass |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 286 | # Finally, try the locale's encoding. This is deprecated; |
| 287 | # the user should declare a non-ASCII encoding |
| 288 | try: |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 289 | # Wait for the editor window to appear |
| 290 | self.editwin.text.update() |
| 291 | enc = askstring( |
| 292 | "Specify file encoding", |
| 293 | "The file's encoding is invalid for Python 3.x.\n" |
| 294 | "IDLE will convert it to UTF-8.\n" |
| 295 | "What is the current encoding of the file?", |
| 296 | initialvalue = locale_encoding, |
| 297 | parent = self.editwin.text) |
| 298 | |
| 299 | if enc: |
| 300 | chars = str(bytes, enc) |
| 301 | self.fileencoding = None |
| 302 | return chars, True |
| 303 | except (UnicodeDecodeError, LookupError): |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 304 | pass |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 305 | return None, False # None on failure |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 306 | |
| 307 | def maybesave(self): |
| 308 | if self.get_saved(): |
| 309 | return "yes" |
| 310 | message = "Do you want to save %s before closing?" % ( |
| 311 | self.filename or "this untitled document") |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 312 | confirm = tkMessageBox.askyesnocancel( |
| 313 | title="Save On Close", |
| 314 | message=message, |
| 315 | default=tkMessageBox.YES, |
| 316 | master=self.text) |
| 317 | if confirm: |
| 318 | reply = "yes" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 319 | self.save(None) |
| 320 | if not self.get_saved(): |
| 321 | reply = "cancel" |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 322 | elif confirm is None: |
| 323 | reply = "cancel" |
| 324 | else: |
| 325 | reply = "no" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 326 | self.text.focus_set() |
| 327 | return reply |
| 328 | |
| 329 | def save(self, event): |
| 330 | if not self.filename: |
| 331 | self.save_as(event) |
| 332 | else: |
| 333 | if self.writefile(self.filename): |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 334 | self.set_saved(True) |
Kurt B. Kaiser | ddeaf11 | 2003-03-04 04:42:04 +0000 | [diff] [blame] | 335 | try: |
| 336 | self.editwin.store_file_breaks() |
| 337 | except AttributeError: # may be a PyShell |
| 338 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 339 | self.text.focus_set() |
| 340 | return "break" |
| 341 | |
| 342 | def save_as(self, event): |
| 343 | filename = self.asksavefile() |
| 344 | if filename: |
| 345 | if self.writefile(filename): |
| 346 | self.set_filename(filename) |
| 347 | self.set_saved(1) |
Kurt B. Kaiser | ddeaf11 | 2003-03-04 04:42:04 +0000 | [diff] [blame] | 348 | try: |
| 349 | self.editwin.store_file_breaks() |
| 350 | except AttributeError: |
| 351 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 352 | self.text.focus_set() |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 353 | self.updaterecentfileslist(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 354 | return "break" |
| 355 | |
| 356 | def save_a_copy(self, event): |
| 357 | filename = self.asksavefile() |
| 358 | if filename: |
| 359 | self.writefile(filename) |
| 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 writefile(self, filename): |
| 365 | self.fixlastline() |
Kurt B. Kaiser | 3e623ba | 2007-10-09 23:12:31 +0000 | [diff] [blame] | 366 | text = self.text.get("1.0", "end-1c") |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 367 | if self.eol_convention != "\n": |
Kurt B. Kaiser | 3e623ba | 2007-10-09 23:12:31 +0000 | [diff] [blame] | 368 | text = text.replace("\n", self.eol_convention) |
Kurt B. Kaiser | cdadf24 | 2007-10-15 02:40:08 +0000 | [diff] [blame] | 369 | chars = self.encode(text) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 370 | try: |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 371 | f = open(filename, "wb") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 372 | f.write(chars) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 373 | f.flush() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 374 | f.close() |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 375 | return True |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 376 | except IOError as msg: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 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): |
Martin v. Löwis | 4d9ed9f | 2007-08-13 13:30:04 +0000 | [diff] [blame] | 382 | if isinstance(chars, bytes): |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 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 |
Martin v. Löwis | 5216d08 | 2008-12-29 18:43:40 +0000 | [diff] [blame] | 386 | # Preserve a BOM that might have been present on opening |
| 387 | if self.fileencoding == 'BOM': |
| 388 | return BOM_UTF8 + chars.encode("utf-8") |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 389 | # See whether there is anything non-ASCII in it. |
| 390 | # If not, no need to figure out the encoding. |
| 391 | try: |
| 392 | return chars.encode('ascii') |
| 393 | except UnicodeError: |
| 394 | pass |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 395 | # Check if there is an encoding declared |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 396 | try: |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 397 | # a string, let coding_spec slice it to the first two lines |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 398 | enc = coding_spec(chars) |
| 399 | failed = None |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 400 | except LookupError as msg: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 401 | failed = msg |
| 402 | enc = None |
Martin v. Löwis | 5216d08 | 2008-12-29 18:43:40 +0000 | [diff] [blame] | 403 | else: |
| 404 | if not enc: |
| 405 | # PEP 3120: default source encoding is UTF-8 |
| 406 | enc = 'utf-8' |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 407 | if enc: |
| 408 | try: |
| 409 | return chars.encode(enc) |
| 410 | except UnicodeError: |
| 411 | failed = "Invalid encoding '%s'" % enc |
Martin v. Löwis | 5216d08 | 2008-12-29 18:43:40 +0000 | [diff] [blame] | 412 | tkMessageBox.showerror( |
| 413 | "I/O Error", |
| 414 | "%s.\nSaving as UTF-8" % failed, |
| 415 | master = self.text) |
| 416 | # Fallback: save as UTF-8, with BOM - ignoring the incorrect |
| 417 | # declared encoding |
| 418 | return BOM_UTF8 + chars.encode("utf-8") |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 419 | |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 420 | def fixlastline(self): |
| 421 | c = self.text.get("end-2c") |
| 422 | if c != '\n': |
| 423 | self.text.insert("end-1c", "\n") |
| 424 | |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 425 | def print_window(self, event): |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 426 | confirm = tkMessageBox.askokcancel( |
| 427 | title="Print", |
| 428 | message="Print to Default Printer", |
| 429 | default=tkMessageBox.OK, |
| 430 | master=self.text) |
| 431 | if not confirm: |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 432 | self.text.focus_set() |
| 433 | return "break" |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 434 | tempfilename = None |
Kurt B. Kaiser | 9067c8d | 2003-06-09 03:12:42 +0000 | [diff] [blame] | 435 | saved = self.get_saved() |
| 436 | if saved: |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 437 | filename = self.filename |
Kurt B. Kaiser | 9067c8d | 2003-06-09 03:12:42 +0000 | [diff] [blame] | 438 | # shell undo is reset after every prompt, looks saved, probably isn't |
| 439 | if not saved or filename is None: |
Kurt B. Kaiser | 61e2c9a | 2003-06-14 17:56:25 +0000 | [diff] [blame] | 440 | (tfd, tempfilename) = tempfile.mkstemp(prefix='IDLE_tmp_') |
| 441 | filename = tempfilename |
| 442 | os.close(tfd) |
| 443 | if not self.writefile(tempfilename): |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 444 | os.unlink(tempfilename) |
| 445 | return "break" |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 446 | platform = os.name |
| 447 | printPlatform = True |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 448 | if platform == 'posix': #posix platform |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 449 | command = idleConf.GetOption('main','General', |
| 450 | 'print-command-posix') |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 451 | command = command + " 2>&1" |
| 452 | elif platform == 'nt': #win32 platform |
| 453 | command = idleConf.GetOption('main','General','print-command-win') |
| 454 | else: #no printing for this platform |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 455 | printPlatform = False |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 456 | if printPlatform: #we can try to print for this platform |
| 457 | command = command % filename |
| 458 | pipe = os.popen(command, "r") |
Kurt B. Kaiser | 9067c8d | 2003-06-09 03:12:42 +0000 | [diff] [blame] | 459 | # 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] | 460 | output = pipe.read().strip() |
| 461 | status = pipe.close() |
| 462 | if status: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 463 | output = "Printing failed (exit status 0x%x)\n" % \ |
| 464 | status + output |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 465 | if output: |
| 466 | output = "Printing command: %s\n" % repr(command) + output |
| 467 | tkMessageBox.showerror("Print status", output, master=self.text) |
| 468 | else: #no printing for this platform |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 469 | message = "Printing is not enabled for this platform: %s" % platform |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 470 | tkMessageBox.showinfo("Print status", message, master=self.text) |
Kurt B. Kaiser | 61e2c9a | 2003-06-14 17:56:25 +0000 | [diff] [blame] | 471 | if tempfilename: |
| 472 | os.unlink(tempfilename) |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 473 | return "break" |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 474 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 475 | opendialog = None |
| 476 | savedialog = None |
| 477 | |
| 478 | filetypes = [ |
Terry Reedy | 2b4cb96 | 2010-11-23 06:07:04 +0000 | [diff] [blame] | 479 | ("Python files", "*.py *.pyw", "TEXT"), |
| 480 | ("Text files", "*.txt", "TEXT"), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 481 | ("All files", "*"), |
| 482 | ] |
| 483 | |
| 484 | def askopenfile(self): |
| 485 | dir, base = self.defaultfilename("open") |
| 486 | if not self.opendialog: |
| 487 | self.opendialog = tkFileDialog.Open(master=self.text, |
| 488 | filetypes=self.filetypes) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 489 | filename = self.opendialog.show(initialdir=dir, initialfile=base) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 490 | return filename |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 491 | |
| 492 | def defaultfilename(self, mode="open"): |
| 493 | if self.filename: |
| 494 | return os.path.split(self.filename) |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 495 | elif self.dirname: |
| 496 | return self.dirname, "" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 497 | else: |
| 498 | try: |
| 499 | pwd = os.getcwd() |
| 500 | except os.error: |
| 501 | pwd = "" |
| 502 | return pwd, "" |
| 503 | |
| 504 | def asksavefile(self): |
| 505 | dir, base = self.defaultfilename("save") |
| 506 | if not self.savedialog: |
| 507 | self.savedialog = tkFileDialog.SaveAs(master=self.text, |
| 508 | filetypes=self.filetypes) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 509 | filename = self.savedialog.show(initialdir=dir, initialfile=base) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 510 | return filename |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 511 | |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 512 | def updaterecentfileslist(self,filename): |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 513 | "Update recent file list on all editor windows" |
Kurt B. Kaiser | 3a4e24b | 2007-08-22 18:06:14 +0000 | [diff] [blame] | 514 | if self.editwin.flist: |
| 515 | self.editwin.update_recent_files_list(filename) |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 516 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 517 | def test(): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 518 | root = Tk() |
| 519 | class MyEditWin: |
| 520 | def __init__(self, text): |
| 521 | self.text = text |
| 522 | self.flist = None |
| 523 | self.text.bind("<Control-o>", self.open) |
| 524 | self.text.bind("<Control-s>", self.save) |
| 525 | self.text.bind("<Alt-s>", self.save_as) |
| 526 | self.text.bind("<Alt-z>", self.save_a_copy) |
| 527 | def get_saved(self): return 0 |
| 528 | def set_saved(self, flag): pass |
| 529 | def reset_undo(self): pass |
| 530 | def open(self, event): |
| 531 | self.text.event_generate("<<open-window-from-file>>") |
| 532 | def save(self, event): |
| 533 | self.text.event_generate("<<save-window>>") |
| 534 | def save_as(self, event): |
| 535 | self.text.event_generate("<<save-window-as-file>>") |
| 536 | def save_a_copy(self, event): |
| 537 | self.text.event_generate("<<save-copy-of-window-as-file>>") |
| 538 | text = Text(root) |
| 539 | text.pack() |
| 540 | text.focus_set() |
| 541 | editwin = MyEditWin(text) |
| 542 | io = IOBinding(editwin) |
| 543 | root.mainloop() |
| 544 | |
| 545 | if __name__ == "__main__": |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 546 | test() |