Use chcp only if sed is in PATH (#891)

chcp parsing is fragile because of the system locale. There's no convenient way to parse out the codepage value without regex just by relying on cmd.exe builtins and default binaries in PATH.

Vim can be used to parse chcp output but it requires an additional `system` per `s:system` and `chcp` can change within the same console so caching the value won't work on the terminal.

Powershell supports regex but it has a long startup even with `-NoProfile` so running it when `&shell` is not powershell slows down `:PlugInstall` more.
This commit is contained in:
Jan Edmund Lazo 2019-10-14 07:55:41 -04:00 committed by GitHub
parent 849b76be90
commit fcfd5b7e1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -353,14 +353,16 @@ if s:is_win
" Copied from fzf
function! s:wrap_cmds(cmds)
let use_chcp = executable('sed')
return map([
\ '@echo off',
\ 'setlocal enabledelayedexpansion',
\ 'for /f "tokens=*" %%a in (''chcp'') do for %%b in (%%a) do set origchcp=%%b',
\ 'chcp 65001 > nul'
\ ]
\ 'setlocal enabledelayedexpansion']
\ + (use_chcp ? [
\ 'for /f "usebackq" %%a in (`chcp ^| sed "s/[^0-9]//gp"`) do set origchcp=%%a',
\ 'chcp 65001 > nul'] : [])
\ + (type(a:cmds) == type([]) ? a:cmds : [a:cmds])
\ + ['chcp !origchcp! > nul', 'endlocal'],
\ + (use_chcp ? ['chcp !origchcp! > nul'] : [])
\ + ['endlocal'],
\ 'v:val."\r"')
endfunction