What if #if/#endif blocks are nested ?

Yakov is correct, that nested #if/#endif blocks would cause trouble.

My first thought at a [100% untested] solution would be something like

:g/^\s*#endif/norm dV%

which would find all of the #endif tokens and delete their associated blocks, exploiting Vim's ability to use "%" to match #if/#else/#endif blocks. However, funky behaviors emerge if you have an #else block, and you want some of it in to remain behind:

#if COWS_ROLLERSKATE
        foo = "limbo, limbo";
#else
        foo = "mu"
#endif


With my suggestion, you would delete the whole thing, and it would look like "foo" never gets set. It also nukes *all* the if/endif blocks, not just select ones (such as if you only wanted to nuke the "#if COWS_ROLLERSKATE" blocks, but not "#if POPE_CATHOLIC" blocks)

If you don't have #else clauses, you might be able to solve that 2nd problem with something like

:g/^\s*#if COWS_ROLLERSKATE/norm dV%

Otherwise, you'd have to do some funky testing, along the lines of (again, 100% untested)

:g/^\s*#if COWS/exec("norm %")| exec('norm '.(getline(".")=~'^\s*#else'?'%':'').'dV%')

which should check if, after using "%", you landed on an #else line, in which case it adds an extra "%" to go to the "#endif" line before deleteing back to the original. All sorts of odd cases occur with nesting and #else blocks, so you'd have to tinker to get the desired behavior.

-tim



Reply via email to