(sublime) 修復Ubuntu下中文輸入問題

  1. 創建如下兩個文件
  2. 將兩個文件放在同一目錄下
  3. 運行run.sh,按照提示操作即可

這裏參考的百度經驗:sublime text 3 解決中文輸入
然後爲了方便,把操作都寫成了腳本。如果run.sh運行出錯遇到錯誤,自己看着shell腳本 微調一下吧


文件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:run.sh

#!/bin/bash

# https://www.cnblogs.com/zhaojk2010/p/5595468.html
# https://blog.csdn.net/qq_33530388/article/details/60592980

echo "===================================="
echo " 開始修改 sublime 中文輸入設置"
echo "參考"
echo 'https://www.cnblogs.com/zhaojk2010/p/5595468.html'
echo 'https://blog.csdn.net/qq_33530388/article/details/60592980'
echo 
echo " starting....."
echo 

# 檢測路徑
echo 
echo "----------------------------------"
echo "正在檢測文件"
c_file_location=`pwd`
echo "sublime_imfix.c 默認路徑爲 ${c_file_location}/sublime_imfix.c"
echo "(默認路徑 可能由於腳本啓動位置原因設置有誤,請重新輸入正確路徑即可)"

tag=true
while(${tag})
do
	if [[ ! -f "${c_file_location}/sublime_imfix.c" ]]
    	then
    	echo 
    	echo "文件 ${c_file_location}/sublime_imfix.c 不存在"
    	echo -n "請輸入文件 sublime_imfix.c 的位置(默認路徑位置與'run.sh'同目錄,不包含'/sublime_imfix.c') :"
    	read c_file_location
    else
    	echo 
		echo "檢測到文件 sublime_imfix.c"
		echo "continue...."
		cd ${c_file_location}
		tag=false
    fi
done


# 安裝編譯依賴
echo 
echo "----------------------------------"
echo "安裝編譯依賴"
sudo apt-get install build-essential libgtk2.0-dev


# 編譯sublime_imfix.c , 編譯成共享庫 libsulime-imfix.so
echo 
echo "----------------------------------"
echo "編譯sublime_imfix.c"
gcc -shared -o libsublime-imfix.so sublime_imfix.c  `pkg-config --libs --cflags gtk+-2.0` -fPIC
echo 
echo "已經 編譯成共享庫 libsulime-imfix.so!!!!!!!!!!!!!!!!!!!! 成功"


# 將 libsublime-imfix.so 拷貝到 sublime_text 所在文件夾"/opt/sublime_text"下
# 如果找不到sublime文件夾,就存到"/usr/lib"下
echo 
echo "----------------------------------"
echo "設置 libsublime-imfix.so 拷貝路徑 = /opt/sublime_text"

lib_location=/opt/sublime_text

if [[ ! -d "${lib_location}" ]]; then 
	echo  
    echo "文件夾 /opt/sublime_text 不存在, 路徑更換爲通用路徑 /usr/lib"
    lib_location=/usr/lib
else
    echo "文件夾  /opt/sublime_text 存在"
fi

echo 
echo 路徑設置成功
echo 複製ing....

sudo cp libsublime-imfix.so ${lib_location}/
echo "libsublime-imfix.so 已複製到 ${lib_location}/ 下"


# 修改subl運行命令文件
# (在命令行中輸入 "subl 文件")
echo 
echo "----------------------------------"
echo "修改subl運行命令文件"

subl_bin_location=/usr/bin/subl

if [[ ! -f "${subl_bin_location}" ]]
	then
	echo 
	echo "文件 /usr/bin/subl  不存在"
	echo "subl運行命令文件 修改失敗!!!!!!!!!!!!!!!!!!!!"
else
	echo 
	echo "文件  /usr/bin/subl 存在"

	# 是否已經替換過
	new_subl_bin_txt='LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text "$@"'
	if grep -q "${new_subl_bin_txt}" ${subl_bin_location}
		then
	    echo "但是 已經是目標結果字符串,無需替換"
	    echo "結束替換"
	else
		echo "正在修改 subl運行命令文件...."
		# 替換 運行文件 中的 運行命令
		# old_subl_bin_txt='exec /opt/sublime_text/sublime_text "$@"'
		# new_subl_bin_txt='LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text "$@"'
		# sed -i 's/${old_subl_bin_txt}/${new_subl_bin_txt}/g' ${subl_bin_location}
		old_subl_bin_txt='exec /opt/sublime_text/sublime_text \"$@\"'
		new_subl_bin_txt='LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text \"$@\"'
		sed -i "s#${old_subl_bin_txt}#${new_subl_bin_txt}#g" ${subl_bin_location}
	fi
	echo 
	echo "subl運行命令文件 修改成功!!!!!!!!!!!!!!!!!!!!"
	
