欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Linux 下 Sublime Text 中文输入问题的解决方案

程序员文章站 2022-06-29 16:59:36
这篇文章主要为大家介绍了Linux下遇到Sublime Text 中文输入问题的解决办法,下面脚本之家的小编和大家分享了一个解决Sublime Text 无法输入中文这个问题的方案,需要的朋友可以参考... 14-10-09...

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

2.编译动态库:


复制代码
代码如下:
gcc -shared -o libsublime-imfix.so sublime_imfix.c `pkg-config --libs --cflags gtk+-2.0` -fpic

3. 设置 ld_preload 并启动 sublime text:


复制代码
代码如下:
ld_preload=./libsublime-imfix.so sublime_text

为了不用每次启动sublime text都打这么一长串东西,写了个启动sublime的脚本:


复制代码
代码如下:
#!/bin/bash

sublime_home="/opt/sublime_text"
ld_lib=$sublime_home/libsublime-imfix.so
sh -c "ld_preload=$ld_lib $sublime_home/sublime_text $@"

把libsublime-imfix.so放到sublime text目录下并修改脚本中的sublime_home,这样sublime text 就可以输入中文啦,谢谢阅读,希望能帮到大家,请继续关注,我们会努力分享更多优秀的文章。