Saturday, April 18, 2015

Re: How to add new line after comma character if command is not part of a function?

On Saturday, April 18, 2015 at 12:57:55 AM UTC+8, Igor Forca wrote:
> Hi,
>
> *** Sample data ***
> sub(abc,xxx)
> sub(abc),abs(aaaa,bbb)
> avg(sub(abc),abs(aaaa,bbb)),del(xxx,yy)
> avg(sub(abc),abs(aaaa,bbb))
> trig(abc,bbb,ccc,sum(abc))
> *******************
>
> In above text I would like to add a new line character after comma in comma is not part of the function parameter. OK, I can explain this by rules.
>
> Rule 1:
> Type in command /( and press enter the ( is searched for.
> Press % to jump to the closing ).
> If comma character , is between above opening ( and closing ) character then there should be NO new line after comma character, simply because comma
> is delimiter between parameters inside function. This rule applies for instance for line 1.
>
> Rule 2:
> Now pressing n character to jump to next opening ( character.
> If n jumps over comma character then new line character should be added after comma, because comma in this case is not a function parameter delimiter.
> This rule applies to line two. Why? Cursor at first ( character [position 4] pressing % jumps on position [8]. Now pressing n jumps over comma character [position 9] to ( character in position [13]. So after comma [position 9] should be a new line.
>
> So final result should be:
> *******************
> sub(abc,xxx)
> sub(abc), <--- new line added
> abs(aaaa,bbb)
> avg(sub(abc),abs(aaaa,bbb)), <--- new line added
> del(xxx,yy)
> avg(sub(abc),abs(aaaa,bbb))
> trig(abc,bbb,ccc,sum(abc))
> *******************
>
> Any idea how to write search and replace?
> Thanks


this better not be solved by searching&replacing.

try this: first set a variable "level" to 0, then read in the text as a stream, for every character encountered execute:

*. if opening brace, increase level by 1
*. if closing brace, decrease level by 1
*. if comma and level==0, insert newline

if your language of choice doesn't allow changing the size of the sequence while iterating, you may first replace the comma with whatever unique character and after the iteration is over do search&replace, done.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: