├── README.md ├── Main.sublime-menu └── sublime_gbk.py /README.md: -------------------------------------------------------------------------------- 1 | ### With this plugin you can: 2 | 3 | 1. Open a GBK File 4 | 5 | 1. Save file with GBK encoding 6 | 7 | 1. Change file encoding from utf8 to GBK or GBK to utf8 -------------------------------------------------------------------------------- /Main.sublime-menu: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "file", 4 | "children": 5 | [ 6 | { 7 | "caption": "GBK or UTF8", 8 | "id": "extra_encoding", 9 | "children": 10 | [ 11 | { 12 | "caption": "Save with GBK", 13 | "command": "save_with_gbk" 14 | }, 15 | { 16 | "caption": "Save with UTF8", 17 | "command": "save_with_utf8" 18 | } 19 | ] 20 | } 21 | ] 22 | } 23 | ] 24 | -------------------------------------------------------------------------------- /sublime_gbk.py: -------------------------------------------------------------------------------- 1 | #coding: utf8 2 | 3 | import sublime, sublime_plugin 4 | import os, re 5 | 6 | def gbk2utf8(view): 7 | try: 8 | reg_all = sublime.Region(0, view.size()) 9 | gbk = view.substr(reg_all).encode('gbk') 10 | except: 11 | gbk = open(view.file_name(), 'rb').read() 12 | text = gbk.decode('gbk') 13 | 14 | tmp_file = u"%s.dump"%view.file_name() 15 | ver = int(sublime.version()) 16 | 17 | if ver >= 3000: 18 | f = open(tmp_file, 'w', encoding='utf-8') 19 | f.write(text) 20 | f.close() 21 | else: 22 | f = open(tmp_file, 'w') 23 | f.write(text.encode('utf8')) 24 | f.close() 25 | 26 | window = sublime.active_window() 27 | 28 | tmp_view = window.open_file(tmp_file) 29 | 30 | if not tmp_view: 31 | tmp_view = window.open_file(tmp_file) 32 | 33 | tmp_view.set_syntax_file(view.settings().get('syntax')) 34 | window.focus_view(view) 35 | window.run_command('close') 36 | window.focus_view(tmp_view) 37 | sublime.status_message('gbk encoding detected, open with utf8.') 38 | 39 | def saveWithEncoding(view, file_name = None, encoding = 'gbk'): 40 | if not file_name: 41 | file_name = view.file_name() 42 | reg_all = sublime.Region(0, view.size()) 43 | text = view.substr(reg_all).encode(encoding) 44 | gbk = open(file_name, 'wb') 45 | gbk.write(text) 46 | gbk.close() 47 | 48 | class EventListener(sublime_plugin.EventListener): 49 | def on_load(self, view): 50 | gbk2utf8(view) 51 | 52 | def on_post_save(self, view): 53 | if ".dump" in view.file_name(): 54 | file_name = view.file_name()[:-5] 55 | saveWithEncoding(view, file_name) 56 | 57 | def on_close(self,view): 58 | if ".dump" in view.file_name(): 59 | os.remove(view.file_name()) 60 | 61 | 62 | class SaveWithGbkCommand(sublime_plugin.TextCommand): 63 | def __init__(self, view): 64 | self.view = view 65 | def run(self, edit): 66 | file_name = self.view.file_name() 67 | 68 | if(not file_name): 69 | return 70 | 71 | if ".dump" not in self.view.file_name(): 72 | saveWithEncoding(self.view) 73 | sublime.active_window().run_command('close') 74 | sublime.active_window().open_file(self.view.file_name()) 75 | else: 76 | sublime.active_window().run_command('save') 77 | 78 | class SaveWithUtf8Command(sublime_plugin.TextCommand): 79 | def __init__(self, view): 80 | self.view = view 81 | def run(self, edit): 82 | file_name = self.view.file_name() 83 | 84 | if(not file_name): 85 | return 86 | 87 | if ".dump" in self.view.file_name(): 88 | file_name = self.view.file_name()[:-5] 89 | saveWithEncoding(self.view, file_name, 'utf-8') 90 | sublime.active_window().run_command('close') 91 | sublime.active_window().open_file(file_name) 92 | else: 93 | sublime.active_window().run_command('save') 94 | 95 | --------------------------------------------------------------------------------