はじめに
macOS上のGNU Emacs(標準版)でmigemoを使い、ローマ字入力で日本語をインクリメンタル検索できるようにする方法を解説します。
さらに、isearch(C-s)開始時に自動的に英数入力に切り替わるようにして、日本語入力モードのままでもスムーズに検索を開始できるようにします。
動作確認環境
| 項目 | バージョン |
|---|---|
| macOS | 14.2.1 (Sonoma) |
| チップ | Apple Silicon (arm64) |
| Emacs | GNU Emacs 30.2 |
| パッケージ管理 | straight.el |
動作イメージ
-
C-sを押してnihonと入力 → 「日本」がハイライトされる - 日本語入力モードでも
C-sを押すと自動的に英数に切り替わる - isearch終了後は元の入力モードに戻る
1. cmigemoのインストール
Homebrewでcmigemoをインストールします。
brew install cmigemo
インストール後、辞書ファイルが以下のいずれかに配置されます。
- Apple Silicon:
/opt/homebrew/share/migemo/utf-8/migemo-dict - Intel Mac:
/usr/local/share/migemo/utf-8/migemo-dict
2. migemo.elの設定
Emacsの設定ファイルに以下を追加します。
;;
;; migemo.elの設定 (for macOS)
;; ローマ字で日本語をインクリメンタル検索できるようにする
;;
(when (and (executable-find "cmigemo")
(require 'migemo nil t))
(setq migemo-command "cmigemo")
(setq migemo-options '("-q" "--emacs"))
(setq migemo-dictionary "/opt/homebrew/share/migemo/utf-8/migemo-dict") ; Apple Silicon
;; Intel Macの場合は以下に変更
;; (setq migemo-dictionary "/usr/local/share/migemo/utf-8/migemo-dict")
(setq migemo-user-dictionary nil)
(setq migemo-regex-dictionary nil)
(setq migemo-coding-system 'utf-8-unix)
;; migemo を初期化
(migemo-init)
;; isearch で migemo を有効にする
(setq migemo-isearch-enable-p t))
straight.elを使っている場合は、migemoパッケージを宣言します。
(straight-use-package 'migemo)
3. isearch開始時に自動で英数入力に切り替える
GNU Emacs(標準版)には mac-auto-ascii-mode がないため、外部ツール macism を使ってIMEを制御します。
3.1 macismのインストール
# ソースからビルド(OS が古く、最新の Xcode がインストールできなかったため)
cd /tmp
curl -sL https://github.com/laishulu/macism/archive/refs/heads/master.zip -o macism.zip
unzip -q macism.zip
cd macism-master
make
# ~/bin にインストール(パスが通っている場所ならどこでも可)
mkdir -p ~/bin
cp macism ~/bin/
動作確認
~/bin/macism # 現在の入力ソースが表示される
3.2 Emacsの設定
migemoの設定に以下を追加します。
;; isearch開始時に日本語入力なら英数に切り替え、終了時に戻す
;; macism を使用
(when (executable-find "macism")
(defvar my-macism-path (executable-find "macism")
"Path to macism executable.")
(defvar my-input-source-before-isearch nil
"Input source before starting isearch.")
(defun my-isearch-pre-hook ()
"Save current input source and switch to ASCII if Japanese."
(when my-macism-path
(let ((current-source
(with-temp-buffer
(call-process my-macism-path nil t nil)
(string-trim (buffer-string)))))
(if (string-match-p "Japanese\\|Kotoeri" current-source)
(progn
(setq my-input-source-before-isearch current-source)
(call-process my-macism-path nil nil nil "com.apple.keylayout.ABC"))
(setq my-input-source-before-isearch nil)))))
(defun my-isearch-post-hook ()
"Restore input source if it was changed."
(when (and my-macism-path my-input-source-before-isearch)
(call-process my-macism-path nil nil nil my-input-source-before-isearch)
(setq my-input-source-before-isearch nil)))
(add-hook 'isearch-mode-hook #'my-isearch-pre-hook)
(add-hook 'isearch-mode-end-hook #'my-isearch-post-hook))
4. 完成版の設定
上記をまとめた完成版の設定です。
;;
;; migemo.elの設定 (for macOS)
;; ローマ字で日本語をインクリメンタル検索できるようにする
;;
(when (and (executable-find "cmigemo")
(require 'migemo nil t))
(setq migemo-command "cmigemo")
(setq migemo-options '("-q" "--emacs"))
(setq migemo-dictionary "/opt/homebrew/share/migemo/utf-8/migemo-dict")
(setq migemo-user-dictionary nil)
(setq migemo-regex-dictionary nil)
(setq migemo-coding-system 'utf-8-unix)
;; migemo を初期化
(migemo-init)
;; isearch で migemo を有効にする
(setq migemo-isearch-enable-p t)
;; isearch開始時に日本語入力なら英数に切り替え、終了時に戻す
;; macism を使用
(when (executable-find "macism")
(defvar my-macism-path (executable-find "macism")
"Path to macism executable.")
(defvar my-input-source-before-isearch nil
"Input source before starting isearch.")
(defun my-isearch-pre-hook ()
"Save current input source and switch to ASCII if Japanese."
(when my-macism-path
(let ((current-source
(with-temp-buffer
(call-process my-macism-path nil t nil)
(string-trim (buffer-string)))))
(if (string-match-p "Japanese\\|Kotoeri" current-source)
(progn
(setq my-input-source-before-isearch current-source)
(call-process my-macism-path nil nil nil "com.apple.keylayout.ABC"))
(setq my-input-source-before-isearch nil)))))
(defun my-isearch-post-hook ()
"Restore input source if it was changed."
(when (and my-macism-path my-input-source-before-isearch)
(call-process my-macism-path nil nil nil my-input-source-before-isearch)
(setq my-input-source-before-isearch nil)))
(add-hook 'isearch-mode-hook #'my-isearch-pre-hook)
(add-hook 'isearch-mode-end-hook #'my-isearch-post-hook)))
動作確認
- Emacsを再起動
- 日本語を含むファイルを開く
- 日本語入力モードにする
-
C-sを押す → 自動的に英数入力に切り替わる -
nihonと入力 → 「日本」がハイライトされる -
C-gまたはRETでisearch終了 → 日本語入力モードに戻る
補足
Emacs Mac portを使う場合
Emacs Mac port(railwaycat版)を使っている場合は、mac-auto-ascii-mode が利用できるため、macismは不要です。
(when (fboundp 'mac-auto-ascii-mode)
(mac-auto-ascii-mode 1))
パフォーマンスについて
macismは外部プロセスのため、C-s 押下時にわずかな遅延(数十〜数百ミリ秒)が発生する場合があります。shell-command ではなく call-process を使うことで遅延を最小限に抑えています。