LoginSignup
12
18

More than 5 years have passed since last update.

OSX の Emacs で日本語入力

Last updated at Posted at 2018-08-07

1. はじめに

El Capitan(OSX 10.11) で OS のアップデートを放置していた MacBook Air があったので,Mojave(macOS 10.14) のリリース時にアップデートでもしようと思っていたら,我が MacBook Air は,Mid 2011 でサポートされないことが分かりました.対象外なものは仕方ないので,取り敢えず,High Sierra(macos 10.13) をバックアップして,クリーンインストールしました.特出することもないので,インストール作業は割愛します.また,HomebrewCommand Line Tools が動作することが前提です.まあ,Homebrew が動作するなら Command Line Tools も問題ないとは思うのですが…….

2. Emacs の種類

Emacs Wiki によると Homebrew からは,

Version
Brew brew install --with-cocoa emacs
Cask brew cask install emacs
Emacs-Plus brew tap d12frosted/emacs-plus
brew install emacs-plus
Yamamoto Mitsaharu brew tap railwaycat/emacsmacport
brew install emacs-mac

のような種類があるようです.Yamamoto Mitsuharu 氏のバージョンを使えば,日本語入力がちらつくこともなく快適な Emacs Life が送れると思いますが,今回は,WSL 上の Emacs で mozc_emacs_helper を使用したこともあり,極力設定ファイルを共通化したかったので,Vanilla builds の Cask version を使用しました.

3. mozc_emacs_helper のインストール

基本的な的な手順は,How to build Mozc on OS X のままです.

3.1. Google IME のインストール

Google の日本語入力と連携したいので,Google IME をインストールします.

3.2. ツールのインストール

$ brew install ninja

3.3. ソースコードの取得とパッチの適用

$ mkdir -p ~/Downloads/src
$ git clone https://github.com/google/mozc.git -b master --single-branch --recursive

$ cd mozc
$ vi mozc_emacs_helper.patch
$ patch -p1 < mozc_emacs_helper.patch
mozc_emacs_helper.patch
diff --git a/src/mac/mac.gyp b/src/mac/mac.gyp
index 9cf0cf4f..435f8765 100644
--- a/src/mac/mac.gyp
+++ b/src/mac/mac.gyp
@@ -49,6 +49,7 @@
         '../renderer/renderer.gyp:renderer_client',
         '../session/session_base.gyp:ime_switch_util',
         '../testing/testing.gyp:gtest_main',
+        '../unix/emacs/emacs.gyp:mozc_emacs_helper',
         'gen_key_mappings',
       ],
       'variables': {
@@ -585,7 +586,6 @@
             ['branding=="GoogleJapaneseInput"', {
               'dependencies': [
                 'DevConfirmPane',
-                'codesign_client',
               ],
             }],
           ],

3.4. mozc_emacs_helper の構築

$ ls /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKsSDK のバージョンを確認,$ sw_versOS のバージョンを確認します.ここでは,両方 10.13 だったので,10.13 を指定してビルドします.

$ cd src
$ GYP_DEFINES="mac_sdk=10.13 mac_deployment_target=10.13" python build_mozc.py gyp --noqt --branding=GoogleJapaneseInput
$ python build_mozc.py build -c Release unix/emacs/emacs.gyp:mozc_emacs_helper

Google IME と通信できるか確認します.

$ cd out_mac/Release/
$ echo -e '(0 CreateSession)\n(1 SendKey 1 hiragana)\n(2 SendKey 1 hiragana)\n(3 SendKey 1 97)' | ./mozc_emacs_helper

正常に通信できていると,下記のような出力になります.

