Created
February 24, 2011 11:53
-
-
Save tsukkee/842084 to your computer and use it in GitHub Desktop.
PythonのデコレータでVim ScriptからでもPythonからでも呼べる関数を定義する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python <<EOM | |
# coding=utf-8 | |
import vim | |
vim.command(""" | |
function! Hoge(str) | |
return a:str . a:str | |
endfunction | |
""") | |
_functions_for_vim = {} | |
def vimfunc(name): | |
def _(func): | |
_functions_for_vim[name] = func | |
vim.command(""" | |
function! {0}(...) | |
python vim.command('return "' + _functions_for_vim['{0}'](vim.eval('a:000')) + '"') | |
endfunction | |
""".format(name)) | |
def __(*args, **keywords): | |
return func(args) | |
return __ | |
return _ | |
@vimfunc('s:fuga') | |
def fuga(args): | |
return args[0] * int(args[1]) | |
@vimfunc('Piyo') | |
def piyo(args): | |
return ','.join(args) | |
EOM | |
echo Hoge('ほげ') | |
" => ほげほげ | |
echo s:fuga('ふが', 5) | |
" => ふがふがふがふがふが | |
python print fuga('ふが', 5) | |
" => ふがふがふがふがふが | |
echo Piyo(3, 4, 5) | |
python print piyo('3', '4', '5') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment