@@ -350,6 +350,23 @@ if s:is_win
350
350
function ! s: is_local_plug (repo)
351
351
return a: repo = ~? ' ^[a-z]:\|^[%~]'
352
352
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
353
370
else
354
371
function ! s: rtp (spec)
355
372
return s: dirpath (a: spec .dir . get (a: spec , ' rtp' , ' ' ))
@@ -807,9 +824,7 @@ function! s:bang(cmd, ...)
807
824
" but it won't work on Windows.
808
825
let cmd = a: 0 ? s: with_cd (a: cmd , a: 1 ) : a: cmd
809
826
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)
813
828
endif
814
829
let g: _plug_bang = (s: is_win && has (' gui_running' ) ? ' silent ' : ' ' ).' !' .escape (cmd, ' #!%' )
815
830
execute " normal! :execute g:_plug_bang\<cr>\<cr> "
@@ -1092,7 +1107,7 @@ function! s:update_finish()
1092
1107
elseif has_key (spec, ' tag' )
1093
1108
let tag = spec.tag
1094
1109
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 ))
1096
1111
if ! v: shell_error && ! empty (tags )
1097
1112
let tag = tags [0 ]
1098
1113
call s: log4 (name, printf (' Latest tag for %s -> %s' , spec.tag , tag ))
@@ -1149,7 +1164,7 @@ function! s:job_abort()
1149
1164
silent ! call job_stop (j .jobid)
1150
1165
endif
1151
1166
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 ))
1153
1168
endif
1154
1169
endfor
1155
1170
let s: jobs = {}
@@ -1202,15 +1217,10 @@ endfunction
1202
1217
1203
1218
function ! s: spawn (name, cmd, opts)
1204
1219
let job = { ' name' : a: name , ' running' : 1 , ' error' : 0 , ' lines' : [' ' ],
1205
- \ ' batchfile' : (s: is_win && (s: nvim || s: vim8 )) ? tempname ().' .bat' : ' ' ,
1206
1220
\ ' new' : get (a: opts , ' new' , 0 ) }
1207
1221
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]
1214
1224
1215
1225
if s: nvim
1216
1226
call extend (job, {
@@ -1260,9 +1270,6 @@ function! s:reap(name)
1260
1270
call s: log (bullet, a: name , empty (result) ? ' OK' : result)
1261
1271
call s: bar ()
1262
1272
1263
- if has_key (job, ' batchfile' ) && ! empty (job.batchfile)
1264
- call delete (job.batchfile)
1265
- endif
1266
1273
call remove (s: jobs , a: name )
1267
1274
endfunction
1268
1275
@@ -1352,8 +1359,8 @@ while 1 " Without TCO, Vim stack is bound to explode
1352
1359
\ printf (' git clone %s %s %s %s 2>&1' ,
1353
1360
\ has_tag ? ' ' : s: clone_opt ,
1354
1361
\ 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 })
1357
1364
endif
1358
1365
1359
1366
if ! s: jobs [name].running
@@ -1980,17 +1987,23 @@ function! s:update_ruby()
1980
1987
EOF
1981
1988
endfunction
1982
1989
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' )." '"
1989
1997
endfunction
1990
1998
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 )
1994
2007
endif
1995
2008
return shellescape (a: arg )
1996
2009
endfunction
@@ -2024,18 +2037,17 @@ function! s:format_message(bullet, name, message)
2024
2037
endif
2025
2038
endfunction
2026
2039
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 )
2029
2043
endfunction
2030
2044
2031
2045
function ! s: system (cmd, ... )
2032
2046
try
2033
2047
let [sh , shellcmdflag , shrd] = s: chsh (1 )
2034
2048
let cmd = a: 0 > 0 ? s: with_cd (a: cmd , a: 1 ) : a: cmd
2035
2049
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)
2039
2051
endif
2040
2052
return system (cmd)
2041
2053
finally
@@ -2113,7 +2125,7 @@ endfunction
2113
2125
2114
2126
function ! s: rm_rf (dir )
2115
2127
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 ))
2117
2129
endif
2118
2130
endfunction
2119
2131
@@ -2222,7 +2234,7 @@ function! s:upgrade()
2222
2234
let new = tmp . ' /plug.vim'
2223
2235
2224
2236
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)))
2226
2238
if v: shell_error
2227
2239
return s: err (' Error upgrading vim-plug: ' . out)
2228
2240
endif
@@ -2365,11 +2377,9 @@ function! s:preview_commit()
2365
2377
setlocal previewwindow filetype = git buftype = nofile nobuflisted modifiable
2366
2378
try
2367
2379
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
2369
2381
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)
2373
2383
endif
2374
2384
execute ' silent %!' cmd
2375
2385
finally
@@ -2418,9 +2428,9 @@ function! s:diff()
2418
2428
call s: append_ul (2 , origin ? ' Pending updates:' : ' Last update:' )
2419
2429
for [k , v ] in plugs
2420
2430
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)' ))
2422
2432
if has_key (v , ' rtp' )
2423
- let cmd .= ' -- ' .s: shellesc (v .rtp )
2433
+ let cmd .= ' -- ' .plug#shellescape (v .rtp )
2424
2434
endif
2425
2435
let diff = s: system_chomp (cmd, v .dir )
2426
2436
if ! empty (diff )
0 commit comments