comparison src/mbyte.c @ 12547:aa3f6d093f4b v8.0.1152

patch 8.0.1152: encoding of error message wrong in Cygwin terminal commit https://github.com/vim/vim/commit/2a02745709127bd56ccdbac8c568b3c25f3072a7 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Sep 26 19:10:37 2017 +0200 patch 8.0.1152: encoding of error message wrong in Cygwin terminal Problem: Encoding of error message wrong in Cygwin terminal. Solution: Get locale from environment variables. (Ken Takata)
author Christian Brabandt <cb@256bit.org>
date Tue, 26 Sep 2017 19:15:05 +0200
parents 683fdeb89017
children f58755eb453e
comparison
equal deleted inserted replaced
12546:088dd3306316 12547:aa3f6d093f4b
4383 } 4383 }
4384 #endif 4384 #endif
4385 4385
4386 #if defined(FEAT_MBYTE) || defined(PROTO) 4386 #if defined(FEAT_MBYTE) || defined(PROTO)
4387 4387
4388 #ifdef HAVE_LANGINFO_H 4388 # ifdef HAVE_LANGINFO_H
4389 # include <langinfo.h> 4389 # include <langinfo.h>
4390 #endif 4390 # endif
4391 4391
4392 /* 4392 # ifndef FEAT_GUI_W32
4393 * Get the canonicalized encoding of the current locale. 4393 /*
4394 * Get the canonicalized encoding from the specified locale string "locale"
4395 * or from the environment variables LC_ALL, LC_CTYPE and LANG.
4394 * Returns an allocated string when successful, NULL when not. 4396 * Returns an allocated string when successful, NULL when not.
4395 */ 4397 */
4396 char_u * 4398 char_u *
4397 enc_locale(void) 4399 enc_locale_env(char *locale)
4398 { 4400 {
4399 #ifndef WIN3264 4401 char *s = locale;
4400 char *s;
4401 char *p; 4402 char *p;
4402 int i; 4403 int i;
4403 #endif
4404 char buf[50]; 4404 char buf[50];
4405 #ifdef WIN3264
4406 long acp = GetACP();
4407
4408 if (acp == 1200)
4409 STRCPY(buf, "ucs-2le");
4410 else if (acp == 1252) /* cp1252 is used as latin1 */
4411 STRCPY(buf, "latin1");
4412 else
4413 sprintf(buf, "cp%ld", acp);
4414 #else
4415 # ifdef HAVE_NL_LANGINFO_CODESET
4416 if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL)
4417 # endif
4418 # if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
4419 if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL)
4420 # endif
4421 if ((s = getenv("LC_ALL")) == NULL || *s == NUL)
4422 if ((s = getenv("LC_CTYPE")) == NULL || *s == NUL)
4423 s = getenv("LANG");
4424 4405
4425 if (s == NULL || *s == NUL) 4406 if (s == NULL || *s == NUL)
4426 return FAIL; 4407 if ((s = getenv("LC_ALL")) == NULL || *s == NUL)
4408 if ((s = getenv("LC_CTYPE")) == NULL || *s == NUL)
4409 s = getenv("LANG");
4410
4411 if (s == NULL || *s == NUL)
4412 return NULL;
4427 4413
4428 /* The most generic locale format is: 4414 /* The most generic locale format is:
4429 * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]] 4415 * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
4430 * If there is a '.' remove the part before it. 4416 * If there is a '.' remove the part before it.
4431 * if there is something after the codeset, remove it. 4417 * if there is something after the codeset, remove it.
4456 buf[i] = TOLOWER_ASC(s[i]); 4442 buf[i] = TOLOWER_ASC(s[i]);
4457 else 4443 else
4458 break; 4444 break;
4459 } 4445 }
4460 buf[i] = NUL; 4446 buf[i] = NUL;
4461 #endif
4462 4447
4463 return enc_canonize((char_u *)buf); 4448 return enc_canonize((char_u *)buf);
4464 } 4449 }
4465 4450 # endif
4466 #if defined(WIN3264) || defined(PROTO) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD) 4451
4452 /*
4453 * Get the canonicalized encoding of the current locale.
4454 * Returns an allocated string when successful, NULL when not.
4455 */
4456 char_u *
4457 enc_locale(void)
4458 {
4459 # ifdef WIN3264
4460 char buf[50];
4461 long acp = GetACP();
4462
4463 if (acp == 1200)
4464 STRCPY(buf, "ucs-2le");
4465 else if (acp == 1252) /* cp1252 is used as latin1 */
4466 STRCPY(buf, "latin1");
4467 else
4468 sprintf(buf, "cp%ld", acp);
4469
4470 return enc_canonize((char_u *)buf);
4471 # else
4472 char *s;
4473
4474 # ifdef HAVE_NL_LANGINFO_CODESET
4475 if ((s = nl_langinfo(CODESET)) == NULL || *s == NUL)
4476 # endif
4477 # if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
4478 if ((s = setlocale(LC_CTYPE, NULL)) == NULL || *s == NUL)
4479 # endif
4480 s = NULL;
4481
4482 return enc_locale_env(s);
4483 # endif
4484 }
4485
4486 # if defined(WIN3264) || defined(PROTO) || defined(FEAT_CYGWIN_WIN32_CLIPBOARD)
4467 /* 4487 /*
4468 * Convert an encoding name to an MS-Windows codepage. 4488 * Convert an encoding name to an MS-Windows codepage.
4469 * Returns zero if no codepage can be figured out. 4489 * Returns zero if no codepage can be figured out.
4470 */ 4490 */
4471 int 4491 int
4488 return 0; 4508 return 0;
4489 if (IsValidCodePage(cp)) 4509 if (IsValidCodePage(cp))
4490 return cp; 4510 return cp;
4491 return 0; 4511 return 0;
4492 } 4512 }
4493 #endif 4513 # endif
4494 4514
4495 # if defined(USE_ICONV) || defined(PROTO) 4515 # if defined(USE_ICONV) || defined(PROTO)
4496 4516
4497 static char_u *iconv_string(vimconv_T *vcp, char_u *str, int slen, int *unconvlenp, int *resultlenp); 4517 static char_u *iconv_string(vimconv_T *vcp, char_u *str, int slen, int *unconvlenp, int *resultlenp);
4498 4518