Created
April 23, 2013 08:49
-
-
Save jeewood/5441897 to your computer and use it in GitHub Desktop.
gbk encoding support improved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sublime | |
import sublime_plugin | |
import os | |
import urllib | |
TEMP_PATH = os.path.join(os.getcwd(), 'tmp') | |
PRESTR = '(utf8)' | |
SEPERATOR = ' ++ ' | |
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') | |
file_name = view.file_name().encode('utf-8') | |
tmp_file = os.path.dirname(file_name) + '\\' + PRESTR + SEPERATOR + urllib.quote_plus(os.path.basename(file_name)) | |
# sublime.message_dialog(tmp_file) | |
f = file(tmp_file, 'w') | |
f.write(text.encode('utf8')) | |
f.close() | |
window = sublime.active_window() | |
v = window.find_open_file(tmp_file) | |
if not v: | |
window.open_file(tmp_file) | |
sublime.status_message('gbk encoding detected, open with utf8.') | |
def saveWithEncoding(view, file_name=None, encoding='gbk'): | |
if not file_name: | |
file_name = view.file_name() | |
reg_all = sublime.Region(0, view.size()) | |
text = view.substr(reg_all).encode(encoding) | |
gbk = file(file_name, 'w') | |
gbk.write(text) | |
gbk.close() | |
class EventListener(sublime_plugin.EventListener): | |
def on_load(self, view): | |
gbk2utf8(view) | |
def on_close(self, view): | |
parts = view.file_name().split(SEPERATOR) | |
if len(parts) > 1 and os.path.basename(parts[0]) == PRESTR: | |
file_name = os.path.dirname(parts[0]) + '\\' + parts[1] | |
saveWithEncoding(view, file_name) | |
os.remove(view.file_name()) | |
def on_post_save(self, view): | |
parts = view.file_name().split(SEPERATOR) | |
if len(parts) > 1 and os.path.basename(parts[0]) == PRESTR: | |
file_name = os.path.dirname(parts[0]) + '\\' + parts[1] | |
saveWithEncoding(view, file_name) | |
class SaveWithGbkCommand(sublime_plugin.TextCommand): | |
def __init__(self, view): | |
self.view = view | |
def run(self, edit): | |
file_name = self.view.file_name() | |
if not file_name: | |
return | |
parts = file_name.split(SEPERATOR) | |
if len(parts) > 1 and os.path.basename(parts[0]) == PRESTR: | |
file_name = os.path.dirname(parts[0]) + '\\' + parts[1] | |
saveWithEncoding(self.view, file_name) | |
sublime.active_window().run_command('close') | |
sublime.active_window().open_file(self.view.file_name()) | |
else: | |
sublime.active_window().run_command('save') | |
class SaveWithUtf8Command(sublime_plugin.TextCommand): | |
def __init__(self, view): | |
self.view = view | |
def run(self, edit): | |
file_name = self.view.file_name() | |
if not file_name: | |
return | |
parts = file_name.split(SEPERATOR) | |
if len(parts) > 1 and os.path.basename(parts[0]) == PRESTR: | |
file_name = os.path.dirname(parts[0]) + '\\' + parts[1] | |
saveWithEncoding(self.view, file_name, 'utf-8') | |
sublime.active_window().run_command('close') | |
sublime.active_window().open_file(file_name) | |
else: | |
sublime.active_window().run_command('save') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to use it