Loading ...
Sorry, an error occurred while loading the content.

My function needs to be sourced twice. Why?

Expand Messages
  • Christophe-Marie Duquesne
    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
    Message 1 of 3 , Sep 1, 2009
    • 0 Attachment
      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
      -~----------~----~----~----~------~----~------~--~---

    • Christophe-Marie Duquesne
      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
      Message 2 of 3 , Sep 1, 2009
      • 0 Attachment
        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@...> 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
        -~----------~----~----~----~------~----~------~--~---

      • Charles Campbell
        ... 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
        Message 3 of 3 , Sep 1, 2009
        • 0 Attachment
          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


          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        Your message has been successfully submitted and would be delivered to recipients shortly.