diff --git a/plug.vim b/plug.vim index c94876d..3b406b8 100644 --- a/plug.vim +++ b/plug.vim @@ -136,6 +136,10 @@ function! s:source(from, ...) endfor endfunction +function! s:assoc(dict, key, val) + let a:dict[a:key] = add(get(a:dict, a:key, []), a:val) +endfunction + function! plug#end() if !exists('g:plugs') return s:err('Call plug#begin() first') @@ -147,7 +151,7 @@ function! plug#end() augroup END augroup! PlugLOD endif - let lod = {} + let lod = { 'ft': {}, 'map': {}, 'cmd': {} } filetype off for name in g:plugs_order @@ -162,19 +166,12 @@ function! plug#end() for cmd in s:to_a(plug.on) if cmd =~ '^.\+' if empty(mapcheck(cmd)) && empty(mapcheck(cmd, 'i')) - for [mode, map_prefix, key_prefix] in - \ [['i', '', ''], ['n', '', ''], ['v', '', 'gv'], ['o', '', '']] - execute printf( - \ '%snoremap %s %s:call lod_map(%s, %s, "%s")', - \ mode, cmd, map_prefix, string(cmd), string(name), key_prefix) - endfor + call s:assoc(lod.map, cmd, name) endif call add(s:triggers[name].map, cmd) elseif cmd =~ '^[A-Z]' if exists(':'.cmd) != 2 - execute printf( - \ 'command! -nargs=* -range -bang %s call s:lod_cmd(%s, "", , , , %s)', - \ cmd, string(cmd), string(name)) + call s:assoc(lod.cmd, cmd, name) endif call add(s:triggers[name].cmd, cmd) endif @@ -186,19 +183,31 @@ function! plug#end() if !empty(types) call s:source(s:rtp(plug), 'ftdetect/**/*.vim', 'after/ftdetect/**/*.vim') endif - for key in types - if !has_key(lod, key) - let lod[key] = [] - endif - call add(lod[key], name) + for type in types + call s:assoc(lod.ft, type, name) endfor endif endfor - for [key, names] in items(lod) + for [cmd, names] in items(lod.cmd) + execute printf( + \ 'command! -nargs=* -range -bang %s call s:lod_cmd(%s, "", , , , %s)', + \ cmd, string(cmd), string(names)) + endfor + + for [map, names] in items(lod.map) + for [mode, map_prefix, key_prefix] in + \ [['i', '', ''], ['n', '', ''], ['v', '', 'gv'], ['o', '', '']] + execute printf( + \ '%snoremap %s %s:call lod_map(%s, %s, "%s")', + \ mode, map, map_prefix, string(map), string(names), key_prefix) + endfor + endfor + + for [ft, names] in items(lod.ft) augroup PlugLOD execute printf('autocmd FileType %s call lod_ft(%s, %s)', - \ key, string(key), string(names)) + \ ft, string(ft), string(names)) augroup END endfor @@ -336,11 +345,11 @@ function! s:remove_triggers(name) return endif for cmd in s:triggers[a:name].cmd - execute 'delc' cmd + execute 'silent! delc' cmd endfor for map in s:triggers[a:name].map - execute 'unmap' map - execute 'iunmap' map + execute 'silent! unmap' map + execute 'silent! iunmap' map endfor call remove(s:triggers, a:name) endfunction @@ -367,13 +376,13 @@ function! s:lod_ft(pat, names) doautocmd filetypeindent FileType endfunction -function! s:lod_cmd(cmd, bang, l1, l2, args, name) - call s:lod([a:name], ['ftdetect', 'after/ftdetect', 'plugin', 'after/plugin']) +function! s:lod_cmd(cmd, bang, l1, l2, args, names) + call s:lod(a:names, ['ftdetect', 'after/ftdetect', 'plugin', 'after/plugin']) execute printf('%s%s%s %s', (a:l1 == a:l2 ? '' : (a:l1.','.a:l2)), a:cmd, a:bang, a:args) endfunction -function! s:lod_map(map, name, prefix) - call s:lod([a:name], ['ftdetect', 'after/ftdetect', 'plugin', 'after/plugin']) +function! s:lod_map(map, names, prefix) + call s:lod(a:names, ['ftdetect', 'after/ftdetect', 'plugin', 'after/plugin']) let extra = '' while 1 let c = getchar(0) diff --git a/test/regressions.vader b/test/regressions.vader index e0f6724..3a388c9 100644 --- a/test/regressions.vader +++ b/test/regressions.vader @@ -170,3 +170,34 @@ Execute (#139-3 Should fail when not possible to fast-forward): silent %y Assert @" =~ 'Not possible to fast-forward', @" endfor + +********************************************************************** +Execute (#145: Merging on-demand loading triggers - cmd): + unlet! g:xxx g:yyy + call plug#begin() + Plug '$PLUG_FIXTURES/xxx', { 'on': 'XXX' } + Plug '$PLUG_FIXTURES/yyy', { 'on': ['XXX', 'YYY'] } + call plug#end() + + silent! XXX + + Assert exists('g:xxx'), 'xxx is not loaded' + Assert exists('g:yyy'), 'yyy is not loaded' + Assert !exists(':YYY') + +Execute (#145: Merging on-demand loading triggers - map): + unlet! g:xxx g:yyy + + call plug#begin() + Plug '$PLUG_FIXTURES/xxx', { 'on': '(xxx)' } + Plug '$PLUG_FIXTURES/yyy', { 'on': ['(xxx)' ,'(yyy)' ] } + call plug#end() + + Assert !empty(mapcheck("(xxx)")) + Assert !empty(mapcheck("(yyy)")) + +# FIXME feedkeys() cannot be tested with Vader + call plug#load('xxx', 'yyy') + Assert empty(mapcheck("(xxx)")) + Assert empty(mapcheck("(yyy)")) +