From fcfd5b7e1f2ac9f90e933037cd2ef1d0ef2ff992 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Mon, 14 Oct 2019 07:55:41 -0400 Subject: [PATCH] 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. --- plug.vim | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/plug.vim b/plug.vim index 2571fe3..b8a07f1 100644 --- a/plug.vim +++ b/plug.vim @@ -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