Skip to content

Instantly share code, notes, and snippets.

@tsukkee
Created February 24, 2011 11:53
Show Gist options
  • Save tsukkee/842084 to your computer and use it in GitHub Desktop.
Save tsukkee/842084 to your computer and use it in GitHub Desktop.
PythonのデコレータでVim ScriptからでもPythonからでも呼べる関数を定義する
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