isearch-search-fun-default的defadvice错误

时间:2013-05-02 08:16:09

标签: emacs elisp defadvice advising-functions

这是我之前的帖子(is it possible to preprocess the input string before isearch-forward in Emacs)的继续。我正在尝试使用变量jpkotta来实现isearch-search-fun-function的答案。我只是建议isearch-search-fun-default包含我自己的函数(isearch-str-forwardisearch-str-backward,而不是编写我自己的函数,只是为了演示的目的),这样每次我输入“abc”, isearch将突出显示并搜索正则表达式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))

1 个答案:

答案 0 :(得分:1)

我不完全确定你为什么会收到这些错误(我猜你可能需要使用ad-return-value),但为什么要使用建议呢?通常它应该是最后的手段,在这种情况下,很容易避免建议。

另外,不要在函数名前加上“isearch”。由于emacs只有一个命名空间(我不是在谈论lisp1与lisp2),因此最好将变量和函数命名为唯一的。就个人而言,我使用前缀“jpk /”,但我在这里使用了“my-”。

(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)

另外,我不确定你想要什么,但它看起来与我的包flex-isearch相似。