这是我上一篇文章的继续(是否可以在 Emacs 中的 isearch-forward 之前预处理输入字符串)。我正在尝试jpkotta
使用变量来实现答案isearch-search-fun-function
。与其编写我自己的函数,我只是建议isearch-search-fun-default
包含我自己的函数(isearch-str-forward
并且isearch-str-backward
,只是为了演示的目的),这样每次我输入“abc”时,isearch 都会突出显示并搜索 regexp a[ ]*b[ ]*c[ ]*
。
问题是,当我建议函数然后对“abc”进行 isearch 时,它给了我I-search: abc [(void-function nil)]
. 但是,如果我将代码defadvise
放入原始isearch-search-fun-default
函数中,它就可以工作!所以我很困惑。Elisp 手册中说ad-do-it
的只是原始函数代码的占位符,因此这两种方法,建议函数或更改原始函数,最终应该生成相同的代码。为什么我建议时出现错误?
(defun isearch-mangle-str (str)
"For input STR \"abc\", it will return \"a[ ]*b[ ]*c[ ]*\"."
(let ((i 0) (out ""))
(dotimes (i (length str))
(setq out (concat out (substring str i (1+ i)) "[ ]" "*")))
out))
(defun isearch-str-forward (str &optional bound noerror)
"Search forward for STR."
(let ((string (isearch-mangle-str str)))
(re-search-forward string bound noerror)))
(defun isearch-str-backward (str &optional bound noerror)
"Search backward for STR."
(let ((string (isearch-mangle-str str)))
(re-search-backward string bound noerror)))
(defvar my-search-p t)
(defadvice isearch-search-fun-default (around my-isearch-search-fun activate)
(if my-search-p
(if isearch-forward 'isearch-str-forward
'isearch-str-backward)
ad-do-it))