Ubuntu安装sublime以及解决中文输入问题

昨天脑子抽了一下,更新了一下显卡驱动,不知道为什么,ubuntu就罢工了,搞了一天也搞不定,就只能重装了系统。 由于一直使用sublime,而sublime在ubuntu下中文输入有问题,网上有解决办法,但是不知为何,我按照这些教程来做,总不能完美解决。每次都要搞好久,索性写篇博文记录一下,省的忘记安装过程。

第一步:安装SublimeText2

我是使用PPA来安装的,因为在官网下载压缩包,虽然免安装,但是无法完美的设置命令行快捷键和桌面图标(我对ubuntu没太研究,能用就好)。

$ sudo add-apt-repository ppa:webupd8team/sublime-text-2
$ sudo apt-get update sudo apt-get install sublime-text (# or dev version # $sudo apt-get install sublime-text-dev)

安装完成后,就可以在命令行使用subl或者在dash中点击sublime的图标来打开sublime,但是此时还不能输入中文。

第二步

###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.安装C/C++编译环境和gtk libgtk2.0-dev
$ sudo apt-get install build-essential
$ sudo apt-get install libgtk2.0-dev

###3.编译共享内库

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

###4.设置LD_PRELOAD并启动SublimeText
在终端输入以下代码查看是否能打开sublime并输入中文

$ LD_PRELOAD=./libsublime-imfix.so subl

成功的话将libsublime-imfix.so文件移动到/usr/lib目录(也可以随你喜欢放到其他,只要下面的步骤中路径填写正确就可以。)

###5.修改/usr/share/applications/sublime-text-2.desktop
使用文本编辑器打开该文件,查找其中的Exec行,一般有三个 将其修改为

Exec=env LD_PRELOAD=/usr/lib/libsublime-imfix.so /usr/bin/subl %F
......
Exec=env LD_PRELOAD=/usr/lib/libsublime-imfix.so /usr/bin/subl -n
......
Exec=env LD_PRELOAD=/usr/lib/libsublime-imfix.so /usr/bin/subl --command new_file

###6.修改/usr/bin/subl为

#!/bin/bash
LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text_2/sublime_text --class=sublime-text-2 "$@"

##总结
至此,sublime就可以完美支持中文输入了,包括在dash中打开和在命令行使用subl打开,都可以进行中文输入。(输入法为搜狗输入法,理论上fcitx类输入法都可以。我没有测试)