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