星期一, 8月 25, 2014

Sublime Text 3 中文輸入問題

Sublime Text 很好用,但是我一直受無法中文輸入所苦。之前想說算了,就強迫自己盡量都用英文,但是現在要搞多國語系,不能輸入中文就麻煩了。所以還是想辦法把他搞定...


網路上大都是 Sublime Text 2 的教學文章,我綜合參考了以下幾篇文章與步驟,終於成功:

    • 安裝 Package Control 步驟:
      1. Ctrl + ` 叫出 Sublime Console 指令輸入畫面
      2. 將以下安裝指令貼上按 ENTER 執行
      • import urllib2,os; pf='Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler( ))); open( os.path.join( ipp, pf), 'wb' ).write( urllib2.urlopen( 'http://sublime.wbond.net/' +pf.replace( ' ','%20' )).read()); print( 'Please restart Sublime Text to finish installation')
      • 執行上述指令會出現錯誤:No module named ‘urllib2′,所以我又參考了文章「Sublime Text 3 安装 Package Control」,修正程式碼如下:
      • import urllib.request,os; pf='Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler( ))); open( os.path.join( ipp, pf), 'wb' ).write( urllib.request.urlopen( 'http://sublime.wbond.net/' +pf.replace( ' ','%20' )).read()); print( 'Please restart Sublime Text to finish installation')
    • 重新打開 Sublime 在選單可以看到:Preferences / Package Control
    • 接下來就能以 Package Control 安裝 InputHelper 套件。
      • InputHelper 專案首頁:https://github.com/xgenvn/InputHelper
      • 安裝 InputHelper 步驟:
        • 打開 Preferences / Package Control 彈出選單,選擇「Add Repository」設定新的套件來源。
        • 輸入「https://github.com/xgenvn/InputHelper」並按下 ENTER 確認。
        • 新增來源之後,再重新打開 Preferences / Package Control 選單,選擇「Install Package」安裝新套件。
        • 輸入套件關鍵字「InputHelper」,只要打完 Inpu ... 就能看到篩選後的結果,選擇「InputHelper」開始安裝動作。*雖然 Package Control 預設的套件清單(Repository)已經內建 InputHelper,但是該檔案下載位址有誤,會造成安裝失敗,所以上面才必須手動添加套件來源。
      • 修正問題:
        • 雖然到這邊已經裝好 InputHelper,但實際上還無法執行,因為自動安裝的 InputHelper 有一個 linux_text_input_gui.py 檔案權限設定不正確。
        • 使用 chmod 指令手動修正:chmod a+x ~/.config/sublime-text-3/Packages/InputHelper/lib/linux_text_input_gui.py
        • 如果一切順利,重新啟動 Sublime Text 之後,就能使用「Ctrl + Shift + Z」叫出 InputHelper 輸入視窗,輸入文字後按下 ENTER 就能將訊息貼回編輯器。
  • 但事實上就是不會那麼順利,因為在上面步驟提到的「修正問題」中,我根本沒看到 Packages 下有 InputHelper 這個目錄。所以我後來又參考了這篇文章:「【笔记】CentOS安装Sublime Text 3」,按照其中的「附录:InputHelper插件问题,“Ctrl+Shift+Z"调不出对话框,解决手顺。」,我很幸運地在 Step 4 就可以輸入中文了... (以下節錄該步驟)

Step 1: “Ctrl+`“调控制台,“Ctrl+Shift+Z"看输出:
?
1
2
3
4
5
6
7
Traceback (most recent call last):
  File "/opt/sublime_text/sublime_plugin.py", line 549, in run_
    return self.run(edit)
  File "inputhelper in /home/join/.config/sublime-text-3/Installed Packages/InputHelper.sublime-package", line 21, in run
  File "X/subprocess.py", line 818, in __init__
  File "X/subprocess.py", line 1416, in _execute_child
FileNotFoundError: [Errno 2] No such file or directory: '/home/join/.config/sublime-text-3/Packages/InputHelper/lib/linux_text_input_gui.py'
Step 2: 解压"Installed Packages/“下"InputHelper.sublime-package"到"Packages/“,并删除原包,重启Sublime。以直接用源代码。(參考路徑:/home/username/.config/sublime-text-3/Installed Packages/)
Step 3: “Ctrl+Shift+Z"再看输出:
?
1
2
3
4
5
6
7
8
Traceback (most recent call last):
  File "/opt/sublime_text/sublime_plugin.py", line 549, in run_
    return self.run(edit)
  File "/home/join/.config/sublime-text-3/Packages/InputHelper/inputhelper.py", line 21, in run
    proc = subprocess.Popen(args, stdout=subprocess.PIPE)
  File "X/subprocess.py", line 818, in __init__
  File "X/subprocess.py", line 1416, in _execute_child
