Saturday, June 19, 2010

Re: numbering lines in groups

> What do you mean by a "function object"?

Well, you could read http://en.wikipedia.org/wiki/Function_object; I
meant an object initialized with the starting values, then called like
a function to get the next number string. That would avoid the nasty
global variables. Vim 'dictionaries' (associative arrays, a lot like
perl hashes) can be used a bit this. Here's my "pedestrian" effort
rewritten with a dictionary used as some kind of object:

function! Renum(lpb)
let d = {'b':1, 'l':0, 'lpb': a:lpb, 'result': function('ResInc')}
'<,'>s/^/\=d.result()/
endfunction
function! ResInc() dict
let result = printf('( %4d_%X_)', self.b, self.l)
if self.l < self.lpb
let self.l += 1
else
let self.l = 0
let self.b += 1
endif
return result
endfunction
vmap <F4> :<c-u>call Renum(4)<cr>

Note that globals have been banished.

Regards, John

--
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

No comments: