To: vim_dev@googlegroups.com Subject: Patch 8.0.1152 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.0.1152 Problem: Encoding of error message wrong in Cygwin terminal. Solution: Get locale from environment variables. (Ken Takata) Files: src/main.c, src/mbyte.c, src/proto/mbyte.pro *** ../vim-8.0.1151/src/main.c 2017-09-16 20:54:47.106560363 +0200 --- src/main.c 2017-09-26 18:56:48.623982770 +0200 *************** *** 2564,2569 **** --- 2564,2584 ---- #if defined(WIN3264) && !defined(FEAT_GUI_W32) if (is_cygpty_used()) { + # if defined(FEAT_MBYTE) && defined(HAVE_BIND_TEXTDOMAIN_CODESET) \ + && defined(FEAT_GETTEXT) + char *s, *tofree = NULL; + + /* Set the encoding of the error message based on $LC_ALL or + * other environment variables instead of 'encoding'. + * Note that the message is shown on a Cygwin terminal (e.g. + * mintty) which encoding is based on $LC_ALL or etc., not the + * current codepage used by normal Win32 console programs. */ + tofree = s = enc_locale_env(NULL); + if (s == NULL) + s = "utf-8"; /* Use "utf-8" by default. */ + (void)bind_textdomain_codeset(VIMPACKAGE, s); + vim_free(tofree); + # endif mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n")); exit(1); } *** ../vim-8.0.1151/src/mbyte.c 2017-09-13 22:09:53.429787547 +0200 --- src/mbyte.c 2017-09-26 18:52:24.549607914 +0200 *************** *** 4385,4429 **** #if defined(FEAT_MBYTE) || defined(PROTO) ! #ifdef HAVE_LANGINFO_H ! # include ! #endif /* ! * Get the canonicalized encoding of the current locale. * Returns an allocated string when successful, NULL when not. */ char_u * ! enc_locale(void) { ! #ifndef WIN3264 ! char *s; char *p; int i; - #endif char buf[50]; - #ifdef WIN3264 - long acp = GetACP(); ! if (acp == 1200) ! STRCPY(buf, "ucs-2le"); ! else if (acp == 1252) /* cp1252 is used as latin1 */ ! STRCPY(buf, "latin1"); ! else ! sprintf(buf, "cp%ld", acp); ! #else ! # ifdef HAVE_NL_LANGINFO_CODESET ! if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL) ! # endif ! # if defined(HAVE_LOCALE_H) || defined(X_LOCALE) ! if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL) ! # endif ! if ((s = getenv("LC_ALL")) == NULL || *s == NUL) ! if ((s = getenv("LC_CTYPE")) == NULL || *s == NUL) ! s = getenv("LANG"); if (s == NULL || *s == NUL) ! return FAIL; /* The most generic locale format is: * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]] --- 4385,4415 ---- #if defined(FEAT_MBYTE) || defined(PROTO) ! # ifdef HAVE_LANGINFO_H ! # include ! # endif + # ifndef FEAT_GUI_W32 /* ! * Get the canonicalized encoding from the specified locale string "locale" ! * or from the environment variables LC_ALL, LC_CTYPE and LANG. * Returns an allocated string when successful, NULL when not. */ char_u * ! enc_locale_env(char *locale) { ! char *s = locale; char *p; int i; char buf[50]; ! if (s == NULL || *s == NUL) ! if ((s = getenv("LC_ALL")) == NULL || *s == NUL) ! if ((s = getenv("LC_CTYPE")) == NULL || *s == NUL) ! s = getenv("LANG"); if (s == NULL || *s == NUL) ! return NULL; /* The most generic locale format is: * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]] *************** *** 4458,4469 **** break; } buf[i] = NUL; - #endif return enc_canonize((char_u *)buf); } ! #if defined(WIN3264) || defined(PROTO) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD) /* * Convert an encoding name to an MS-Windows codepage. * Returns zero if no codepage can be figured out. --- 4444,4489 ---- break; } buf[i] = NUL; return enc_canonize((char_u *)buf); } + # endif + + /* + * Get the canonicalized encoding of the current locale. + * Returns an allocated string when successful, NULL when not. + */ + char_u * + enc_locale(void) + { + # ifdef WIN3264 + char buf[50]; + long acp = GetACP(); + + if (acp == 1200) + STRCPY(buf, "ucs-2le"); + else if (acp == 1252) /* cp1252 is used as latin1 */ + STRCPY(buf, "latin1"); + else + sprintf(buf, "cp%ld", acp); + + return enc_canonize((char_u *)buf); + # else + char *s; + + # ifdef HAVE_NL_LANGINFO_CODESET + if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL) + # endif + # if defined(HAVE_LOCALE_H) || defined(X_LOCALE) + if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL) + # endif + s = NULL; + + return enc_locale_env(s); + # endif + } ! # if defined(WIN3264) || defined(PROTO) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD) /* * Convert an encoding name to an MS-Windows codepage. * Returns zero if no codepage can be figured out. *************** *** 4490,4496 **** return cp; return 0; } ! #endif # if defined(USE_ICONV) || defined(PROTO) --- 4510,4516 ---- return cp; return 0; } ! # endif # if defined(USE_ICONV) || defined(PROTO) *** ../vim-8.0.1151/src/proto/mbyte.pro 2017-08-30 13:22:24.540288850 +0200 --- src/proto/mbyte.pro 2017-09-26 18:52:24.549607914 +0200 *************** *** 71,76 **** --- 71,77 ---- int mb_fix_col(int col, int row); char_u *enc_skip(char_u *p); char_u *enc_canonize(char_u *enc); + char_u *enc_locale_env(char *locale); char_u *enc_locale(void); int encname2codepage(char_u *name); void *my_iconv_open(char_u *to, char_u *from); *** ../vim-8.0.1151/src/version.c 2017-09-26 17:40:40.591681236 +0200 --- src/version.c 2017-09-26 18:54:18.252911129 +0200 *************** *** 763,764 **** --- 763,766 ---- { /* Add new patch number below this line */ + /**/ + 1152, /**/ -- Spam seems to be something useful to novices. Later you realize that it's a bunch of indigestable junk that only clogs your system. Applies to both the food and the e-mail! /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///