Skip to content

Commit cf4a749

Browse files
committed
Fix MacVim's locale not having encoding when launched from Dock
When launching MacVim not from terminal, macOS doesn't set the $LANG environment variable, and the locale API doesn't have a way to return the encoding (probably because everything is UTF-8). As such Vim would set the locale to something like "en_US", which breaks certain tools that expects an encoding part set. Fix this by simply appending ".UTF-8" to the constructed locale (e.g. "en_US.UTF-8") if $LANG doesn't exist. This makes sense as we default to UTF-8 in MacVim and macOS is basically UTF-8 native anyway. Also, add a test that will make sure this is the case. Fix macvim-dev#1033
1 parent dce1eac commit cf4a749

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/os_mac_conv.c

+10
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,16 @@ mac_lang_init(void)
576576
kLocaleRegionMask | kLocaleRegionVariantMask,
577577
sizeof buf, buf) == noErr && *buf)
578578
{
579+
# ifdef FEAT_GUI_MACVIM
580+
// This usually happens when the user directly launches from the Dock
581+
// instead of terminal. macOS doesn't really provide the encoding part
582+
// in the locale API, since it assumes everything is UTF-8 anyway. We
583+
// should manually append a UTF-8 encoding component to the locale
584+
// string. This helps tools that wants to parse the encoding compoennt
585+
// of the locale.
586+
strlcat(buf, ".UTF-8", sizeof(buf)/sizeof(char));
587+
# endif
588+
579589
vim_setenv((char_u *)"LANG", (char_u *)buf);
580590
# ifdef HAVE_LOCALE_H
581591
setlocale(LC_ALL, "");

src/testdir/test_macvim.vim

+41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
" Test for MacVim behaviors and regressions
22

33
source check.vim
4+
source term_util.vim
45
CheckFeature gui_macvim
56

67
" Tests for basic existence of commands and options to make sure no
@@ -54,3 +55,43 @@ func Test_macvim_mappings()
5455
call feedkeys("\<ForceClick>", "xt")
5556
call assert_equal(5, g:marker_value)
5657
endfunc
58+
59+
" Test that we correctly set the locale to have .UTF-8 if launching from the
60+
" Dock or the $LANG env variable is not set.
61+
func Test_macvim_default_locale_utf8()
62+
CheckRunVimInTerminal
63+
if !has('terminal') || !executable('/bin/sh')
64+
return
65+
endif
66+
let buf = term_start('/bin/sh')
67+
68+
" Wait for shell prompt.
69+
call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
70+
71+
" Unset the $LANG environmental variable. This causes Vim to try to set a
72+
" default one in macOS.
73+
call term_sendkeys(buf, "unset LANG\<CR>")
74+
75+
" Run Vim and ask it to output the $LANG variable. It should be
76+
" automatically created since it doesn't exist.
77+
call term_sendkeys(buf, v:progpath
78+
\ . " --clean -X"
79+
\ . " -c 'echo $LANG'\<CR>")
80+
81+
" Wait for Vim to come up and show something in the status line.
82+
let term_rows = term_getsize(buf)[0]
83+
call term_wait(buf)
84+
call WaitFor({-> len(term_getline(buf, term_rows)) > 0})
85+
86+
" Check that the locale actually has .UTF-8 in it. We can't check for
87+
" "en_US.UTF-8" because we shouldn't assume what locale the tester is
88+
" using.
89+
call assert_match('^[a-zA-Z-_]\+\.UTF-8$', term_getline(buf, term_rows), "Default locale doesn't have UTF-8 encoding set")
90+
91+
" Cleanly exist from Vim/terminal and clean up.
92+
call term_sendkeys(buf, ":qall!\<CR>")
93+
call term_wait(buf)
94+
call WaitForAssert({-> assert_match('[$#] $', term_getline(buf, '.'))})
95+
call StopShellInTerminal(buf)
96+
exe buf . 'bwipe!'
97+
endfunc

0 commit comments

Comments
 (0)