grep 대신 helm-ag - windows에서는 ripgrep으로

MSYS2 pacman으로 ag를 설치해서 사용하는데, 한글 검색이 안 된다. 커맨드 라인에서도 이런가?

테스트 한 ag

$ pacman -Qs silver
local/mingw-w64-x86_64-ag 2.0.0.r1912.ccdbe93-1
  The Silver Searcher: An attempt to make something better than ack, which itself is better than grep (mingw-w64)
$ ag --version
ag version 2.0.0

한글 검색이 안 되네

$ grep 한글 emacs.d/init.el
;; 한글
;; 한글 환경에서는 cp949 인코딩이 디폴트이기 때문.
$ ag 한글 emacs.d/init.el

안 된다.

$ ag --debug 한글 init.el
...
DEBUG: Query is ▒ѱ▒
...
DEBUG: searching path init.el for ▒ѱ▒
...

이런 기능 좋네. –debug 옵션 마음에 든다. query 문자열 인코딩이 이상하다.

그냥 빠른 거 되는 거 찾자

ripgrep이란 게 있네. MinGW로 빌드한 pc-windows-gnu 버전을 다운로드했다.

$ rg --version
ripgrep 0.5.2
$ rg 한글 emacs.d/init.el
25:;; 한글
27:;; 한글 환경에서는 cp949 인코딩이 디폴트이기 때문.

깊게 안 파봤다. 잘 되는 rg로 가자. MinGW ag 패키지가 문제인 것 같다.

helm-ag에서 rg 사용

(use-package helm-ag
  :ensure t
  :config
  (when windows?
    (setq helm-ag-base-command "rg --no-heading --vimgrep")))

rg가 ag보다 빠르다고 해도 인지도가 낮다. macOS에서는 ag를 그대로 사용하려고 windows에서만 변경한다.

helm-do-ag 함수에서 검색하다 hang 발생

;; Package-Version: 20170209.745
;; Version: 0.58

버전에서 발생.

(use-package helm
  :ensure t
  :config
  (when windows?
    (setq helm-input-idle-delay 0.1)
    (setq helm-cycle-resume-delay 2)
    (setq helm-follow-input-idle-delay 1)))

Deadlock on Windows with helm-do-ag 이슈에 적힌 방법을 따라했다. delay로 반창고를 붙였다.

helm-do-ag 함수로 한글 검색이 안 된다

Error parsing regex near '?쒓?' at character offset 0: Missing expression for repetition operator.

한글로 검색하면 이런 에러 메시지가 나온다. 뭐야. 완전 쉣이네.

utf-8이 아니라 cp949를 사용해서 테스트. 출력을 utf-8로 해석해서 제대로 안 보이지만 입력이 제대로 전달되는 걸 확인했다.

(use-package helm-ag
  :ensure t
  :config
  (when windows?
    (advice-add 'helm-do-ag
                :before (lambda (&rest _)
                          (setq default-process-coding-system
                                '(utf-8-dos . korean-iso-8bit-unix))))
    (advice-add 'helm-do-ag
                :after (lambda (&rest _)
                         (setq default-process-coding-system
                               '(utf-8-dos . utf-8-unix))))))

그래서 조합. 입력은 korean-iso-8bit-unix 인코딩을 사용하고 출력은 utf-8-dos 인코딩을 사용. 이걸 전체 시스템에 적용하면 magit process 인자 해석이 제대로 안 된다. 그래서 advice system 함수를 사용해서 함수 호출 전, 호출 후에 인코딩을 바꾸게 했다. 이것도 반창고 붙이고 넘어간다.

coding system 참 세분되어 있다. default-process-coding-system 변수는 process IO에만 관여한다.

헥헥. 이제 끝. 잘 된다.

C-x C-s C-x C-c


크리에이티브 커먼즈 라이선스


Published: August 05 2017


Feedback plz <3 @ohyecloudy, ohyecloudy@gmail.com