Skip to content

Don't use chcp. #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2019
Merged

Don't use chcp. #913

merged 1 commit into from
Dec 11, 2019

Conversation

janlazo
Copy link
Contributor

@janlazo janlazo commented Nov 29, 2019

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.
iconv() and libcallnr to get the current codepage (can this be cached?).
Neovim always defaults to set encoding=utf-8.

vim-plug runs commands in a batchfile with utf-8 characters.
On terminal Vim, run the batchfile in a separate minimized console
to not break the terminal console.

https://dev.to/mattn/please-stop-hack-chcp-65001-27db

cc @mattn @junegunn

Close #908

plug.vim Outdated
\ + ['endlocal'],
\ 'v:val."\r"')
endfunction

function! s:batchfile(cmd)
let batchfile = tempname().'.bat'
call writefile(s:wrap_cmds(a:cmd), batchfile)
let cmd = plug#shellescape(batchfile, {'shell': &shell, 'script': 1})
if &shell =~# 'powershell\.exe$'
let cmd = plug#shellescape(batchfile, {'shell': &shell, 'script': 0})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should have been 0 to begin with because the batchfile will run on the shell, not inside another batchfile.

plug.vim Outdated
return s:shellesc_cmd(a:arg, script)
elseif shell =~# 'powershell\.exe$' || shell =~# 'pwsh$'
elseif shell =~# 'powershell\.exe' || shell =~# 'pwsh$'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ was a premature optimization because 8.0.1455 and relatex 8.1.x patches were not considered.

plug.vim Outdated
if !has('nvim')
\ && (has('win32') || has('win32unix'))
\ && (!has('multi-byte') || &encoding !=# 'utf-8')
return s:err('Only Vim with +multi_byte feature is supported. Add `set encoding=utf-8` in your vimrc before running `plug#begin()`.')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattn Is this a reasonable restriction to guarantee that the batchfile has utf-8 encoding?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you check support of encoding=utf-8 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to guarantee that writefile works with unicode and batchfile has unicode encoding. I don't know enough about the Vim internals to know if this is needed or is insufficent so I'm asking. Maybe writefile with b flag is enough?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The batch file must be written in code page NOT unicode. So you can use iconv().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let cp = libcallnr('kernel32.dll', 'GetACP', 0)
let text = iconv(text, &encoding, printf('cp%d', cp))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iconv replaces unicode characters with ?. I'd rather use powershell if the batchfile cannot be executed with unicode characters and non-unicode codepage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you have to handle unicode characters?

@mattn
Copy link
Contributor

mattn commented Nov 29, 2019

@janlazo
Copy link
Contributor Author

janlazo commented Nov 30, 2019

Batchfile is required on Vim to support multiple shells while using cmd.exe syntax and not changing the user's shell settings, which depend on the editor and OS. I will reconsider if #594 is finished before removing the batchfile code because Vim's job_start does not use the user's shell.

On Neovim, only :! is problematic. I'll probably use terminal if it can be blocked.

@janlazo janlazo changed the title Don't use chcp. Spawn console for utf-8 batchfile Don't use chcp. Nov 30, 2019
plug.vim Outdated
@@ -139,6 +139,11 @@ function! s:define_commands()
if !executable('git')
return s:err('`git` executable not found. Most commands will not be available. To suppress this message, prepend `silent!` to `call plug#begin(...)`.')
endif
if !has('nvim')
\ && (has('win32') || has('win32unix'))
\ && (!has('multi-byte') || !has('iconv'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/multi-byte/multi_byte/

@mattn
Copy link
Contributor

mattn commented Dec 2, 2019

LGTM

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
@janlazo
Copy link
Contributor Author

janlazo commented Dec 11, 2019

Works with my config on a Windows VM. Tested with cmd.exe, powershell.exe, and bash.exe. Tested only with codepage 1252 (English).

@janlazo janlazo merged commit 359ce90 into junegunn:master Dec 11, 2019
@janlazo
Copy link
Contributor Author

janlazo commented Dec 11, 2019

@mattn Thanks for the help.

@janlazo janlazo deleted the batchfile-unicode branch December 11, 2019 13:29
junegunn added a commit to junegunn/fzf that referenced this pull request Dec 12, 2019
@janlazo
Copy link
Contributor Author

janlazo commented Dec 15, 2019

@junegunn junegunn/fzf@f68017d, port of this PR, breaks filtering for Commands. Back to square one 😞

janlazo referenced this pull request in janlazo/dotvim8 Oct 16, 2021
De-quote only if first and last character of &shell are double-quotes.
Avoid string escape issues when passing escaped '&shell' values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ERROR "invalid uri ^" when runing PlugStatus
3 participants