2013-12-14 Searching for strings in math mode only

Sometimes I wish AUCTeX had a function similar to search-forward, but looking only at math mode and skipping everything outside it. Well, here it is;). Note: this is a very simplistic implementation, since if the given string appears between the point and end of the buffer, but only in text mode, it will jump there anyway. I’ll probably make it behave better someday, but since it is rather a quick hack, I’ll leave it this way for now. (Also, an isearch switch to look only at math or text mode would be nice. I’ll definitely look into it, too.)

(defun TeX-search-forward-math-mode (string)
  "Search forward for STRING, but only in (La)TeX math mode.
Crude implementation: if found in text mode, but not math mode, it
will move the point there."
  (interactive "MSearch in math mode: ")
  (while (progn
	   (search-forward string)
	   (not (texmathp)))))

What is especially nice about this is that it took me about 5 minutes to put this together, and more than half of that time was checking the interactive syntax (again…). This is what “extensibility” means: not only it is possible to extend Emacs’ capabilities, but it is easy and fast to do so.

A non-math version (as well as regex-searching analogues) are left as easy exercises for the reader;).)

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryTeX