LoginSignup
2
1

More than 5 years have passed since last update.

MacのEmacsでemacs-mozcをかなキーでON-OFFさせる裏技

Last updated at Posted at 2018-09-21

Mac の日本語入力には google 日本語入力を使い、Emacs のときだけ mozc を使っています。
Mac の google 日本語入力は、かなキーで ON/OFF するので、mozc の ON/OFF には、別のキーを割り当てる必要がありますが、
出来ることなら Mac でも Emacs でも、かなキーで IM の ON/OFF できたらと試行錯誤した結果、あんがい簡単に実現できたのでご紹介します。

mozc_emacs_helper をインストールする

  • google 日本語入力と辞書連携して mozc_emacs_helper を導入できるので辞書管理なども一元化できて便利です。
  • 詳しい導入 Tips は、別ページに書いています。
  • mozc_emacs_helper on macOS Sierra

google 日本語入力と emacs-mozc とを使い分ける

;; 英数キーをエミュレートする
;; https://www.inabamasaki.com/archives/1898
(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))

何をしているかというと、下記条件のときに強制的に Mac を英数入力にしているのです。

  • Emacs の mozc が起動されたら Mac の入力ソースを英数にする
  • 他のフレームから Emacs フレームへ移動したら Mac の入力ソースを英数にする

※でもちょっと問題が

  • 上記対応をしていてもあとからうっかり、かなキーを押すと google 日本語入力が起動してしまいます。
  • Emacs がアクティブなときは、Mac の日本語入力モードを OFF にしたいのですがコマンドがよくわからいのでお手上げでした。

⌘英かな.app で、かなキーの振る舞いをカスタマイズする

⌘英かな.app で、Emacs のときだけ、かなキーを強制的に他のキーバインドに変更してしまいます。

Alt Text

  • かな → super+j (super+j にtoggle-input-methodを割り当てている)
  • Command_L → control+g (Emacs で control+g をよく使うので便利)
  • Command_R → super+m (mozc-temp の mozc-temp-convert を設定している)

かなキーを super+j に変更することで Emacs での mozc の ON/OFF が可能になり、かつ google 日本語入力の ON/OFF ができなくなります。

※ここからが裏技です

  • ⌘英かな.appは、除外アプリを設定できます。
  • google 日本語入力を使いたいアプリを全て除外アプリに登録します。
  • つまり、かなキーをカスタマイズして使うのは、Emacs だけという設定になるわけです。
  • 残念ながら、この裏技が使えるのは、かなキーのある JIS キーボードだけ
  • US キーボードの場合は、別のアルゴリズムを考える必要があります。
  • かなり強引な手法ですが、快適さにはかえられません。

【参考】

私のEmacsのmozc設定を貼っておきます。

;; Bild mozc_emacs_helper on macos sierra
;; https://gist.github.com/ynkjm/7d6f22bb4338f84b1b287bf9abe79001
;; ===============================================================
(setq default-input-method "japanese-mozc")
(custom-set-variables '(mozc-leim-title " あ"))
(use-package mozc-cursor-color)

;; keyremap "s-j" to "kana" by eisuu.app for mac
(when (eq system-type 'darwin)
  (bind-key "s-j" 'toggle-input-method))
;; For linux
(when (eq system-type 'gnu/linux) 
  (bind-key* "<hiragana-katakana>" 'toggle-input-method))

;; Turn to key code 102 when forcus in to emacs
(when (eq system-type 'darwin) 
  (defun my-eisuu-key ()
  (interactive)
  (call-process "osascript" nil t nil "-e" "tell application \"System Events\" to key code 102"))
  (add-hook 'focus-in-hook 'my-eisuu-key))

;; Important settings
(defadvice toggle-input-method (around toggle-input-method-around activate)
  "Input method function of key-chord.el to not be set to nil."
  (let ((input-method-function-save input-method-function))
    ad-do-it
    (setq input-method-function input-method-function-save)))

;; mozc-temp
;; =================================================
;; Keyremap "s-m" to "command_r" by eisuu.app for mac
(when (eq system-type 'darwin)
(bind-key "s-m" 'mozc-temp-convert))
;; For linux
(when (eq system-type 'gnu/linux)
  (bind-key* "<henkan>" 'mozc-temp-convert))
2
1
0

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
2
1