Tuesday, June 7, 2011

Re: How to get returned value from python functions in vim scripts ?

On Jun 7, 8:36 am, Ivan Krasilnikov <inf...@gmail.com> wrote:
> On Tue, Jun 7, 2011 at 08:47, Lenin <lenin....@gmail.com> wrote:
> > Alec: It seems :py vim.command('return 1') will throw an error that says
> > "return not inside a function".
>
> It works fine if you call it inside a vim function:
>
> :function Func()
> :  py vim.command('return 1')
> :endfunction

The script vimuiex (2606 on vim.org) uses callbacks to process the
returned values:

In Vim:
function g:MyCallback(param)
" process the parameter
endfunc

In Python:
vim.eval("g:MyCallback(%d)", retval)

The script doesn't actually use global functions, they are always
local to the script. The drawback is that you have to pass to Python
the name of the function to call:

In vim:
exec vxlib#plugin#MakeSID() " this is defined in VxLib, script 3061;
it defines s:SNR
function s:MyCallback(param)
" process
endfunc
python DoSomething(s:SNR . 'MyCallback', ...)

In Python:
def DoSomething(fnToCall, ...)
#...
vim.eval("%s(%d)" % (fnToCall, retval))

Marko

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