PermissionError: [Errno 13] Permission denied
Step 4: 修改"inputhelper.py"添加输出,看Popen了什么。为"InputHelper/lib/linux_text_input_gui.py”。继而查看其权限为”-rw-rw-r–“,如下直接改为777:
?
1
chmod 777 /home/join/.config/sublime-text-3/Packages/InputHelper/lib/linux_text_input_gui.py
Step 5: 继续"Ctrl+Shift+Z”,没效果。直接运行"linux_text_input_gui.py”,发现:
?
1
ImportError: No module named pygtk
Step 6: “pip install PyGTK"安装pygtk,但:
?
1
CompressionError: bz2 module is not available
Step 7: 源于编译Python2.7时没bz2模块,make时会提示缺失"bz2”。编译前安装有bzip2即可:
?
1
2
# 需要bzip2,不论如何,执行下行命令
yum install bzip2 bzip2-devel -y
重编译安装,见CentOS升级Python2.7及安装pip
Step 8: 重新"pip install PyGTK”,然后:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/PyGTK
Storing debug log for failure in /root/.pip/pip.log
# 直接从源码安装
wget http://pypi.douban.com/packages/source/P/PyGTK/pygtk-2.24.0.tar.bz2
tar jxvf pygtk-2.24.0.tar.bz2
cd pygtk-2.24.0
python setup.py install
# 一样提示错误:
# ********************************************************************
# * Building PyGTK using distutils is only supported on windows. *
# * To build PyGTK in a supported way, read the INSTALL file.    *
# ********************************************************************
vi INSTALL
# 看来要重编
./configure
# issue: configure: error: Package requirements (pygobject-2.0 >= 2.21.3) were not met: ... No package 'pygobject-2.0' found ...
# 缺少pygobject
# yum list pygobject*
yum install pygobject2 pygobject2-devel -y
# issue: configure: error: Package requirements (pygobject-2.0 >= 2.21.3) were not met:
# Requested 'pygobject-2.0 >= 2.21.3' but version of PyGObject is 2.20.0
# pygobject版本不够
rpm -e pygobject2-devel
# 下个高版本的PyGObject
wget http://ftp.gnome.org/pub/GNOME/sources/pygobject/3.10/pygobject-3.10.2.tar.xz
tar Jxvf pygobject-3.10.2.tar.xz
cd pygobject-3.10.2
Step 9: 感觉很折腾,应该是由于目前为Python2.7才依赖了高版本的PyGObject,所以:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 而原先Python2.6还装着,所以:
vi /home/join/.config/sublime-text-3/Packages/InputHelper/lib/linux_text_input_gui.py
# 把第一行改为"#!/usr/bin/env python2.6.6"
# 然后"Ctrl+Shift+Z"果真弹出了对话框,但Sublime UI阻塞,未响应。
cd /home/join/.config/sublime-text-3/Packages/InputHelper/lib/
python2.6.6 linux_text_input_gui.py
# 输入内容后回车,看到如下错误:
# AttributeError: 'gtk.Entry' object has no attribute 'get_buffer'
# Traceback (most recent call last):
#   File "linux_text_input_gui.py", line 14, in destroy
#     self.print_text()
#   File "linux_text_input_gui.py", line 9, in print_text
#     buffer = self.textInput.get_buffer()
# AttributeError: 'gtk.Entry' object has no attribute 'get_buffer'
# 给"linux_text_input_gui.py""SimpleTextInput::print_text()"添加打印,看"self.textInput"的属性。直接有"get_text",所以做如下修改:
# def print_text(self):
#     # buffer = self.textInput.get_buffer()
#     # print(buffer.get_text())
#     print(self.textInput.get_text())
# 问题解决。pygtk版本不同的原因么?

1 意見:

Unknown 提到...

Good Info. My guess is Python's unicode problem. When you go through https://sublime.wbond.net/ and search for "CJK", it will guide you to the problem subl has. Or...if you're lazy, you can check out this. https://github.com/seanliang/ConvertToUTF8

However, since you already solve the issue, you can just keep that as ref.

It has been a while I didn't use Sublime Text cause I already get used to Vim. And I didn't realize Sublime can't input Chinese, Ha..