Skip to content

Commit fcfd5b7

Browse files
authored
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.
1 parent 849b76b commit fcfd5b7

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

plug.vim

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,16 @@ if s:is_win
353353

354354
" Copied from fzf
355355
function! s:wrap_cmds(cmds)
356+
let use_chcp = executable('sed')
356357
return map([
357358
\ '@echo off',
358-
\ 'setlocal enabledelayedexpansion',
359-
\ 'for /f "tokens=*" %%a in (''chcp'') do for %%b in (%%a) do set origchcp=%%b',
360-
\ 'chcp 65001 > nul'
361-
\ ]
359+
\ 'setlocal enabledelayedexpansion']
360+
\ + (use_chcp ? [
361+
\ 'for /f "usebackq" %%a in (`chcp ^| sed "s/[^0-9]//gp"`) do set origchcp=%%a',
362+
\ 'chcp 65001 > nul'] : [])
362363
\ + (type(a:cmds) == type([]) ? a:cmds : [a:cmds])
363-
\ + ['chcp !origchcp! > nul', 'endlocal'],
364+
\ + (use_chcp ? ['chcp !origchcp! > nul'] : [])
365+
\ + ['endlocal'],
364366
\ 'v:val."\r"')
365367
endfunction
366368

0 commit comments

Comments
 (0)