The main question is in the title, that is, I have a function, which is invoked by the user and it should 'remember' necessary stuff between invocations, so the next time the user calls it (like, presses the mapping) the function should be aware of certain data.
So if anyone could suggest how can I better achieve this, it'd great.
But I also have the current attempt, which is trying to make this through a mapping and storing values inside a variable of a higher scope. And it doesn't work, for some reason it works only for the odd calls. That is, I source the script, go to the insert mode, invoke the mapping 10 times, then
:echo b:var and it's 5. If I press the mapping 9 times it also equals to 5. If I press 11 times, it's 6.
let b:var = 0 function! Foo(arg) return a:arg + 1 endfunction inoremap a <Esc>:let b:var = Foo(b:var)<CR>
Could someone explain, what's happening here, please?
I'd rather use return values, than directly setting
b:var inside the function, since it should be easier to make automated tests.