auctex-devel
[Top][All Lists]

Re: [AUCTeX-devel] (old) TeX definitions and query-replace-regexp

 From: Mosè Giordano Subject: Re: [AUCTeX-devel] (old) TeX definitions and query-replace-regexp Date: Sat, 9 Aug 2014 21:59:34 +0200

Hi Uwe,

2014-08-09 21:15 GMT+02:00 Uwe Brauer <address@hidden>:
>
> Hi
>
> I am in a state of advanced desperation. I have a couple of old latex
> files which contain a lot of definitions (with arguments) of the sort
>
>
> \newcommand{\nv}[2]{\|#1\|_{H^{#2}}}
>
> which are used in the document like this
> $\nv{G^a}{s} \leq C_2$ and
>
> so I just wanted to have a simple elisp function which replaces the \nv
> calls by its definition.
>
> I thought it should be something like this
> (query-replace-regexp
> "\$$\\\\nv{[a-z,A-Z,0-9]*\\^[a-z,A-Z,0-9]*}{[a-z,A-Z,0-9]*}\$$"
> "\\\\|\\1\\\\|_{H^{\\2}}")
>
> but it failed, I tried something simpler
> (query-replace-regexp "\$$\\\\nv{G\\^a}{s}\$$" "\\\\|\\1\\\\|_{H^{\\2}}")
>
> no success neither. I admit regexps drive me crazy and would appreciate
> any help.

If you want to replace all occurrences of the regexp, using a while
re-search-forward' + replace-match' is a better idea.  Your error is
that you aren't specifying groups for the two arguments of the macro,
so you can't use them in the replace text.

The following should do the trick:
--8<---------------cut here---------------start------------->8---
(while (re-search-forward "\\\\nv{\$$[^}]*\$$}{\$$[^}]*\$$}" nil t)
(replace-match "\\\\|\\1\\\\|_{H^{\\2}}"))
--8<---------------cut here---------------end--------------->8---
but please note that it will automatically replace all occurrences of
\nv{...}{...}, so I'd suggest to try it in a test buffer.
re-builder' is your friend when you want to build regexp ;-)

Bye,
Mosè

`