Monday, September 17, 2012

Mappings for reusing result of function without defining additional function

When using a function from a plugin, say F1() I want to check if the result is not the empty string, if it is call some other function, F2(), if it is not execute the string resulting from function F1().
Calling F1() has a side effect like moving the cursor around so I don't want to call it more than once per time I use the mapping.

In order to do this I create and additional function

function F1_wrap()
let g:f1_res = F1()
return g:f1_res
endfunction

and then define the mapping as
inoremap <c-a> <c-r>=(F1_wrap() == '') ? F2() : g:f1_res <cr>

This is of course not a big deal, but I was wondering if there is there a way of doing this without creating an additional function?


Thanks,
Jorge

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