Reputation: 940
Why does this exceedingly simple function:
function! ParseAllEvents()
let i = 1
while i > 0
exec 'ParseEvent('.i.')'
let i -= 1
endwhile
endfunction
and/or:
function! ParseAllEvents()
let i = 1
while i > 0
ParseEvent(i)
let i -= 1
endwhile
endfunction
Generate this error?
E488: Trailing Characters: ParseEvent(1)
The ParseEvent(i)
function works fine when called in the command line
Upvotes: 2
Views: 1882
Reputation: 59307
So, as we've been discussing in the comments, it was a matter of "calling" the
function by prepending a :call
.
Generally a function will be evaluated anywhere an expression is expected, however this does not mean that they are evaluated directly in your script, since Vim script is just a chain of ex commands (those which begin with a colon). A function is not an ex command.
Let's come to the practical side, take a look in what the user manual says in chapter 41:
41.3 Expressions
Vim has a rich, yet simple way to handle expressions. You can read the definition here: |expression-syntax|. Here we will show the most common items.
The numbers, strings and variables mentioned above are expressions by themselves. Thus everywhere an expression is expected, you can use a number, string or variable. Other basic items in an expression are:
$NAME environment variable &name option @r register
The expressions referred here aren't the ex commands. Most of the time expressions are evaluated in the commands arguments. This is a Vim expression:
i+=1
But you cant use it in Vim script directly, since it's not an ex command. You need something like:
:let i+=1
Now check the help for :let
:
:let {var-name} = {expr1} :let E18 Set internal variable {var-name} to the result of the expression {expr1}. The variable will get the type from the {expr}. If {var-name} didn't exist yet, it is created.
We're looking for {expr1}
. This means an expression is expected — that's
what you need to check before using an ex command.
Back to the functions, note that the :call
command then allows you to call
a function in an ex context.
So if the command being used expects an expression argument, go ahead and
include your functions, and other regular stuff. They will be evaluated,
variables will have their value "yielded" and so on. The :execute
comes
handy if the command accepts a text argument. For example, if you need to move
the current line to a line number stored in a variable, you can use the :m
command. The help:
:[range]m[ove] {address} :m :mo :move E134 Move the lines given by [range] to below the line given by {address}.
As you can see, an address is expected directly, not an expression. If you
have the number in a variable called line
and do this:
:m line
That's an error, because there is no line numbered line
. Then you need
:exec
to evaluate the expression before executing it — that's what it does,
takes an expression as argument, evaluated it and executed as an ex command.
:exec "m " . line
" ^^^^^^^^^^^
" This expression evaluates to, say, "m 14" which is then executed
Upvotes: 1