vim-argwrap/autoload/argwrap/hooks.vim
Camille Dejoye 0fc75c596c improvement: generalize the hook system
It is still not possible to override an existing hook but with this
system users can create new hooks really simply without having to update
any configuration since they will be autodetected.

The only missing thins is the possibility to disable hook, therefore it
is the responsibility of each hook to provide a way to the user to use
it or not.
2020-06-07 15:26:06 +02:00

46 lines
1.4 KiB
VimL

function! s:loadGlobalHooks() abort " {{{
if !exists('g:argwrap_global_hooks')
let g:argwrap_global_hooks = []
for hook in globpath(&runtimepath, 'autoload/argwrap/hooks/*.vim', 0, 1)
let l:filename = matchstr(hook, '\vhooks/\zs.+\ze\.vim$')
call add(g:argwrap_global_hooks, printf('argwrap#hooks#%s', l:filename))
endfor
endif
return g:argwrap_global_hooks
endfunction " }}}
function! s:loadFiletypeHooks(filetype) abort " {{{
if !exists('g:argwrap_filetype_hooks.'.a:filetype)
let g:argwrap_filetype_hooks[a:filetype] = []
let l:hooks = g:argwrap_filetype_hooks[a:filetype]
for filetypeHook in globpath(&runtimepath, 'autoload/argwrap/hooks/filetype/*/*.vim', 0, 1)
let l:filetype = matchstr(filetypeHook, '\vhooks/filetype/\zs.+\ze/.+\.vim$')
let l:filename = matchstr(filetypeHook, '\vhooks/filetype/.+/\zs.+\ze\.vim$')
call add(l:hooks, printf('argwrap#hooks#filetype#%s#%s', l:filetype, l:filename))
endfor
endif
return g:argwrap_filetype_hooks[a:filetype]
endfunction " }}}
function! s:load() abort " {{{
if !exists('b:argwrap_hooks')
let b:argwrap_hooks = s:loadGlobalHooks() + s:loadFiletypeHooks(&filetype)
endif
return b:argwrap_hooks
endfunction " }}}
function! argwrap#hooks#execute(name, ...) abort " {{{
for hook in s:load()
silent! call call(printf('%s#%s', hook, a:name), a:000)
endfor
endfunction " }}}
" vim: ts=2 sw=2 et fdm=marker