Skip to content

Instantly share code, notes, and snippets.

@chikatoike
Created November 8, 2012 22:12
Show Gist options
  • Save chikatoike/4042078 to your computer and use it in GitHub Desktop.
Save chikatoike/4042078 to your computer and use it in GitHub Desktop.
Vim sciprtで関数内関数的な
command! -nargs=* InnerFunction
\ let l:__func = {
\ 'type': 'function',
\ 'name': matchstr(<q-args>, '^\s*\([^(]\+\)'),
\ 'statement': [
\ 'function! <args>',
\ ' call extend(l:, get(l:, "self", {}))',
\ ],
\ }
command! InnerEndFunction
\ execute join(l:__func.statement + [
\ 'endfunction'
\ ] , "\n")
command! -bang -nargs=* Let call s:add_statement(l:__func, 'let', <bang>0, <q-args>)
command! -bang -nargs=* Call call s:add_statement(l:__func, 'call', <bang>0, <q-args>)
command! -bang -nargs=* Execute call s:add_statement(l:__func, 'execute', <bang>0, <q-args>)
command! -bang -nargs=* Return call s:add_statement(l:__func, 'return', <bang>0, <q-args>)
command! -bang -nargs=* Echo call s:add_statement(l:__func, 'echo', <bang>0, <q-args>)
command! -bang -nargs=* If call s:add_statement(l:__func, 'if', <bang>0, <q-args>)
command! -bang -nargs=* EndIf call s:add_statement(l:__func, 'endif', <bang>0, <q-args>)
command! -bang -nargs=* For call s:add_statement(l:__func, 'for', <bang>0, <q-args>)
command! -bang -nargs=* EndFor call s:add_statement(l:__func, 'endfor', <bang>0, <q-args>)
command! -bang -nargs=* While call s:add_statement(l:__func, 'while', <bang>0, <q-args>)
command! -bang -nargs=* EndWhile call s:add_statement(l:__func, 'endwhile', <bang>0, <q-args>)
command! -bang -nargs=* Try call s:add_statement(l:__func, 'try', <bang>0, <q-args>)
command! -bang -nargs=* Catch call s:add_statement(l:__func, 'catch', <bang>0, <q-args>)
command! -bang -nargs=* Finally call s:add_statement(l:__func, 'finally', <bang>0, <q-args>)
command! -bang -nargs=* EndTry call s:add_statement(l:__func, 'endtry', <bang>0, <q-args>)
function! s:add_statement(info, command, bang, expr)
call add(a:info.statement, a:command . ' ' . a:expr)
endfunction
function! s:outer()
let a = 1
let b = 2
" local function
InnerFunction l:.Inner()
Let c = a + b
Echo c
InnerEndFunction
call l:.Inner()
" global function
InnerFunction GlobalFunc()
Let a = 1
Let b = 2
Let c = a + b
Echo c
InnerEndFunction
call GlobalFunc()
endfunction
call s:outer()
@chikatoike
Copy link
Author

l:.Inner() は s:outer() のローカル変数を参照しています。

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