既然您现在已经添加了整个功能......
您得到未定义变量和无效表达式的原因Login()
是因为客户端的范围结束于EOF
。在返回行 vim 不知道client
因为它只在 python 块中定义。
你可以做的就是定义一个 python 函数,在里面为你做这件事Post()
。像下面这样的东西。
python << EOF
import vim, weibo
def Login():
appkey = 'xxx'
appsecret = 'xxxxx'
callback_url = 'xxxxxxxx'
acs_token = 'xxxxx'
expr_in = 'xxxx'
client = weibo.APIClient(app_key=appkey, app_secret=appsecret,\
redirect_uri=callback_url)
client.set_access_token(acs_token, expr_in)
del vim.current.buffer[:]
return client
EOF
function! Post()
python << EOF
try:
client = Login()
client.post.statuses__update(status="hello")
except Exception, e:
print e
EOF
endfunction
注意:由于所有内容都传递给 python 的同一个实例,因此您可以定义Login()
作为 vim 函数之外的普通 python 函数,稍后可以执行您想要的操作。它不需要位于同一个 python 块中。
旧答案
你需要把符号EOF
在 python 部分的末尾。否则,vim 会继续向 python 提供命令。
python << EOF
vim 帮助中的相应部分:h python-commands复制如下。
:[range]py[thon] << {endmarker}
{script}
{endmarker}
Execute Python script {script}.
{endmarker} must NOT be preceded by any white space. If {endmarker} is
omitted from after the "<<", a dot '.' must be used after {script}, like
for the |:append| and |:insert| commands.
This form of the |:python| command is mainly useful for including python code
in Vim scripts.
Your {endmarker}
is EOF
。但是,由于您没有显示整个功能,我不确定您需要将其放在哪里EOF
至于你的代码。
vim.command("let obj = Login()")
这条线是正确的。当且仅当 Login() 执行时没有错误。但是,您显示的代码片段登录有错误。