在 Ubuntu 里安裝了 Sublime 卻不能輸入中文?
這可不好。
怎么辦呢?
Follow Me!
1 獲得 sublime-imfix.c 文件
有 GitHub 賬號的,可以從 https://github.com/YoungZHU/sublime-imfix.git 獲取。
或者直接復制以下代碼:
1 /* 2 sublime-imfix.c 3 Use LD_PRELOAD to interpose some function to fix sublime input method support for linux. 4 By Cjacker Huang <jianzhong.huang at i-soft.com.cn> 5 6 gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC 7 LD_PRELOAD=./libsublime-imfix.so sublime_text 8 */ 9 #include <gtk/gtk.h> 10 #include <gdk/gdkx.h> 11 typedef GdkSegment GdkRegionBox; 12 13 struct _GdkRegion 14 { 15 long size; 16 long numRects; 17 GdkRegionBox *rects; 18 GdkRegionBox extents; 19 }; 20 21 GtkIMContext *local_context; 22 23 void 24 gdk_region_get_clipbox (const GdkRegion *region, 25 GdkRectangle *rectangle) 26 { 27 g_return_if_fail (region != NULL); 28 g_return_if_fail (rectangle != NULL); 29 30 rectangle->x = region->extents.x1; 31 rectangle->y = region->extents.y1; 32 rectangle->width = region->extents.x2 - region->extents.x1; 33 rectangle->height = region->extents.y2 - region->extents.y1; 34 GdkRectangle rect; 35 rect.x = rectangle->x; 36 rect.y = rectangle->y; 37 rect.width = 0; 38 rect.height = rectangle->height; 39 //The caret width is 2; 40 //Maybe sometimes we will make a mistake, but for most of the time, it should be the caret. 41 if(rectangle->width == 2 && GTK_IS_IM_CONTEXT(local_context)) { 42 gtk_im_context_set_cursor_location(local_context, rectangle); 43 } 44 } 45 46 //this is needed, for example, if you input something in file dialog and return back the edit area 47 //context will lost, so here we set it again. 48 49 static GdkFilterReturn event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer im_context) 50 { 51 XEvent *xev = (XEvent *)xevent; 52 if(xev->type == KeyRelease && GTK_IS_IM_CONTEXT(im_context)) { 53 GdkWindow * win = g_object_get_data(G_OBJECT(im_context),"window"); 54 if(GDK_IS_WINDOW(win)) 55 gtk_im_context_set_client_window(im_context, win); 56 } 57 return GDK_FILTER_CONTINUE; 58 } 59 60 void gtk_im_context_set_client_window (GtkIMContext *context, 61 GdkWindow *window) 62 { 63 GtkIMContextClass *klass; 64 g_return_if_fail (GTK_IS_IM_CONTEXT (context)); 65 klass = GTK_IM_CONTEXT_GET_CLASS (context); 66 if (klass->set_client_window) 67 klass->set_client_window (context, window); 68 69 if(!GDK_IS_WINDOW (window)) 70 return; 71 g_object_set_data(G_OBJECT(context),"window",window); 72 int width = gdk_window_get_width(window); 73 int height = gdk_window_get_height(window); 74 if(width != 0 && height !=0) { 75 gtk_im_context_focus_in(context); 76 local_context = context; 77 } 78 gdk_window_add_filter (window, event_filter, context); 79 }
注:假設 sublime-imfix.c 文件被放置到了 home (~)目錄下。
放到哪個目錄都沒關系,只要后面編譯該文件時找對目錄就行。
2 安裝依賴庫
終端下輸入如下命令:
sudo apt-get install build-essential libgtk2.0-dev
3 編譯
終端下輸入以下命令進行編譯:
gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
編譯好后,當前目錄多了一個名為: libsublime-imfix.so 的庫文件。
注:該命令需要在 sublime-imfix.c 所在目錄執行。
按照第一步的說明,本例在 home(~)目錄下 執行。
4 將編譯好的庫文件移到 sublime 的安裝目錄下
終端下輸入以下命令:
mv libsublime-imfix.so $SUBLIME_HOME/
注:
1)該命令需要在 libsublime-imfix.so 所在目錄執行,本例中為 home(~)
1) $SUBLIME_HOME 表示 sublime 的安裝目錄
2) 直接放到 sublime 的安裝目錄,或者 放到其安裝目錄下的 lib 目錄($SUBLIME_HOME/lib)均可。只要后面引用文件時注意目錄
5 啟動 sublime
終端下輸入以下命令啟動 sublime:
LD_PRELOAD=./libsublime-imfix.so ./sublime_text
注:該命令需要在 sublime 的安裝目錄下執行
否則,需要將命令中的兩個文件換成絕對路徑
另,有 GitHub 賬號的,可以從 https://github.com/YoungZHU/sublime-imfix.git 查看。
如果需要創建快捷方式,或鎖定到啟動器的(Ubuntu),和上面介紹的第5步一樣,需要指定庫文件。
以筆者的Ubuntu為例,將其鎖定到左側的啟動器,會有一個 sublime_text.desktop 的文件,目錄: ~/.local/share/applications/
文件內容如下:
1 [Desktop Entry] 2 Encoding=UTF-8 3 Version=1.0 4 Type=Application 5 Name=Sublime Text 2 6 Icon=/home/young/Software/Sublime/Icon/256x256/sublime_text.png 7 Path=/home/young/Software/Sublime 8 Exec=bash -c "LD_PRELOAD=/home/young/Software/Sublime/lib/libsublime-imfix.so /home/young/Software/Sublime/sublime_text" 9 StartupNotify=false 10 StartupWMClass=sublime_text 11 OnlyShowIn=Unity; 12 X-UnityGenerated=true
主要的改動在第 8 行。
這樣,不用從命令行啟動 sublime 也可以輸入中文了。