Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 下 Sublime Text 無法輸入中文?LD_PRELOAD 注入法完美解決

編輯:關於 Linux

Sublime Text 在 Linux (GTK2 環境) 下長期存在無法跟隨輸入法游標、無法輸入中文的問題。本文提供一鍵編譯注入共享庫的解決方案,經測試可在 Ubuntu 20.04+/CentOS 7+/Arch 等主流發行版完美運行。

根本原因

Sublime Text 2/3 早期版本使用自訂 GTK IM Context 邏輯,未正確處理 gtk_im_context_set_client_window 與游標位置事件,導致輸入法無法定位游標位置,候選視窗跑偏或無法彈出。

一鍵部署腳本

#!/bin/bash
# sublime-imfix 一鍵安裝腳本
set -e

cat > /tmp/sublime_imfix.c <<'EOF'
/* sublime-imfix.c - 修正 Sublime Text GTK2 輸入法支援 */
#include 
#include 

GtkIMContext *local_context = NULL;

void gdk_region_get_clipbox(const GdkRegion *region, GdkRectangle *rect) {
    g_return_if_fail(region && rect);
    rect->x = region->extents.x1;
    rect->y = region->extents.y1;
    rect->width = region->extents.x2 - region->extents.x1;
    rect->height = region->extents.y2 - region->extents.y1;
    if (rect->width == 2 && GTK_IS_IM_CONTEXT(local_context)) {
        gtk_im_context_set_cursor_location(local_context, rect);
    }
}

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 = 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 w = gdk_window_get_width(window);
    int h = gdk_window_get_height(window);
    if (w && h) {
        gtk_im_context_focus_in(context);
        local_context = context;
    }
    gdk_window_add_filter(window, event_filter, context);
}
EOF

# 編譯
gcc -shared -o /opt/sublime_text/libsublime-imfix.so /tmp/sublime_imfix.c \
    $(pkg-config --cflags --libs gtk+-2.0) -fPIC

# 建立啟動包裝腳本
cat > /usr/local/bin/subl <<'EOF'
#!/bin/bash
export LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so
exec /opt/sublime_text/sublime_text "$@"
EOF
chmod +x /usr/local/bin/subl

echo "安裝完成!請使用 'subl' 指令啟動 Sublime Text"

手動步驟 (若不想跑腳本)

  1. 將上方 C 碼存為 sublime_imfix.c。
  2. 編譯:gcc -shared -o libsublime-imfix.so sublime_imfix.c $(pkg-config --cflags --libs gtk+-2.0) -fPIC
  3. 移動 .so 至 Sublime 安裝目錄 (如 /opt/sublime_text/)。
  4. 建立啟動別名或腳本:LD_PRELOAD=./libsublime-imfix.so sublime_text。

驗證

  • 啟動 Sublime,切換至中文輸入法 (fcitx5/ibus/rime 等)。
  • 游標處輸入拼音,候選視窗應正確跟隨游標位置。
  • 可正常輸入、選字、換行。

注意事項

  • 僅適用 GTK2 版 Sublime Text 3 (Build 3211 之前);Sublime Text 4 已遷移 GTK3,官方修復較好,若仍有問題請升級 ST4。
  • 若系統缺 gtk+-2.0 開發套件,請先安裝:sudo apt install libgtk2.0-dev / sudo dnf install gtk2-devel。
  • Wayland 會話下可能需啟用 XWayland 相容模式。

套用此修補後,Sublime Text 即可在 Linux 下流暢輸入中文,告別切換視窗複製貼上的痛苦。

Copyright © Windows教程網 All Rights Reserved