ubuntu14.04 下subline编辑器的安装与配置输入中文解决方案

本文介绍如何在 Sublime Text 编辑器中实现中文输入功能,通过自定义 C 文件和利用 LD_PRELOAD 环境变量进行函数拦截,确保输入法支持,并提供详细的配置步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

改编自:http://blog.youkuaiyun.com/cywosp/article/details/32350899

安装必要的库(如下)和搜狗linux输入法。
sudo apt-get update
安装C/C++的编译环境和gtk libgtk2.0-dev
sudo apt-get install build-essential  
sudo apt-get install libgtk2.0-dev
sudo apt-get install gtk+-2.0

官方网站上下载64位的.deb文件(http://www.sublimetext.com/3) ,具体为 http://c758482.r82.cf2.rackcdn.com/sublime-text_build-3083_amd64.deb  文件,
下载后双击即会自动使用默认的安装软件安装。
sublime-imfix.c 文件自己创建

//===========--start--====================



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



//===========--stop--====================


cd /opt/sublime_text/

把 sublime-imfix.c 文件复制到   /opt/sublime_text/  下面。

切换到 root用户(否则权限不够)
运行:gcc -shared -o libsublime-imfix.so sublime-imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC

// 如果是root用户是无法输入中文的。
切换到普通用户输入:  LD_PRELOAD=./libsublime-imfix.so subl
就可以使用sublime输入中文了。

继续切换到root用户:
方便启动,运行
sudo cp libsublime-imfix.so /usr/lib/

修改文件:
sudo vim /usr/share/applications/sublime_text.desktop
1,将     Exec=/opt/sublime_text/sublime_text %F    修改为
    Exec=bash -c 'LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text' %F
2,将   Exec=/opt/sublime_text/sublime_text -n   修改为
    Exec=bash -c 'LD_PRELOAD=/usr/lib/libsublime-imfix.so /opt/sublime_text/sublime_text' -n

最后把图标锁定到任务栏,就可以正常了


配置sublime:可以不用设置,根据自己喜好。
preferences-->settings-user     菜单那里复制粘贴一下内容

{  
    "auto_complete": false,  
    "auto_match_enabled": false,  
    "color_scheme": "Packages/Color Scheme - Default/Mac Classic.tmTheme",  
    "fallback_encoding": "UTF-8",  
    "font_face": "微软雅黑",  
    "font_size": 13,  
    "ignored_packages":  
    [  
        "Vintage."      // 后面加上点就是gvim的操作方式  
    ],  
    "tab_size": 2,  
    "update_check": false,  //不更新  
    "word_wrap": true  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水月情缘雪飞飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值