diff options
author | Kurt B. Kaiser <kbk@shore.net> | 2003-05-10 00:49:56 +0000 |
---|---|---|
committer | Kurt B. Kaiser <kbk@shore.net> | 2003-05-10 00:49:56 +0000 |
commit | a053f3388791208bfccb94c9407ffa3f5f79ecc8 (patch) | |
tree | 2f5bb0ae78394f5eb9d3e0a5f1b3b5a9481da4f7 /Lib/idlelib/IOBinding.py | |
parent | 57bfe5dc5a4873026679fb911939beb69e35a9e8 (diff) | |
download | cpython-git-a053f3388791208bfccb94c9407ffa3f5f79ecc8.tar.gz |
[ SF 710733 - Martin v. Loewis] Improving source encoding dialog
M IOBinding.py
M config-main.def
M configDialog.py
Diffstat (limited to 'Lib/idlelib/IOBinding.py')
-rw-r--r-- | Lib/idlelib/IOBinding.py | 90 |
1 files changed, 77 insertions, 13 deletions
diff --git a/Lib/idlelib/IOBinding.py b/Lib/idlelib/IOBinding.py index 6f46a60d78..5d7a221cfb 100644 --- a/Lib/idlelib/IOBinding.py +++ b/Lib/idlelib/IOBinding.py @@ -13,6 +13,8 @@ import tempfile import tkFileDialog import tkMessageBox import re +from Tkinter import * +from SimpleDialog import SimpleDialog from configHandler import idleConf @@ -67,6 +69,52 @@ encoding = encoding.lower() coding_re = re.compile("coding[:=]\s*([-\w_.]+)") +class EncodingMessage(SimpleDialog): + "Inform user that an encoding declaration is needed." + def __init__(self, master, enc): + self.should_edit = False + + self.root = top = Toplevel(master) + top.bind("<Return>", self.return_event) + top.bind("<Escape>", self.do_ok) + top.protocol("WM_DELETE_WINDOW", self.wm_delete_window) + top.wm_title("I/O Warning") + top.wm_iconname("I/O Warning") + self.top = top + + l1 = Label(top, + text="Non-ASCII found, yet no encoding declared. Add a line like") + l1.pack(side=TOP, anchor=W) + l2 = Entry(top, font="courier") + l2.insert(0, "# -*- coding: %s -*-" % enc) + # For some reason, the text is not selectable anymore if the + # widget is disabled. + # l2['state'] = DISABLED + l2.pack(side=TOP, anchor = W, fill=X) + l3 = Label(top, text="to your file\n" + "Choose OK to save this file as %s\n" + "Edit your general options to silence this warning" % enc) + l3.pack(side=TOP, anchor = W) + + buttons = Frame(top) + buttons.pack(side=TOP, fill=X) + # Both return and cancel mean the same thing: do nothing + self.default = self.cancel = 0 + b1 = Button(buttons, text="Ok", default="active", + command=self.do_ok) + b1.pack(side=LEFT, fill=BOTH, expand=1) + b2 = Button(buttons, text="Edit my file", + command=self.do_edit) + b2.pack(side=LEFT, fill=BOTH, expand=1) + + self._set_transient(master) + + def do_ok(self): + self.done(0) + + def do_edit(self): + self.done(1) + def coding_spec(str): """Return the encoding declaration according to PEP 263. @@ -368,18 +416,35 @@ class IOBinding: return BOM_UTF8 + chars.encode("utf-8") # Nothing was declared, and we had not determined an encoding # on loading. Recommend an encoding line. - try: - chars = chars.encode(encoding) - enc = encoding - except UnicodeError: - chars = BOM_UTF8 + chars.encode("utf-8") - enc = "utf-8" - tkMessageBox.showerror( - "I/O Error", - "Non-ASCII found, yet no encoding declared. Add a line like\n" - "# -*- coding: %s -*- \nto your file" % enc, - master = self.text) - return chars + config_encoding = idleConf.GetOption("main","EditorWindow", + "encoding") + if config_encoding == 'utf-8': + # User has requested that we save files as UTF-8 + return BOM_UTF8 + chars.encode("utf-8") + ask_user = True + try: + chars = chars.encode(encoding) + enc = encoding + if config_encoding == 'locale': + ask_user = False + except UnicodeError: + chars = BOM_UTF8 + chars.encode("utf-8") + enc = "utf-8" + if not ask_user: + return chars + dialog = EncodingMessage(self.editwin.top, enc) + dialog.go() + if dialog.num == 1: + # User asked us to edit the file + encline = "# -*- coding: %s -*-\n" % enc + firstline = self.text.get("1.0", "2.0") + if firstline.startswith("#!"): + # Insert encoding after #! line + self.text.insert("2.0", encline) + else: + self.text.insert("1.0", encline) + return self.encode(self.text.get("1.0", "end-1c")) + return chars def fixlastline(self): c = self.text.get("end-2c") @@ -487,5 +552,4 @@ def test(): root.mainloop() if __name__ == "__main__": - from Tkinter import * test() |