fi


# 修改 sublime_text.desktop
# (圖形界面快捷方式)
echo 
echo "----------------------------------"
echo "修改subl運行命令文件"

subl_desktop_location=/usr/share/applications/sublime_text.desktop

if [[ ! -f "${subl_desktop_location}" ]]
	then
	echo 
	echo "文件 sublime_text.desktop  不存在"
	echo "subl圖形界面快捷方式 修改失敗!!!!!!!!!!!!!!!!!!!!"
else
	echo 
	echo "文件 sublime_text.desktop 存在"
	echo "正在替換 desktop文件 中的 Exec命令...."

	# 替換 desktop文件 中的 Exec命令
	#####################################
	# 1 / Desktop Entry
	echo 
	echo "Desktop Entry...."
	
	new_subl_DesktopEntry_exe='Exec=bash -c "LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text %F"'
	
	if grep -q "${new_subl_DesktopEntry_exe}" ${subl_desktop_location}
		then
	    echo "已經是目標結果字符串,無需替換"
	else
	   	echo "正在替換"
	   	# old_subl_DesktopEntry_exe='Exec=/opt/sublime_text/sublime_text %F'
		# new_subl_DesktopEntry_exe='Exec=bash -c "LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text %F"'
		# sed -i 's/${old_subl_DesktopEntry_exe}/${new_subl_DesktopEntry_exe}/g' ${subl_desktop_location}
		old_subl_DesktopEntry_exe='Exec=/opt/sublime_text/sublime_text %F'
		new_subl_DesktopEntry_exe='Exec=bash -c \"LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text %F\"'
		sed -i "s#${old_subl_DesktopEntry_exe}#${new_subl_DesktopEntry_exe}#g" ${subl_desktop_location}
	fi
	
	echo "修改成功"


	#####################################
	# 2 / Desktop Action Window
	echo 
	echo "Desktop Action Window...."
	
	new_subl_DesktopActionWindow_exe='Exec=bash -c "LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text -n"'
	
	if grep -q "${new_subl_DesktopActionWindow_exe}" ${subl_desktop_location}
		then
	    echo "已經是目標結果字符串,無需替換"
	else
	   	echo "正在替換"
	   	# old_subl_DesktopActionWindow_exe='Exec=/opt/sublime_text/sublime_text -n'
		# new_subl_DesktopActionWindow_exe='Exec=bash -c "LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text -n"'
		# sed -i 's/${old_subl_DesktopActionWindow_exe}/${new_subl_DesktopActionWindow_exe}/g' ${subl_desktop_location}
		old_subl_DesktopActionWindow_exe='Exec=/opt/sublime_text/sublime_text -n'
		new_subl_DesktopActionWindow_exe='Exec=bash -c \"LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text -n\"'
		sed -i "s#${old_subl_DesktopActionWindow_exe}#${new_subl_DesktopActionWindow_exe}#g" ${subl_desktop_location}
	fi

	echo "修改成功"


	#####################################
	# 3 / Desktop Action Document
	echo 
	echo "Desktop Action Document...."
	
	new_subl_DesktopActionDocument_exe='Exec=bash -c "LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text --command new_file"'
	
	if grep -q "${new_subl_DesktopActionDocument_exe}" ${subl_desktop_location}
		then
	    echo "已經是目標結果字符串,無需替換"
	else
	   	echo "正在替換"
	   	# old_subl_DesktopActionDocument_exe='Exec=/opt/sublime_text/sublime_text --command new_file'
		# new_subl_DesktopActionDocument_exe='Exec=bash -c "LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text --command new_file"'
		# sed -i 's/${old_subl_DesktopActionDocument_exe}/${new_subl_DesktopActionDocument_exe}/g' ${subl_desktop_location}
		old_subl_DesktopActionDocument_exe='Exec=/opt/sublime_text/sublime_text --command new_file'
		new_subl_DesktopActionDocument_exe='Exec=bash -c \"LD_PRELOAD=${lib_location}/libsublime-imfix.so exec /opt/sublime_text/sublime_text --command new_file\"'
		sed -i "s#${old_subl_DesktopActionDocument_exe}#${new_subl_DesktopActionDocument_exe}#g" ${subl_desktop_location}
	fi
	
	echo "修改成功"

	echo 
	echo "subl圖形界面快捷方式 修改成功!!!!!!!!!!!!!!!!!!!!"
fi

echo 
echo "===================================="
echo "sublime 中文輸入設置 修改完成"
echo "done !!!"

碼字不易整理費時
copy之前請雙手合十

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章