1

For example, if I type "foo spam" after the prompt of isearch-forward, how can I advice some functions in isearch.el to convert the string to "foo[.?:,!]?[ \n]spam" before search? Basically I want to search for the two adjacent words in the text. I know I can use isearch-forward-regexp to do that, but it's painful and error-prone to type out the regexp directly and I have many strings of that kind to search. I tried to understand code in isearch.el but failed. hope some Emacs gurus could help me out. Thanks.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
RNA
  • 146,987
  • 15
  • 52
  • 70

2 Answers2

4

You may be interested in isearch-toggle-word (M-s w) during your incremental search.

http://www.gnu.org/software/emacs/manual/html_node/emacs/Word-Search.html

This will search for individual words ignoring punctuation, spaces and line ends.

Juancho
  • 7,207
  • 27
  • 31
  • +1. I didn't know `M-s w` can search multiple words. However, I still think preprocess the input string for isearch is still valuable because it would provide a general solution to a wider kinds of problems. For example, even in this situation, what if one want to see two adjacent words in one sentence (without punctuations). – RNA May 01 '13 at 03:31
1

Here's a rough crack at it:

(defun isearch-transform-string ()
  (interactive)
  (let* ((string (replace-regexp-in-string
                  "foo" "bar" isearch-string)))
    (setq isearch-string string
          isearch-message (mapconcat 'isearch-text-char-description string ""))
    (isearch-search-and-update)))

(define-key isearch-mode-map (kbd "C-c C-t") 'isearch-transform-string)

I think you have to bind it to a key in isearch-mode-map; I don't think M-x works quite right in isearch-mode.

If you require more power, there is a variable isearch-search-fun-function, which you can set to a function that returns a function. The returned function is used to find the next match.

jpkotta
  • 9,237
  • 3
  • 29
  • 34
  • the first solution works but not ideal. I tried the second one but have an error, which I posted as another question: http://stackoverflow.com/questions/16333324/defadvice-error-for-isearch-search-fun-default – RNA May 02 '13 at 08:19