LoginSignup
7

More than 3 years have passed since last update.

emacs-mozcを快適に使う

Last updated at Posted at 2020-06-20

Emacs利用時にはXIMを無効にする

emacs-mozcを使っているときは、紛らわしいのでfcitxをOFFにしておきたい…と、あれこれ悩んでいたのですが、「Emacs利用時にはXIMを無効にする」というのはが常識だったようで今更ながら ~/.Xresources を設定しました。

! ~/.Xresources
! Emacs XIMを無効化
Emacs*useXIM: false

私の場合、fcitxのON/OFFをWindowsにあわせて hiragana-katakana に設定しているのですが、
上記を設定することでEmacs利用時にはこのキーが空いてくるので、emacs-mozcのON/OFFも hiragana-katakana に割り当てることにしました。

ようするに、emacs利用時でもそうでないときでも同じキーでFEPのON/OFFが可能となりました。

shell-commandでmozc-toolを起動させると固まる

Emacs利用時にXIMを無効にした結果、これまでemacs環境からshell-commandで使っていたmozc-toolの動作が怪しくなりました。

  • mozc-toolとemacsとの間でクリップボードがうまく機能しない。
  • mozc-toolで漢字登録するためにfcitxを起動させると、emacsが固まってしまう。

async-shell-commandで起動させると問題なく機能する

そこでmozc-toolをターミナルから起動させたところ、何ら問題なく動作した。そこで shell-command に代えて非同期実行の compile を使ったところうまくいきました。compile buffer は開いてほしくないので delete-other-windows してます。

(defun my:mozc-word-regist ()
  "Run the mozc-tool in the background."
  (interactive)
  (compile "/usr/lib/mozc/mozc_tool --mode=word_register_dialog")
  (delete-other-windows))

スクリーンショット_2020-06-20_17-04-59.png

一応、他のコマンドも全て設定しましたが、その全てをキーバインドする必要もないので、ivyから選択できるように工夫してみました。

(bind-key
 "<f8>"
 (defun my:select-mozc-tool ()
   "Narrow the only espy command in M-x."
   (interactive)
   (counsel-M-x "^my:mozc ")))

mozc-tempを導入しておくと更に便利

mozc-tempはmozc.elによる入力をモードレス化するラッパーで、ac-mozcをもとに作成されました。
モードレスは一見便利ですがリアルタイムで変換されないのでどうしてもミスタイプが多くなりあまり効率的とはいえません。

ただ、普通にmozcを使っていて、うっかりmozcをONしないで入力してしまうことがたまにありますが、mozc-tempを起動させることで無駄な再入力を避けることができます。
ようは、「二刀流にしておいて、いいとこ取りで使う」という感じですかね。

句読点では即確定させる

句読点などを入力したとき、わざわざmozcに変換してもらう必要はないので以下を設定しておくことでワンアクションスピーディーになります。

(defun mozc-insert-str (str)
  (mozc-handle-event 'enter)
  (toggle-input-method)
  (insert str)
  (toggle-input-method))
(add-hook 'mozc-mode-hook
          (lambda ()
            (define-key mozc-mode-map "?" '(lambda () (interactive) (mozc-insert-str "?")))
            (define-key mozc-mode-map "," '(lambda () (interactive) (mozc-insert-str "、")))
            (define-key mozc-mode-map "." '(lambda () (interactive) (mozc-insert-str "。")))))

上記以外にも、[!]とか、よく使う記号などを登録しておいてもいいですね。

ローマ字変換にazikを使っている人は、「z.」に「…」を設定してるケースもあると思いますので、設定を変更するなどの工夫が必要です。

癒やし系の設定

どうでもよい遊び感覚の設定を紹介します。

スクリーンショット_2020-06-19_21-05-57.png

emacs-mozcを導入すると、ONにしたときにモードラインに「MOZC] と表示が出ます。設定することで、ごく一般的な「あ」とか「かな」にすることができます。
私の場合は次のようにしています。

(setq mozc-leim-title "♡かな")

その他のtool

カーソルのcolor設定にはmozc-cursor-colorというpackageを使うと簡単で便利です。また、変換候補の表示には、mozc-cand-posframeというpackageがお薦めです。
前者はgithubからel-getもしくは直接Downloadします。後者はMelpaからpackage-installできます。

私のmozc設定

最後に私の全てのmozc設定を公開しておきます。

(leaf mozc
  :ensure t
  :bind* ("<hiragana-katakana>" . toggle-input-method)
  :bind ("<f8>" . my:select-mozc-tool)
  :config
  (setq default-input-method "japanese-mozc"
        mozc-helper-program-name "mozc_emacs_helper"
        mozc-leim-title "♡かな")
  :preface
  (defadvice toggle-input-method (around toggle-input-method-around activate)
    "Input method function in key-chord.el not to be nil."
    (let ((input-method-function-save input-method-function))
      ad-do-it
      (setq input-method-function input-method-function-save)))
  (defun mozc-insert-str (str)
    "If punctuation marks, immediately confirm."
    (mozc-handle-event 'enter)
    (toggle-input-method)
    (insert str)
    (toggle-input-method))
  (add-hook 'mozc-mode-hook
            (lambda ()
              (define-key mozc-mode-map "?" '(lambda () (interactive) (mozc-insert-str "?")))
              (define-key mozc-mode-map "," '(lambda () (interactive) (mozc-insert-str "、")))
              (define-key mozc-mode-map "." '(lambda () (interactive) (mozc-insert-str "。")))))
  :init
  (leaf mozc-temp
    :ensure t
    :bind* ("<henkan>" . mozc-temp-convert))
  (leaf mozc-cursor-color
    :el-get iRi-E/mozc-el-extensions
    :require t
    :config
    (setq mozc-cursor-color-alist
          '((direct . "#BD93F9")
            (read-only . "#84A0C6")
            (hiragana . "#CC3333"))))
  (leaf mozc-cand-posframe
    :ensure t
    :require t
    :config
    (setq mozc-candidate-style 'posframe)
    :init
    (leaf posframe :ensure t)))


(leaf *user-mozc-tool
  :init
  (defun my:select-mozc-tool ()
    "Narrow the only espy command in M-x."
    (interactive)
    (counsel-M-x "^my:mozc "))

  (defun my:mozc-config-dialog ()
    "Run the mozc-tool in the background."
    (interactive)
    (compile "/usr/lib/mozc/mozc_tool --mode=config_dialog")
    (delete-other-windows))

  (defun my:mozc-dictionary-tool ()
    "Run the mozc-tool in the background."
    (interactive)
    (compile "/usr/lib/mozc/mozc_tool --mode=dictionary_tool")
    (delete-other-windows))

  (defun my:mozc-word-regist ()
    "Run the mozc-tool in the background."
    (interactive)
    (compile "/usr/lib/mozc/mozc_tool --mode=word_register_dialog")
    (delete-other-windows))

  (defun my:mozc-hand-writing ()
    "Run the mozc-tool in the background."
    (interactive)
    (compile "/usr/lib/mozc/mozc_tool --mode=hand_writing")
    (delete-other-windows)))

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
7