Tuesday, September 1, 2009

My function needs to be sourced twice. Why?

Hi,

I'm trying to write a function in vimscript that would toggle a value either to its default value, or to an other value. Here is what I wrote.

" Toggles value to default or other
function! ToggleValue(value, default, other)
    exec 'let evalvalue = '.a:value
    if evalvalue == a:default
        exec 'let '.a:value.' = '.a:other
    else
        exec 'let '.a:value.' = '.a:default
    endif
    exec 'let '.a:value
endfunction

This seems to work, e.g.
:let g:foo = 0
:call ToggleValue("g:foo", 0, 1) "prints g:foo #0
:call ToggleValue("g:foo", 0, 1) "prints g:foo #1
:call ToggleValue("g:foo", 0, 1) "prints g:foo #0

However, I don't understand why, to get it working, I need to source my vimrc twice.

Can someone give me a hint?

Thank you!
--
Christophe-Marie Duquesne

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

No comments: