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

Re: setline vs call setline

Expand Messages
  • ZyX
    Reply to message «setline vs call setline», sent 21:20:49 01 July 2011, Friday by cyboman: You cannot ever call a function with ... . Functions are
    Message 1 of 6 , Jul 1, 2011
      Reply to message «setline vs call setline»,
      sent 21:20:49 01 July 2011, Friday
      by cyboman:

      You cannot ever call a function with
      :function()
      . Functions are accessible only from something that forces expression context
      (and :call command is one of these). Commands are not functions, though
      sometimes they have similar names (like `call', `function', `substitute').

      Original message:
      > i'm writing a small script which uses setline. i'm somewhat new to vim
      > scripting. whenever setline was called i would get an error saying
      > that this is not an editor command. after looking up in the
      > documentation for setline, i changed it to call setline and everything
      > started working. but i'm confused. when should i use call and when i
      > simply just call a function?
      >
      > any help is appreciated.
    • Taylor Hedberg
      Vim scripts are composed of ex commands. Functions are expressions, not ex commands, so they can t be used bare in a script; that is, they must be wrapped
      Message 2 of 6 , Jul 1, 2011
        Vim scripts are composed of ex commands. Functions are expressions, not
        ex commands, so they can't be used "bare" in a script; that is, they
        must be "wrapped" in a proper command.

        Not a valid Vimscript statement:

        foo()

        Valid:

        call foo()

        :call is just an ex command that essentially just invokes the given
        function and ignores its result. This is useful when you just care about
        the side effects of a function but not the value it returns (if any).

        Plenty of other commands can call functions as well, and all are valid
        as statements in a script. The only restriction is that you can't invoke
        a function by itself, without some kind of command that wraps it.
        Another simple example is :echo, which evaluates its argument (which may
        be a function) and prints the result.

        Hopefully that clears things up a little.

        --
        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
      • Tony Mechelynck
        ... In addition to all the replies you already got, there is an additional possibility: if setline(lnum, text) echoerr something went wrong calling setline(
        Message 3 of 6 , Jul 2, 2011
          On 01/07/11 19:20, cyboman wrote:
          > i'm writing a small script which uses setline. i'm somewhat new to vim
          > scripting. whenever setline was called i would get an error saying
          > that this is not an editor command. after looking up in the
          > documentation for setline, i changed it to call setline and everything
          > started working. but i'm confused. when should i use call and when i
          > simply just call a function?
          >
          > any help is appreciated.
          >

          In addition to all the replies you already got, there is an additional
          possibility:

          if setline(lnum, text)
          echoerr "something went wrong calling setline(" lnum
          \ . ", '" . string(text) . "')"
          endif

          since setline() returns a non-zero value if there was an error.


          Best regards,
          Tony.
          --
          Lackland's Laws:
          (1) Never be first.
          (2) Never be last.
          (3) Never volunteer for anything

          --
          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
        • cyboman
          ... thanks for the help everyone. i think i got it. taylor, your explanation did clear it up. -- You received this message from the vim_use maillist. Do not
          Message 4 of 6 , Jul 2, 2011
            On Jul 1, 1:38 pm, Taylor Hedberg <tmhedb...@...> wrote:
            > Vim scripts are composed of ex commands. Functions are expressions, not
            > ex commands, so they can't be used "bare" in a script; that is, they
            > must be "wrapped" in a proper command.
            >
            > Not a valid Vimscript statement:
            >
            >     foo()
            >
            > Valid:
            >
            >     call foo()
            >
            > :call is just an ex command that essentially just invokes the given
            > function and ignores its result. This is useful when you just care about
            > the side effects of a function but not the value it returns (if any).
            >
            > Plenty of other commands can call functions as well, and all are valid
            > as statements in a script. The only restriction is that you can't invoke
            > a function by itself, without some kind of command that wraps it.
            > Another simple example is :echo, which evaluates its argument (which may
            > be a function) and prints the result.
            >
            > Hopefully that clears things up a little.

            thanks for the help everyone. i think i got it. taylor, your
            explanation did clear it up.

            --
            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
          Your message has been successfully submitted and would be delivered to recipients shortly.