Mozcは、Google 日本語入力のオープンソース版のIMEです。
そしてmozc.elは、Mozcを使ってEmacsで日本語入力するためのEmacs Lispです。
今回は、Mac上のEmacsでmozc.elを使って日本語入力できるようにします。

EmacsとMozcは以下のような感じで通信しているみたいです。
[Emacs + mozc.el] <-- S式 --> [mozc_emacs_helper] <-- IPC --> [Mozc server]
EmacsとMozc server
が通信するのにmozc_emacs_helper
が必要ですが、公式にはMac上でのビルドに対応していません。
なのでビルド用のスクリプトをちょっといじって、ビルドできるようにしました。
要件
公式のMacBuildInstructionsには
- システム要件
- OSX 10.5 or later intel only
- ソフトウェア要件
- Xcode
- Qt 4.6.3 or later (optional)
とあります。Qtはオプションということなので今回は無しでビルドします。
私は OS X 10.9.2 + Xcode 5.1.1 で実行しました。
ビルド手順
詳細は公式のMacBuildInstructionsも参照してください。
ソースコードの取得
$ cd ~/
$ svn co http://src.chromium.org/svn/trunk/tools/depot_tools
$ export PATH=`pwd`/depot_tools:"$PATH"
$ mkdir -p ~/src/mozc
$ cd ~/src/mozc
$ gclient config http://mozc.googlecode.com/svn/trunk/src
$ gclient sync
gclient sync
で
Error: Command svn checkout https://src.chromium.org/chrome/trunk/src/tools/clang/scripts@171017 ...
みたいなエラーが出ましたが気にしないことにします。
build_mozc.py
を上書きします。
$ cd ~/src/mozc/src
$ curl -O https://raw.githubusercontent.com/igjit/mac-emacs-mozc/master/build_mozc.py
ビルドの実行
$ python build_mozc.py gyp --noqt
$ python build_mozc.py build_tools -c Release
$ python build_mozc.py build -c Release mac/mac.gyp:GoogleJapaneseInput mac/mac.gyp:gen_launchd_confs unix/emacs/emacs.gyp:mozc_emacs_helper
インストールします。
$ sudo cp -r out_mac/Release/Mozc.app /Library/Input\ Methods/
$ sudo cp out_mac/DerivedSources/Release/mac/org.mozc.inputmethod.Japanese.Converter.plist /Library/LaunchAgents
$ sudo cp out_mac/DerivedSources/Release/mac/org.mozc.inputmethod.Japanese.Renderer.plist /Library/LaunchAgents
ここで一旦GUIをログアウトします。
再ログイン後にMozcが使えるようになります。
動作確認
$ cd ~/src/mozc/src
$ echo -e '(0 CreateSession)\n(1 SendKey 1 97)' | out_mac/Release/mozc_emacs_helper
((mozc-emacs-helper . t)(version . "1.13.1651.101")(config . ((preedit-method . roman))))
((emacs-event-id . 0)(emacs-session-id . 1)(output . ()))
((emacs-event-id . 1)(emacs-session-id . 1)(output . ((id . "9469176874988818267")(mode . hiragana)(consumed . t)(preedit . ((cursor . 1)(segment ((annotation . underline)(value . "あ")(value-length . 1)(key . "あ")))))(candidates . ((size . 1)(candidate ((index . 0)(value . "あ")(annotation . ((description . "ひらがな")))(id . 0)))(position . 0)(category . suggestion)(display-type . main)(footer . ((label . "Tabキーで選択")))))(status . ((activated . t)(mode . hiragana)(comeback-mode . hiragana)))(all-candidate-words . ((candidates ((id . 0)(index . 0)(value . "あ")(annotation . ((description . "ひらがな")))))(category . suggestion))))))
$
Emacsの設定
init.elに以下を追記します。
;; (require 'mozc)
(load "~/src/mozc/src/unix/emacs/mozc.el")
(setq default-input-method "japanese-mozc")
;; exec-pathが通っていれば不要
(setq mozc-helper-program-name "~/src/mozc/src/out_mac/Release/mozc_emacs_helper")
C-\
で日本語入力が切り替わるはずです。
動作確認ができたら mozc.el
, mozc_emacs_helper
をそれぞれ load-path
, exec-path
の通った場所に置いてください。
See also
mozc.elを使ったモードレス日本語入力インタフェース、ac-mozc を作っています。
良かったらこちらも試してみてください。

(追記)
この手順でビルドできない方がいたので、別の方法を試してみました。
MacのEmacsでmozc.elを使う (mozc-serverをVagrantで動かす)