Skip to content

Commit 8a44109

Browse files
janlazojunegunn
authored andcommitted
Fix Windows support for Unix shells and powershell (#860)
Excerpt from `:h shell-powershell`: To use powershell (on Windows): set shell=powershell shellquote=( shellpipe=\| shellxquote= set shellcmdflag=-NoLogo\ -NoProfile\ -ExecutionPolicy\ RemoteSigned\ -Command set shellredir=\|\ Out-File\ -Encoding\ UTF8
1 parent 0b32d2d commit 8a44109

File tree

3 files changed

+87
-39
lines changed

3 files changed

+87
-39
lines changed

plug.vim

+49-39
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,23 @@ if s:is_win
350350
function! s:is_local_plug(repo)
351351
return a:repo =~? '^[a-z]:\|^[%~]'
352352
endfunction
353+
354+
" Copied from fzf
355+
function! s:wrap_cmds(cmds)
356+
return map(['@echo off', 'for /f "tokens=4" %%a in (''chcp'') do set origchcp=%%a', 'chcp 65001 > nul'] +
357+
\ (type(a:cmds) == type([]) ? a:cmds : [a:cmds]) +
358+
\ ['chcp %origchcp% > nul'], 'v:val."\r"')
359+
endfunction
360+
361+
function! s:batchfile(cmd)
362+
let batchfile = tempname().'.bat'
363+
call writefile(s:wrap_cmds(a:cmd), batchfile)
364+
let cmd = plug#shellescape(batchfile, {'shell': &shell, 'script': 1})
365+
if &shell =~# 'powershell\.exe$'
366+
let cmd = '& ' . cmd
367+
endif
368+
return [batchfile, cmd]
369+
endfunction
353370
else
354371
function! s:rtp(spec)
355372
return s:dirpath(a:spec.dir . get(a:spec, 'rtp', ''))
@@ -807,9 +824,7 @@ function! s:bang(cmd, ...)
807824
" but it won't work on Windows.
808825
let cmd = a:0 ? s:with_cd(a:cmd, a:1) : a:cmd
809826
if s:is_win
810-
let batchfile = tempname().'.bat'
811-
call writefile(["@echo off\r", cmd . "\r"], batchfile)
812-
let cmd = s:shellesc(expand(batchfile))
827+
let [batchfile, cmd] = s:batchfile(cmd)
813828
endif
814829
let g:_plug_bang = (s:is_win && has('gui_running') ? 'silent ' : '').'!'.escape(cmd, '#!%')
815830
execute "normal! :execute g:_plug_bang\<cr>\<cr>"
@@ -1092,7 +1107,7 @@ function! s:update_finish()
10921107
elseif has_key(spec, 'tag')
10931108
let tag = spec.tag
10941109
if tag =~ '\*'
1095-
let tags = s:lines(s:system('git tag --list '.s:shellesc(tag).' --sort -version:refname 2>&1', spec.dir))
1110+
let tags = s:lines(s:system('git tag --list '.plug#shellescape(tag).' --sort -version:refname 2>&1', spec.dir))
10961111
if !v:shell_error && !empty(tags)
10971112
let tag = tags[0]
10981113
call s:log4(name, printf('Latest tag for %s -> %s', spec.tag, tag))
@@ -1149,7 +1164,7 @@ function! s:job_abort()
11491164
silent! call job_stop(j.jobid)
11501165
endif
11511166
if j.new
1152-
call s:system('rm -rf ' . s:shellesc(g:plugs[name].dir))
1167+
call s:system('rm -rf ' . plug#shellescape(g:plugs[name].dir))
11531168
endif
11541169
endfor
11551170
let s:jobs = {}
@@ -1202,15 +1217,10 @@ endfunction
12021217

12031218
function! s:spawn(name, cmd, opts)
12041219
let job = { 'name': a:name, 'running': 1, 'error': 0, 'lines': [''],
1205-
\ 'batchfile': (s:is_win && (s:nvim || s:vim8)) ? tempname().'.bat' : '',
12061220
\ 'new': get(a:opts, 'new', 0) }
12071221
let s:jobs[a:name] = job
1208-
let cmd = has_key(a:opts, 'dir') ? s:with_cd(a:cmd, a:opts.dir) : a:cmd
1209-
if !empty(job.batchfile)
1210-
call writefile(["@echo off\r", cmd . "\r"], job.batchfile)
1211-
let cmd = s:shellesc(expand(job.batchfile))
1212-
endif
1213-
let argv = add(s:is_win ? ['cmd', '/c'] : ['sh', '-c'], cmd)
1222+
let cmd = has_key(a:opts, 'dir') ? s:with_cd(a:cmd, a:opts.dir, 0) : a:cmd
1223+
let argv = s:is_win ? ['cmd', '/s', '/c', '"'.cmd.'"'] : ['sh', '-c', cmd]
12141224

12151225
if s:nvim
12161226
call extend(job, {
@@ -1260,9 +1270,6 @@ function! s:reap(name)
12601270
call s:log(bullet, a:name, empty(result) ? 'OK' : result)
12611271
call s:bar()
12621272

1263-
if has_key(job, 'batchfile') && !empty(job.batchfile)
1264-
call delete(job.batchfile)
1265-
endif
12661273
call remove(s:jobs, a:name)
12671274
endfunction
12681275

@@ -1352,8 +1359,8 @@ while 1 " Without TCO, Vim stack is bound to explode
13521359
\ printf('git clone %s %s %s %s 2>&1',
13531360
\ has_tag ? '' : s:clone_opt,
13541361
\ prog,
1355-
\ s:shellesc(spec.uri),
1356-
\ s:shellesc(s:trim(spec.dir))), { 'new': 1 })
1362+
\ plug#shellescape(spec.uri, {'script': 0}),
1363+
\ plug#shellescape(s:trim(spec.dir), {'script': 0})), { 'new': 1 })
13571364
endif
13581365

13591366
if !s:jobs[name].running
@@ -1980,17 +1987,23 @@ function! s:update_ruby()
19801987
EOF
19811988
endfunction
19821989

1983-
function! s:shellesc_cmd(arg)
1984-
let escaped = substitute(a:arg, '[&|<>()@^]', '^&', 'g')
1985-
let escaped = substitute(escaped, '%', '%%', 'g')
1986-
let escaped = substitute(escaped, '"', '\\^&', 'g')
1987-
let escaped = substitute(escaped, '\(\\\+\)\(\\^\)', '\1\1\2', 'g')
1988-
return '^"'.substitute(escaped, '\(\\\+\)$', '\1\1', '').'^"'
1990+
function! s:shellesc_cmd(arg, script)
1991+
let escaped = substitute('"'.a:arg.'"', '[&|<>()@^!"]', '^&', 'g')
1992+
return substitute(escaped, '%', (a:script ? '%' : '^') . '&', 'g')
1993+
endfunction
1994+
1995+
function! s:shellesc_ps1(arg)
1996+
return "'".substitute(escape(a:arg, '\"'), "'", "''", 'g')."'"
19891997
endfunction
19901998

1991-
function! s:shellesc(arg)
1992-
if &shell =~# 'cmd.exe$'
1993-
return s:shellesc_cmd(a:arg)
1999+
function! plug#shellescape(arg, ...)
2000+
let opts = a:0 > 0 && type(a:1) == s:TYPE.dict ? a:1 : {}
2001+
let shell = get(opts, 'shell', s:is_win ? 'cmd.exe' : 'sh')
2002+
let script = get(opts, 'script', 1)
2003+
if shell =~# 'cmd\.exe$'
2004+
return s:shellesc_cmd(a:arg, script)
2005+
elseif shell =~# 'powershell\.exe$' || shell =~# 'pwsh$'
2006+
return s:shellesc_ps1(a:arg)
19942007
endif
19952008
return shellescape(a:arg)
19962009
endfunction
@@ -2024,18 +2037,17 @@ function! s:format_message(bullet, name, message)
20242037
endif
20252038
endfunction
20262039

2027-
function! s:with_cd(cmd, dir)
2028-
return printf('cd%s %s && %s', s:is_win ? ' /d' : '', s:shellesc(a:dir), a:cmd)
2040+
function! s:with_cd(cmd, dir, ...)
2041+
let script = a:0 > 0 ? a:1 : 1
2042+
return printf('cd%s %s && %s', s:is_win ? ' /d' : '', plug#shellescape(a:dir, {'script': script}), a:cmd)
20292043
endfunction
20302044

20312045
function! s:system(cmd, ...)
20322046
try
20332047
let [sh, shellcmdflag, shrd] = s:chsh(1)
20342048
let cmd = a:0 > 0 ? s:with_cd(a:cmd, a:1) : a:cmd
20352049
if s:is_win
2036-
let batchfile = tempname().'.bat'
2037-
call writefile(["@echo off\r", cmd . "\r"], batchfile)
2038-
let cmd = s:shellesc(expand(batchfile))
2050+
let [batchfile, cmd] = s:batchfile(cmd)
20392051
endif
20402052
return system(cmd)
20412053
finally
@@ -2113,7 +2125,7 @@ endfunction
21132125

21142126
function! s:rm_rf(dir)
21152127
if isdirectory(a:dir)
2116-
call s:system((s:is_win ? 'rmdir /S /Q ' : 'rm -rf ') . s:shellesc(a:dir))
2128+
call s:system((s:is_win ? 'rmdir /S /Q ' : 'rm -rf ') . plug#shellescape(a:dir))
21172129
endif
21182130
endfunction
21192131

@@ -2222,7 +2234,7 @@ function! s:upgrade()
22222234
let new = tmp . '/plug.vim'
22232235

22242236
try
2225-
let out = s:system(printf('git clone --depth 1 %s %s', s:shellesc(s:plug_src), s:shellesc(tmp)))
2237+
let out = s:system(printf('git clone --depth 1 %s %s', plug#shellescape(s:plug_src), plug#shellescape(tmp)))
22262238
if v:shell_error
22272239
return s:err('Error upgrading vim-plug: '. out)
22282240
endif
@@ -2365,11 +2377,9 @@ function! s:preview_commit()
23652377
setlocal previewwindow filetype=git buftype=nofile nobuflisted modifiable
23662378
try
23672379
let [sh, shellcmdflag, shrd] = s:chsh(1)
2368-
let cmd = 'cd '.s:shellesc(g:plugs[name].dir).' && git show --no-color --pretty=medium '.sha
2380+
let cmd = 'cd '.plug#shellescape(g:plugs[name].dir).' && git show --no-color --pretty=medium '.sha
23692381
if s:is_win
2370-
let batchfile = tempname().'.bat'
2371-
call writefile(["@echo off\r", cmd . "\r"], batchfile)
2372-
let cmd = expand(batchfile)
2382+
let [batchfile, cmd] = s:batchfile(cmd)
23732383
endif
23742384
execute 'silent %!' cmd
23752385
finally
@@ -2418,9 +2428,9 @@ function! s:diff()
24182428
call s:append_ul(2, origin ? 'Pending updates:' : 'Last update:')
24192429
for [k, v] in plugs
24202430
let range = origin ? '..origin/'.v.branch : 'HEAD@{1}..'
2421-
let cmd = 'git log --graph --color=never '.join(map(['--pretty=format:%x01%h%x01%d%x01%s%x01%cr', range], 's:shellesc(v:val)'))
2431+
let cmd = 'git log --graph --color=never '.join(map(['--pretty=format:%x01%h%x01%d%x01%s%x01%cr', range], 'plug#shellescape(v:val)'))
24222432
if has_key(v, 'rtp')
2423-
let cmd .= ' -- '.s:shellesc(v.rtp)
2433+
let cmd .= ' -- '.plug#shellescape(v.rtp)
24242434
endif
24252435
let diff = s:system_chomp(cmd, v.dir)
24262436
if !empty(diff)

test/functional.vader

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Execute (plug#shellescape() works without optional arguments):
2+
if has('unix')
3+
AssertEqual "''", plug#shellescape("")
4+
AssertEqual "'foo'\\'''", plug#shellescape("foo'")
5+
endif
6+
7+
Execute (plug#shellescape() ignores invalid optional argument):
8+
if has('unix')
9+
AssertEqual "''", plug#shellescape("", '')
10+
AssertEqual "'foo'\\'''", plug#shellescape("foo'", [])
11+
endif
12+
13+
Execute (plug#shellescape() depends on the shell):
14+
AssertEqual "'foo'\\'''", plug#shellescape("foo'", {'shell': 'sh'})
15+
AssertEqual '^"foo''^"', plug#shellescape("foo'", {'shell': 'cmd.exe'})
16+
AssertEqual "'foo'''", plug#shellescape("foo'", {'shell': 'powershell.exe'})
17+
AssertEqual "'foo'''", plug#shellescape("foo'", {'shell': 'pwsh'})
18+
19+
Execute (plug#shellescape() supports non-trivial cmd.exe escaping):
20+
" batchfile
21+
AssertEqual '^"^^%%PATH^^%%^"', plug#shellescape("^%PATH^%", {
22+
\ 'shell': 'cmd.exe',
23+
\ })
24+
AssertEqual '^"^^%%PATH^^%%^"', plug#shellescape("^%PATH^%", {
25+
\ 'shell': 'cmd.exe',
26+
\ 'script': 1,
27+
\ })
28+
" command prompt
29+
AssertEqual '^"^^^%PATH^^^%^"', plug#shellescape("^%PATH^%", {
30+
\ 'shell': 'cmd.exe',
31+
\ 'script': 0,
32+
\ }),
33+
34+
Execute (plug#shellescape() supports non-trivial powershell.exe escaping):
35+
AssertEqual '''\"Foo\\''''\\Bar\"''', plug#shellescape('"Foo\''\Bar"', {
36+
\ 'shell': 'powershell.exe',
37+
\ }),

test/test.vader

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Execute (Print Interpreter Version):
9090

9191
Include: workflow.vader
9292
Include: regressions.vader
93+
Include: functional.vader
9394

9495
Execute (Cleanup):
9596
silent! call RmRf(g:temp_plugged)

0 commit comments

Comments
 (0)