跳转到主要内容

一个Python到Vim的桥梁装饰器,允许在原生Vim脚本中透明地调用Python函数。

项目描述

这是什么?

vim_bridge 是一个Python到Vim的桥梁装饰器,允许在原生Vim脚本中透明地调用Python函数。

安装

只需使用setuptools、easy_install或pip安装vim_bridge Python包。

使用

在Vim脚本中,如下装饰你的Python函数,以将其作为原生Vim调用公开。参数和返回值都会被转换,因此应该是透明的。

python << endpython
from vim_bridge import bridged

@bridged
def SayHello(first, last):
    return "Hello, %s %s!" % (first, last)

endpython

" Now call directly into the Python function!
echo SayHello("John", "Doe")
           " prints "Hello, John Doe!"

支持

以下数据类型已被证明可以正常工作

  • 字符串

  • 整数

  • 列表

  • 异常

更多示例

传递列表

python << endpython
from vim_bridge import bridged

@bridged
def GetLongest(list):
    return max(map(lambda s: len(s), list))

endpython

echo GetLongest(['one', 'two', 'three', 'four'])
            " returns 5 (because "three" is 5 chars long)

捕获异常

python << endpython
from vim_bridge import bridged

@bridged
def WillCauseException():
    raise Exception("Oops")

endpython

" This will throw an error to the user...
echo WillCauseException()

" But here's how you can catch that in Vim
try
    echo WillCauseException()
catch
    echo "Something went wrong. Aborting."
finally
    echo "Cleaning up."
endtry

使用Python stdlib函数来完成纯Vim脚本难以完成的任务

python << END
import os.path
from vim_bridge import bridged

@bridged
def NormalizePath(path):
    return os.path.realpath(path)
END

echo NormalizePath("/this/../or/./.././that/is/./a/.//very/../obscure/..//././long/./../path/name")
echo NormalizePath("..")

你可以在Python块内部或从Vim内部使用桥接的函数定义,这没有关系。在这个例子中,NormalizePath同时从Python和Vim调用。

python << END
import os.path
from vim_bridge import bridged

@bridged
def NormalizePath(path):
    return os.path.realpath(path)

@bridged
def RealPath(path):
    # It does not matter if you call NormalizePath from here...
    return NormalizePath(path)
END

" ...or from here
echo NormalizePath("/this/../or/./.././that/is/./a/.//very/../obscure/..//././long/./../path/name")
echo RealPath("..")

自vim_bridge 0.4版本起,函数名的大小写约定将自动转换为与Vim的约定(甚至可以说是要求,因为函数名必须以大写字母开头)。除了大小写外,在Python函数前加上下划线将导致该函数在Vim上下文中定义为以<SID>为前缀的函数(即一个“私有”函数,不能从脚本外部调用)。

python << eop
import os
import vim
from vim_bridge import bridged

@bridged
def public():
    return "I am public."

@bridged
def _private():
    return "I am private (available in the current script only)."

@bridged
def my_name_is_auto_converted():
    return "In Python, I'm called my_name_is_auto_converted, " + \
           "but in Vim, I'm called MyNameIsAutoConverted :)"

@bridged
def _long_private_name():
    return "I'm private, and my case is converted automatically."
eop

echo Public()
echo s:Private()
echo MyNameIsAutoConverted()
echo s:LongPrivateName()

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

vim_bridge-0.6.tar.gz (4.2 kB 查看哈希值)

上传时间

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面