0

这是我上一篇文章的继续(是否可以在 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))
4

1 回答 1

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

于 2013-05-02T23:51:28.553 回答