annotate src/locale.c @ 23845:7517eb94239b v8.2.2464

patch 8.2.2464: using freed memory if window closed in autocommand Commit: https://github.com/vim/vim/commit/8ab375706e6712308f8cf7529bcae56684a6f385 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Feb 3 21:56:59 2021 +0100 patch 8.2.2464: using freed memory if window closed in autocommand Problem: Using freed memory if window closed in autocommand. (houyunsong) Solution: Check the window still exists.
author Bram Moolenaar <Bram@vim.org>
date Wed, 03 Feb 2021 22:00:04 +0100
parents b32b67a108f2
children a2e6da79274d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21437
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2 *
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 *
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 * locale.c: functions for language/locale configuration
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 #include "vim.h"
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 && (defined(FEAT_EVAL) || defined(FEAT_MULTI_LANG))
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 # define HAVE_GET_LOCALE_VAL
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 static char_u *
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 get_locale_val(int what)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 char_u *loc;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 // Obtain the locale value from the libraries.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 loc = (char_u *)setlocale(what, NULL);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 # ifdef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 if (loc != NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 char_u *p;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 // setocale() returns something like "LC_COLLATE=<name>;LC_..." when
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 // one of the values (e.g., LC_CTYPE) differs.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 p = vim_strchr(loc, '=');
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 if (p != NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 loc = ++p;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 while (*p != NUL) // remove trailing newline
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 if (*p < ' ' || *p == ';')
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 *p = NUL;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 break;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 ++p;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 return loc;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 #endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 #ifdef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 * On MS-Windows locale names are strings like "German_Germany.1252", but
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 * gettext expects "de". Try to translate one into another here for a few
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 * supported languages.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62 static char_u *
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 gettext_lang(char_u *name)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 int i;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 static char *(mtable[]) = {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67 "afrikaans", "af",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68 "czech", "cs",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 "dutch", "nl",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70 "german", "de",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 "english_united kingdom", "en_GB",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 "spanish", "es",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 "french", "fr",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 "italian", "it",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75 "japanese", "ja",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 "korean", "ko",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77 "norwegian", "no",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
78 "polish", "pl",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
79 "russian", "ru",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 "slovak", "sk",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 "swedish", "sv",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 "ukrainian", "uk",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 "chinese_china", "zh_CN",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 "chinese_taiwan", "zh_TW",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85 NULL};
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
86
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87 for (i = 0; mtable[i] != NULL; i += 2)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 if (STRNICMP(mtable[i], name, STRLEN(mtable[i])) == 0)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 return (char_u *)mtable[i + 1];
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 return name;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 #endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 #if defined(FEAT_MULTI_LANG) || defined(PROTO)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 * Return TRUE when "lang" starts with a valid language name.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 * Rejects NULL, empty string, "C", "C.UTF-8" and others.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 static int
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 is_valid_mess_lang(char_u *lang)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 return lang != NULL && ASCII_ISALPHA(lang[0]) && ASCII_ISALPHA(lang[1]);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 * Obtain the current messages language. Used to set the default for
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 * 'helplang'. May return NULL or an empty string.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 char_u *
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 get_mess_lang(void)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 char_u *p;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 # ifdef HAVE_GET_LOCALE_VAL
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 # if defined(LC_MESSAGES)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 p = get_locale_val(LC_MESSAGES);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 # else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 // This is necessary for Win32, where LC_MESSAGES is not defined and $LANG
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 // may be set to the LCID number. LC_COLLATE is the best guess, LC_TIME
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 // and LC_MONETARY may be set differently for a Japanese working in the
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 // US.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122 p = get_locale_val(LC_COLLATE);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124 # else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 p = mch_getenv((char_u *)"LC_ALL");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 if (!is_valid_mess_lang(p))
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128 p = mch_getenv((char_u *)"LC_MESSAGES");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 if (!is_valid_mess_lang(p))
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 p = mch_getenv((char_u *)"LANG");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 # ifdef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134 p = gettext_lang(p);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 return is_valid_mess_lang(p) ? p : NULL;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138 #endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 // Complicated #if; matches with where get_mess_env() is used below.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 #if (defined(FEAT_EVAL) && !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 && defined(LC_MESSAGES))) \
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143 || ((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
144 && !defined(LC_MESSAGES))
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
145 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146 * Get the language used for messages from the environment.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148 static char_u *
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149 get_mess_env(void)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 char_u *p;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 p = mch_getenv((char_u *)"LC_ALL");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 if (p == NULL || *p == NUL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 p = mch_getenv((char_u *)"LC_MESSAGES");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 if (p == NULL || *p == NUL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159 p = mch_getenv((char_u *)"LANG");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 if (p != NULL && VIM_ISDIGIT(*p))
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161 p = NULL; // ignore something like "1043"
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 # ifdef HAVE_GET_LOCALE_VAL
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 if (p == NULL || *p == NUL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 p = get_locale_val(LC_CTYPE);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168 return p;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 #endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 #if defined(FEAT_EVAL) || defined(PROTO)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 * Set the "v:lang" variable according to the current locale setting.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 * Also do "v:lc_time"and "v:ctype".
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 void
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 set_lang_var(void)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 char_u *loc;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183 # ifdef HAVE_GET_LOCALE_VAL
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 loc = get_locale_val(LC_CTYPE);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 # else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 // setlocale() not supported: use the default value
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 loc = (char_u *)"C";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 set_vim_var_string(VV_CTYPE, loc, -1);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 // When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 // back to LC_CTYPE if it's empty.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 # if defined(HAVE_GET_LOCALE_VAL) && defined(LC_MESSAGES)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 loc = get_locale_val(LC_MESSAGES);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195 # else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
196 loc = get_mess_env();
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
198 set_vim_var_string(VV_LANG, loc, -1);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
199
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 # ifdef HAVE_GET_LOCALE_VAL
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 loc = get_locale_val(LC_TIME);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
202 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203 set_vim_var_string(VV_LC_TIME, loc, -1);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
204
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205 # ifdef HAVE_GET_LOCALE_VAL
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 loc = get_locale_val(LC_COLLATE);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207 # else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
208 // setlocale() not supported: use the default value
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209 loc = (char_u *)"C";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
211 set_vim_var_string(VV_COLLATE, loc, -1);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 #endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
214
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217 * Setup to use the current locale (for ctype() and many other things).
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
218 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219 void
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
220 init_locale(void)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 setlocale(LC_ALL, "");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 # ifdef FEAT_GUI_GTK
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 // Tell Gtk not to change our locale settings.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 gtk_disable_setlocale();
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
228 # if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
229 // Make sure strtod() uses a decimal point, not a comma.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230 setlocale(LC_NUMERIC, "C");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
231 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
232
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
233 # ifdef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234 // Apparently MS-Windows printf() may cause a crash when we give it 8-bit
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 // text while it's expecting text in the current locale. This call avoids
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
236 // that.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237 setlocale(LC_CTYPE, "C");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
240 # ifdef FEAT_GETTEXT
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
242 int mustfree = FALSE;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 char_u *p;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 # ifdef DYNAMIC_GETTEXT
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 // Initialize the gettext library
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247 dyn_libintl_init();
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249 // expand_env() doesn't work yet, because g_chartab[] is not
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250 // initialized yet, call vim_getenv() directly
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
251 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
252 if (p != NULL && *p != NUL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
253 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
254 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
255 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 if (mustfree)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258 vim_free(p);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259 textdomain(VIMPACKAGE);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
261 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
262 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
264 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
265 * ":language": Set the language (locale).
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
267 void
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
268 ex_language(exarg_T *eap)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
269 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
270 char *loc;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 char_u *p;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
272 char_u *name;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273 int what = LC_ALL;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
274 char *whatstr = "";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
275 # ifdef LC_MESSAGES
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
276 # define VIM_LC_MESSAGES LC_MESSAGES
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
277 # else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
278 # define VIM_LC_MESSAGES 6789
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
279 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
280
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
281 name = eap->arg;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
282
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
283 // Check for "messages {name}", "ctype {name}" or "time {name}" argument.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284 // Allow abbreviation, but require at least 3 characters to avoid
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
285 // confusion with a two letter language name "me" or "ct".
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
286 p = skiptowhite(eap->arg);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
287 if ((*p == NUL || VIM_ISWHITE(*p)) && p - eap->arg >= 3)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
288 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 if (STRNICMP(eap->arg, "messages", p - eap->arg) == 0)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
291 what = VIM_LC_MESSAGES;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292 name = skipwhite(p);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
293 whatstr = "messages ";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
294 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
295 else if (STRNICMP(eap->arg, "ctype", p - eap->arg) == 0)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
297 what = LC_CTYPE;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 name = skipwhite(p);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
299 whatstr = "ctype ";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
300 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
301 else if (STRNICMP(eap->arg, "time", p - eap->arg) == 0)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
302 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
303 what = LC_TIME;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
304 name = skipwhite(p);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
305 whatstr = "time ";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
306 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
307 else if (STRNICMP(eap->arg, "collate", p - eap->arg) == 0)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
308 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
309 what = LC_COLLATE;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
310 name = skipwhite(p);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
311 whatstr = "collate ";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
312 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
314
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
315 if (*name == NUL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
316 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
317 # ifndef LC_MESSAGES
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
318 if (what == VIM_LC_MESSAGES)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
319 p = get_mess_env();
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
320 else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
321 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
322 p = (char_u *)setlocale(what, NULL);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
323 if (p == NULL || *p == NUL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
324 p = (char_u *)"Unknown";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
325 smsg(_("Current %slanguage: \"%s\""), whatstr, p);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
326 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
327 else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
328 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
329 # ifndef LC_MESSAGES
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
330 if (what == VIM_LC_MESSAGES)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
331 loc = "";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
332 else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
333 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
334 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
335 loc = setlocale(what, (char *)name);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
336 # if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
337 // Make sure strtod() uses a decimal point, not a comma.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
338 setlocale(LC_NUMERIC, "C");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
339 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
340 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
341 if (loc == NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
342 semsg(_("E197: Cannot set language to \"%s\""), name);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
343 else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
344 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
345 # ifdef HAVE_NL_MSG_CAT_CNTR
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
346 // Need to do this for GNU gettext, otherwise cached translations
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
347 // will be used again.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
348 extern int _nl_msg_cat_cntr;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
349
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
350 ++_nl_msg_cat_cntr;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
352 // Reset $LC_ALL, otherwise it would overrule everything.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
353 vim_setenv((char_u *)"LC_ALL", (char_u *)"");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
354
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
355 if (what != LC_TIME && what != LC_COLLATE)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
356 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
357 // Tell gettext() what to translate to. It apparently doesn't
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
358 // use the currently effective locale. Also do this when
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
359 // FEAT_GETTEXT isn't defined, so that shell commands use this
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
360 // value.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
361 if (what == LC_ALL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
362 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
363 vim_setenv((char_u *)"LANG", name);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
364
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
365 // Clear $LANGUAGE because GNU gettext uses it.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
366 vim_setenv((char_u *)"LANGUAGE", (char_u *)"");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
367 # ifdef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
368 // Apparently MS-Windows printf() may cause a crash when
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
369 // we give it 8-bit text while it's expecting text in the
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
370 // current locale. This call avoids that.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
371 setlocale(LC_CTYPE, "C");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
373 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
374 if (what != LC_CTYPE)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
375 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
376 char_u *mname;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
377 # ifdef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
378 mname = gettext_lang(name);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
379 # else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
380 mname = name;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
381 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
382 vim_setenv((char_u *)"LC_MESSAGES", mname);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
383 # ifdef FEAT_MULTI_LANG
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
384 set_helplang_default(mname);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
385 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
386 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
387 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
388
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
389 # ifdef FEAT_EVAL
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
390 // Set v:lang, v:lc_time, v:collate and v:ctype to the final result.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
391 set_lang_var();
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
392 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
393 # ifdef FEAT_TITLE
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
394 maketitle();
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
395 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
396 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
397 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
398 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
399
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
400 static char_u **locales = NULL; // Array of all available locales
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
401
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
402 static int did_init_locales = FALSE;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
403
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
404 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
405 * Return an array of strings for all available locales + NULL for the
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
406 * last element. Return NULL in case of error.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
407 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
408 static char_u **
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
409 find_locales(void)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
410 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
411 garray_T locales_ga;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
412 char_u *loc;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
413 char_u *locale_list;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
414 # ifdef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
415 size_t len = 0;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
416 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
417
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
418 // Find all available locales by running command "locale -a". If this
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
419 // doesn't work we won't have completion.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
420 # ifndef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
421 locale_list = get_cmd_output((char_u *)"locale -a",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
422 NULL, SHELL_SILENT, NULL);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
423 # else
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
424 // Find all available locales by examining the directories in
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
425 // $VIMRUNTIME/lang/
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427 int options = WILD_SILENT|WILD_USE_NL|WILD_KEEP_ALL;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
428 expand_T xpc;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
429 char_u *p;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
431 ExpandInit(&xpc);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
432 xpc.xp_context = EXPAND_DIRECTORIES;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
433 locale_list = ExpandOne(&xpc, (char_u *)"$VIMRUNTIME/lang/*",
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
434 NULL, options, WILD_ALL);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
435 ExpandCleanup(&xpc);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
436 if (locale_list == NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
437 // Add a dummy input, that will be skipped lated but we need to
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
438 // have something in locale_list so that the C locale is added at
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
439 // the end.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
440 locale_list = vim_strsave((char_u *)".\n");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
441 p = locale_list;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
442 // find the last directory delimiter
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
443 while (p != NULL && *p != NUL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
444 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
445 if (*p == '\n')
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
446 break;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
447 if (*p == '\\')
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
448 len = p - locale_list;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
449 p++;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
450 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
451 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
452 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
453 if (locale_list == NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
454 return NULL;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
455 ga_init2(&locales_ga, sizeof(char_u *), 20);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
456
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
457 // Transform locale_list string where each locale is separated by "\n"
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
458 // into an array of locale strings.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
459 loc = (char_u *)strtok((char *)locale_list, "\n");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
460
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
461 while (loc != NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
462 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
463 int ignore = FALSE;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
464
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
465 # ifdef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
466 if (len > 0)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
467 loc += len + 1;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
468 // skip locales with a dot (which indicates the charset)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
469 if (vim_strchr(loc, '.') != NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
470 ignore = TRUE;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
471 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
472 if (!ignore)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
473 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
474 if (ga_grow(&locales_ga, 1) == FAIL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
475 break;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
476
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
477 loc = vim_strsave(loc);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
478 if (loc == NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
479 break;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
480
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
481 ((char_u **)locales_ga.ga_data)[locales_ga.ga_len++] = loc;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
482 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
483 loc = (char_u *)strtok(NULL, "\n");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
484 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
485
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
486 # ifdef MSWIN
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
487 // Add the C locale
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
488 if (ga_grow(&locales_ga, 1) == OK)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
489 ((char_u **)locales_ga.ga_data)[locales_ga.ga_len++] =
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
490 vim_strsave((char_u *)"C");
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
491 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
492
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
493 vim_free(locale_list);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
494 if (ga_grow(&locales_ga, 1) == FAIL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
495 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
496 ga_clear(&locales_ga);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
497 return NULL;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
498 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
499 ((char_u **)locales_ga.ga_data)[locales_ga.ga_len] = NULL;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
500 return (char_u **)locales_ga.ga_data;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
501 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
502
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
503 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
504 * Lazy initialization of all available locales.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
505 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
506 static void
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
507 init_locales(void)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
508 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
509 if (!did_init_locales)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
510 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
511 did_init_locales = TRUE;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
512 locales = find_locales();
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
513 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
514 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
515
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
516 # if defined(EXITFREE) || defined(PROTO)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
517 void
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
518 free_locales(void)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
519 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
520 int i;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
521 if (locales != NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
522 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
523 for (i = 0; locales[i] != NULL; i++)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
524 vim_free(locales[i]);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
525 VIM_CLEAR(locales);
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
526 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
527 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
528 # endif
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
529
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
530 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
531 * Function given to ExpandGeneric() to obtain the possible arguments of the
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
532 * ":language" command.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
533 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
534 char_u *
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
535 get_lang_arg(expand_T *xp UNUSED, int idx)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
536 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
537 if (idx == 0)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
538 return (char_u *)"messages";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
539 if (idx == 1)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
540 return (char_u *)"ctype";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
541 if (idx == 2)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
542 return (char_u *)"time";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
543 if (idx == 3)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
544 return (char_u *)"collate";
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
545
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
546 init_locales();
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
547 if (locales == NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
548 return NULL;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
549 return locales[idx - 4];
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
550 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
551
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
552 /*
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
553 * Function given to ExpandGeneric() to obtain the available locales.
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
554 */
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
555 char_u *
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
556 get_locales(expand_T *xp UNUSED, int idx)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
557 {
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
558 init_locales();
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
559 if (locales == NULL)
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
560 return NULL;
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
561 return locales[idx];
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
562 }
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
563
b32b67a108f2 patch 8.2.1269: language and locale code spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
564 #endif