Skip to content

Instantly share code, notes, and snippets.

@amashigeseiji
Last active September 20, 2015 19:21
Show Gist options
  • Save amashigeseiji/b4d2bc5a17e653e5d93b to your computer and use it in GitHub Desktop.
Save amashigeseiji/b4d2bc5a17e653e5d93b to your computer and use it in GitHub Desktop.
vimscript で動的に function 定義をする
" vimscript
" functionじゃないrcファイルとかをまるごとfunctionとして再定義する
" 動的なfunction生成できる
" 下の例だと、外部ファイルの内容をまるまる function にする
function! s:define_function(funcname, file) dict
let l:file = readfile(a:file)
let l:def = "function! s:" . a:funcname . "() dict\n"
for i in l:file
let l:def .= i . "\n"
endfor
let l:def .= 'endfunction'
exec l:def
let self.new_difined = function('s:new_difined')
endfunction
let hoge = {
\ 'define_function': function('s:define_function')
\}
call hoge.define_function('new_defined', 'hoge.vim')
call hoge.new_difined()
@amashigeseiji
Copy link
Author

定義は読み込んでおきたいけど遅延実行させたいときとかに使えそう

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment