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[ ]*
.
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))