Getting IBus working with Emacs

Emacs comes with a lot of Chinese input methods like pinyin, four-corner method, and various forms of Cangjie among others (listed quite handily here). For basic usage, it actually does fairly well. I’ve been able to use the four corner method to look up characters of which I don’t know the pronunciation. However, Emacs’s 4corner and Cangjie methods are limited in that they only use traditional characters and can’t look up simplified characters. So if I tried to look up 龙 (“dragon”), which looks like 4corner “43040” to me, I wouldn’t be able to, since it’s a simplified character. I’d only be able to look up the traditional form of dragon: “龍” (which is “01211”). So I looked for other input methods that might support both traditional and simplified, one of which is Wubi. Wubi isn’t available for Emacs, but can be installed via IBus.

I installed IBus and tried it out. It’s input is pretty good, and better than Emacs’s pinyin in that it has phrase matching. So if I wanted to enter in “lǎoshī” (“teacher”, “老师”) in Emacs, it would get “lao -> 老” correct, but would guess that “shi” is “是”, since shì (是) is more common than shī (师). IBus’s pinyin is smart enough to recognize “laoshi” as “老师”, among other words and phrases.

IBus worked out of the box for applications like Chromium and even xterm, but for some reason it seemed to have no effect whatsoever in Emacs. I thought this had something to do with not having ibus-el installed, so I installed it via apt. Even with correct setup I still had problems. Nothing was showing up. When I tried ibus-toggle I got the error 'IBusELInputContext' object has no attribute 'enable'. It turns out that IBus 1.5 no longer works with ibus-el, and that ibus-el pretty much doesn’t work anymore (see this discussion). But some seemed to be able to get IBus working without ibus-el. Since Emacs has XIM support, it should be able to support it automatically. But whenever I entered text, only English characters appeared, without the IBus character selection dialog popup. I tried adding

export GTK_IM_MODULE=ibus
export XMODIFIERS=@im=ibus
export QT_IM_MODULE=ibus

to my ~/.zshrc (it turns out you probably don’t need to, as GTK_IM_MODULE and XMODIFIERS were already set to these values).

I found someone mention the workaround of using LC_CTYPE="zh_CN.UTF-8" emacs to start Emacs. It turns out that this somewhat works. I started to see the IBus character selection dialog popup, but I wasn’t able to enter any characters. I tracked the problem to Gtk-WARNING **: Locale not supported by C library, which suggested that I didn’t actually have “zh_CN.UTF-8” installed. So I installed it via sudo dpkg-reconfigure locales and selected the appropriate option. Now if I start emacs using

LC_CTYPE="zh_CN.UTF-8" emacs

it can accept input through Wubi, Pinyin, Cangjie5, and others. Cool!

There’s still some problems with using IBus on Emacs without ibus-el. It’s hard to do commands like C-x k (kill-buffer) without the k being read as something else. Usually you have to switch to temporary English mode using Shift, or switch back to the US keyboard. Maybe someday ibus-el will work with IBus again, but the API conflicts seem to suggest this won’t happen anytime soon.

Anyways, that’s it. Hopefully this helps if you with Emacs and IBus if you were tearing your hair out like I was. 再见!