请稍候...
确定
取消
  • Ubuntulogo

    Ubuntu

    Ubuntu继Debian安全牢固特性,server版稳定,桌面版易用,全球最受欢迎的Linux OS之一
  • 解决Ubuntu下sublime无法输入中文

      时间:2016-09-15 22:07:56

      阅读:644 回复:0 A- A+

      使用Ubuntu系统做开发的朋友可能会发现安装sublime后不论是从desktop启动还是命令行启动写代码均无法输入中文,这将对我们使用sublime开发带来不便。

      鉴于网络上一些教程存在误导性或给出的.so库文件源代码不全,所以还是抽空写一下解决方法,给有需要的朋友做个参考。

      我们新建一个sublime_imfix.c文件,编辑文件内容为:

      /*
      sublime-imfix.c
      tahenniu.com
      */
      #include 
      #include 
      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);
      }
      

      然后保存,使用gcc编译为so库文件sublime_imfix.so

      我们将编译好的库文件放入/opt/sublime_text/目录下(一般使用仓库安装的sublime都是这个路径,具体以自己实际路劲为准)

      接着我们编辑:

      subl /usr/bin/subl
      我们注释掉原来配置改为如下:

      #!/bin/sh
      #exec /opt/sublime_text/sublime_text "$@"
      #上面注释掉的是原配置
      LD_PRELOAD=/opt/sublime_text/sublime_imfix.so exec /opt/sublime_text/sublime_text "$@"
      我们为了使在桌面应用和右键打开subl也能正常输入中文还要做如下配置:

      将原配置文件备份以备不时之需

      cp /usr/share/applications/sublime_text.desktop /usr/share/applications/sublime_text.desktop.bk
      备份完我们打开配置文件

      subl /usr/share/applications/sublime_text.desktop
      修改内容为如下:

      
      Version=1.0
      Type=Application
      Name=Sublime Text
      GenericName=Text Editor
      Comment=Sophisticated text editor for code, markup and prose
      #Exec=/opt/sublime_text/sublime_text %F
      Exec=bash -c "LD_PRELOAD=/opt/sublime_text/sublime_imfix.so exec /opt/sublime_text/sublime_text %F"
      Terminal=false
      MimeType=text/plain;
      Icon=sublime-text
      Categories=TextEditor;Development;
      StartupNotify=true
      Actions=Window;Document;
      Name=New Window
      #Exec=/opt/sublime_text/sublime_text -n
      Exec=bash -c "LD_PRELOAD=/opt/sublime_text/sublime_imfix.so exec /opt/sublime_text/sublime_text -n"
      OnlyShowIn=Unity;
      Name=New File
      #Exec=/opt/sublime_text/sublime_text --command new_file
      Exec=bash -c "LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so exec /opt/sublime_text/sublime_text --command new_file"
      OnlyShowIn=Unity;
      完了保存退出,重启系统,好了,不论是从命令行启动还是桌面启动你的sublime均可以正常输入中文,又能愉快的撸代码了。

      网友观点 文明社会,理性发言,和谐讨论

        暂无网友互动记录!

    发表我的看法(70字以内)