Terry Jan Reedy | 38d3864 | 2020-07-16 18:24:49 -0400 | [diff] [blame] | 1 | import io |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 2 | import os |
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 |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 5 | import tempfile |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 6 | import tokenize |
Terry Jan Reedy | bbdc065 | 2015-10-30 02:47:06 -0400 | [diff] [blame] | 7 | |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 8 | import tkinter.filedialog as tkFileDialog |
| 9 | import tkinter.messagebox as tkMessageBox |
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 | |
Terry Jan Reedy | 7c15341 | 2016-06-26 17:48:02 -0400 | [diff] [blame] | 12 | import idlelib |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 13 | from idlelib.config import idleConf |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 14 | |
Serhiy Storchaka | 2515a28 | 2020-06-30 03:18:22 +0300 | [diff] [blame] | 15 | encoding = 'utf-8' |
| 16 | if sys.platform == 'win32': |
| 17 | errors = 'surrogatepass' |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 18 | else: |
Serhiy Storchaka | 2515a28 | 2020-06-30 03:18:22 +0300 | [diff] [blame] | 19 | errors = 'surrogateescape' |
Terry Jan Reedy | 7c15341 | 2016-06-26 17:48:02 -0400 | [diff] [blame] | 20 | |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 21 | |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 22 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 23 | class IOBinding: |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 24 | # One instance per editor Window so methods know which to save, close. |
| 25 | # Open returns focus to self.editwin if aborted. |
| 26 | # EditorWindow.open_module, others, belong here. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 27 | |
| 28 | def __init__(self, editwin): |
| 29 | self.editwin = editwin |
| 30 | self.text = editwin.text |
| 31 | self.__id_open = self.text.bind("<<open-window-from-file>>", self.open) |
| 32 | self.__id_save = self.text.bind("<<save-window>>", self.save) |
| 33 | self.__id_saveas = self.text.bind("<<save-window-as-file>>", |
| 34 | self.save_as) |
| 35 | self.__id_savecopy = self.text.bind("<<save-copy-of-window-as-file>>", |
| 36 | self.save_a_copy) |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 37 | self.fileencoding = 'utf-8' |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 38 | self.__id_print = self.text.bind("<<print-window>>", self.print_window) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 39 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 40 | def close(self): |
| 41 | # Undo command bindings |
| 42 | self.text.unbind("<<open-window-from-file>>", self.__id_open) |
| 43 | self.text.unbind("<<save-window>>", self.__id_save) |
| 44 | self.text.unbind("<<save-window-as-file>>",self.__id_saveas) |
| 45 | 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] | 46 | self.text.unbind("<<print-window>>", self.__id_print) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 47 | # Break cycles |
| 48 | self.editwin = None |
| 49 | self.text = None |
| 50 | self.filename_change_hook = None |
| 51 | |
| 52 | def get_saved(self): |
| 53 | return self.editwin.get_saved() |
| 54 | |
| 55 | def set_saved(self, flag): |
| 56 | self.editwin.set_saved(flag) |
| 57 | |
| 58 | def reset_undo(self): |
| 59 | self.editwin.reset_undo() |
| 60 | |
| 61 | filename_change_hook = None |
| 62 | |
| 63 | def set_filename_change_hook(self, hook): |
| 64 | self.filename_change_hook = hook |
| 65 | |
| 66 | filename = None |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 67 | dirname = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 68 | |
| 69 | def set_filename(self, filename): |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 70 | if filename and os.path.isdir(filename): |
| 71 | self.filename = None |
| 72 | self.dirname = filename |
| 73 | else: |
| 74 | self.filename = filename |
| 75 | self.dirname = None |
| 76 | self.set_saved(1) |
| 77 | if self.filename_change_hook: |
| 78 | self.filename_change_hook() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 79 | |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 80 | def open(self, event=None, editFile=None): |
Terry Jan Reedy | cd6b8c6 | 2012-05-26 20:23:45 -0400 | [diff] [blame] | 81 | flist = self.editwin.flist |
Terry Jan Reedy | a948c79 | 2012-06-02 20:22:58 -0400 | [diff] [blame] | 82 | # Save in case parent window is closed (ie, during askopenfile()). |
Terry Jan Reedy | cd6b8c6 | 2012-05-26 20:23:45 -0400 | [diff] [blame] | 83 | if flist: |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 84 | if not editFile: |
| 85 | filename = self.askopenfile() |
| 86 | else: |
| 87 | filename=editFile |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 88 | if filename: |
Terry Jan Reedy | a948c79 | 2012-06-02 20:22:58 -0400 | [diff] [blame] | 89 | # If editFile is valid and already open, flist.open will |
| 90 | # shift focus to its existing window. |
| 91 | # If the current window exists and is a fresh unnamed, |
| 92 | # unmodified editor window (not an interpreter shell), |
| 93 | # pass self.loadfile to flist.open so it will load the file |
| 94 | # in the current window (if the file is not already open) |
| 95 | # instead of a new window. |
| 96 | if (self.editwin and |
| 97 | not getattr(self.editwin, 'interp', None) and |
| 98 | not self.filename and |
| 99 | self.get_saved()): |
Terry Jan Reedy | cd6b8c6 | 2012-05-26 20:23:45 -0400 | [diff] [blame] | 100 | flist.open(filename, self.loadfile) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 101 | else: |
Terry Jan Reedy | cd6b8c6 | 2012-05-26 20:23:45 -0400 | [diff] [blame] | 102 | flist.open(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 103 | else: |
Terry Jan Reedy | cd6b8c6 | 2012-05-26 20:23:45 -0400 | [diff] [blame] | 104 | if self.text: |
| 105 | self.text.focus_set() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 106 | return "break" |
Terry Jan Reedy | a948c79 | 2012-06-02 20:22:58 -0400 | [diff] [blame] | 107 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 108 | # Code for use outside IDLE: |
| 109 | if self.get_saved(): |
| 110 | reply = self.maybesave() |
| 111 | if reply == "cancel": |
| 112 | self.text.focus_set() |
| 113 | return "break" |
Steven M. Gava | 1d46e40 | 2002-03-27 08:40:46 +0000 | [diff] [blame] | 114 | if not editFile: |
| 115 | filename = self.askopenfile() |
| 116 | else: |
| 117 | filename=editFile |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 118 | if filename: |
| 119 | self.loadfile(filename) |
| 120 | else: |
| 121 | self.text.focus_set() |
| 122 | return "break" |
| 123 | |
Kurt B. Kaiser | 8d5c8b5 | 2007-10-10 00:36:38 +0000 | [diff] [blame] | 124 | eol_convention = os.linesep # default |
Guido van Rossum | c2f77dd | 2003-04-25 18:36:31 +0000 | [diff] [blame] | 125 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 126 | def loadfile(self, filename): |
| 127 | try: |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 128 | try: |
| 129 | with tokenize.open(filename) as f: |
| 130 | chars = f.read() |
| 131 | fileencoding = f.encoding |
| 132 | eol_convention = f.newlines |
| 133 | converted = False |
| 134 | except (UnicodeDecodeError, SyntaxError): |
| 135 | # Wait for the editor window to appear |
| 136 | self.editwin.text.update() |
| 137 | enc = askstring( |
| 138 | "Specify file encoding", |
| 139 | "The file's encoding is invalid for Python 3.x.\n" |
| 140 | "IDLE will convert it to UTF-8.\n" |
| 141 | "What is the current encoding of the file?", |
| 142 | initialvalue='utf-8', |
| 143 | parent=self.editwin.text) |
| 144 | with open(filename, encoding=enc) as f: |
| 145 | chars = f.read() |
| 146 | fileencoding = f.encoding |
| 147 | eol_convention = f.newlines |
| 148 | converted = True |
| 149 | except OSError as err: |
| 150 | tkMessageBox.showerror("I/O Error", str(err), parent=self.text) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 151 | return False |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 152 | except UnicodeDecodeError: |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 153 | tkMessageBox.showerror("Decoding Error", |
| 154 | "File %s\nFailed to Decode" % filename, |
| 155 | parent=self.text) |
| 156 | return False |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 157 | |
Serhiy Storchaka | 0dd463c | 2020-07-25 06:21:30 +0300 | [diff] [blame^] | 158 | if not isinstance(eol_convention, str): |
| 159 | # If the file does not contain line separators, it is None. |
| 160 | # If the file contains mixed line separators, it is a tuple. |
| 161 | if eol_convention is not None: |
| 162 | tkMessageBox.showwarning("Mixed Newlines", |
| 163 | "Mixed newlines detected.\n" |
| 164 | "The file will be changed on save.", |
| 165 | parent=self.text) |
| 166 | converted = True |
| 167 | eol_convention = os.linesep # default |
| 168 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 169 | self.text.delete("1.0", "end") |
| 170 | self.set_filename(None) |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 171 | self.fileencoding = fileencoding |
| 172 | self.eol_convention = eol_convention |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 173 | self.text.insert("1.0", chars) |
| 174 | self.reset_undo() |
| 175 | self.set_filename(filename) |
Martin v. Löwis | 7e15845 | 2009-01-18 20:23:36 +0000 | [diff] [blame] | 176 | if converted: |
| 177 | # We need to save the conversion results first |
| 178 | # before being able to execute the code |
| 179 | self.set_saved(False) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 180 | self.text.mark_set("insert", "1.0") |
Ned Deily | f25e3d5 | 2011-07-26 18:17:33 -0700 | [diff] [blame] | 181 | self.text.yview("insert") |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 182 | self.updaterecentfileslist(filename) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 183 | return True |
| 184 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 185 | def maybesave(self): |
| 186 | if self.get_saved(): |
| 187 | return "yes" |
| 188 | message = "Do you want to save %s before closing?" % ( |
| 189 | self.filename or "this untitled document") |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 190 | confirm = tkMessageBox.askyesnocancel( |
| 191 | title="Save On Close", |
| 192 | message=message, |
| 193 | default=tkMessageBox.YES, |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 194 | parent=self.text) |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 195 | if confirm: |
| 196 | reply = "yes" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 197 | self.save(None) |
| 198 | if not self.get_saved(): |
| 199 | reply = "cancel" |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 200 | elif confirm is None: |
| 201 | reply = "cancel" |
| 202 | else: |
| 203 | reply = "no" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 204 | self.text.focus_set() |
| 205 | return reply |
| 206 | |
| 207 | def save(self, event): |
| 208 | if not self.filename: |
| 209 | self.save_as(event) |
| 210 | else: |
| 211 | if self.writefile(self.filename): |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 212 | self.set_saved(True) |
Kurt B. Kaiser | ddeaf11 | 2003-03-04 04:42:04 +0000 | [diff] [blame] | 213 | try: |
| 214 | self.editwin.store_file_breaks() |
| 215 | except AttributeError: # may be a PyShell |
| 216 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 217 | self.text.focus_set() |
| 218 | return "break" |
| 219 | |
| 220 | def save_as(self, event): |
| 221 | filename = self.asksavefile() |
| 222 | if filename: |
| 223 | if self.writefile(filename): |
| 224 | self.set_filename(filename) |
| 225 | self.set_saved(1) |
Kurt B. Kaiser | ddeaf11 | 2003-03-04 04:42:04 +0000 | [diff] [blame] | 226 | try: |
| 227 | self.editwin.store_file_breaks() |
| 228 | except AttributeError: |
| 229 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 230 | self.text.focus_set() |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 231 | self.updaterecentfileslist(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 232 | return "break" |
| 233 | |
| 234 | def save_a_copy(self, event): |
| 235 | filename = self.asksavefile() |
| 236 | if filename: |
| 237 | self.writefile(filename) |
| 238 | self.text.focus_set() |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 239 | self.updaterecentfileslist(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 240 | return "break" |
| 241 | |
| 242 | def writefile(self, filename): |
Zackery Spytz | c8b53dc | 2019-11-12 03:54:10 -0700 | [diff] [blame] | 243 | text = self.fixnewlines() |
Kurt B. Kaiser | cdadf24 | 2007-10-15 02:40:08 +0000 | [diff] [blame] | 244 | chars = self.encode(text) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 245 | try: |
Terry Jan Reedy | 95f34ab | 2013-08-04 15:39:03 -0400 | [diff] [blame] | 246 | with open(filename, "wb") as f: |
| 247 | f.write(chars) |
Guido van Rossum | 4f098b3 | 2019-05-13 05:31:30 -0700 | [diff] [blame] | 248 | f.flush() |
| 249 | os.fsync(f.fileno()) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 250 | return True |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 251 | except OSError as msg: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 252 | tkMessageBox.showerror("I/O Error", str(msg), |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 253 | parent=self.text) |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 254 | return False |
| 255 | |
Zackery Spytz | c8b53dc | 2019-11-12 03:54:10 -0700 | [diff] [blame] | 256 | def fixnewlines(self): |
| 257 | "Return text with final \n if needed and os eols." |
| 258 | if (self.text.get("end-2c") != '\n' |
| 259 | and not hasattr(self.editwin, "interp")): # Not shell. |
| 260 | self.text.insert("end-1c", "\n") |
| 261 | text = self.text.get("1.0", "end-1c") |
| 262 | if self.eol_convention != "\n": |
| 263 | text = text.replace("\n", self.eol_convention) |
| 264 | return text |
| 265 | |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 266 | def encode(self, chars): |
Martin v. Löwis | 4d9ed9f | 2007-08-13 13:30:04 +0000 | [diff] [blame] | 267 | if isinstance(chars, bytes): |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 268 | # This is either plain ASCII, or Tk was returning mixed-encoding |
| 269 | # text to us. Don't try to guess further. |
| 270 | return chars |
Martin v. Löwis | 5216d08 | 2008-12-29 18:43:40 +0000 | [diff] [blame] | 271 | # Preserve a BOM that might have been present on opening |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 272 | if self.fileencoding == 'utf-8-sig': |
| 273 | return chars.encode('utf-8-sig') |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 274 | # See whether there is anything non-ASCII in it. |
| 275 | # If not, no need to figure out the encoding. |
| 276 | try: |
| 277 | return chars.encode('ascii') |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 278 | except UnicodeEncodeError: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 279 | pass |
Kurt B. Kaiser | 44fa8f6 | 2007-09-07 05:06:21 +0000 | [diff] [blame] | 280 | # Check if there is an encoding declared |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 281 | try: |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 282 | encoded = chars.encode('ascii', 'replace') |
| 283 | enc, _ = tokenize.detect_encoding(io.BytesIO(encoded).readline) |
| 284 | return chars.encode(enc) |
| 285 | except SyntaxError as err: |
| 286 | failed = str(err) |
| 287 | except UnicodeEncodeError: |
| 288 | failed = "Invalid encoding '%s'" % enc |
Martin v. Löwis | 5216d08 | 2008-12-29 18:43:40 +0000 | [diff] [blame] | 289 | tkMessageBox.showerror( |
| 290 | "I/O Error", |
| 291 | "%s.\nSaving as UTF-8" % failed, |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 292 | parent=self.text) |
Martin v. Löwis | 5216d08 | 2008-12-29 18:43:40 +0000 | [diff] [blame] | 293 | # Fallback: save as UTF-8, with BOM - ignoring the incorrect |
| 294 | # declared encoding |
Serhiy Storchaka | 694d31e | 2020-06-30 09:33:22 +0300 | [diff] [blame] | 295 | return chars.encode('utf-8-sig') |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 296 | |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 297 | def print_window(self, event): |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 298 | confirm = tkMessageBox.askokcancel( |
| 299 | title="Print", |
| 300 | message="Print to Default Printer", |
| 301 | default=tkMessageBox.OK, |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 302 | parent=self.text) |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 303 | if not confirm: |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 304 | self.text.focus_set() |
| 305 | return "break" |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 306 | tempfilename = None |
Kurt B. Kaiser | 9067c8d | 2003-06-09 03:12:42 +0000 | [diff] [blame] | 307 | saved = self.get_saved() |
| 308 | if saved: |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 309 | filename = self.filename |
Kurt B. Kaiser | 9067c8d | 2003-06-09 03:12:42 +0000 | [diff] [blame] | 310 | # shell undo is reset after every prompt, looks saved, probably isn't |
| 311 | if not saved or filename is None: |
Kurt B. Kaiser | 61e2c9a | 2003-06-14 17:56:25 +0000 | [diff] [blame] | 312 | (tfd, tempfilename) = tempfile.mkstemp(prefix='IDLE_tmp_') |
| 313 | filename = tempfilename |
| 314 | os.close(tfd) |
| 315 | if not self.writefile(tempfilename): |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 316 | os.unlink(tempfilename) |
| 317 | return "break" |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 318 | platform = os.name |
| 319 | printPlatform = True |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 320 | if platform == 'posix': #posix platform |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 321 | command = idleConf.GetOption('main','General', |
| 322 | 'print-command-posix') |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 323 | command = command + " 2>&1" |
| 324 | elif platform == 'nt': #win32 platform |
| 325 | command = idleConf.GetOption('main','General','print-command-win') |
| 326 | else: #no printing for this platform |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 327 | printPlatform = False |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 328 | if printPlatform: #we can try to print for this platform |
Serhiy Storchaka | 69db587 | 2013-01-12 18:16:18 +0200 | [diff] [blame] | 329 | command = command % shlex.quote(filename) |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 330 | pipe = os.popen(command, "r") |
Kurt B. Kaiser | 9067c8d | 2003-06-09 03:12:42 +0000 | [diff] [blame] | 331 | # 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] | 332 | output = pipe.read().strip() |
| 333 | status = pipe.close() |
| 334 | if status: |
Kurt B. Kaiser | 01166da | 2002-09-16 22:03:37 +0000 | [diff] [blame] | 335 | output = "Printing failed (exit status 0x%x)\n" % \ |
| 336 | status + output |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 337 | if output: |
| 338 | output = "Printing command: %s\n" % repr(command) + output |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 339 | tkMessageBox.showerror("Print status", output, parent=self.text) |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 340 | else: #no printing for this platform |
Kurt B. Kaiser | 0a42982 | 2011-05-12 15:25:24 -0400 | [diff] [blame] | 341 | message = "Printing is not enabled for this platform: %s" % platform |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 342 | tkMessageBox.showinfo("Print status", message, parent=self.text) |
Kurt B. Kaiser | 61e2c9a | 2003-06-14 17:56:25 +0000 | [diff] [blame] | 343 | if tempfilename: |
| 344 | os.unlink(tempfilename) |
Steven M. Gava | 7981ce5 | 2002-06-11 04:45:34 +0000 | [diff] [blame] | 345 | return "break" |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 346 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 347 | opendialog = None |
| 348 | savedialog = None |
| 349 | |
Terry Jan Reedy | 5961e7c | 2017-10-01 19:01:27 -0400 | [diff] [blame] | 350 | filetypes = ( |
Terry Reedy | 2b4cb96 | 2010-11-23 06:07:04 +0000 | [diff] [blame] | 351 | ("Python files", "*.py *.pyw", "TEXT"), |
| 352 | ("Text files", "*.txt", "TEXT"), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 353 | ("All files", "*"), |
Terry Jan Reedy | 5961e7c | 2017-10-01 19:01:27 -0400 | [diff] [blame] | 354 | ) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 355 | |
Ned Deily | c65ef9b | 2012-07-09 18:16:11 -0700 | [diff] [blame] | 356 | defaultextension = '.py' if sys.platform == 'darwin' else '' |
| 357 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 358 | def askopenfile(self): |
| 359 | dir, base = self.defaultfilename("open") |
| 360 | if not self.opendialog: |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 361 | self.opendialog = tkFileDialog.Open(parent=self.text, |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 362 | filetypes=self.filetypes) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 363 | filename = self.opendialog.show(initialdir=dir, initialfile=base) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 364 | return filename |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 365 | |
| 366 | def defaultfilename(self, mode="open"): |
| 367 | if self.filename: |
| 368 | return os.path.split(self.filename) |
Kurt B. Kaiser | d2f4861 | 2003-06-05 02:34:04 +0000 | [diff] [blame] | 369 | elif self.dirname: |
| 370 | return self.dirname, "" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 371 | else: |
| 372 | try: |
| 373 | pwd = os.getcwd() |
Andrew Svetlov | 786fbd8 | 2012-12-17 19:51:15 +0200 | [diff] [blame] | 374 | except OSError: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 375 | pwd = "" |
| 376 | return pwd, "" |
| 377 | |
| 378 | def asksavefile(self): |
| 379 | dir, base = self.defaultfilename("save") |
| 380 | if not self.savedialog: |
Ned Deily | c65ef9b | 2012-07-09 18:16:11 -0700 | [diff] [blame] | 381 | self.savedialog = tkFileDialog.SaveAs( |
Terry Jan Reedy | 3be2e54 | 2015-09-25 22:22:55 -0400 | [diff] [blame] | 382 | parent=self.text, |
Ned Deily | c65ef9b | 2012-07-09 18:16:11 -0700 | [diff] [blame] | 383 | filetypes=self.filetypes, |
| 384 | defaultextension=self.defaultextension) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 385 | filename = self.savedialog.show(initialdir=dir, initialfile=base) |
Martin v. Löwis | 307021f | 2005-11-27 16:59:04 +0000 | [diff] [blame] | 386 | return filename |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 387 | |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 388 | def updaterecentfileslist(self,filename): |
Kurt B. Kaiser | bfed346 | 2002-12-14 04:38:51 +0000 | [diff] [blame] | 389 | "Update recent file list on all editor windows" |
Kurt B. Kaiser | 3a4e24b | 2007-08-22 18:06:14 +0000 | [diff] [blame] | 390 | if self.editwin.flist: |
| 391 | self.editwin.update_recent_files_list(filename) |
Chui Tey | 993e81a | 2002-11-04 03:11:10 +0000 | [diff] [blame] | 392 | |
Terry Jan Reedy | 2733618 | 2015-05-14 18:10:50 -0400 | [diff] [blame] | 393 | def _io_binding(parent): # htest # |
Terry Jan Reedy | bbdc065 | 2015-10-30 02:47:06 -0400 | [diff] [blame] | 394 | from tkinter import Toplevel, Text |
Terry Jan Reedy | bbdc065 | 2015-10-30 02:47:06 -0400 | [diff] [blame] | 395 | |
| 396 | root = Toplevel(parent) |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 397 | root.title("Test IOBinding") |
Terry Jan Reedy | a748032 | 2016-07-10 17:28:10 -0400 | [diff] [blame] | 398 | x, y = map(int, parent.geometry().split('+')[1:]) |
| 399 | root.geometry("+%d+%d" % (x, y + 175)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 400 | class MyEditWin: |
| 401 | def __init__(self, text): |
| 402 | self.text = text |
| 403 | self.flist = None |
| 404 | self.text.bind("<Control-o>", self.open) |
Terry Jan Reedy | b31a284 | 2016-01-27 11:51:50 -0500 | [diff] [blame] | 405 | self.text.bind('<Control-p>', self.print) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 406 | self.text.bind("<Control-s>", self.save) |
Terry Jan Reedy | b31a284 | 2016-01-27 11:51:50 -0500 | [diff] [blame] | 407 | self.text.bind("<Alt-s>", self.saveas) |
| 408 | self.text.bind('<Control-c>', self.savecopy) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 409 | def get_saved(self): return 0 |
| 410 | def set_saved(self, flag): pass |
| 411 | def reset_undo(self): pass |
| 412 | def open(self, event): |
| 413 | self.text.event_generate("<<open-window-from-file>>") |
Terry Jan Reedy | b31a284 | 2016-01-27 11:51:50 -0500 | [diff] [blame] | 414 | def print(self, event): |
| 415 | self.text.event_generate("<<print-window>>") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 416 | def save(self, event): |
| 417 | self.text.event_generate("<<save-window>>") |
Terry Jan Reedy | b31a284 | 2016-01-27 11:51:50 -0500 | [diff] [blame] | 418 | def saveas(self, event): |
| 419 | self.text.event_generate("<<save-window-as-file>>") |
| 420 | def savecopy(self, event): |
| 421 | self.text.event_generate("<<save-copy-of-window-as-file>>") |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 422 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 423 | text = Text(root) |
| 424 | text.pack() |
| 425 | text.focus_set() |
| 426 | editwin = MyEditWin(text) |
Terry Jan Reedy | 2733618 | 2015-05-14 18:10:50 -0400 | [diff] [blame] | 427 | IOBinding(editwin) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 428 | |
| 429 | if __name__ == "__main__": |
Terry Jan Reedy | ea3dc80 | 2018-06-18 04:47:59 -0400 | [diff] [blame] | 430 | from unittest import main |
| 431 | main('idlelib.idle_test.test_iomenu', verbosity=2, exit=False) |
Terry Jan Reedy | 7c15341 | 2016-06-26 17:48:02 -0400 | [diff] [blame] | 432 | |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 433 | from idlelib.idle_test.htest import run |
| 434 | run(_io_binding) |