From 91a643c0ed31e4e7890a4e91c7a4d06ca8a68540 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Sat, 3 Aug 2019 20:40:26 -0400 Subject: [PATCH] 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. --- plug.vim | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/plug.vim b/plug.vim index 2e1580d..826bd5b 100644 --- a/plug.vim +++ b/plug.vim @@ -350,6 +350,23 @@ if s:is_win function! s:is_local_plug(repo) return a:repo =~? '^[a-z]:\|^[%~]' 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 function! s:rtp(spec) 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. let cmd = a:0 ? s:with_cd(a:cmd, a:1) : a:cmd if s:is_win - let batchfile = tempname().'.bat' - call writefile(["@echo off\r", cmd . "\r"], batchfile) - let cmd = s:shellesc(batchfile, &shell) - if &shell =~# 'powershell\.exe$' - let cmd = '& ' . cmd - endif + let [batchfile, cmd] = s:batchfile(cmd) endif let g:_plug_bang = (s:is_win && has('gui_running') ? 'silent ' : '').'!'.escape(cmd, '#!%') execute "normal! :execute g:_plug_bang\\" @@ -2036,12 +2048,7 @@ function! s:system(cmd, ...) let [sh, shellcmdflag, shrd] = s:chsh(1) let cmd = a:0 > 0 ? s:with_cd(a:cmd, a:1) : a:cmd if s:is_win - let batchfile = tempname().'.bat' - call writefile(["@echo off\r", cmd . "\r"], batchfile) - let cmd = s:shellesc(batchfile, &shell) - if &shell =~# 'powershell\.exe$' - let cmd = '& ' . cmd - endif + let [batchfile, cmd] = s:batchfile(cmd) endif return system(cmd) finally @@ -2373,12 +2380,7 @@ function! s:preview_commit() let [sh, shellcmdflag, shrd] = s:chsh(1) let cmd = 'cd '.s:shellesc(g:plugs[name].dir).' && git show --no-color --pretty=medium '.sha if s:is_win - let batchfile = tempname().'.bat' - call writefile(["@echo off\r", cmd . "\r"], batchfile) - let cmd = s:shellesc(batchfile, &shell) - if &shell =~# 'powershell\.exe$' - let cmd = '& ' . cmd - endif + let [batchfile, cmd] = s:batchfile(cmd) endif execute 'silent %!' cmd finally