问题描述
这是我上一篇文章的一个继续()。我正在使用变量 isearch-search-fun-function
来实现 jpkotta
的回答。而不是编写自己的函数,我只是建议 isearch-search-fun-default
包含我自己的函数( isearch-str-forward
和
isearch-str-backward
,只是为了演示的目的),所以每次我键入abc,isearch将突出显示并搜索regexp a [] * b [] * c [] *
。
This is a continue of my previous post (is it possible to preprocess the input string before isearch-forward in Emacs). I am trying to implement jpkotta
's answer using the variable isearch-search-fun-function
. Instead of writing my own function, I just advise the isearch-search-fun-default
to include my own functions (isearch-str-forward
and isearch-str-backward
, just for the purpose of demo) so that everytime I type "abc", isearch will highlight and search the regexp a[ ]*b[ ]*c[ ]*
.
问题是,当我建议该功能然后执行abc的搜索时,它给了我的错误 I-search:abc [(void-function nil)]
。但是如果我将代码放在我的 defadvise
中,就可以在原始的 isearch-search-fun-default
函数中工作!所以我很困惑Elisp手册说 ad-do-it
只是原始功能代码的占位符,所以这两种方法,建议功能或改变原来的功能,应该产生相同的最后的代码。为什么我建议这个错误?
The problem is, when I advised the function and then do isearch of "abc", it gave me the error of I-search: abc [(void-function nil)]
. But if I put the code inside my defadvise
into the original isearch-search-fun-default
function, it works! So I get confused. The Elisp manual said ad-do-it
is just a placeholder for the original function code, so these two approaches, advising the function or changing the original function, should generate the same code at last. Why the error when I advise 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))
推荐答案
我不完全确定为什么你得到这些错误(我的猜测你可能需要使用 ad-return-value
),但为什么要使用建议?通常它应该是最后的手段,在这种情况下,很容易避免建议。
I'm not entirely sure why you're getting those errors (my guess is that you probably need to use ad-return-value
), but why use advice? Usually it should be a last resort, and in this case it is very easy to avoid advising.
此外,不要使用isearch将您的函数名称前缀。因为emacs只有一个命名空间(我不是在说lisp1和lisp2),所以很好的做法是将变量和函数唯一地命名。个人来说,我使用前缀jpk /,但我在这里使用了my-。
Also, don't prefix your function names with "isearch". Since emacs only has one namespace (I'm not talking about lisp1 vs. lisp2), it's good practice to name your variables and functions uniquely. Personally, I use a prefix "jpk/", but I've used "my-" here.
(defun my-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 my-isearch-str-forward (str &optional bound noerror)
"Search forward for STR."
(let ((string (my-isearch-mangle-str str)))
(re-search-forward string bound noerror)))
(defun my-isearch-str-backward (str &optional bound noerror)
"Search backward for STR."
(let ((string (my-isearch-mangle-str str)))
(re-search-backward string bound noerror)))
(defvar my-isearch-p t)
(defun my-isearch-search-fun ()
(if my-isearch-p
(if isearch-forward 'my-isearch-str-forward 'my-isearch-str-backward)
(isearch-search-fun-default)))
(setq isearch-search-fun-function 'my-isearch-search-fun)
另外,我不确定你想要什么,但它似乎类似于我的包。
Also, I'm not sure exactly what you want, but it seems similar to my package flex-isearch.
这篇关于isearch-search-fun-default的defadvice错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!