- 创建如下两个文件
- 将两个文件放在同一目录下
- 运行run.sh,按照提示操作即可
这里参考的百度经验:sublime text 3 解决中文输入;
然后为了方便,把操作都写成了脚本。如果run.sh
运行出错遇到错误,自己看着shell脚本 微调一下吧
/*
sublime-imfix.c
Use LD_PRELOAD to interpose some function to fix sublime input method support for linux.
By Cjacker Huang
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
#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);
}
#!/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之前
,请双手合十
。