Ubuntu14.04下Sublime Text 3解决无法输入中文
在Ubuntu 14.04中安装了SublimeText 3之后发现既然不支持输入中文,于是在网上搜罗一下,发现很多人遇到了同样的问题,但是解决办法大该就只有一个。下面根据自身的安装及解决办法总结如下:
1. SublimeText 3的安装
安装方式有多种,本文所描述的是从官网下载与自己系统相匹配的.deb 文件,下载后双击即可安装。
2. 相关依赖软件的安装
sudo apt-get install build-essential libgtk2.0-dev
3. 支持中文输入
拷贝如下代码到文件sublime-imfix.c文件中,该文件需要自己创建,放在主文件夹下(home文件夹)
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 gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC 6 LD_PRELOAD=./libsublime-imfix.so sublime_text 7 */ 8 #include <gtk/gtk.h> 9 #include <gdk/gdkx.h> 10 typedef GdkSegment GdkRegionBox; 11 12 struct _GdkRegion 13 { 14 long size; 15 long numRects; 16 GdkRegionBox *rects; 17 GdkRegionBox extents; 18 }; 19 20 GtkIMContext *local_context; 21 22 void 23 gdk_region_get_clipbox (const GdkRegion *region, 24 GdkRectangle *rectangle) 25 { 26 g_return_if_fail (region != NULL); 27 g_return_if_fail (rectangle != NULL); 28 29 rectangle->x = region->extents.x1; 30 rectangle->y = region->extents.y1; 31 rectangle->width = region->extents.x2 - region->extents.x1; 32 rectangle->height = region->extents.y2 - region->extents.y1; 33 GdkRectangle rect; 34 rect.x = rectangle->x; 35 rect.y = rectangle->y; 36 rect.width = 0; 37 rect.height = rectangle->height; 38 //The caret width is 2; 39 //Maybe sometimes we will make a mistake, but for most of the time, it should be the caret. 40 if(rectangle->width == 2 && GTK_IS_IM_CONTEXT(local_context)) { 41 gtk_im_context_set_cursor_location(local_context, rectangle); 42 } 43 } 44 45 //this is needed, for example, if you input something in file dialog and return back the edit area 46 //context will lost, so here we set it again. 47 48 static GdkFilterReturn event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer im_context) 49 { 50 XEvent *xev = (XEvent *)xevent; 51 if(xev->type == KeyRelease && GTK_IS_IM_CONTEXT(im_context)) { 52 GdkWindow * win = g_object_get_data(G_OBJECT(im_context),"window"); 53 if(GDK_IS_WINDOW(win)) 54 gtk_im_context_set_client_window(im_context, win); 55 } 56 return GDK_FILTER_CONTINUE; 57 } 58 59 void gtk_im_context_set_client_window (GtkIMContext *context, 60 GdkWindow *window) 61 { 62 GtkIMContextClass *klass; 63 g_return_if_fail (GTK_IS_IM_CONTEXT (context)); 64 klass = GTK_IM_CONTEXT_GET_CLASS (context); 65 if (klass->set_client_window) 66 klass->set_client_window (context, window); 67 68 if(!GDK_IS_WINDOW (window)) 69 return; 70 g_object_set_data(G_OBJECT(context),"window",window); 71 int width = gdk_window_get_width(window); 72 int height = gdk_window_get_height(window); 73 if(width != 0 && height !=0) { 74 gtk_im_context_focus_in(context); 75 local_context = context; 76 } 77 gdk_window_add_filter (window, event_filter, context); 78 }
打开终端,输入如下命令:
gcc -shared -o libsublime-imfix.so sublime-imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
最后在当前目录下得到libsublime-imfix.so这个共享库。再将libsublime-imfix.so拷贝到系统库的默认路径中:
sudo cp libsublime-imfix.so /usr/lib/
修改/usr/share/applications/sublime_text.desktop文件
sudo gedit /usr/share/applications/sublime_text.desktop
打开后将Exec=/opt/sublime_text/sublime_text %F修改为
Exec=bash -c 'LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text' %F
将Exec=/opt/sublime_text/sublime_text -n修改为
Exec=bash -c 'LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text' -n
保存关闭,重新打开SublimeText 3就可以支持中文输入了。到这里默认已经装好了中文输入法(搜狗输入法linux版)。
http://www.cnblogs.com/moon1992/