Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
過去に作った自動ロードプラグイン
if !exists('g:auplugin_disable')
let g:auplugin_disable = 0
endif
command! -nargs=1 -complete=customlist,auplugin#_complete AuLoad call s:load_plugin(<q-args>)
function! auplugin#_complete(arglead, cmdline, cursorpos)"{{{
let list = split(globpath(&runtimepath, 'auplugin/*'), '\n')
let list += split(globpath(&runtimepath, 'bundle/*'), '\n')
let rtp = map(split(&runtimepath, ','), 'expand(v:val)')
call filter(list, 'index(rtp, v:val) < 0')
call map(list, 'fnamemodify(v:val, ":p:~")')
call map(list, 'substitute(v:val, "/$", "", "")')
return filter(list, 'stridx(v:val, a:arglead) == 0')
endfunction"}}}
" TODO: Load plugin at filetype detected.
function! auplugin#ftplugin(path, filetype)
endfunction
function! auplugin#command(path, command, ...)
let path = expand(a:path)
if g:auplugin_disable || (get(a:000, 0, 1) == 0)
call s:append_runtimepath(path)
return
else
" TODO: ロード済みの場合は何もしないで終了する
call s:remove_runtimepath(path)
endif
execute 'command! -nargs=*' a:command printf("call auplugin#_load_command('%s', '%s', <q-args>)", escape(path, "'"), a:command)
endfunction
function! auplugin#map(path, mode, mapping, ...)
let path = expand(a:path)
if g:auplugin_disable || (get(a:000, 0, 1) == 0)
call s:append_runtimepath(path)
return
else
call s:remove_runtimepath(path)
endif
let mode = a:mode
if mode ==# ''
let mode = 'nvo'
endif
" mode variable accepts array of n, v, o.
for mode in split(mode, '\zs')
if mapcheck(a:mapping, mode) ==# ''
execute mode.'noremap <silent>' a:mapping printf(":<C-u>call auplugin#_load_map('%s', '%s', '%s')<CR>", mode, escape(path, "'"), a:mapping)
endif
endfor
endfunction
function! auplugin#textobj(path, mapping)
let path = expand(a:path)
if g:auplugin_disable
call s:append_runtimepath(path)
return
else
call s:remove_runtimepath(path)
endif
call auplugin#map(path, 'vo', a:mapping)
" if mapcheck(a:mapping, 'v') ==# ''
" execute 'vnoremap' a:mapping printf(":<C-u>call auplugin#_load_map('%s', '%s', '%s')<CR>", 'v', escape(a:path, "'"), a:mapping)
" endif
" if mapcheck(a:mapping, 'o') ==# ''
" execute 'onoremap' a:mapping printf(":<C-u>call auplugin#_load_map('%s', '%s', '%s')<CR>", 'o', escape(a:path, "'"), a:mapping)
" endif
endfunction
" TODO: Support unite sources.
function! auplugin#_load_map(mode, path, mapping)
echo 'Load mapping:' a:mapping
" Consume input keys.
let input = s:consume_inputs()
" Clear pre-load mapping.
execute 'unmap' a:mapping
" Load plugin.
call s:load_plugin(a:path)
if a:mode ==# 'v'
call feedkeys('gv', 'n')
elseif a:mode ==# 'o'
" TODO: omap
" v:prevcount?
" Cancel waiting operator mode.
" call feedkeys("\<C-\\>\<C-n>", 'n')
call feedkeys("\<Esc>", 'n')
call feedkeys(v:operator, 'm')
endif
let mapping = substitute(a:mapping, '<Plug>', "\<Plug>", 'g')
call feedkeys(mapping . input, 'm')
endfunction
function! auplugin#_load_command(path, command, args)
echo 'Load command:' a:command
" Clear pre-load command.
execute 'delcommand' a:command
" Load plugin.
call s:load_plugin(a:path)
execute a:command a:args
endfunction
function! s:load_plugin(path)
let path = expand(a:path)
let path = substitute(path, '/$', '', '')
call s:append_runtimepath(path)
call s:source_plugin(path)
endfunction
function! s:append_runtimepath(path)
" TODO: g:auplugin_disable = 1 の時に起動時に連続してrtputil#appendを呼び出すと遅い。
if exists('*rtputil#append')
call rtputil#append(a:path)
else
let path = fnameescape(expand(a:path))
execute 'set runtimepath^=' . path
execute 'set runtimepath+=' . path . '/after'
endif
endfunction
function! s:remove_runtimepath(path)
let r = &runtimepath
let path = fnameescape(expand(a:path))
execute 'set runtimepath-=' . path
execute 'set runtimepath-=' . path . '/after'
" If can't remove, retry with expanded runtimepath.
if r ==# &runtimepath
let list = map(split(&runtimepath, ','), 'expand(v:val)') " Substitute path separator.
call filter(list, 'stridx(v:val, a:path) != 0') " Remove path and path/after.
let &runtimepath = join(list, ',')
endif
endfunction
" function! s:rtp_rm(dir)
" execute 'set rtp-='.fnameescape(neobundle#util#expand(a:dir))
" execute 'set rtp-='.fnameescape(neobundle#util#expand(a:dir.'/after'))
" endfunction
"
" function! s:rtp_add(dir) abort
" execute 'set rtp^='.fnameescape(neobundle#util#expand(a:dir))
" execute 'set rtp+='.fnameescape(neobundle#util#expand(a:dir.'/after'))
" endfunction
function! s:source_plugin(path)
let files = split(globpath(a:path, 'ftdetect/**/*.vim'), '\n')
let files += split(globpath(a:path, 'after/ftdetect/**/*.vim'), '\n')
let files += split(globpath(a:path, 'plugin/**/*.vim'), '\n')
let files += split(globpath(a:path, 'after/plugin/**/*.vim'), '\n')
for file in files
source `=file`
endfor
endfunction
function! s:consume_inputs()
let input = ''
let termstr = "<M-_>"
call feedkeys(termstr, 'n')
while 1
let input .= nr2char(getchar())
let idx = stridx(input, termstr)
if idx >= 1
let input = input[: idx - 1]
break
elseif idx == 0
let input = ''
break
endif
endwhile
return input
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment