Tuesday, September 1, 2009

Re: My function needs to be sourced twice. Why?

Yes,

I'm answering my own mail : the function was already defined elsewhere (I had written a file in .vim/plugin/ where I wanted to define this function, and I had forgotten to remove it. I just lost 2 hours :$


On Tue, Sep 1, 2009 at 3:31 PM, Charles Campbell <Charles.E.Campbell@nasa.gov> wrote:

Christophe-Marie Duquesne wrote:
> 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?
Hmm -- it seemed to work fine for me with but a single sourcing.
However, the results I got were

g:foo                 #1
g:foo                 #0
g:foo                 #1

which seems correct to me (a quiet initialize g:foo to zero, toggle to
1, toggle to 0, toggle to 1).

Regards,
Chip Campbell






--
Christophe-Marie Duquesne

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

No comments:

Post a Comment