Fix a input method problem in Sublime Text 3

Sublime Text

Sublime Text 作为自己喜爱的跨平台的 The most sexy text editor,一直以来都很受欢迎,尤其是进入 2015 年以后,推翻了自己之前在版本 2 上「万年未更」的方式,3dev 版本更新的及其频繁。

事实上,在 Ubuntu 以及其他 Linux 环境下,Sublime text 3 默认是不支持基于 fctix 的搜狗输入法中文输入的,所以对中文开发者来说多多少少,会有些不便。不过有位来自中国的开发者 cjacker 编写了一个库文件,通过在启动 Sublime text 时预加载该库的方法,实现搜狗输入法的状态下的 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-essentiallibgtk2.0-dev
故需提前安装妥当:

sudo apt-get install build-essential libgtk2.0-dev

###解决方法:

####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

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);
}

####2.将其编译为共享库文件:

gcc -shared -o libsublime-imfix.so sublime-imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC

####3.找到该文件并在启动 Sublime text 时将其预加载:

LD_PRELOAD=./libsublime-imfix.so subl

(注:Sublime Text 3 的启动命令默认为 subl)

###Tips:预加载的几种方法:
对于不习惯命令行启动应用的人来说,每次启动 Sublime Text 时都要手动的在 Terminal 里将 libsublime-imfix 库进行预加载,未免有些麻烦。
可以通过两种方法将其解决:

  1. 修改图标连接的方式:vi /usr/share/applications/sublime_text.desktop 实现启动时的预加载
  2. 编辑 vi /usr/bin/subl 文件实现启时的预加载

对于情况1:
首先 libsublime-imfix.so 拷贝至系统库中:

cp libsublime-imfix.so /usr/lib/

之后打开.desktop 文件:

vi /usr/share/applications/sublime_text.desktop

sublime_text.desktop 文件内容如下:

[Desktop Entry]
Version=1.0
Type=Application
Name=Sublime Text
GenericName=Text Editor
Comment=Sophisticated text editor for code, markup and prose
Exec=/opt/sublime_text/sublime_text %F
Terminal=false
MimeType=text/plain;
Icon=sublime-text
Categories=TextEditor;Development;
StartupNotify=true
Actions=Window;Document;

[Desktop Action Window]
Name=New Window
Exec=/opt/sublime_text/sublime_text -n
OnlyShowIn=Unity;

[Desktop Action Document]
Name=New File
Exec=/opt/sublime_text/sublime_text --command new_file
OnlyShowIn=Unity;

对应的两处 Exec 分别将其修改为:
Old:

Exec=/opt/sublime_text/sublime_text %F

New:

Exec=bash -c 'LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text' %F

Old:

Exec=/opt/sublime_text/sublime_text -n

New:

Exec=bash -c 'LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text' -n

对于情况2:
首先把 libsublime-imfix.so 拷贝到相关目录,然后这里拷贝至 /usr/lib/ 目录下。之后,
编辑 vi /usr/bin/subl 文件:
文件中内容为:

#!/bin/sh
exec /opt/sublime_text/sublime_text "$@"

在 exec 一栏上进行相关修改,以 leozhang 的为例:

#!/bin/sh
LD_PRELOAD=/home/leozhang/.config/sublime-text-3/libsublime-imfix.so exec  /opt/sublime_text/sublime_text "$@"

保存并退出,之后在终端启动 subl 即可输入中文。

Sublime Text

无「民事行为能力」人慎点