Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 71f2433c698015228bd000491901b72e8bba22d5 @akira-cn akira-cn committed Feb 23, 2012
Showing with 79 additions and 0 deletions.
  1. +23 −0 Main.sublime-menu
  2. +7 −0 README.md
  3. +49 −0 sublime_gbk.py
  4. BIN sublime_gbk.pyc
23 Main.sublime-menu
@@ -0,0 +1,23 @@
+[
+ {
+ "id": "file",
+ "children":
+ [
+ {
+ "caption": "GBK or UTF8",
+ "id": "extra_encoding",
+ "children":
+ [
+ {
+ "caption": "Save as GBK",
+ "command": "save_as_gbk"
+ },
+ {
+ "caption": "Save as UTF8",
+ "command": "save_as_utf8"
+ }
+ ]
+ }
+ ]
+ }
+]
7 README.md
@@ -0,0 +1,7 @@
+### With this plugin you can:
+
+1. Open a GBK File
+
+1. Save file with GBK encoding
+
+1. Change file encoding from utf8 to GBK or GBK to utf8
49 sublime_gbk.py
@@ -0,0 +1,49 @@
+import sublime, sublime_plugin
+
+def gbk2utf8(view):
+ try:
+ reg_all = sublime.Region(0, view.size())
+ gbk = view.substr(reg_all).encode('gbk')
+ except:
+ gbk = file(view.file_name()).read()
+ text = gbk.decode('gbk')
+ view.settings().set('encoding', 'gbk')
+ edit = view.begin_edit()
+ view.replace(edit, reg_all, text)
+ view.end_edit(edit)
+ view.set_encoding('utf-8')
+ sublime.status_message('gbk encoding detected, open with utf8.')
+
+def saveAsGbk(view):
+ reg_all = sublime.Region(0, view.size())
+ text = view.substr(reg_all).encode('gbk')
+ gbk = file(view.file_name(), 'w')
+ gbk.write(text)
+ gbk.close()
+
+class EventListener(sublime_plugin.EventListener):
+ def on_modified(self, view):
+ if(view.settings().get('user_deactivated')):
+ gbk2utf8(view)
+ def on_deactivated(self, view):
+ view.settings().set('user_deactivated', True)
+ def on_load(self, view):
+ gbk2utf8(view)
+ def on_post_save(self, view):
+ if(view.settings().get('encoding') == 'gbk'):
+ saveAsGbk(view)
+
+
+class SaveAsGbkCommand(sublime_plugin.TextCommand):
+ def __init__(self, view):
+ self.view = view
+ def run(self, edit):
+ self.view.settings().set('encoding', 'gbk')
+ sublime.active_window().run_command('save')
+
+class SaveAsUtf8Command(sublime_plugin.TextCommand):
+ def __init__(self, view):
+ self.view = view
+ def run(self, edit):
+ self.view.settings().set('encoding', None)
+ sublime.active_window().run_command('save')
BIN sublime_gbk.pyc
Binary file not shown.

0 comments on commit 71f2433

Please sign in to comment.