.vim/autoload/plug.vim

changeset 878
ab714f06ef57
parent 817
3c7ab878a1b5
child 888
44d34f2382f6
equal deleted inserted replaced
877:17b523f558f2 878:ab714f06ef57
97 let s:plug_buf = get(s:, 'plug_buf', -1) 97 let s:plug_buf = get(s:, 'plug_buf', -1)
98 let s:mac_gui = has('gui_macvim') && has('gui_running') 98 let s:mac_gui = has('gui_macvim') && has('gui_running')
99 let s:is_win = has('win32') 99 let s:is_win = has('win32')
100 let s:nvim = has('nvim-0.2') || (has('nvim') && exists('*jobwait') && !s:is_win) 100 let s:nvim = has('nvim-0.2') || (has('nvim') && exists('*jobwait') && !s:is_win)
101 let s:vim8 = has('patch-8.0.0039') && exists('*job_start') 101 let s:vim8 = has('patch-8.0.0039') && exists('*job_start')
102 let s:me = resolve(expand('<sfile>:p')) 102 if s:is_win && &shellslash
103 set noshellslash
104 let s:me = resolve(expand('<sfile>:p'))
105 set shellslash
106 else
107 let s:me = resolve(expand('<sfile>:p'))
108 endif
103 let s:base_spec = { 'branch': 'master', 'frozen': 0 } 109 let s:base_spec = { 'branch': 'master', 'frozen': 0 }
104 let s:TYPE = { 110 let s:TYPE = {
105 \ 'string': type(''), 111 \ 'string': type(''),
106 \ 'list': type([]), 112 \ 'list': type([]),
107 \ 'dict': type({}), 113 \ 'dict': type({}),
108 \ 'funcref': type(function('call')) 114 \ 'funcref': type(function('call'))
109 \ } 115 \ }
110 let s:loaded = get(s:, 'loaded', {}) 116 let s:loaded = get(s:, 'loaded', {})
111 let s:triggers = get(s:, 'triggers', {}) 117 let s:triggers = get(s:, 'triggers', {})
112 118
119 if s:is_win
120 function! s:plug_call(fn, ...)
121 let shellslash = &shellslash
122 try
123 set noshellslash
124 return call(a:fn, a:000)
125 finally
126 let &shellslash = shellslash
127 endtry
128 endfunction
129 else
130 function! s:plug_call(fn, ...)
131 return call(a:fn, a:000)
132 endfunction
133 endif
134
135 function! s:plug_getcwd()
136 return s:plug_call('getcwd')
137 endfunction
138
139 function! s:plug_fnamemodify(fname, mods)
140 return s:plug_call('fnamemodify', a:fname, a:mods)
141 endfunction
142
143 function! s:plug_expand(fmt)
144 return s:plug_call('expand', a:fmt, 1)
145 endfunction
146
147 function! s:plug_tempname()
148 return s:plug_call('tempname')
149 endfunction
150
113 function! plug#begin(...) 151 function! plug#begin(...)
114 if a:0 > 0 152 if a:0 > 0
115 let s:plug_home_org = a:1 153 let s:plug_home_org = a:1
116 let home = s:path(fnamemodify(expand(a:1), ':p')) 154 let home = s:path(s:plug_fnamemodify(s:plug_expand(a:1), ':p'))
117 elseif exists('g:plug_home') 155 elseif exists('g:plug_home')
118 let home = s:path(g:plug_home) 156 let home = s:path(g:plug_home)
119 elseif !empty(&rtp) 157 elseif !empty(&rtp)
120 let home = s:path(split(&rtp, ',')[0]) . '/plugged' 158 let home = s:path(split(&rtp, ',')[0]) . '/plugged'
121 else 159 else
122 return s:err('Unable to determine plug home. Try calling plug#begin() with a path argument.') 160 return s:err('Unable to determine plug home. Try calling plug#begin() with a path argument.')
123 endif 161 endif
124 if fnamemodify(home, ':t') ==# 'plugin' && fnamemodify(home, ':h') ==# s:first_rtp 162 if s:plug_fnamemodify(home, ':t') ==# 'plugin' && s:plug_fnamemodify(home, ':h') ==# s:first_rtp
125 return s:err('Invalid plug home. '.home.' is a standard Vim runtime path and is not allowed.') 163 return s:err('Invalid plug home. '.home.' is a standard Vim runtime path and is not allowed.')
126 endif 164 endif
127 165
128 let g:plug_home = home 166 let g:plug_home = home
129 let g:plugs = {} 167 let g:plugs = {}
136 174
137 function! s:define_commands() 175 function! s:define_commands()
138 command! -nargs=+ -bar Plug call plug#(<args>) 176 command! -nargs=+ -bar Plug call plug#(<args>)
139 if !executable('git') 177 if !executable('git')
140 return s:err('`git` executable not found. Most commands will not be available. To suppress this message, prepend `silent!` to `call plug#begin(...)`.') 178 return s:err('`git` executable not found. Most commands will not be available. To suppress this message, prepend `silent!` to `call plug#begin(...)`.')
179 endif
180 if has('win32')
181 \ && &shellslash
182 \ && (&shell =~# 'cmd\(\.exe\)\?$' || &shell =~# 'powershell\(\.exe\)\?$')
183 return s:err('vim-plug does not support shell, ' . &shell . ', when shellslash is set.')
184 endif
185 if !has('nvim')
186 \ && (has('win32') || has('win32unix'))
187 \ && !has('multi_byte')
188 return s:err('Vim needs +multi_byte feature on Windows to run shell commands. Enable +iconv for best results.')
141 endif 189 endif
142 command! -nargs=* -bar -bang -complete=customlist,s:names PlugInstall call s:install(<bang>0, [<f-args>]) 190 command! -nargs=* -bar -bang -complete=customlist,s:names PlugInstall call s:install(<bang>0, [<f-args>])
143 command! -nargs=* -bar -bang -complete=customlist,s:names PlugUpdate call s:update(<bang>0, [<f-args>]) 191 command! -nargs=* -bar -bang -complete=customlist,s:names PlugUpdate call s:update(<bang>0, [<f-args>])
144 command! -nargs=0 -bar -bang PlugClean call s:clean(<bang>0) 192 command! -nargs=0 -bar -bang PlugClean call s:clean(<bang>0)
145 command! -nargs=0 -bar PlugUpgrade if s:upgrade() | execute 'source' s:esc(s:me) | endif 193 command! -nargs=0 -bar PlugUpgrade if s:upgrade() | execute 'source' s:esc(s:me) | endif
201 \ len(s:glob(s:rtp(a:plug), 'after/plugin'))) 249 \ len(s:glob(s:rtp(a:plug), 'after/plugin')))
202 endfunction 250 endfunction
203 251
204 function! plug#end() 252 function! plug#end()
205 if !exists('g:plugs') 253 if !exists('g:plugs')
206 return s:err('Call plug#begin() first') 254 return s:err('plug#end() called without calling plug#begin() first')
207 endif 255 endif
208 256
209 if exists('#PlugLOD') 257 if exists('#PlugLOD')
210 augroup PlugLOD 258 augroup PlugLOD
211 autocmd! 259 autocmd!
322 return 1 370 return 1
323 endfunction 371 endfunction
324 372
325 function! s:git_version_requirement(...) 373 function! s:git_version_requirement(...)
326 if !exists('s:git_version') 374 if !exists('s:git_version')
327 let s:git_version = map(split(split(s:system('git --version'))[2], '\.'), 'str2nr(v:val)') 375 let s:git_version = map(split(split(s:system(['git', '--version']))[2], '\.'), 'str2nr(v:val)')
328 endif 376 endif
329 return s:version_requirement(s:git_version, a:000) 377 return s:version_requirement(s:git_version, a:000)
330 endfunction 378 endfunction
331 379
332 function! s:progress_opt(base) 380 function! s:progress_opt(base)
351 return a:repo =~? '^[a-z]:\|^[%~]' 399 return a:repo =~? '^[a-z]:\|^[%~]'
352 endfunction 400 endfunction
353 401
354 " Copied from fzf 402 " Copied from fzf
355 function! s:wrap_cmds(cmds) 403 function! s:wrap_cmds(cmds)
356 let use_chcp = executable('sed') 404 let cmds = [
357 return map([
358 \ '@echo off', 405 \ '@echo off',
359 \ 'setlocal enabledelayedexpansion'] 406 \ 'setlocal enabledelayedexpansion']
360 \ + (use_chcp ? [
361 \ 'for /f "usebackq" %%a in (`chcp ^| sed "s/[^0-9]//gp"`) do set origchcp=%%a',
362 \ 'chcp 65001 > nul'] : [])
363 \ + (type(a:cmds) == type([]) ? a:cmds : [a:cmds]) 407 \ + (type(a:cmds) == type([]) ? a:cmds : [a:cmds])
364 \ + (use_chcp ? ['chcp !origchcp! > nul'] : []) 408 \ + ['endlocal']
365 \ + ['endlocal'], 409 if has('iconv')
366 \ 'v:val."\r"') 410 if !exists('s:codepage')
411 let s:codepage = libcallnr('kernel32.dll', 'GetACP', 0)
412 endif
413 return map(cmds, printf('iconv(v:val."\r", "%s", "cp%d")', &encoding, s:codepage))
414 endif
415 return map(cmds, 'v:val."\r"')
367 endfunction 416 endfunction
368 417
369 function! s:batchfile(cmd) 418 function! s:batchfile(cmd)
370 let batchfile = tempname().'.bat' 419 let batchfile = s:plug_tempname().'.bat'
371 call writefile(s:wrap_cmds(a:cmd), batchfile) 420 call writefile(s:wrap_cmds(a:cmd), batchfile)
372 let cmd = plug#shellescape(batchfile, {'shell': &shell, 'script': 1}) 421 let cmd = plug#shellescape(batchfile, {'shell': &shell, 'script': 0})
373 if &shell =~# 'powershell\.exe$' 422 if &shell =~# 'powershell\(\.exe\)\?$'
374 let cmd = '& ' . cmd 423 let cmd = '& ' . cmd
375 endif 424 endif
376 return [batchfile, cmd] 425 return [batchfile, cmd]
377 endfunction 426 endfunction
378 else 427 else
573 endif 622 endif
574 623
575 try 624 try
576 let repo = s:trim(a:repo) 625 let repo = s:trim(a:repo)
577 let opts = a:0 == 1 ? s:parse_options(a:1) : s:base_spec 626 let opts = a:0 == 1 ? s:parse_options(a:1) : s:base_spec
578 let name = get(opts, 'as', fnamemodify(repo, ':t:s?\.git$??')) 627 let name = get(opts, 'as', s:plug_fnamemodify(repo, ':t:s?\.git$??'))
579 let spec = extend(s:infer_properties(name, repo), opts) 628 let spec = extend(s:infer_properties(name, repo), opts)
580 if !has_key(g:plugs, name) 629 if !has_key(g:plugs, name)
581 call add(g:plugs_order, name) 630 call add(g:plugs_order, name)
582 endif 631 endif
583 let g:plugs[name] = spec 632 let g:plugs[name] = spec
584 let s:loaded[name] = get(s:loaded, name, 0) 633 let s:loaded[name] = get(s:loaded, name, 0)
585 catch 634 catch
586 return s:err(v:exception) 635 return s:err(repo . ' ' . v:exception)
587 endtry 636 endtry
588 endfunction 637 endfunction
589 638
590 function! s:parse_options(arg) 639 function! s:parse_options(arg)
591 let opts = copy(s:base_spec) 640 let opts = copy(s:base_spec)
592 let type = type(a:arg) 641 let type = type(a:arg)
642 let opt_errfmt = 'Invalid argument for "%s" option of :Plug (expected: %s)'
593 if type == s:TYPE.string 643 if type == s:TYPE.string
644 if empty(a:arg)
645 throw printf(opt_errfmt, 'tag', 'string')
646 endif
594 let opts.tag = a:arg 647 let opts.tag = a:arg
595 elseif type == s:TYPE.dict 648 elseif type == s:TYPE.dict
596 call extend(opts, a:arg) 649 call extend(opts, a:arg)
650 for opt in ['branch', 'tag', 'commit', 'rtp', 'dir', 'as']
651 if has_key(opts, opt)
652 \ && (type(opts[opt]) != s:TYPE.string || empty(opts[opt]))
653 throw printf(opt_errfmt, opt, 'string')
654 endif
655 endfor
656 for opt in ['on', 'for']
657 if has_key(opts, opt)
658 \ && type(opts[opt]) != s:TYPE.list
659 \ && (type(opts[opt]) != s:TYPE.string || empty(opts[opt]))
660 throw printf(opt_errfmt, opt, 'string or list')
661 endif
662 endfor
663 if has_key(opts, 'do')
664 \ && type(opts.do) != s:TYPE.funcref
665 \ && (type(opts.do) != s:TYPE.string || empty(opts.do))
666 throw printf(opt_errfmt, 'do', 'string or funcref')
667 endif
597 if has_key(opts, 'dir') 668 if has_key(opts, 'dir')
598 let opts.dir = s:dirpath(expand(opts.dir)) 669 let opts.dir = s:dirpath(s:plug_expand(opts.dir))
599 endif 670 endif
600 else 671 else
601 throw 'Invalid argument type (expected: string or dictionary)' 672 throw 'Invalid argument type (expected: string or dictionary)'
602 endif 673 endif
603 return opts 674 return opts
604 endfunction 675 endfunction
605 676
606 function! s:infer_properties(name, repo) 677 function! s:infer_properties(name, repo)
607 let repo = a:repo 678 let repo = a:repo
608 if s:is_local_plug(repo) 679 if s:is_local_plug(repo)
609 return { 'dir': s:dirpath(expand(repo)) } 680 return { 'dir': s:dirpath(s:plug_expand(repo)) }
610 else 681 else
611 if repo =~ ':' 682 if repo =~ ':'
612 let uri = repo 683 let uri = repo
613 else 684 else
614 if repo !~ '/' 685 if repo !~ '/'
757 nnoremap <silent> <buffer> ]] :silent! call <SID>section('')<cr> 828 nnoremap <silent> <buffer> ]] :silent! call <SID>section('')<cr>
758 nnoremap <silent> <buffer> [[ :silent! call <SID>section('b')<cr> 829 nnoremap <silent> <buffer> [[ :silent! call <SID>section('b')<cr>
759 endfunction 830 endfunction
760 831
761 function! s:prepare(...) 832 function! s:prepare(...)
762 if empty(getcwd()) 833 if empty(s:plug_getcwd())
763 throw 'Invalid current working directory. Cannot proceed.' 834 throw 'Invalid current working directory. Cannot proceed.'
764 endif 835 endif
765 836
766 for evar in ['$GIT_DIR', '$GIT_WORK_TREE'] 837 for evar in ['$GIT_DIR', '$GIT_WORK_TREE']
767 if exists(evar) 838 if exists(evar)
813 silent! execute 'f' fnameescape(name) 884 silent! execute 'f' fnameescape(name)
814 endfunction 885 endfunction
815 886
816 function! s:chsh(swap) 887 function! s:chsh(swap)
817 let prev = [&shell, &shellcmdflag, &shellredir] 888 let prev = [&shell, &shellcmdflag, &shellredir]
818 if !s:is_win && a:swap 889 if !s:is_win
819 set shell=sh shellredir=>%s\ 2>&1 890 set shell=sh
891 endif
892 if a:swap
893 if &shell =~# 'powershell\(\.exe\)\?$' || &shell =~# 'pwsh$'
894 let &shellredir = '2>&1 | Out-File -Encoding UTF8 %s'
895 elseif &shell =~# 'sh' || &shell =~# 'cmd\(\.exe\)\?$'
896 set shellredir=>%s\ 2>&1
897 endif
820 endif 898 endif
821 return prev 899 return prev
822 endfunction 900 endfunction
823 901
824 function! s:bang(cmd, ...) 902 function! s:bang(cmd, ...)
847 let bar = substitute(getline(2)[1:-2], '.*\zs=', 'x', '') 925 let bar = substitute(getline(2)[1:-2], '.*\zs=', 'x', '')
848 call s:progress_bar(2, bar, len(bar)) 926 call s:progress_bar(2, bar, len(bar))
849 endfunction 927 endfunction
850 928
851 function! s:is_updated(dir) 929 function! s:is_updated(dir)
852 return !empty(s:system_chomp('git log --pretty=format:"%h" "HEAD...HEAD@{1}"', a:dir)) 930 return !empty(s:system_chomp(['git', 'log', '--pretty=format:%h', 'HEAD...HEAD@{1}'], a:dir))
853 endfunction 931 endfunction
854 932
855 function! s:do(pull, force, todo) 933 function! s:do(pull, force, todo)
856 for [name, spec] in items(a:todo) 934 for [name, spec] in items(a:todo)
857 if !isdirectory(spec.dir) 935 if !isdirectory(spec.dir)
884 else 962 else
885 let error = s:bang(spec.do) 963 let error = s:bang(spec.do)
886 endif 964 endif
887 elseif type == s:TYPE.funcref 965 elseif type == s:TYPE.funcref
888 try 966 try
967 call s:load_plugin(spec)
889 let status = installed ? 'installed' : (updated ? 'updated' : 'unchanged') 968 let status = installed ? 'installed' : (updated ? 'updated' : 'unchanged')
890 call spec.do({ 'name': name, 'status': status, 'force': a:force }) 969 call spec.do({ 'name': name, 'status': status, 'force': a:force })
891 catch 970 catch
892 let error = v:exception 971 let error = v:exception
893 endtry 972 endtry
910 return stridx(a:a, a:b) == 0 || stridx(a:b, a:a) == 0 989 return stridx(a:a, a:b) == 0 || stridx(a:b, a:a) == 0
911 endfunction 990 endfunction
912 991
913 function! s:checkout(spec) 992 function! s:checkout(spec)
914 let sha = a:spec.commit 993 let sha = a:spec.commit
915 let output = s:system('git rev-parse HEAD', a:spec.dir) 994 let output = s:system(['git', 'rev-parse', 'HEAD'], a:spec.dir)
916 if !v:shell_error && !s:hash_match(sha, s:lines(output)[0]) 995 if !v:shell_error && !s:hash_match(sha, s:lines(output)[0])
917 let output = s:system( 996 let output = s:system(
918 \ 'git fetch --depth 999999 && git checkout '.s:esc(sha).' --', a:spec.dir) 997 \ 'git fetch --depth 999999 && git checkout '.plug#shellescape(sha).' --', a:spec.dir)
919 endif 998 endif
920 return output 999 return output
921 endfunction 1000 endfunction
922 1001
923 function! s:finish(pull) 1002 function! s:finish(pull)
1028 call s:prepare(1) 1107 call s:prepare(1)
1029 call append(0, ['', '']) 1108 call append(0, ['', ''])
1030 normal! 2G 1109 normal! 2G
1031 silent! redraw 1110 silent! redraw
1032 1111
1033 let s:clone_opt = get(g:, 'plug_shallow', 1) ? 1112 let s:clone_opt = []
1034 \ '--depth 1' . (s:git_version_requirement(1, 7, 10) ? ' --no-single-branch' : '') : '' 1113 if get(g:, 'plug_shallow', 1)
1114 call extend(s:clone_opt, ['--depth', '1'])
1115 if s:git_version_requirement(1, 7, 10)
1116 call add(s:clone_opt, '--no-single-branch')
1117 endif
1118 endif
1035 1119
1036 if has('win32unix') || has('wsl') 1120 if has('win32unix') || has('wsl')
1037 let s:clone_opt .= ' -c core.eol=lf -c core.autocrlf=input' 1121 call extend(s:clone_opt, ['-c', 'core.eol=lf', '-c', 'core.autocrlf=input'])
1038 endif 1122 endif
1039 1123
1040 let s:submodule_opt = s:git_version_requirement(2, 8) ? ' --jobs='.threads : '' 1124 let s:submodule_opt = s:git_version_requirement(2, 8) ? ' --jobs='.threads : ''
1041 1125
1042 " Python version requirement (>= 2.7) 1126 " Python version requirement (>= 2.7)
1118 call s:log4(name, printf('Latest tag for %s -> %s', spec.tag, tag)) 1202 call s:log4(name, printf('Latest tag for %s -> %s', spec.tag, tag))
1119 call append(3, '') 1203 call append(3, '')
1120 endif 1204 endif
1121 endif 1205 endif
1122 call s:log4(name, 'Checking out '.tag) 1206 call s:log4(name, 'Checking out '.tag)
1123 let out = s:system('git checkout -q '.s:esc(tag).' -- 2>&1', spec.dir) 1207 let out = s:system('git checkout -q '.plug#shellescape(tag).' -- 2>&1', spec.dir)
1124 else 1208 else
1125 let branch = s:esc(get(spec, 'branch', 'master')) 1209 let branch = get(spec, 'branch', 'master')
1126 call s:log4(name, 'Merging origin/'.branch) 1210 call s:log4(name, 'Merging origin/'.s:esc(branch))
1127 let out = s:system('git checkout -q '.branch.' -- 2>&1' 1211 let out = s:system('git checkout -q '.plug#shellescape(branch).' -- 2>&1'
1128 \. (has_key(s:update.new, name) ? '' : ('&& git merge --ff-only origin/'.branch.' 2>&1')), spec.dir) 1212 \. (has_key(s:update.new, name) ? '' : ('&& git merge --ff-only '.plug#shellescape('origin/'.branch).' 2>&1')), spec.dir)
1129 endif 1213 endif
1130 if !v:shell_error && filereadable(spec.dir.'/.gitmodules') && 1214 if !v:shell_error && filereadable(spec.dir.'/.gitmodules') &&
1131 \ (s:update.force || has_key(s:update.new, name) || s:is_updated(spec.dir)) 1215 \ (s:update.force || has_key(s:update.new, name) || s:is_updated(spec.dir))
1132 call s:log4(name, 'Updating submodules. This may take a while.') 1216 call s:log4(name, 'Updating submodules. This may take a while.')
1133 let out .= s:bang('git submodule update --init --recursive'.s:submodule_opt.' 2>&1', spec.dir) 1217 let out .= s:bang('git submodule update --init --recursive'.s:submodule_opt.' 2>&1', spec.dir)
1167 silent! call jobstop(j.jobid) 1251 silent! call jobstop(j.jobid)
1168 elseif s:vim8 1252 elseif s:vim8
1169 silent! call job_stop(j.jobid) 1253 silent! call job_stop(j.jobid)
1170 endif 1254 endif
1171 if j.new 1255 if j.new
1172 call s:system('rm -rf ' . plug#shellescape(g:plugs[name].dir)) 1256 call s:rm_rf(g:plugs[name].dir)
1173 endif 1257 endif
1174 endfor 1258 endfor
1175 let s:jobs = {} 1259 let s:jobs = {}
1176 endfunction 1260 endfunction
1177 1261
1213 endif 1297 endif
1214 call call(a:fn, [a:job, a:data]) 1298 call call(a:fn, [a:job, a:data])
1215 endfunction 1299 endfunction
1216 1300
1217 function! s:nvim_cb(job_id, data, event) dict abort 1301 function! s:nvim_cb(job_id, data, event) dict abort
1218 return a:event == 'stdout' ? 1302 return (a:event == 'stdout' || a:event == 'stderr') ?
1219 \ s:job_cb('s:job_out_cb', self, 0, join(a:data, "\n")) : 1303 \ s:job_cb('s:job_out_cb', self, 0, join(a:data, "\n")) :
1220 \ s:job_cb('s:job_exit_cb', self, 0, a:data) 1304 \ s:job_cb('s:job_exit_cb', self, 0, a:data)
1221 endfunction 1305 endfunction
1222 1306
1223 function! s:spawn(name, cmd, opts) 1307 function! s:spawn(name, cmd, opts)
1224 let job = { 'name': a:name, 'running': 1, 'error': 0, 'lines': [''], 1308 let job = { 'name': a:name, 'running': 1, 'error': 0, 'lines': [''],
1225 \ 'new': get(a:opts, 'new', 0) } 1309 \ 'new': get(a:opts, 'new', 0) }
1226 let s:jobs[a:name] = job 1310 let s:jobs[a:name] = job
1227 let cmd = has_key(a:opts, 'dir') ? s:with_cd(a:cmd, a:opts.dir, 0) : a:cmd
1228 let argv = s:is_win ? ['cmd', '/s', '/c', '"'.cmd.'"'] : ['sh', '-c', cmd]
1229 1311
1230 if s:nvim 1312 if s:nvim
1313 if has_key(a:opts, 'dir')
1314 let job.cwd = a:opts.dir
1315 endif
1316 let argv = a:cmd
1231 call extend(job, { 1317 call extend(job, {
1232 \ 'on_stdout': function('s:nvim_cb'), 1318 \ 'on_stdout': function('s:nvim_cb'),
1319 \ 'on_stderr': function('s:nvim_cb'),
1233 \ 'on_exit': function('s:nvim_cb'), 1320 \ 'on_exit': function('s:nvim_cb'),
1234 \ }) 1321 \ })
1235 let jid = jobstart(argv, job) 1322 let jid = s:plug_call('jobstart', argv, job)
1236 if jid > 0 1323 if jid > 0
1237 let job.jobid = jid 1324 let job.jobid = jid
1238 else 1325 else
1239 let job.running = 0 1326 let job.running = 0
1240 let job.error = 1 1327 let job.error = 1
1241 let job.lines = [jid < 0 ? argv[0].' is not executable' : 1328 let job.lines = [jid < 0 ? argv[0].' is not executable' :
1242 \ 'Invalid arguments (or job table is full)'] 1329 \ 'Invalid arguments (or job table is full)']
1243 endif 1330 endif
1244 elseif s:vim8 1331 elseif s:vim8
1332 let cmd = join(map(copy(a:cmd), 'plug#shellescape(v:val, {"script": 0})'))
1333 if has_key(a:opts, 'dir')
1334 let cmd = s:with_cd(cmd, a:opts.dir, 0)
1335 endif
1336 let argv = s:is_win ? ['cmd', '/s', '/c', '"'.cmd.'"'] : ['sh', '-c', cmd]
1245 let jid = job_start(s:is_win ? join(argv, ' ') : argv, { 1337 let jid = job_start(s:is_win ? join(argv, ' ') : argv, {
1246 \ 'out_cb': function('s:job_cb', ['s:job_out_cb', job]), 1338 \ 'out_cb': function('s:job_cb', ['s:job_out_cb', job]),
1339 \ 'err_cb': function('s:job_cb', ['s:job_out_cb', job]),
1247 \ 'exit_cb': function('s:job_cb', ['s:job_exit_cb', job]), 1340 \ 'exit_cb': function('s:job_cb', ['s:job_exit_cb', job]),
1341 \ 'err_mode': 'raw',
1248 \ 'out_mode': 'raw' 1342 \ 'out_mode': 'raw'
1249 \}) 1343 \})
1250 if job_status(jid) == 'run' 1344 if job_status(jid) == 'run'
1251 let job.jobid = jid 1345 let job.jobid = jid
1252 else 1346 else
1253 let job.running = 0 1347 let job.running = 0
1254 let job.error = 1 1348 let job.error = 1
1255 let job.lines = ['Failed to start job'] 1349 let job.lines = ['Failed to start job']
1256 endif 1350 endif
1257 else 1351 else
1258 let job.lines = s:lines(call('s:system', [cmd])) 1352 let job.lines = s:lines(call('s:system', has_key(a:opts, 'dir') ? [a:cmd, a:opts.dir] : [a:cmd]))
1259 let job.error = v:shell_error != 0 1353 let job.error = v:shell_error != 0
1260 let job.running = 0 1354 let job.running = 0
1261 endif 1355 endif
1262 endfunction 1356 endfunction
1263 1357
1287 call s:switch_out() 1381 call s:switch_out()
1288 endif 1382 endif
1289 endfunction 1383 endfunction
1290 1384
1291 function! s:logpos(name) 1385 function! s:logpos(name)
1292 for i in range(4, line('$')) 1386 let max = line('$')
1387 for i in range(4, max > 4 ? max : 4)
1293 if getline(i) =~# '^[-+x*] '.a:name.':' 1388 if getline(i) =~# '^[-+x*] '.a:name.':'
1294 for j in range(i + 1, line('$')) 1389 for j in range(i + 1, max > 5 ? max : 5)
1295 if getline(j) !~ '^ ' 1390 if getline(j) !~ '^ '
1296 return [i, j - 1] 1391 return [i, j - 1]
1297 endif 1392 endif
1298 endfor 1393 endfor
1299 return [i, i] 1394 return [i, i]
1349 let has_tag = has_key(spec, 'tag') 1444 let has_tag = has_key(spec, 'tag')
1350 if !new 1445 if !new
1351 let [error, _] = s:git_validate(spec, 0) 1446 let [error, _] = s:git_validate(spec, 0)
1352 if empty(error) 1447 if empty(error)
1353 if pull 1448 if pull
1354 let fetch_opt = (has_tag && !empty(globpath(spec.dir, '.git/shallow'))) ? '--depth 99999999' : '' 1449 let cmd = ['git', 'fetch']
1355 call s:spawn(name, printf('git fetch %s %s 2>&1', fetch_opt, prog), { 'dir': spec.dir }) 1450 if has_tag && !empty(globpath(spec.dir, '.git/shallow'))
1451 call extend(cmd, ['--depth', '99999999'])
1452 endif
1453 if !empty(prog)
1454 call add(cmd, prog)
1455 endif
1456 call s:spawn(name, cmd, { 'dir': spec.dir })
1356 else 1457 else
1357 let s:jobs[name] = { 'running': 0, 'lines': ['Already installed'], 'error': 0 } 1458 let s:jobs[name] = { 'running': 0, 'lines': ['Already installed'], 'error': 0 }
1358 endif 1459 endif
1359 else 1460 else
1360 let s:jobs[name] = { 'running': 0, 'lines': s:lines(error), 'error': 1 } 1461 let s:jobs[name] = { 'running': 0, 'lines': s:lines(error), 'error': 1 }
1361 endif 1462 endif
1362 else 1463 else
1363 call s:spawn(name, 1464 let cmd = ['git', 'clone']
1364 \ printf('git clone %s %s %s %s 2>&1', 1465 if !has_tag
1365 \ has_tag ? '' : s:clone_opt, 1466 call extend(cmd, s:clone_opt)
1366 \ prog, 1467 endif
1367 \ plug#shellescape(spec.uri, {'script': 0}), 1468 if !empty(prog)
1368 \ plug#shellescape(s:trim(spec.dir), {'script': 0})), { 'new': 1 }) 1469 call add(cmd, prog)
1470 endif
1471 call s:spawn(name, extend(cmd, [spec.uri, s:trim(spec.dir)]), { 'new': 1 })
1369 endif 1472 endif
1370 1473
1371 if !s:jobs[name].running 1474 if !s:jobs[name].running
1372 call s:reap(name) 1475 call s:reap(name)
1373 endif 1476 endif
1400 1503
1401 G_NVIM = vim.eval("has('nvim')") == '1' 1504 G_NVIM = vim.eval("has('nvim')") == '1'
1402 G_PULL = vim.eval('s:update.pull') == '1' 1505 G_PULL = vim.eval('s:update.pull') == '1'
1403 G_RETRIES = int(vim.eval('get(g:, "plug_retries", 2)')) + 1 1506 G_RETRIES = int(vim.eval('get(g:, "plug_retries", 2)')) + 1
1404 G_TIMEOUT = int(vim.eval('get(g:, "plug_timeout", 60)')) 1507 G_TIMEOUT = int(vim.eval('get(g:, "plug_timeout", 60)'))
1405 G_CLONE_OPT = vim.eval('s:clone_opt') 1508 G_CLONE_OPT = ' '.join(vim.eval('s:clone_opt'))
1406 G_PROGRESS = vim.eval('s:progress_opt(1)') 1509 G_PROGRESS = vim.eval('s:progress_opt(1)')
1407 G_LOG_PROB = 1.0 / int(vim.eval('s:update.threads')) 1510 G_LOG_PROB = 1.0 / int(vim.eval('s:update.threads'))
1408 G_STOP = thr.Event() 1511 G_STOP = thr.Event()
1409 G_IS_WIN = vim.eval('s:is_win') == '1' 1512 G_IS_WIN = vim.eval('s:is_win') == '1'
1410 1513
1937 end 2040 end
1938 sleep 0.2 2041 sleep 0.2
1939 end 2042 end
1940 } if VIM::evaluate('s:mac_gui') == 1 2043 } if VIM::evaluate('s:mac_gui') == 1
1941 2044
1942 clone_opt = VIM::evaluate('s:clone_opt') 2045 clone_opt = VIM::evaluate('s:clone_opt').join(' ')
1943 progress = VIM::evaluate('s:progress_opt(1)') 2046 progress = VIM::evaluate('s:progress_opt(1)')
1944 nthr.times do 2047 nthr.times do
1945 mtx.synchronize do 2048 mtx.synchronize do
1946 threads << Thread.new { 2049 threads << Thread.new {
1947 while pair = take1.call 2050 while pair = take1.call
1999 2102
2000 function! s:shellesc_ps1(arg) 2103 function! s:shellesc_ps1(arg)
2001 return "'".substitute(escape(a:arg, '\"'), "'", "''", 'g')."'" 2104 return "'".substitute(escape(a:arg, '\"'), "'", "''", 'g')."'"
2002 endfunction 2105 endfunction
2003 2106
2107 function! s:shellesc_sh(arg)
2108 return "'".substitute(a:arg, "'", "'\\\\''", 'g')."'"
2109 endfunction
2110
2111 " Escape the shell argument based on the shell.
2112 " Vim and Neovim's shellescape() are insufficient.
2113 " 1. shellslash determines whether to use single/double quotes.
2114 " Double-quote escaping is fragile for cmd.exe.
2115 " 2. It does not work for powershell.
2116 " 3. It does not work for *sh shells if the command is executed
2117 " via cmd.exe (ie. cmd.exe /c sh -c command command_args)
2118 " 4. It does not support batchfile syntax.
2119 "
2120 " Accepts an optional dictionary with the following keys:
2121 " - shell: same as Vim/Neovim 'shell' option.
2122 " If unset, fallback to 'cmd.exe' on Windows or 'sh'.
2123 " - script: If truthy and shell is cmd.exe, escape for batchfile syntax.
2004 function! plug#shellescape(arg, ...) 2124 function! plug#shellescape(arg, ...)
2125 if a:arg =~# '^[A-Za-z0-9_/:.-]\+$'
2126 return a:arg
2127 endif
2005 let opts = a:0 > 0 && type(a:1) == s:TYPE.dict ? a:1 : {} 2128 let opts = a:0 > 0 && type(a:1) == s:TYPE.dict ? a:1 : {}
2006 let shell = get(opts, 'shell', s:is_win ? 'cmd.exe' : 'sh') 2129 let shell = get(opts, 'shell', s:is_win ? 'cmd.exe' : 'sh')
2007 let script = get(opts, 'script', 1) 2130 let script = get(opts, 'script', 1)
2008 if shell =~# 'cmd\.exe$' 2131 if shell =~# 'cmd\(\.exe\)\?$'
2009 return s:shellesc_cmd(a:arg, script) 2132 return s:shellesc_cmd(a:arg, script)
2010 elseif shell =~# 'powershell\.exe$' || shell =~# 'pwsh$' 2133 elseif shell =~# 'powershell\(\.exe\)\?$' || shell =~# 'pwsh$'
2011 return s:shellesc_ps1(a:arg) 2134 return s:shellesc_ps1(a:arg)
2012 endif 2135 endif
2013 return shellescape(a:arg) 2136 return s:shellesc_sh(a:arg)
2014 endfunction 2137 endfunction
2015 2138
2016 function! s:glob_dir(path) 2139 function! s:glob_dir(path)
2017 return map(filter(s:glob(a:path, '**'), 'isdirectory(v:val)'), 's:dirpath(v:val)') 2140 return map(filter(s:glob(a:path, '**'), 'isdirectory(v:val)'), 's:dirpath(v:val)')
2018 endfunction 2141 endfunction
2049 2172
2050 function! s:system(cmd, ...) 2173 function! s:system(cmd, ...)
2051 let batchfile = '' 2174 let batchfile = ''
2052 try 2175 try
2053 let [sh, shellcmdflag, shrd] = s:chsh(1) 2176 let [sh, shellcmdflag, shrd] = s:chsh(1)
2054 let cmd = a:0 > 0 ? s:with_cd(a:cmd, a:1) : a:cmd 2177 if type(a:cmd) == s:TYPE.list
2055 if s:is_win 2178 " Neovim's system() supports list argument to bypass the shell
2179 " but it cannot set the working directory for the command.
2180 " Assume that the command does not rely on the shell.
2181 if has('nvim') && a:0 == 0
2182 return system(a:cmd)
2183 endif
2184 let cmd = join(map(copy(a:cmd), 'plug#shellescape(v:val, {"shell": &shell, "script": 0})'))
2185 if &shell =~# 'powershell\(\.exe\)\?$'
2186 let cmd = '& ' . cmd
2187 endif
2188 else
2189 let cmd = a:cmd
2190 endif
2191 if a:0 > 0
2192 let cmd = s:with_cd(cmd, a:1, type(a:cmd) != s:TYPE.list)
2193 endif
2194 if s:is_win && type(a:cmd) != s:TYPE.list
2056 let [batchfile, cmd] = s:batchfile(cmd) 2195 let [batchfile, cmd] = s:batchfile(cmd)
2057 endif 2196 endif
2058 return system(cmd) 2197 return system(cmd)
2059 finally 2198 finally
2060 let [&shell, &shellcmdflag, &shellredir] = [sh, shellcmdflag, shrd] 2199 let [&shell, &shellcmdflag, &shellredir] = [sh, shellcmdflag, shrd]
2103 elseif a:spec.branch !=# branch 2242 elseif a:spec.branch !=# branch
2104 let err = printf('Invalid branch: %s (expected: %s). Try PlugUpdate.', 2243 let err = printf('Invalid branch: %s (expected: %s). Try PlugUpdate.',
2105 \ branch, a:spec.branch) 2244 \ branch, a:spec.branch)
2106 endif 2245 endif
2107 if empty(err) 2246 if empty(err)
2108 let [ahead, behind] = split(s:lastline(s:system(printf( 2247 let [ahead, behind] = split(s:lastline(s:system([
2109 \ 'git rev-list --count --left-right HEAD...origin/%s', 2248 \ 'git', 'rev-list', '--count', '--left-right',
2110 \ a:spec.branch), a:spec.dir)), '\t') 2249 \ printf('HEAD...origin/%s', a:spec.branch)
2250 \ ], a:spec.dir)), '\t')
2111 if !v:shell_error && ahead 2251 if !v:shell_error && ahead
2112 if behind 2252 if behind
2113 " Only mention PlugClean if diverged, otherwise it's likely to be 2253 " Only mention PlugClean if diverged, otherwise it's likely to be
2114 " pushable (and probably not that messed up). 2254 " pushable (and probably not that messed up).
2115 let err = printf( 2255 let err = printf(
2129 return [err, err =~# 'PlugClean'] 2269 return [err, err =~# 'PlugClean']
2130 endfunction 2270 endfunction
2131 2271
2132 function! s:rm_rf(dir) 2272 function! s:rm_rf(dir)
2133 if isdirectory(a:dir) 2273 if isdirectory(a:dir)
2134 call s:system((s:is_win ? 'rmdir /S /Q ' : 'rm -rf ') . plug#shellescape(a:dir)) 2274 return s:system(s:is_win
2275 \ ? 'rmdir /S /Q '.plug#shellescape(a:dir)
2276 \ : ['rm', '-rf', a:dir])
2135 endif 2277 endif
2136 endfunction 2278 endfunction
2137 2279
2138 function! s:clean(force) 2280 function! s:clean(force)
2139 call s:prepare() 2281 call s:prepare()
2161 redraw 2303 redraw
2162 endfor 2304 endfor
2163 2305
2164 let allowed = {} 2306 let allowed = {}
2165 for dir in dirs 2307 for dir in dirs
2166 let allowed[s:dirpath(fnamemodify(dir, ':h:h'))] = 1 2308 let allowed[s:dirpath(s:plug_fnamemodify(dir, ':h:h'))] = 1
2167 let allowed[dir] = 1 2309 let allowed[dir] = 1
2168 for child in s:glob_dir(dir) 2310 for child in s:glob_dir(dir)
2169 let allowed[child] = 1 2311 let allowed[child] = 1
2170 endfor 2312 endfor
2171 endfor 2313 endfor
2211 endfunction 2353 endfunction
2212 2354
2213 function! s:delete(range, force) 2355 function! s:delete(range, force)
2214 let [l1, l2] = a:range 2356 let [l1, l2] = a:range
2215 let force = a:force 2357 let force = a:force
2358 let err_count = 0
2216 while l1 <= l2 2359 while l1 <= l2
2217 let line = getline(l1) 2360 let line = getline(l1)
2218 if line =~ '^- ' && isdirectory(line[2:]) 2361 if line =~ '^- ' && isdirectory(line[2:])
2219 execute l1 2362 execute l1
2220 redraw! 2363 redraw!
2221 let answer = force ? 1 : s:ask('Delete '.line[2:].'?', 1) 2364 let answer = force ? 1 : s:ask('Delete '.line[2:].'?', 1)
2222 let force = force || answer > 1 2365 let force = force || answer > 1
2223 if answer 2366 if answer
2224 call s:rm_rf(line[2:]) 2367 let err = s:rm_rf(line[2:])
2225 setlocal modifiable 2368 setlocal modifiable
2226 call setline(l1, '~'.line[1:]) 2369 if empty(err)
2227 let s:clean_count += 1 2370 call setline(l1, '~'.line[1:])
2228 call setline(4, printf('Removed %d directories.', s:clean_count)) 2371 let s:clean_count += 1
2372 else
2373 delete _
2374 call append(l1 - 1, s:format_message('x', line[1:], err))
2375 let l2 += len(s:lines(err))
2376 let err_count += 1
2377 endif
2378 let msg = printf('Removed %d directories.', s:clean_count)
2379 if err_count > 0
2380 let msg .= printf(' Failed to remove %d directories.', err_count)
2381 endif
2382 call setline(4, msg)
2229 setlocal nomodifiable 2383 setlocal nomodifiable
2230 endif 2384 endif
2231 endif 2385 endif
2232 let l1 += 1 2386 let l1 += 1
2233 endwhile 2387 endwhile
2234 endfunction 2388 endfunction
2235 2389
2236 function! s:upgrade() 2390 function! s:upgrade()
2237 echo 'Downloading the latest version of vim-plug' 2391 echo 'Downloading the latest version of vim-plug'
2238 redraw 2392 redraw
2239 let tmp = tempname() 2393 let tmp = s:plug_tempname()
2240 let new = tmp . '/plug.vim' 2394 let new = tmp . '/plug.vim'
2241 2395
2242 try 2396 try
2243 let out = s:system(printf('git clone --depth 1 %s %s', plug#shellescape(s:plug_src), plug#shellescape(tmp))) 2397 let out = s:system(['git', 'clone', '--depth', '1', s:plug_src, tmp])
2244 if v:shell_error 2398 if v:shell_error
2245 return s:err('Error upgrading vim-plug: '. out) 2399 return s:err('Error upgrading vim-plug: '. out)
2246 endif 2400 endif
2247 2401
2248 if readfile(s:me) ==# readfile(new) 2402 if readfile(s:me) ==# readfile(new)
2433 continue 2587 continue
2434 endif 2588 endif
2435 call s:append_ul(2, origin ? 'Pending updates:' : 'Last update:') 2589 call s:append_ul(2, origin ? 'Pending updates:' : 'Last update:')
2436 for [k, v] in plugs 2590 for [k, v] in plugs
2437 let range = origin ? '..origin/'.v.branch : 'HEAD@{1}..' 2591 let range = origin ? '..origin/'.v.branch : 'HEAD@{1}..'
2438 let cmd = 'git log --graph --color=never '.join(map(['--pretty=format:%x01%h%x01%d%x01%s%x01%cr', range], 'plug#shellescape(v:val)')) 2592 let cmd = ['git', 'log', '--graph', '--color=never']
2593 if s:git_version_requirement(2, 10, 0)
2594 call add(cmd, '--no-show-signature')
2595 endif
2596 call extend(cmd, ['--pretty=format:%x01%h%x01%d%x01%s%x01%cr', range])
2439 if has_key(v, 'rtp') 2597 if has_key(v, 'rtp')
2440 let cmd .= ' -- '.plug#shellescape(v.rtp) 2598 call extend(cmd, ['--', v.rtp])
2441 endif 2599 endif
2442 let diff = s:system_chomp(cmd, v.dir) 2600 let diff = s:system_chomp(cmd, v.dir)
2443 if !empty(diff) 2601 if !empty(diff)
2444 let ref = has_key(v, 'tag') ? (' (tag: '.v.tag.')') : has_key(v, 'commit') ? (' '.v.commit) : '' 2602 let ref = has_key(v, 'tag') ? (' (tag: '.v.tag.')') : has_key(v, 'commit') ? (' '.v.commit) : ''
2445 call append(5, extend(['', '- '.k.':'.ref], map(s:lines(diff), 's:format_git_log(v:val)'))) 2603 call append(5, extend(['', '- '.k.':'.ref], map(s:lines(diff), 's:format_git_log(v:val)')))
2483 if empty(name) || !has_key(g:plugs, name) || 2641 if empty(name) || !has_key(g:plugs, name) ||
2484 \ input(printf('Revert the update of %s? (y/N) ', name)) !~? '^y' 2642 \ input(printf('Revert the update of %s? (y/N) ', name)) !~? '^y'
2485 return 2643 return
2486 endif 2644 endif
2487 2645
2488 call s:system('git reset --hard HEAD@{1} && git checkout '.s:esc(g:plugs[name].branch).' --', g:plugs[name].dir) 2646 call s:system('git reset --hard HEAD@{1} && git checkout '.plug#shellescape(g:plugs[name].branch).' --', g:plugs[name].dir)
2489 setlocal modifiable 2647 setlocal modifiable
2490 normal! "_dap 2648 normal! "_dap
2491 setlocal nomodifiable 2649 setlocal nomodifiable
2492 echo 'Reverted' 2650 echo 'Reverted'
2493 endfunction 2651 endfunction
2503 1 2661 1
2504 let anchor = line('$') - 3 2662 let anchor = line('$') - 3
2505 let names = sort(keys(filter(copy(g:plugs), 2663 let names = sort(keys(filter(copy(g:plugs),
2506 \'has_key(v:val, "uri") && !has_key(v:val, "commit") && isdirectory(v:val.dir)'))) 2664 \'has_key(v:val, "uri") && !has_key(v:val, "commit") && isdirectory(v:val.dir)')))
2507 for name in reverse(names) 2665 for name in reverse(names)
2508 let sha = s:system_chomp('git rev-parse --short HEAD', g:plugs[name].dir) 2666 let sha = s:system_chomp(['git', 'rev-parse', '--short', 'HEAD'], g:plugs[name].dir)
2509 if !empty(sha) 2667 if !empty(sha)
2510 call append(anchor, printf("silent! let g:plugs['%s'].commit = '%s'", name, sha)) 2668 call append(anchor, printf("silent! let g:plugs['%s'].commit = '%s'", name, sha))
2511 redraw 2669 redraw
2512 endif 2670 endif
2513 endfor 2671 endfor
2514 2672
2515 if a:0 > 0 2673 if a:0 > 0
2516 let fn = expand(a:1) 2674 let fn = s:plug_expand(a:1)
2517 if filereadable(fn) && !(a:force || s:ask(a:1.' already exists. Overwrite?')) 2675 if filereadable(fn) && !(a:force || s:ask(a:1.' already exists. Overwrite?'))
2518 return 2676 return
2519 endif 2677 endif
2520 call writefile(getline(1, '$'), fn) 2678 call writefile(getline(1, '$'), fn)
2521 echo 'Saved as '.a:1 2679 echo 'Saved as '.a:1

mercurial