(query-replace "foo" "bar") shows you all occurrences of foo and offers to replace them by bar.

I would love to have a version of query-replace that only picks the occurrences of foo that are "in some context" (to be defined). At the moment, I'm thinking of LaTeX files with the context being "any part that is in math style". Or any part not in math style. These parts are already highlighted by Emacs' latex-mode so it can recognize them.

query-replace can already perform a bit of context checking since its DELIMITED argument allows restricting to occurrences surrounded by word boundaries, but this is a simple special case that can be implemented with regular expressions, e.g., looking for \<foo\>.

My question: do you know of an extension of query-replace in some package somewhere that can do the replace-only-in-math-style trick I need? Or of something similar enough that would be a good start for tweaking and hacking around?

  • 2
    I don't know any such tool, but if you want to write it you may want to give a look at texmathp.el (it's bundled into AUCTeX but doesn't require it to work), which provides you the function texmathp to check whether point is inside math mode. – giordano Nov 8 '15 at 14:16
  • Thanks a million. I've followed your advice and looked at texmathp. It seems that I just have to make a copy of the original query-replace and insert a (if (texmathp) ..) at the right place. I'll try that and report on the outcome. – phs Nov 8 '15 at 14:51
  • Now that I know about the isearch-filter-predicate variable I have set it to something that calls texmathp. It is annoying that they do not have the same interface: isearch-filer-predicate wants a (beg end) region argument, texmathp looks at point in buffer. I'd suggest just allowing an optional pos argument to texmathp. – phs Nov 8 '15 at 20:43
  • @phs Did you manage to bind "isearch-filter-predicate to something that calls texmathp"? I am also interested in this approach. – Gabriele Nicolardi Mar 4 '19 at 22:08
  • @Gabriele. Yes, see my code at http://www.lsv.fr/~phs/query-replace-in-tex-math-only.el – phs Mar 5 '19 at 18:03

Yes. If you use libraries Isearch+ and Zones (including optional library isearch-prop.el), and you set option isearchp-query-replace-zones-flag. to non-nil, then replacement commands such as query-replace are limited to the buffer zones you define.

This works by binding isearch-filter-predicate to a function that recognizes the zones.

(Isearch works the same way - you can limit searching to defined zones. And if you use Bookmark+ then you can bookmark a set of zones.)

You can also query-replace (or search) the complement of a set of zones. Toggle this behavior using C-M-~ during Isearch.

  • Thanks for the info about the libraries and the explanations. For the time I am happy with just binding isearch-filter-predicate to something that calls texmathp but I understand that your solution is more generic. – phs Nov 8 '15 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.