sublime在linux下支持中文输入

gvim sublime_imfix.c

 1     /*
 2     sublime-imfix.c
 3     Use LD_PRELOAD to interpose some function to fix sublime input method support for linux.
 4     By Cjacker Huang <jianzhong.huang at i-soft.com.cn>
 5     gcc -shared -o libsublime-imfix.so sublime_imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC
 6     LD_PRELOAD=./libsublime-imfix.so sublime_text
 7     */
 8     #include <gtk-2.0/gtk/gtk.h>             // 根据系统头文件选择
 9     #include <gtk-2.0/gdk/gdkx.h>
10     typedef GdkSegment GdkRegionBox;
11     struct _GdkRegion
12     {
13       long size;
14       long numRects;
15       GdkRegionBox *rects;
16       GdkRegionBox extents;
17     };
18     GtkIMContext *local_context;
19     void
20     gdk_region_get_clipbox (const GdkRegion *region,
21                 GdkRectangle    *rectangle)
22     {
23       g_return_if_fail (region != NULL);
24       g_return_if_fail (rectangle != NULL);
25       rectangle->x = region->extents.x1;
26       rectangle->y = region->extents.y1;
27       rectangle->width = region->extents.x2 - region->extents.x1;
28       rectangle->height = region->extents.y2 - region->extents.y1;
29       GdkRectangle rect;
30       rect.x = rectangle->x;
31       rect.y = rectangle->y;
32       rect.width = 0;
33       rect.height = rectangle->height; 
34       //The caret width is 2; 
35       //Maybe sometimes we will make a mistake, but for most of the time, it should be the caret.
36       if(rectangle->width == 2 && GTK_IS_IM_CONTEXT(local_context)) {
37             gtk_im_context_set_cursor_location(local_context, rectangle);
38       }
39     }
40     //this is needed, for example, if you input something in file dialog and return back the edit area
41     //context will lost, so here we set it again.
42     static GdkFilterReturn event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer im_context)
43     {
44         XEvent *xev = (XEvent *)xevent;
45         if(xev->type == KeyRelease && GTK_IS_IM_CONTEXT(im_context)) {
46            GdkWindow * win = g_object_get_data(G_OBJECT(im_context),"window");
47            if(GDK_IS_WINDOW(win))
48              gtk_im_context_set_client_window(im_context, win);
49         }
50         return GDK_FILTER_CONTINUE;
51     }
52     void gtk_im_context_set_client_window (GtkIMContext *context,
53               GdkWindow    *window)
54     {
55       GtkIMContextClass *klass;
56       g_return_if_fail (GTK_IS_IM_CONTEXT (context));
57       klass = GTK_IM_CONTEXT_GET_CLASS (context);
58       if (klass->set_client_window)
59         klass->set_client_window (context, window);
60       if(!GDK_IS_WINDOW (window))
61         return;
62       g_object_set_data(G_OBJECT(context),"window",window);
63       int width = gdk_window_get_width(window);
64       int height = gdk_window_get_height(window);
65       if(width != 0 && height !=0) {
66         gtk_im_context_focus_in(context);
67         local_context = context;
68       }
69       gdk_window_add_filter (window, event_filter, context); 
70     }

 

编译

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

如果提示缺少gtk,就

sudo apt-get install libgtk2.0-dev

 

启动命令

LD_PRELOAD=./libsublime-imfix.so ./sublime_text

 

新建一个shell脚本

!/bin/bash
    
SUBLIME_HOME="~/tools/sublime"
LD_LIB=$SUBLIME_HOME/libsublime-imfix.so

sh -c "LD_PRELOAD=$LD_LIB $SUBLIME_HOME/bin/sublime_text $@"

 

你可能感兴趣的:(sublime)