Emacs で C++ Builder 付属の grep で検索してジャンプ

WindowsGNU Emacs 25.2.1 を使っていますが、cygwin の egrep だと shift-jis の2バイト文字が検索できないので、C++ Builder 付属の grep を使って検索をして、検索結果からジャンプできるようにしてみました。

~/.emacs に以下を入れます。

(load "grep")                           ; grep-regexp-alist が defconst されてるせいか先に読み込まないと上手く変更できない
(setq grep-use-null-device nil)         ; Windows では使わない

;; bcc の grep を使用する
(setq grep-command "grep -o -n -i ")
;; -o は unix っぽい形式(これが無いと解釈できない)
;; -n は行番号付加 (これが無いと解釈できない)
;; -i は大文字小文字区別しない
;; -d はディレクトリを下る

;; bcc の grep の出力も解釈できるように
;;
;; unix grep : <file>:<lineno>:<text>
;; bcc grep -on :
;;   ファイルの最初: <file>:<lineno:%-7d> <text>
;;   それ以外:       <file>  <lineno:%-7d> <text>
;;
(setq bcc-grep-regexp-alist
      '(("^\\(.*?[^/\n]\\)[: ][ \t]*\\([1-9][0-9]*\\)\\([ \t]*:\\)?"
         1 2
         ((lambda ()
            (when grep-highlight-matches
              (let* ((beg (match-end 0))
                     (end (save-excursion (goto-char beg) (line-end-position)))
                     (mbeg (text-property-any beg end 'font-lock-face grep-match-face)))
                (when mbeg
                  (- mbeg beg)))))
          .
          (lambda ()
            (when grep-highlight-matches
              (let* ((beg (match-end 0))
                     (end (save-excursion (goto-char beg) (line-end-position)))
                     (mbeg (text-property-any beg end 'font-lock-face grep-match-face))
                     (mend (and mbeg (next-single-property-change mbeg 'font-lock-face nil end))))
                (when mend
                  (- mend beg)))))))))
(setq grep-regexp-alist bcc-grep-regexp-alist)