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