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