((mozc-emacs-helper . t)(version . "2.23.2815.101")(config . ((preedit-method . roman))))
((emacs-event-id . 0)(emacs-session-id . 1)(output . ()))
((emacs-event-id . 1)(emacs-session-id . 1)(output . ((id . "11221667262489946350")(mode . hiragana)(consumed . nil)(key . ((special-key . kana)))(status . ((activated . t)(mode . hiragana)(comeback-mode . hiragana))))))
((emacs-event-id . 2)(emacs-session-id . 1)(output . ((id . "11221667262489946350")(mode . hiragana)(consumed . nil)(key . ((special-key . kana)))(status . ((activated . t)(mode . hiragana)(comeback-mode . hiragana))))))
((emacs-event-id . 3)(emacs-session-id . 1)(output . ((id . "11221667262489946350")(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キーで選択")))(page-size . 9)))(status . ((activated . t)(mode . hiragana)(comeback-mode . hiragana)))(all-candidate-words . ((candidates ((id . 0)(index . 0)(value . "あ")(annotation . ((description . "ひらがな")))))(category . suggestion))))))

mozc_emacs_helper を PATH の通った場所にコピーします.

4. Emacs の設定

ここここにあるように,Mac 側の文字入力の状態に影響されるので,Emacs で文字入力する際には,直接入力(英数モード?)にする必要があるようです.2つ目のサイトの方法をそのまま使わせてもらいました.その他は,WSL の Emacs で日本語入力の設定をほぼそのまま使えます(mozc-helper-program-name の変更のみ).~/emacs.d/init.el あたりに下記内容を追加します.

init.el
;; Mac 固有の設定
;; https://www.inabamasaki.com/archives/1898
;; https://snap.textgh.org/201710031643/
(when (eq system-type 'darwin)
  (defun my-eisuu-key ()
    "Emulating alphanumeric keys"
    (interactive)
    (call-process "osascript" nil t nil "-e" "tell application \"System Events\" to key code 102"))
  ;; Mozc が起動されたら英数にする
  (add-hook 'mozc-mode-hook 'my-eisuu-key)
  ;; フレームがアクティブになった時英数にする
  (add-hook 'focus-in-hook 'my-eisuu-key))

;; NTEmacs @ ウィキ
;; https://www49.atwiki.jp/ntemacs/pages/48.html
(defvar-local mozc-im-mode nil)

(use-package mozc
  :ensure t
  :init
  (setq mozc-helper-program-name "mozc_emacs_helper")
  :custom
  (mozc-leim-title "[も]"))

(use-package mozc-im
  :ensure t
  :after mozc
  :init
  (add-hook 'mozc-im-activate-hook (lambda () (setq mozc-im-mode t)))
  (add-hook 'mozc-im-deactivate-hook (lambda () (setq mozc-im-mode nil)))
  :config
  (setq default-input-method "japanese-mozc-im")
  (add-hook 'isearch-mode-hook (lambda () (setq im-state mozc-im-mode)))
  (add-hook 'isearch-mode-end-hook
            (lambda ()
              (unless (eq im-state mozc-im-mode)
                (if im-state
                    (activate-input-method default-input-method)
                  (deactivate-input-method)))))
  )

(use-package mozc-popup
  :if window-system
  :ensure t
  :config
  (setq mozc-candidate-style 'popup))

;; https://github.com/iRi-E/mozc-el-extensions
;; `mozc-cursor-color.el` を `lisp` に
(use-package mozc-cursor-color
  :after mozc-im
  :load-path "lisp/"
  :config
  (setq mozc-cursor-color-alist
        '((direct . "#ccccc7")
          (read-only . "#50fa7b")
          (hiragana . "#b45bcf")
          (full-katakana . "goldenrod")
          (half-ascii . "dark orchid")
          (full-ascii . "orchid")
          (half-katakana . "dark goldenrod")))

  (advice-add 'mozc-cursor-color-update :around
              (lambda (orig-fun &rest args)
                (let ((mozc-mode mozc-im-mode))
                  (apply orig-fun args))))
  )

C-\ で日本語入力ができると思います.
ただ,何度も言いますが,Yamamoto Mitsuharu 氏のバージョンを使うことをお勧めします.

12
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
18