完美解决 Linux ubuntu下 Sublime Text

作者: snoweek | 来源:发表于2016-05-04 21:01 被阅读3767次

    重装系统后,安装了中文输入法,却发现在sublime 下仍然不能输入中文,经过搜索资料,找到以下解决办法,且均已在本人电脑上经过验证,均有效。知道在将来一定会多次重装linux系统,在此记录一下,也希望可以和大家分享。

    准备工作

    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 subl
    */
    #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
    

    我在安装的时候,并不是一次成功,有一些文件没有一次安装完毕,我会输入

    sudo aot-get update
    

    再重新安装
    3.进入sublime-imfix.c的目录,编译共享内库

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

    此句的执行结果是生成libsublime-imfix.so文件,其位置是执行此句命令所在的目录。

    解决方案

    1.在终端执行此举命令

    LD_PRELOAD=./libsublime-imfix.so subl
    

    会进入sublime,并且可以输入中文了,
    2.新建一个shell脚本,很简单,就一句话。

    #!/bin/bash
    LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so subl
    

    然后将文件另存为sublime(注意:没有后缀),并且放到/home文件夹
    终端输入:

    bash ~/sublime
    

    就可以启动sublime,并且能够输入中文。

    3.修改文件/usr/bin/subl的内容

    sudo gedit /usr/bin/subl
    

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

    修改为

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

    此时,终端输入:

     subl
    

    就可以启动sublime,并且能够输入中文。

    以上三种方式,都属于一次性方案
    级只有输入上述命令行启动sublime才能输入中文,但其实我们更习惯于直接打开sublime。该怎么办呢?下面介绍第四种办法
    4.为了使用鼠标右键打开文件或者直接打开sublime时能够使用中文输入,还需要修改文件sublime_text.desktop的内容。

    sudo gedit /usr/share/applications/sublime_text.desktop
    
    [Desktop Entry]
    [...]
    Exec=bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text %F"
    [...]
     
    [Desktop Action Window]
    [...]
    Exec=bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text -n"
    [...]
     
    [Desktop Action Document]
    [...]
    Exec=bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text --command new_file"
    [...]
    

    注意:
    修改时请注意双引号"",否则会导致不能打开带有空格文件名的文件。
    此处仅修改了/usr/share/applications/sublime-text.desktop,但可以正常使用了。
    opt/sublime_text/目录下的sublime-text.desktop可以修改,也可不修改。

    [Desktop Entry]
    [...]
    Exec=env LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so /opt/sublime_text/sublime_text %F
    [...]
     
    [Desktop Action Window]
    [...]
    Exec=env LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so /opt/sublime_text/sublime_text -n
    [...]
     
    [Desktop Action Document]
    [...]
    Exec=env LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so /opt/sublime_text/sublime_text --command new_file
    [...]
    

    以上四种解决办法,是可以同时存在于电脑上的,可以都试一下,在适当的时候选择最合适的方法即可。
    最后,贴图展示一下自己的成果:

    相关文章

      网友评论

        本文标题:完美解决 Linux ubuntu下 Sublime Text

        本文链接:https://www.haomeiwen.com/subject/bctdrttx.html