Tuesday, June 7, 2011

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

On 07. 06. 2011 14:56, Tony Mechelynck wrote:
> On 07/06/11 13:55, Marko Mahnič wrote:
>>
>> If you keep the quotes and backsalshes, but change the rest you can write (
>> s:SNR -> getcwd(), MyCallback -> /. ) :
>>
>> :python import os
>> :exec "python print os.path.exists(\"" . getcwd() . "/.\")"
>>
>> which works for me (Vim 7.3). If getcwd() returns "/home/user", the generated
>> Python statement is:
>>
>> print os.path.exists("/home/user/.")
>>
>> In the above case you would get the Python statement:
>>
>> DoSomething("32_MyCallback", ...)
>
> no, you would get:
> E???: invalid expression
> because
> >>> exec "python DoSomething(\"" . s:SNR . "MyCallback\", ...)"
> string ^---------------------------------^
> something Vim cannot understand ^--------...
>
Let's break it down:

1. exec accepts a string
2. the string is a concatenation of 3 strings:
- "python DoSomething(\""
- s:SNR
- "MyCallback\", ...)"

The double quote is preceeded by a backslash in the first and the third string
and is treated as a part of the string. We could rewrite them as:
- 'python DoSomething("'
- 'MyCallback", ...)'

s:SNR is the SID of the script, I think it looks sth. like '42_'.

Now we concatenate:
'python DoSomething("' . '42_' . 'MyCallback", ...)'
to get
'python DoSomething("42_MyCallback", ...)'
which is a perfectly valid string for exec (but not for Python, because of the
dots which should be replaced by the actual parameters, if any).

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: