Wednesday, August 7, 2013

How to append lines when iterating over a range.

I want to convert lines like this:

value1|value2|value3

into:

def foo(x="value1"
y="value2"
z="value3")

But I ran into a problem when trying to apply this function to a range. The range gets confused because I am appending additional lines. I solved this problem by using the range keyword and using a:firstline, a:lastline, and a for loop that skips every four lines.

Something like this:
This is the incomplete version because the actual problem I'm trying to solve is slightly different, but the crux of the problem is still the same.

function! ReFormat() range
let num_lines = a:lastline - a:firstline + 1

for linenum in range(a:firstline, a:firstline + (num_lines - 1) * 4, 4)
let line = getline(linenum)
...
endfor
endfunction

Is there not some helper method that knows how to append using ranges. My coworker said that in emacs he's able to get the current cursor position. In vim we can get the current line '.', but if we could get the cursor position as the range iterates, we wouldn't have to worry about the number of lines appended.

Thanks for reading my rambling,
Eric

--
--
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/groups/opt_out.

No comments: