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
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:
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:
But you cant use it in Vim script directly, since it’s not an ex command. You
need something like:
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’swhat you need to check before using an ex command.
Back to the functions, note that the
:callcommand then allows you to calla 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
:executecomeshandy 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
:mcommand. 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
lineand do this:That’s an error, because there is no line numbered
line. Then you need:execto evaluate the expression before executing it — that’s what it does,takes an expression as argument, evaluated it and executed as an ex command.