Skip to content

Commit d2f8ca2

Browse files
authored
Detect shellslash on Windows (#916)
Warn user if shellslash and shell are incompatible. Set/unset shellslash for file functions on Windows. Based on https://github.com/junegunn/fzf/blob/16fc6862a89eef0f02d32ab8b365887522719da8/plugin/fzf.vim#L30-L107 Support shellescape for git-bash Windows user may not set shellslash but wish to use git-bash. This requires custom shellescape for sh,bash shells because builtin shellescape() depends on shellslash. Tested on Vim
1 parent 93b7025 commit d2f8ca2

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

plug.vim

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ let s:mac_gui = has('gui_macvim') && has('gui_running')
9999
let s:is_win = has('win32')
100100
let s:nvim = has('nvim-0.2') || (has('nvim') && exists('*jobwait') && !s:is_win)
101101
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
103109
let s:base_spec = { 'branch': 'master', 'frozen': 0 }
104110
let s:TYPE = {
105111
\ 'string': type(''),
@@ -110,18 +116,50 @@ let s:TYPE = {
110116
let s:loaded = get(s:, 'loaded', {})
111117
let s:triggers = get(s:, 'triggers', {})
112118

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+
113151
function! plug#begin(...)
114152
if a:0 > 0
115153
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'))
117155
elseif exists('g:plug_home')
118156
let home = s:path(g:plug_home)
119157
elseif !empty(&rtp)
120158
let home = s:path(split(&rtp, ',')[0]) . '/plugged'
121159
else
122160
return s:err('Unable to determine plug home. Try calling plug#begin() with a path argument.')
123161
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
125163
return s:err('Invalid plug home. '.home.' is a standard Vim runtime path and is not allowed.')
126164
endif
127165

@@ -139,6 +177,11 @@ function! s:define_commands()
139177
if !executable('git')
140178
return s:err('`git` executable not found. Most commands will not be available. To suppress this message, prepend `silent!` to `call plug#begin(...)`.')
141179
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
142185
command! -nargs=* -bar -bang -complete=customlist,s:names PlugInstall call s:install(<bang>0, [<f-args>])
143186
command! -nargs=* -bar -bang -complete=customlist,s:names PlugUpdate call s:update(<bang>0, [<f-args>])
144187
command! -nargs=0 -bar -bang PlugClean call s:clean(<bang>0)
@@ -367,7 +410,7 @@ if s:is_win
367410
endfunction
368411

369412
function! s:batchfile(cmd)
370-
let batchfile = tempname().'.bat'
413+
let batchfile = s:plug_tempname().'.bat'
371414
call writefile(s:wrap_cmds(a:cmd), batchfile)
372415
let cmd = plug#shellescape(batchfile, {'shell': &shell, 'script': 0})
373416
if &shell =~# 'powershell\.exe'
@@ -575,7 +618,7 @@ function! plug#(repo, ...)
575618
try
576619
let repo = s:trim(a:repo)
577620
let opts = a:0 == 1 ? s:parse_options(a:1) : s:base_spec
578-
let name = get(opts, 'as', fnamemodify(repo, ':t:s?\.git$??'))
621+
let name = get(opts, 'as', s:plug_fnamemodify(repo, ':t:s?\.git$??'))
579622
let spec = extend(s:infer_properties(name, repo), opts)
580623
if !has_key(g:plugs, name)
581624
call add(g:plugs_order, name)
@@ -595,7 +638,7 @@ function! s:parse_options(arg)
595638
elseif type == s:TYPE.dict
596639
call extend(opts, a:arg)
597640
if has_key(opts, 'dir')
598-
let opts.dir = s:dirpath(expand(opts.dir))
641+
let opts.dir = s:dirpath(s:plug_expand(opts.dir))
599642
endif
600643
else
601644
throw 'Invalid argument type (expected: string or dictionary)'
@@ -606,7 +649,7 @@ endfunction
606649
function! s:infer_properties(name, repo)
607650
let repo = a:repo
608651
if s:is_local_plug(repo)
609-
return { 'dir': s:dirpath(expand(repo)) }
652+
return { 'dir': s:dirpath(s:plug_expand(repo)) }
610653
else
611654
if repo =~ ':'
612655
let uri = repo
@@ -759,7 +802,7 @@ function! s:finish_bindings()
759802
endfunction
760803

761804
function! s:prepare(...)
762-
if empty(getcwd())
805+
if empty(s:plug_getcwd())
763806
throw 'Invalid current working directory. Cannot proceed.'
764807
endif
765808

@@ -2001,6 +2044,10 @@ function! s:shellesc_ps1(arg)
20012044
return "'".substitute(escape(a:arg, '\"'), "'", "''", 'g')."'"
20022045
endfunction
20032046

2047+
function! s:shellesc_sh(arg)
2048+
return "'".substitute(a:arg, "'", "'\\\\''", 'g')."'"
2049+
endfunction
2050+
20042051
function! plug#shellescape(arg, ...)
20052052
let opts = a:0 > 0 && type(a:1) == s:TYPE.dict ? a:1 : {}
20062053
let shell = get(opts, 'shell', s:is_win ? 'cmd.exe' : 'sh')
@@ -2010,7 +2057,7 @@ function! plug#shellescape(arg, ...)
20102057
elseif shell =~# 'powershell\.exe' || shell =~# 'pwsh$'
20112058
return s:shellesc_ps1(a:arg)
20122059
endif
2013-
return shellescape(a:arg)
2060+
return s:shellesc_sh(a:arg)
20142061
endfunction
20152062

20162063
function! s:glob_dir(path)
@@ -2163,7 +2210,7 @@ function! s:clean(force)
21632210

21642211
let allowed = {}
21652212
for dir in dirs
2166-
let allowed[s:dirpath(fnamemodify(dir, ':h:h'))] = 1
2213+
let allowed[s:dirpath(s:plug_fnamemodify(dir, ':h:h'))] = 1
21672214
let allowed[dir] = 1
21682215
for child in s:glob_dir(dir)
21692216
let allowed[child] = 1
@@ -2236,7 +2283,7 @@ endfunction
22362283
function! s:upgrade()
22372284
echo 'Downloading the latest version of vim-plug'
22382285
redraw
2239-
let tmp = tempname()
2286+
let tmp = s:plug_tempname()
22402287
let new = tmp . '/plug.vim'
22412288

22422289
try
@@ -2513,7 +2560,7 @@ function! s:snapshot(force, ...) abort
25132560
endfor
25142561

25152562
if a:0 > 0
2516-
let fn = expand(a:1)
2563+
let fn = s:plug_expand(a:1)
25172564
if filereadable(fn) && !(a:force || s:ask(a:1.' already exists. Overwrite?'))
25182565
return
25192566
endif

0 commit comments

Comments
 (0)