macOS上のEmacs 27.xの日本語入力設定 (inline-patch, Ctrl-\,ことえり)
macOS上のEmacsの日本語入力設定
(2021/8/9) BigSurでも使えるように対応しました。
最近のmacOSでのEmacsの日本語入力設定に関しては、EMP版Emacsというものの情報が多い。しかしながらEMP版Emacsは操作感に違和感を感じてしまった。
永らくEmacsをつかっていたり、Unix/Linuxなど他のプラットフォームでもEmacsを使うことがある場合には、昔ながら/共通のキーバインドや操作感の環境にカスタマイズしたくなるのものと思う。とくに実現したいのは、
-
Ctrl-\
で、MacOSのIME(ことえり)による日本語入力ON/OFF
という点である。下記の配布元にはコンパイル済みバイナリの配布もあるようだが、まるべく既存のパッケージ(macports
など)を利用して、汎用的(ルーチンワーク的)にインストール作業を行えるようにしたい。
これまでは、macports
のいわゆるインラインパッチのあたった最後のバージョンのemacs-25
をインストールして使っていたが、その後Emacsもバージョンアップが進んでいるので、macports
にある最新のEmacs 27.2 (2021/3月現在)にインラインパッチを当てて日本語入力可能な環境の構築を試すことにした。
参考情報
Macports による作業
下記では、シェル変数${macports_root}
には、macports
のインストール先(普通は/opt/local/
以下?)を指す。
また、上記の配布元から、emacs-27.1-inline.patch
, revert-89d0c445.patch
, fix-working-text.patch
の3つのファイルをダウンロードして適当なディレクトリ(下記例では~/tmp/
)にダウンロードしてあるもととする。
% cd ~/tmp/
% curl -OL https://raw.githubusercontent.com/takaxp/ns-inline-patch/27f15d87f1ce35d7362f2f583655825eab720a8e/emacs-27.1-inline.patch
% curl -OL https://raw.githubusercontent.com/takaxp/ns-inline-patch/27f15d87f1ce35d7362f2f583655825eab720a8e/fix-working-text.patch
% curl -OL https://raw.githubusercontent.com/takaxp/ns-inline-patch/27f15d87f1ce35d7362f2f583655825eab720a8e/revert-89d0c445.patch
% port clean --all emacs emacs-app
% port -v -f patch emacs-app +imagemagick +rsvg
% pushd ${macports_root}/var/macports/build/*/emacs-app/work/emacs-27.2/
% patch -p1 < ~/tmp/emacs-27.1-inline.patch
% patch -p1 < ~/tmp/revert-89d0c445.patch
% patch -p1 < ~/tmp/fix-working-text.patch
% ./autogen.sh
% popd
% port -v -f install emacs-app +imagemagick +rsvg
% open ${macports_root}/Applications/Emacs.app
ソースが展開される場所のパス、Emacsのバージョン、Emacs.app
のインストール先は、macports
のインストール設定によって環境依存があると思われます。
また、本来ならPortfile
を修正するのが筋とおもうが、それはおいおい試すことにする。
Emacsの設定(~/.emacs.d/init.el
)の編集
下記の設定例は、Emacsのバージョンが27以降なら上記のパッチが当たっているバージョンで、そうでない場合にはmacports
でいわゆるインラインパッチのあたった最後のバージョンと仮定している。
(2021/8/9追記) macOSのバージョンによって"ことえり"のメソッド名が変わっているので注意が必要である。
Big Sur (11.5)
(when (featurep 'ns)
(if (version< "27.0" emacs-version)
(set-language-info "Japanese" 'input-method "MacOSX")
(set-language-info "Japanese" 'input-method "macOS")
(setq default-inline-patch "macOS")
(when (and (memq window-system '(ns nil))
(fboundp 'mac-get-current-input-source))
(custom-set-variables
'(mac-default-input-source "com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese"))
(mac-input-method-mode 1))))
(set-language-environment 'Japanese)
(when (equal system-type 'darwin)
(define-key global-map [?\C-¥] 'toggle-input-method))
Catalina (10.15) 以前
(when (featurep 'ns)
(if (version< "27.0" emacs-version)
(set-language-info "Japanese" 'input-method "MacOSX")
(set-language-info "Japanese" 'input-method "macOS")
(setq default-inline-patch "macOS")
(when (and (memq window-system '(ns nil))
(fboundp 'mac-get-current-input-source))
(custom-set-variables
'(mac-default-input-source "com.apple.inputmethod.Kotoeri.Japanese"))
(mac-input-method-mode 1))))
(set-language-environment 'Japanese)
(when (equal system-type 'darwin)
(define-key global-map [?\C-¥] 'toggle-input-method))
OSのバージョンを気にせず使うには
外部コマンドの実行によりemacs
の起動が遅くなるのを厭わなければ、下記のようなOSバージョン自動判定も可能であろう。
(when (featurep 'ns)
(if (version< emacs-version "27.0")
(set-language-info "Japanese" 'input-method "MacOSX")
(set-language-info "Japanese" 'input-method "macOS")
(setq default-inline-patch "macOS")
(when (and (memq window-system '(ns nil))
(fboundp 'mac-get-current-input-source))
(custom-set-variables
(if (< (string-to-number (nth 0 (split-string
(shell-command-to-string "sw_vers -productVersion")
"[\f\t\n\r\v\.]+")))
11)
'(mac-default-input-source "com.apple.inputmethod.Kotoeri.Japanese")
'(mac-default-input-source "com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese")))
(mac-input-method-mode 1))))
(set-language-environment 'Japanese)
(when (equal system-type 'darwin)
(define-key global-map [?\C-¥] 'toggle-input-method))
これで以前と同じ操作感で日本語入力可能なmacOS
上のEmacs27.x
を構築できた。
Author And Source
この問題について(macOS上のEmacs 27.xの日本語入力設定 (inline-patch, Ctrl-\,ことえり)), 我々は、より多くの情報をここで見つけました https://qiita.com/Nanigashi_Uji/items/f7f2d7cbeff4a941dfc1著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .