Sublime_text支持中文输入

安装编译所需依赖

 
 
  1. sudo apt-get install build-essential libgtk2.0-dev

源代码

 
 
  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/gtk.h>
  9. #include <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)bbbbbbbb
  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. }

编译

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

启动sublime

 
 
  1. LD_PRELOAD=./libsublime-imfix.so subl

添加到默认启动

查看sublime-text所在文件夹:

 
 
  1. which subl
  2. vi /usr/bin/subl

复制libsublime-imfix.so到sublime所在文件夹:

 
 
  1. sudo cp libsublime-imfix.so /opt/sublime_text
方法一
 
 
  1. cd /usr/bin/
  2. sudo touch sublime_text3
  3. sudo chmod a+x sublime_text3
  4. sudo vi sublime_text3

添加内容:

 
 
  1. #!/bin/bash
  2. SUBLIME_HOME="/opt/sublime_text"
  3. LD_LIB=$SUBLIME_HOME/libsublime-imfix.so
  4. sh -c "LD_PRELOAD=$LD_LIB $SUBLIME_HOME/sublime_text $@"

保存退出。

编辑/usr/share/applications将其中的三处/opt/sublime_text/sublime_text修改为/usr/bin/sublime_text3

这样无论在终端中执行sublime_text3还是双击或者右键打开文本都可以输入中文了。

方法二

编辑/usr/bin/subl,在最后一行上面添加:

 
 
  1. export LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so

编辑/usr/share/applications将其中的三处/opt/sublime_text/sublime_text替换为/usr/bin/subl

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值