Browse Source

Load-on-demand with `on` option

Junegunn Choi 12 năm trước cách đây
mục cha
commit
1c2d394782
2 tập tin đã thay đổi với 45 bổ sung5 xóa
  1. 13 0
      README.md
  2. 32 5
      plug.vim

+ 13 - 0
README.md

@@ -13,6 +13,8 @@ Somewhere between [Pathogen](https://github.com/tpope/vim-pathogen) and
 - Parallel installation/update (requires
   [+ruby](http://junegunn.kr/2013/09/installing-vim-with-ruby-support/))
 - Smallest possible feature set
+- Branch/tag support
+- On-demand loading
 - Dependency resolution using `Plugfile` (experimental)
 
 ### Cons.
@@ -36,6 +38,7 @@ call plug#begin('~/.vim/plugged')
 
 Plug 'junegunn/seoul256.vim'
 Plug 'junegunn/vim-easy-align'
+Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
 " Plug 'user/repo1', 'branch_or_tag'
 " Plug 'user/repo2', { 'rtp': 'vim/plugin/dir', 'branch': 'devel' }
 " Plug 'git@github.com:junegunn/vim-github-dashboard.git'
@@ -79,6 +82,16 @@ Plug 'tpope/vim-sensible'
 call plug#end()
 ```
 
+### On-demand loading of plugins
+
+```vim
+" NERD tree will be loaded on the first invocation of NERDTreeToggle command
+Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
+
+" Multiple commands
+Plug 'junegunn/vim-github-dashboard', { 'on': ['GHDashboard', 'GHActivity'] }
+```
+
 ### Dependency resolution
 
 See [Dependency

+ 32 - 5
plug.vim

@@ -13,8 +13,9 @@
 "
 "   Plug 'junegunn/seoul256.vim'
 "   Plug 'junegunn/vim-easy-align'
+"   Plug 'junegunn/goyo.vim', { 'on': 'Goyo' }
 "   " Plug 'user/repo1', 'branch_or_tag'
-"   " Plug 'user/repo2', { 'rtp': 'vim/plugin/dir', 'branch': 'devel' }
+"   " Plug 'user/repo2', { 'rtp': 'vim/plugin/dir', 'branch': 'branch_or_tag' }
 "   " ...
 "
 "   call plug#end()
@@ -110,10 +111,17 @@ function! plug#end()
 
   filetype off
   for plug in values(g:plugs)
-    let rtp = s:rtp(plug)
-    execute "set rtp^=".rtp
-    if isdirectory(rtp.'after')
-      execute "set rtp+=".rtp.'after'
+    if has_key(plug, 'on')
+      let commands = type(plug.on) == 1 ? [plug.on] : plug.on
+      for cmd in commands
+        if !exists(':'.cmd)
+          execute printf(
+          \ "command! -nargs=* -bang %s call s:lod(%s, '<bang>', <q-args>, %s)",
+          \ cmd, string(cmd), string(plug))
+        endif
+      endfor
+    else
+      call s:add_rtp(s:rtp(plug))
     endif
   endfor
   filetype plugin indent on
@@ -128,6 +136,25 @@ function! s:rtp(spec)
   return rtp
 endfunction
 
+function! s:add_rtp(rtp)
+  execute "set rtp^=".a:rtp
+  if isdirectory(a:rtp.'after')
+    execute "set rtp+=".a:rtp.'after'
+  endif
+endfunction
+
+function! s:lod(cmd, bang, args, plug)
+  execute 'delc '.a:cmd
+  let rtp = s:rtp(a:plug)
+  call s:add_rtp(rtp)
+  for dir in ['plugin', 'after']
+    for vim in split(globpath(rtp, dir.'/*.vim'), '\n')
+      execute 'source '.vim
+    endfor
+  endfor
+  execute printf("%s%s %s", a:cmd, a:bang, a:args)
+endfunction
+
 function! s:add(...)
   let force = a:1
   let opts = { 'branch': 'master' }