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

Documentation of Funcref

Expand Messages
  • Jakson Alves de Aquino
    Hi, The documentation of Funcref includes the following statement: A Funcref variable must start with a capital, s: , w: , t: or b: . It seems that
    Message 1 of 5 , Aug 22, 2013
    • 0 Attachment
      Hi,

      The documentation of Funcref includes the following statement:

      A Funcref variable must start with a capital, "s:", "w:",
      "t:" or "b:".

      It seems that sentence is incomplete. I think the intention was to
      write:

      A Funcref variable must start with a capital letter and both
      the function being referred to and the Funcref variable must
      have the same scope, being prefixed by "s:", "w:", "t:" or
      "b:".

      But there is other problem in the documentation: although not
      mentioned, "g:" also works even if it isn't explicitly prefixed in
      the script, as in the example below where script_b.vim sources
      script_a.vim. If we do :so % while editing script_b.vim,
      FunctionA is executed:

      " script_a.vim:
      function FunctionA()
      echo "Hello World!"
      endfunction

      " script_b.vim
      source script_a.vim
      let CallFunctionA = function("FunctionA")
      call CallFunctionA()

      Note: I'm using global Funcref to global functions in the
      Vim-R-plugin. So I hope that the problem is in the documentation
      and not in the actual Vim behavior.

      Thanks!

      --
      Jakson Alves de Aquino
      Federal University of Ceará
      Social Sciences Department
      www.lepem.ufc.br/aquino.php

      --
      --
      You received this message from the "vim_dev" 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

      ---
      You received this message because you are subscribed to the Google Groups "vim_dev" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Nikolay Pavlov
      On Aug 23, 2013 1:24 AM, Jakson Alves de Aquino ... I do not understand. Vim currently has nothing else but global functions. There are
      Message 2 of 5 , Aug 22, 2013
      • 0 Attachment


        On Aug 23, 2013 1:24 AM, "Jakson Alves de Aquino" <jalvesaq@...> wrote:
        >
        >
        > Hi,
        >
        > The documentation of Funcref includes the following statement:
        >
        >       A Funcref variable must start with a capital, "s:", "w:",
        >       "t:" or "b:".
        >
        > It seems that sentence is incomplete. I think the intention was to
        > write:
        >
        >       A Funcref variable must start with a capital letter and both
        >       the function being referred to and the Funcref variable must
        >       have the same scope, being prefixed by "s:", "w:", "t:" or
        >       "b:".

        I do not understand. Vim currently has nothing else but global functions. There are no scoped ones, s:Fname is simply a shortcut for <SNR>N_Fname, where N is current script number. Anonymous functions are global ones with just N as their name, N is number of anonymous functions defined in this session (i.e. it is just an incremented counter).

        Thus I have not seen the "must have the same scope" behavior you describe.

        Also note that using b:Fname (or g:Fname) as function name is undocumented. And it is as well NOT a name of buffer-local function.

        > But there is other problem in the documentation: although not
        > mentioned, "g:" also works even if it isn't explicitly prefixed in
        > the script, as in the example below where script_b.vim sources
        > script_a.vim. If we do :so %  while editing script_b.vim,
        > FunctionA is executed:
        >
        >       " script_a.vim:
        >       function FunctionA()
        >           echo "Hello World!"
        >       endfunction
        >
        >       " script_b.vim
        >       source script_a.vim
        >       let CallFunctionA = function("FunctionA")
        >       call CallFunctionA()
        >
        > Note: I'm using global Funcref to global functions in the
        > Vim-R-plugin. So I hope that the problem is in the documentation
        > and not in the actual Vim behavior.

        I would say that putting funcrefs directly into global or function-local variable is the worst idea: your code will break if someone defines CallFunctionA function. Use dictionaries:

            let d={}
            let d.CallFunctionA = function("FunctionA")

        > Thanks!
        >
        > --
        > Jakson Alves de Aquino
        > Federal University of Ceará
        > Social Sciences Department
        > www.lepem.ufc.br/aquino.php
        >
        > --
        > --
        > You received this message from the "vim_dev" 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
        >
        > ---
        > You received this message because you are subscribed to the Google Groups "vim_dev" group.
        > To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
        > For more options, visit https://groups.google.com/groups/opt_out.

        --
        --
        You received this message from the "vim_dev" 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
         
        ---
        You received this message because you are subscribed to the Google Groups "vim_dev" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • Tony Mechelynck
        ... The function is global. The _Funcref variable_ is a variable and as such it can be a global variable, or it can be script-local, buffer-local, window-local
        Message 3 of 5 , Aug 22, 2013
        • 0 Attachment
          On 23/08/13 06:01, Nikolay Pavlov wrote:
          >
          > On Aug 23, 2013 1:24 AM, "Jakson Alves de Aquino" <jalvesaq@...
          > <mailto:jalvesaq@...>> wrote:
          > >
          > >
          > > Hi,
          > >
          > > The documentation of Funcref includes the following statement:
          > >
          > > A Funcref variable must start with a capital, "s:", "w:",
          > > "t:" or "b:".
          > >
          > > It seems that sentence is incomplete. I think the intention was to
          > > write:
          > >
          > > A Funcref variable must start with a capital letter and both
          > > the function being referred to and the Funcref variable must
          > > have the same scope, being prefixed by "s:", "w:", "t:" or
          > > "b:".
          >
          > I do not understand. Vim currently has nothing else but global
          > functions. There are no scoped ones, s:Fname is simply a shortcut for
          > <SNR>N_Fname, where N is current script number. Anonymous functions are
          > global ones with just N as their name, N is number of anonymous
          > functions defined in this session (i.e. it is just an incremented counter).

          The function is global. The _Funcref variable_ is a variable and as such
          it can be a global variable, or it can be script-local, buffer-local,
          window-local or tabpage-local. Not mentioned is the case where the
          variable would be function-local. Such locality just means that the
          _variable_ is only visible from within the same scope; it says nothing
          about the underlying function which can be called indirectly through the
          variable.

          The difference here is between

          function Funcname(arg1, arg2, arg3)
          ...
          endfunction

          and

          let t:VarFuncRef = function("Funcname")

          Funcname() is the function, and as such it is global.
          t:VarFuncRef is a Funcref variable referencing that function, and in
          this case the variable is tabpage-local.

          A global function can, however, be defined with s: (or the equivalent
          bur more cumbersome SID_) and then it will normally not be visible from
          outside the script where it was defined, except maybe via a
          non-script-local mapping defined within the same script. In order to use
          that s:Function as argument to function() to create a Funcref you must
          be within that very same script.

          >
          > Thus I have not seen the "must have the same scope" behavior you describe.
          >
          > Also note that using b:Fname (or g:Fname) as function name is
          > undocumented. And it is as well NOT a name of buffer-local function.

          See above.

          >
          > > But there is other problem in the documentation: although not
          > > mentioned, "g:" also works even if it isn't explicitly prefixed in
          > > the script, as in the example below where script_b.vim sources
          > > script_a.vim. If we do :so % while editing script_b.vim,
          > > FunctionA is executed:
          [...]

          It is stated elsewhere that g: is implicit for variables used anywhere
          except inside a function, where l: is implicit.


          Best regards,
          Tony.
          --
          A long memory is the most subversive idea in America.

          --
          --
          You received this message from the "vim_dev" 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

          ---
          You received this message because you are subscribed to the Google Groups "vim_dev" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Jakson Alves de Aquino
          ... I m glad that my understanding of the sentence was wrong because I want both the function and the Funcref to be global. ... Thanks for the suggestion, but
          Message 4 of 5 , Aug 23, 2013
          • 0 Attachment
            Em 23-08-2013 01:01, Nikolay Pavlov escreveu:
            >
            > On Aug 23, 2013 1:24 AM, "Jakson Alves de Aquino" <jalvesaq@...
            > <mailto:jalvesaq@...>> wrote:
            >>
            >>
            >> Hi,
            >>
            >> The documentation of Funcref includes the following statement:
            >>
            >> A Funcref variable must start with a capital, "s:", "w:",
            >> "t:" or "b:".
            >>
            >> It seems that sentence is incomplete. I think the intention was to
            >> write:
            >>
            >> A Funcref variable must start with a capital letter and both
            >> the function being referred to and the Funcref variable must
            >> have the same scope, being prefixed by "s:", "w:", "t:" or
            >> "b:".
            >
            > I do not understand. Vim currently has nothing else but global
            > functions. There are no scoped ones, s:Fname is simply a shortcut for
            > <SNR>N_Fname, where N is current script number. Anonymous functions are
            > global ones with just N as their name, N is number of anonymous
            > functions defined in this session (i.e. it is just an incremented counter).
            >
            > Thus I have not seen the "must have the same scope" behavior you describe.

            I'm glad that my understanding of the sentence was wrong because I
            want both the function and the Funcref to be global.

            > Also note that using b:Fname (or g:Fname) as function name is
            > undocumented. And it is as well NOT a name of buffer-local function.
            >
            >> But there is other problem in the documentation: although not
            >> mentioned, "g:" also works even if it isn't explicitly prefixed in
            >> the script, as in the example below where script_b.vim sources
            >> script_a.vim. If we do :so % while editing script_b.vim,
            >> FunctionA is executed:
            >>
            >> " script_a.vim:
            >> function FunctionA()
            >> echo "Hello World!"
            >> endfunction
            >>
            >> " script_b.vim
            >> source script_a.vim
            >> let CallFunctionA = function("FunctionA")
            >> call CallFunctionA()
            >>
            >> Note: I'm using global Funcref to global functions in the
            >> Vim-R-plugin. So I hope that the problem is in the documentation
            >> and not in the actual Vim behavior.
            >
            > I would say that putting funcrefs directly into global or function-local
            > variable is the worst idea: your code will break if someone defines
            > CallFunctionA function. Use dictionaries:
            >
            > let d={}
            > let d.CallFunctionA = function("FunctionA")

            Thanks for the suggestion, but actually I incentive people to
            create extensions to the Vim-R-plugin which will source other
            scripts if the vimrplugin_source variable is defined.

            --
            Jakson Alves de Aquino
            Federal University of Ceará
            Social Sciences Department
            www.lepem.ufc.br/aquino.php

            --
            --
            You received this message from the "vim_dev" 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

            ---
            You received this message because you are subscribed to the Google Groups "vim_dev" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • Jakson Alves de Aquino
            ... Thanks for the explanations, Nikolay Pavlov and Tony Mechelynck! Now it s clear to me. Best regards! -- Jakson Alves de Aquino Federal University of Ceará
            Message 5 of 5 , Aug 23, 2013
            • 0 Attachment
              Em 23-08-2013 03:04, Tony Mechelynck escreveu:
              > On 23/08/13 06:01, Nikolay Pavlov wrote:
              >>
              >> On Aug 23, 2013 1:24 AM, "Jakson Alves de Aquino" <jalvesaq@...
              >> <mailto:jalvesaq@...>> wrote:
              >> >
              >> >
              >> > Hi,
              >> >
              >> > The documentation of Funcref includes the following statement:
              >> >
              >> > A Funcref variable must start with a capital, "s:", "w:",
              >> > "t:" or "b:".
              >> >
              >> > It seems that sentence is incomplete. I think the intention was to
              >> > write:
              >> >
              >> > A Funcref variable must start with a capital letter and both
              >> > the function being referred to and the Funcref variable must
              >> > have the same scope, being prefixed by "s:", "w:", "t:" or
              >> > "b:".
              >>
              >> I do not understand. Vim currently has nothing else but global
              >> functions. There are no scoped ones, s:Fname is simply a shortcut for
              >> <SNR>N_Fname, where N is current script number. Anonymous functions are
              >> global ones with just N as their name, N is number of anonymous
              >> functions defined in this session (i.e. it is just an incremented
              >> counter).
              >
              > The function is global. The _Funcref variable_ is a variable and as such
              > it can be a global variable, or it can be script-local, buffer-local,
              > window-local or tabpage-local. Not mentioned is the case where the
              > variable would be function-local. Such locality just means that the
              > _variable_ is only visible from within the same scope; it says nothing
              > about the underlying function which can be called indirectly through the
              > variable.
              >
              > The difference here is between
              >
              > function Funcname(arg1, arg2, arg3)
              > ...
              > endfunction
              >
              > and
              >
              > let t:VarFuncRef = function("Funcname")
              >
              > Funcname() is the function, and as such it is global.
              > t:VarFuncRef is a Funcref variable referencing that function, and in
              > this case the variable is tabpage-local.
              >
              > A global function can, however, be defined with s: (or the equivalent
              > bur more cumbersome SID_) and then it will normally not be visible from
              > outside the script where it was defined, except maybe via a
              > non-script-local mapping defined within the same script. In order to use
              > that s:Function as argument to function() to create a Funcref you must
              > be within that very same script.
              >
              >>
              >> Thus I have not seen the "must have the same scope" behavior you
              >> describe.
              >>
              >> Also note that using b:Fname (or g:Fname) as function name is
              >> undocumented. And it is as well NOT a name of buffer-local function.
              >
              > See above.
              >
              >>
              >> > But there is other problem in the documentation: although not
              >> > mentioned, "g:" also works even if it isn't explicitly prefixed in
              >> > the script, as in the example below where script_b.vim sources
              >> > script_a.vim. If we do :so % while editing script_b.vim,
              >> > FunctionA is executed:
              > [...]
              >
              > It is stated elsewhere that g: is implicit for variables used anywhere
              > except inside a function, where l: is implicit.

              Thanks for the explanations, Nikolay Pavlov and Tony Mechelynck!
              Now it's clear to me.

              Best regards!

              --
              Jakson Alves de Aquino
              Federal University of Ceará
              Social Sciences Department
              www.lepem.ufc.br/aquino.php

              --
              --
              You received this message from the "vim_dev" 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

              ---
              You received this message because you are subscribed to the Google Groups "vim_dev" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            Your message has been successfully submitted and would be delivered to recipients shortly.