Thank you all, it's very helpful.

2011/6/8 Vlad Irnov <[email protected]>

>
>
> On Jun 7, 12:47 am, Lenin <[email protected]> wrote:
> > Alec: It seems :py vim.command('return 1') will throw an error that says
> > "return not inside a function".
> >
> > Marcin: Yes, I am using vim variables to transfer values from python
> > functions currently, but I think this method is ugly, I'd like to know a
> > better solution.
>
> It may be ugly but it's perfectly serviceable. It can also be tricky
> because the exact syntax depends on the type of value being
> transfered.
> For strings
>  :py vim.command("let g:vim_var='%s'" %python_var.replace("'", "''"))
> For integers, simple dictionaries and lists:
>  :py vim.command("let g:vim_var=%s" %python_var)
>  :py vim.command("let g:vim_var=%s" %({'a':1,'b':2}))
>
> With strings, backslashes and quotes need to be taken care of. The
> easiest is to use single quotes in Vim assignment and double all
> single quotes in strings. Example:
>
> python << EOF
> import vim
> def py_func(): return """a'b"c\nd\t\\e"""
> EOF
> py print repr(py_func())
> py vim.command("let g:py_var='%s'" %py_func().replace("'", "''"))
> echo g:py_var
>
> Script-local or function-local variable can be used to avoid polluting
> Vim's global namespace. Python function can create Vim function-local
> var when it is called from Vim function. That is, inside a Python
> function:
> vim.command("let l:result='%s'" %result.replace("'", "''"))
>
> I don't think there is better solution.
>
> --
> 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
>

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

Reply via email to