substitutions using grammars

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

substitutions using grammars

Pablo Garralda

I'm not a vim expert user, but I've been using it long enough to make it my
favorite :) . Anyway, I'm using Vim to edit LaTeX documents with lots macros
and grouping and, sometimes, I feel that regular expressions are not enough.
Imagine that I'm re-arranging the macros and I want to erase the 2nd argument
of "somemacro".

i.e I want to turn this

\somemacro{ Exp1 }{ Exp2 }{Exp 3}

into something like

\somemacro{ Exp1 }{Exp 3}.

Is there any way to use LaTeX grammar (with some plugin or script) to make this
simple substitution. Bear in mind that the expressions can contain an arbitrary
number of {grouping} nesting.

Any hint?

Cheers,
----------------------------------
Pablo Garralda
Stevens Institute of Technology
----------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: substitutions using grammars

Tim Chase-2
> i.e I want to turn this
>
> \somemacro{ Exp1 }{ Exp2 }{Exp 3}
>
> into something like
>
> \somemacro{ Exp1 }{Exp 3}.
>
> Is there any way to use LaTeX grammar (with some plugin or
> script) to make this simple substitution. Bear in mind that
> the expressions can contain an arbitrary number of {grouping}
> nesting.

It's the last bit of that about "arbitrary number of {grouping}
nesting" that throws a wrinkle in the matter.  Vim handles the
simple case pretty well...something like this untested:

:%s/\(\\somemacro{\_[^}]*}\)\_s*{\_[^}]*}\_s*\({\_[^}]*}\)/\1\3

However, when you start talking about arbitrary nestings, things
get a little hairier.

You might be able to create custom macros each time, making use
of the "%" command to match pairs of "{" and "}"
characters...something like

nnoremap <f4> /\\somemacro\_s*{/e<cr>%/{<cr>da{

However, it would likely be a case-by-case sort of composition of
these replacements.  Just a few musings on the matter.

-tim