Sunday, July 24, 2011

Re: why is call needed in vimscript?

On 25/07/11 00:33, Jose Caballero wrote:
> Hi,
>
> I was trying to write a small function in vimscript that moves the
> cursor to a given line, in order to perform some actions.
> I was trying with something like this
>
> function F()
> ....
> cursor(4,0)
> ....
> endfunction
>
>
> but I got and E492 error.
> However, with
>
> call cursor(4,0)
>
> everything seems to work.
> Can someone point me to a good link when it is explain why and when call
> is needed?
> :help call doesn't explain it, or I don't understand it.
>
>
>
> Thanks,
> Jose
>
> --
> 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

cursor(4,0) is the _value_ of the function. For instance, in a Vim
compiled with +float, acos(-1) is the arc-cosine (in radians) of minus
one, i.e., the number pi. All functions return a value (if you execute a
:return statement without an explicit return value, the function returns
the integer zero).

You can use that value in an expression: e.g.

if cursor(4,0)
echoerr 'Couldn't set cursor to (4,0)'
endif

With :call, you call the function and throw away the return value —
meaning that in that case you're only interested in the "side effects"
(moving the cursor, in this example) and not in the result (for an
arithmetic function, the result of the calculation; the result of the
cursor() function can be 0 for success or -1 for failure).

":help :call" says it quite clearly IMO: see the first and last
sentences of its first paragraph, quoted below:

> Call a function. The name of the function and its arguments
> are as specified with |:function|. Up to 20 arguments can be
> used. The returned value is discarded.


Best regards,
Tony.
--
Come, let us hasten to a higher plane,
Where dyads tread the fairy fields of Venn,
Their indices bedecked from one to _n_,
Commingled in an endless Markov chain!
-- Stanislaw Lem, "Cyberiad"

--
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

No comments:

Post a Comment