在 Ubuntu 里安裝了 Sublime 卻不能輸入中文?
這可不好。
怎么辦呢?
Follow Me!
有 GitHub 賬號(hào)的,可以從 https://github.com/YoungZHU/sublime-imfix.git 獲取。
或者直接復(fù)制以下代碼:
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 _GdkRegion14 {15 long size;16 long numRects;17 GdkRegionBox *rects;18 GdkRegionBox extents;19 };20 21 GtkIMContext *local_context;22 23 void24 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 area47 //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 }
注:假設(shè) sublime-imfix.c 文件被放置到了 home (~)目錄下。
放到哪個(gè)目錄都沒關(guān)系,只要后面編譯該文件時(shí)找對(duì)目錄就行。
終端下輸入如下命令:
sudo apt-get install build-essential libgtk2.0-dev
終端下輸入以下命令進(jìn)行編譯:
gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
編譯好后,當(dāng)前目錄多了一個(gè)名為: libsublime-imfix.so 的庫(kù)文件。
注:該命令需要在 sublime-imfix.c 所在目錄執(zhí)行。
按照第一步的說明,本例在 home(~)目錄下 執(zhí)行。
終端下輸入以下命令:
mv libsublime-imfix.so $SUBLIME_HOME/
注:
1)該命令需要在 libsublime-imfix.so 所在目錄執(zhí)行,本例中為 home(~)
1) $SUBLIME_HOME 表示 sublime 的安裝目錄
2) 直接放到 sublime 的安裝目錄,或者 放到其安裝目錄下的 lib 目錄($SUBLIME_HOME/lib)均可。只要后面引用文件時(shí)注意目錄
終端下輸入以下命令啟動(dòng) sublime:
LD_PRELOAD=./libsublime-imfix.so ./sublime_text
注:該命令需要在 sublime 的安裝目錄下執(zhí)行
否則,需要將命令中的兩個(gè)文件換成絕對(duì)路徑
另,有 GitHub 賬號(hào)的,可以從 https://github.com/YoungZHU/sublime-imfix.git 查看。
如果需要?jiǎng)?chuàng)建快捷方式,或鎖定到啟動(dòng)器的(Ubuntu),和上面介紹的第5步一樣,需要指定庫(kù)文件。
以筆者的Ubuntu為例,將其鎖定到左側(cè)的啟動(dòng)器,會(huì)有一個(gè) sublime_text.desktop 的文件,目錄: ~/.local/share/applications/
文件內(nèi)容如下:
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=false10 StartupWMClass=sublime_text11 OnlyShowIn=Unity;12 X-UnityGenerated=true
主要的改動(dòng)在第 8 行。
這樣,不用從命令行啟動(dòng) sublime 也可以輸入中文了。
聯(lián)系客服