Sublime Text 作为自己喜爱的跨平台的跨平台编辑器,一直以来都很受欢迎,尤其是进入 2015 年以后,推翻了在版本 2 上「万年不变」的更新方式,3dev 版本的更新及其频繁。
事实上,在 Ubuntu 以及其他 Linux 发行版下,Sublime text 3 默认是不支持搜狗输入法(基于 fctix)中文输入的,所以对中文开发者来说多多少少会有些不便。好在有位来自中国的开发者 cjacker 编写了一个库文件,通过在启动 Sublime text 时预加载该库的方法,实现搜狗输入法的中文输入。
This is a dirty fix but at least works. cursor position update also
supported.Use LD_PRELOAD to reimplement gtk_im_context_set_client_window and set
im focus in. use “gdk_region_get_clipbox” to catch the caret position.
(It’s really difficult to find which function can catch the
position….)Here I made a assumption that the caret width is always 2, since it is 2.
the height is the “font glyph height”.
步入正题:
Environment:
- OS:Ubuntu 15.04 (64-bit)
- Sublime Text 3 (Build 3083)
- Gcc version 4.9.2 (Ubuntu 4.9.2-10ubuntu13)
前期准备:
由于需要编译一个共享库,需要依赖到 build-essential
和 libgtk2.0-dev
所以需提前安装妥当:
1 | sudo apt-get install build-essential libgtk2.0-dev |
解决方法:
Step 1.保存如下代码为 sublime-imfix.c 文件
/*sublime-imfix.c
Use LD_PRELOAD to interpose some function to fix sublime input method support for linux.
By Cjacker Huang <jianzhong.huang at i-soft.com.cn>
gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
LD_PRELOAD=./libsublime-imfix.so sublime_text
*/
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
typedef GdkSegment GdkRegionBox;
struct _GdkRegion
{
long size;
long numRects;
GdkRegionBox *rects;
GdkRegionBox extents;
};
GtkIMContext *local_context;
void
gdk_region_get_clipbox (const GdkRegion *region,
GdkRectangle *rectangle)
{
g_return_if_fail (region != NULL);
g_return_if_fail (rectangle != NULL);
rectangle->x = region->extents.x1;
rectangle->y = region->extents.y1;
rectangle->width = region->extents.x2 - region->extents.x1;
rectangle->height = region->extents.y2 - region->extents.y1;
GdkRectangle rect;
rect.x = rectangle->x;
rect.y = rectangle->y;
rect.width = 0;
rect.height = rectangle->height;
//The caret width is 2;
//Maybe sometimes we will make a mistake, but for most of the time, it should be the caret.
if(rectangle->width == 2 && GTK_IS_IM_CONTEXT(local_context)) {
gtk_im_context_set_cursor_location(local_context, rectangle);
}
}
//this is needed, for example, if you input something in file dialog and return back the edit area
//context will lost, so here we set it again.
static GdkFilterReturn event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer im_context)
{
XEvent *xev = (XEvent *)xevent;
if(xev->type == KeyRelease && GTK_IS_IM_CONTEXT(im_context)) {
GdkWindow * win = g_object_get_data(G_OBJECT(im_context),"window");
if(GDK_IS_WINDOW(win))
gtk_im_context_set_client_window(im_context, win);
}
return GDK_FILTER_CONTINUE;
}
void gtk_im_context_set_client_window (GtkIMContext *context,
GdkWindow *window)
{
GtkIMContextClass *klass;
g_return_if_fail (GTK_IS_IM_CONTEXT (context));
klass = GTK_IM_CONTEXT_GET_CLASS (context);
if (klass->set_client_window)
klass->set_client_window (context, window);
if(!GDK_IS_WINDOW (window))
return;
g_object_set_data(G_OBJECT(context),"window",window);
int width = gdk_window_get_width(window);
int height = gdk_window_get_height(window);
if(width != 0 && height !=0) {
gtk_im_context_focus_in(context);
local_context = context;
}
gdk_window_add_filter (window, event_filter, context);
}
Step 2.编译共享库文件:
1 | gcc -shared -o libsublime-imfix.so sublime-imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC |
Step 3.找到该文件并在启动 Sublime text 时将其预加载:
1 | LD_PRELOAD=./libsublime-imfix.so subl |
(注:Sublime Text 3 使用的启动命令默认为 subl)
预加载的几种方法:
对于不习惯命令行启动应用的人来说,每次启动 Sublime Text 时都要手动的在 Terminal 里将 libsublime-imfix 库进行预加载,未免有些麻烦。
可以通过 2 种方法将其解决:
- 通过修改图标连接的方式:
vi /usr/share/applications/sublime_text.desktop
实现启动时的预加载 - 编辑
vi /usr/bin/subl
文件实现启时的预加载
第一种方法:
首先将 libsublime-imfix.so
拷贝至系统库中:
1 | cp libsublime-imfix.so /usr/lib/ |
之后打开 .desktop
文件:
1 | vi /usr/share/applications/sublime_text.desktop |
sublime_text.desktop 文件内容如下:
1 | [Desktop Entry] |
对应的两处 Exec
分别将其修改为:
Old:
1 | Exec=/opt/sublime_text/sublime_text %F |
New:
1 | Exec=bash -c 'LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text' %F |
Old:
1 | Exec=/opt/sublime_text/sublime_text -n |
New:
1 | Exec=bash -c 'LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text' -n |
第二种方法:
首先把 libsublime-imfix.so
拷贝到相关目录,然后这里拷贝至 /usr/lib/
目录下。之后编辑 vi /usr/bin/subl
文件。
文件中内容为:
1 | #!/bin/sh |
在 exec 一栏进行相关修改,替换实际的使用文件,这里以 leozhang 的为例:
1 | #!/bin/sh |
保存并退出,之后在终端启动 subl 即可输入中文。