Add s:batchfile() for creating batchfiles

Outputs a list containing the batchfile filepath and the new command
to run the batchfile regardless of the user's shell.
This commit is contained in:
Jan Edmund Lazo 2019-08-03 20:40:26 -04:00
parent bb12f7998d
commit 91a643c0ed

View File

@ -350,6 +350,23 @@ if s:is_win
function! s:is_local_plug(repo) function! s:is_local_plug(repo)
return a:repo =~? '^[a-z]:\|^[%~]' return a:repo =~? '^[a-z]:\|^[%~]'
endfunction endfunction
" Copied from fzf
function! s:wrap_cmds(cmds)
return map(['@echo off', 'for /f "tokens=4" %%a in (''chcp'') do set origchcp=%%a', 'chcp 65001 > nul'] +
\ (type(a:cmds) == type([]) ? a:cmds : [a:cmds]) +
\ ['chcp %origchcp% > nul'], 'v:val."\r"')
endfunction
function! s:batchfile(cmd)
let batchfile = tempname().'.bat'
call writefile(s:wrap_cmds(a:cmd), batchfile)
let cmd = s:shellesc(batchfile, &shell)
if &shell =~# 'powershell\.exe$'
let cmd = '& ' . cmd
endif
return [batchfile, cmd]
endfunction
else else
function! s:rtp(spec) function! s:rtp(spec)
return s:dirpath(a:spec.dir . get(a:spec, 'rtp', '')) return s:dirpath(a:spec.dir . get(a:spec, 'rtp', ''))
@ -807,12 +824,7 @@ function! s:bang(cmd, ...)
" but it won't work on Windows. " but it won't work on Windows.
let cmd = a:0 ? s:with_cd(a:cmd, a:1) : a:cmd let cmd = a:0 ? s:with_cd(a:cmd, a:1) : a:cmd
if s:is_win if s:is_win
let batchfile = tempname().'.bat' let [batchfile, cmd] = s:batchfile(cmd)
call writefile(["@echo off\r", cmd . "\r"], batchfile)
let cmd = s:shellesc(batchfile, &shell)
if &shell =~# 'powershell\.exe$'
let cmd = '& ' . cmd
endif
endif endif
let g:_plug_bang = (s:is_win && has('gui_running') ? 'silent ' : '').'!'.escape(cmd, '#!%') let g:_plug_bang = (s:is_win && has('gui_running') ? 'silent ' : '').'!'.escape(cmd, '#!%')
execute "normal! :execute g:_plug_bang\<cr>\<cr>" execute "normal! :execute g:_plug_bang\<cr>\<cr>"
@ -2036,12 +2048,7 @@ function! s:system(cmd, ...)
let [sh, shellcmdflag, shrd] = s:chsh(1) let [sh, shellcmdflag, shrd] = s:chsh(1)
let cmd = a:0 > 0 ? s:with_cd(a:cmd, a:1) : a:cmd let cmd = a:0 > 0 ? s:with_cd(a:cmd, a:1) : a:cmd
if s:is_win if s:is_win
let batchfile = tempname().'.bat' let [batchfile, cmd] = s:batchfile(cmd)
call writefile(["@echo off\r", cmd . "\r"], batchfile)
let cmd = s:shellesc(batchfile, &shell)
if &shell =~# 'powershell\.exe$'
let cmd = '& ' . cmd
endif
endif endif
return system(cmd) return system(cmd)
finally finally
@ -2373,12 +2380,7 @@ function! s:preview_commit()
let [sh, shellcmdflag, shrd] = s:chsh(1) let [sh, shellcmdflag, shrd] = s:chsh(1)
let cmd = 'cd '.s:shellesc(g:plugs[name].dir).' && git show --no-color --pretty=medium '.sha let cmd = 'cd '.s:shellesc(g:plugs[name].dir).' && git show --no-color --pretty=medium '.sha
if s:is_win if s:is_win
let batchfile = tempname().'.bat' let [batchfile, cmd] = s:batchfile(cmd)
call writefile(["@echo off\r", cmd . "\r"], batchfile)
let cmd = s:shellesc(batchfile, &shell)
if &shell =~# 'powershell\.exe$'
let cmd = '& ' . cmd
endif
endif endif
execute 'silent %!' cmd execute 'silent %!' cmd
finally finally