4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Windows上でEmacsからripgrepを日本語で動作させる

Last updated at Posted at 2022-06-20

どういうこと?

WindowsでEmacsとripgrepを組み合わせて使いたいとします。
このとき、Emacsでは (prefer-coding-system 'utf-8-unix) と設定しています。
また rg へのパスは通しているものとします。

この状態でEmacsから ripgrep-regexpconsult-ripgrep など、rg コマンドで日本語を含んだ検索をしようとしても、うまく動作しません。

どうして?

Windowsのプロセスはcp932で受けとるからです。

どうした?

対象の関数のときだけで coding-system-for-write を切り替えると動作しました。
例えば consult-ripgrep なら

(defun advice:with-cp932 (orig-fun &rest args)
  (setq coding-system-for-write 'cp932)
  (apply orig-fun args)
  (setq coding-system-for-write 'utf-8-unix)) ; 使っているcoding-systemに戻す
(advice-add 'consult-ripgrep :around 'advice:with-cp932)

とすれば動作しました。

なお、consult-ripgrep中に C-g すると、 coding-system-for-writecp932 のままになります。とりあえず、

(add-hook 'before-save-hook (lambda () (setq coding-system-for-write 'utf-8-unix)))

として、むりやり coding-system-for-write を戻しています。
うまいやりかたを知っていたら教えてください。

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?