(defcustom my-consult-py-prefix ?:
"The prefix character when using consult to search Pinyin."
:group 'consult
:type 'character)
(defun my--consult-py-regexp-compiler (input type ignore-case)
"Compile the INPUT string to a list of regular expressions.
The function should return a pair, the list of regular expressions and a
highlight function. The highlight function should take a single
argument, the string to highlight given the INPUT. TYPE is the desired
type of regular expression, which can be `basic', `extended', `emacs' or
`pcre'. If IGNORE-CASE is non-nil return a highlight function which
matches case insensitively."
(require 'pinyinlib)
(setq input (consult--split-escaped
(if (char-equal my-consult-py-prefix (string-to-char input))
;; Detect the first entered character. If it matches
;; `my-consult-py-prefix', convert the subsequent
;; characters into Pinyin regexp.
(pinyinlib-build-regexp-string (substring input 1))
input)))
(cons (mapcar (lambda (x) (consult--convert-regexp x type)) input)
(when-let (regexps (seq-filter #'consult--valid-regexp-p input))
(apply-partially #'consult--highlight-regexps regexps ignore-case))))
(advice-add 'consult--default-regexp-compiler :override #'my--consult-py-regexp-compiler)
实际上就是 cireu 大佬所说的复制出来后 override,但毕竟只需要维护一个,就轻松多了
唯一的问题是在 Windows 下使用 consult-find,consult-grep 有些小毛病,会把不匹配的结果也输出出来,匹配到的结果会做高亮处理(在 wsl 下测试一切正常,未出现 Windows 上的这些问题)
但常用的 consult-ripgrep,consult-fd 使用都正常,所以上面那些小毛病就不处理了(估计是 Windows 的问题,折腾也白耗心神。。。)