I've just finished adding incremental search by java method name to the elisp minor mode jxminor that I maintain. When I started I thought it would be nearly impossible to do a decent job but it turned out much better than I could have hoped.
If your an elisp expert you may find what follows rather obvious. If so do you have any suggestions for improvement?
Anyway, the code that provides incremental search is 2074 lines of code and comments. There is no way I was going to be able to reproduce that functionality so I thought at first that my best option would be to patch it somehow. The actual work of searching is done by one function isearch-search and it chooses between word-search-forward/backward, re-search-forward/backward and search-forward/backward depending on various variable settings. If you just press C-s or C-r you'll end up calling search-forward or search-backward. If another pair of functions could be added that would solve the problem.
However, when I looked at the code though I had the thought "if I could just rebind search-forward and search-backward so that my functions got called instead that would solve the problem". This is easy in scheme as there's only one namespace. Looking around I found the function fset that changes the function that a symbol points to. Now the problem is reduced to being able to save the functions search-forward/backward are bound to, rebind them to the method name search functions, execute isearch-search and restore the original bindings.
Turns out this is easy as well. Using the defadvice macro you can arrange for code you write to be "wrapped around" any other function. So the wrapper I wrote does the save, execute and restore described. What's even better is it's easy to turn the wrapper function on and off so you don't have to have a complex one that can deal with being called in all sorts of situations.
The final solution is as simple as having the java incremental search function turn the wrapper on for isearch-search and then call the normal incremental search function. This gives you the full mini buffer editing capability that isearch provides but with a different underlying function to do the actual searching.
I was very impressed, as I always am, with what you can do in emacs and how very easy it is to do it.
Posted by Alex at February 28, 2002 10:19 PM