Browse Source

Encode batchfile in current codepage. (#913)

Changing chcp breaks cmd.exe if switching from multi-byte to 65001.
cmd.exe depends on codepage to parse batchfile
so batchfile cannot have unicode characters.
Target powershell if unicode support is required.

User should fix their terminal font to display unicode characters.
Terminal programs can only use Wide String APIs.
For Vim, this requires +multi_byte feature and `set encoding=utf-8`
in the user's vimrc.
Neovim always defaults to `set encoding=utf-8`.

https://dev.to/mattn/please-stop-hack-chcp-65001-27db
Jan Edmund Lazo 6 years ago
parent
commit
359ce90b9b
1 changed files with 7 additions and 6 deletions
  1. 7 6
      plug.vim

+ 7 - 6
plug.vim

@@ -182,6 +182,11 @@ function! s:define_commands()
   \ && (&shell =~# 'cmd\.exe' || &shell =~# 'powershell\.exe')
     return s:err('vim-plug does not support shell, ' . &shell . ', when shellslash is set.')
   endif
+  if !has('nvim')
+    \ && (has('win32') || has('win32unix'))
+    \ && (!has('multi_byte') || !has('iconv'))
+    return s:err('Vim needs +iconv, +multi_byte features on Windows to run shell commands.')
+  endif
   command! -nargs=* -bar -bang -complete=customlist,s:names PlugInstall call s:install(<bang>0, [<f-args>])
   command! -nargs=* -bar -bang -complete=customlist,s:names PlugUpdate  call s:update(<bang>0, [<f-args>])
   command! -nargs=0 -bar -bang PlugClean call s:clean(<bang>0)
@@ -395,18 +400,14 @@ if s:is_win
   endfunction
 
   " Copied from fzf
+  let s:codepage = libcallnr('kernel32.dll', 'GetACP', 0)
   function! s:wrap_cmds(cmds)
-    let use_chcp = executable('sed')
     return map([
       \ '@echo off',
       \ '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])
-    \ + (use_chcp ? ['chcp !origchcp! > nul'] : [])
     \ + ['endlocal'],
-    \ 'v:val."\r"')
+    \ printf('iconv(v:val."\r", "%s", "cp%d")', &encoding, s:codepage))
   endfunction
 
   function! s:batchfile(cmd)