python3 to vim桥装饰器,允许在本机vim脚本中透明调用python3函数。
vim-bridge3的Python项目详细描述
python 2的original的python 3端口:https://github.com/nvie/vim_bridge
这是什么?
vim_bridge3是一个python-to-vim桥装饰器,它允许透明调用 本地vim脚本中的python函数。
安装
使用setuptools安装vim_bridge3python包, easy_install,或pip。
用法
在vim脚本中,按照如下方式装饰python函数,将它们公开为 本地vim可调用。参数和返回值都被分类,因此它应该 透明:
python3 << 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!"
支持
以下数据类型已证明有效:
- 字符串
- 整数
- 列表
- 例外情况
更多示例
传递列表:
python3 << 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)
捕捉异常:
python3 << 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脚本:
python3 << endpython import os.path from vim_bridge import bridged @bridged def NormalizePath(path): return os.path.realpath(path) endpython echo NormalizePath("/this/../or/./.././that/is/./a/.//very/../obscure/..//././long/./../path/name") echo NormalizePath("..")
可以在python块中使用桥接函数定义 它本身,或者从vim内部,都无关紧要。在这个例子中, normalizePath是从python和vim调用的:
python3 << endpython 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) endpython " ...or from here echo NormalizePath("/this/../or/./.././that/is/./a/.//very/../obscure/..//././long/./../path/name") echo RealPath("..")
由于vim_bridge 0.4,函数名大小写约定是 自动转换以匹配vim的约定(和requirement 甚至,因为函数名必须以大写字母开头。 除了外壳外,用下划线预置Python函数 导致在vim上下文中将函数定义为 <SID>-前缀函数(即不能是 从脚本外部调用):
python3 << endpython 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." endpython echo Public() echo s:Private() echo MyNameIsAutoConverted() echo s:LongPrivateName()
更改
- 2010年文森特·德里森(@nvie)的原始代码。BSD许可证。
- 2016年Roland Puntaier(@rpuntie)对Python2和Python3的支持。
- Walter Doekes(@wdoekes)在2017年对python3的支持。
- 2018年罗兰·普泰尔(Roland Puntaier,@Rpuntaie)为Pypi设计的Vim_Bridge3。