eval: port v:collate

Cherry-picked from patch v8.2.0988. Required for patch v8.2.1933.
This commit is contained in:
Sean Dewar
2021-04-20 00:01:50 +01:00
parent 96f62394cf
commit 1d72b6e4cd
7 changed files with 55 additions and 7 deletions

View File

@@ -1495,6 +1495,15 @@ v:cmdarg This variable is used for two purposes:
the argument for the ":hardcopy" command. This can be used the argument for the ":hardcopy" command. This can be used
in 'printexpr'. in 'printexpr'.
*v:collate* *collate-variable*
v:collate The current locale setting for collation order of the runtime
environment. This allows Vim scripts to be aware of the
current locale encoding. Technical: it's the value of
LC_COLLATE. When not using a locale the value is "C".
This variable can not be set directly, use the |:language|
command.
See |multi-lang|.
*v:cmdbang* *cmdbang-variable* *v:cmdbang* *cmdbang-variable*
v:cmdbang Set like v:cmdarg for a file read/write command. When a "!" v:cmdbang Set like v:cmdarg for a file read/write command. When a "!"
was used the value is 1, otherwise it is 0. Note that this was used the value is 1, otherwise it is 0. Note that this

View File

@@ -31,6 +31,7 @@ use of "-" and "_".
:lan[guage] mes[sages] :lan[guage] mes[sages]
:lan[guage] cty[pe] :lan[guage] cty[pe]
:lan[guage] tim[e] :lan[guage] tim[e]
:lan[guage] col[late]
Print the current language (aka locale). Print the current language (aka locale).
With the "messages" argument the language used for With the "messages" argument the language used for
messages is printed. Technical: LC_MESSAGES. messages is printed. Technical: LC_MESSAGES.
@@ -38,15 +39,19 @@ use of "-" and "_".
character encoding is printed. Technical: LC_CTYPE. character encoding is printed. Technical: LC_CTYPE.
With the "time" argument the language used for With the "time" argument the language used for
strftime() is printed. Technical: LC_TIME. strftime() is printed. Technical: LC_TIME.
With the "collate" argument the language used for
collation order is printed. Technical: LC_COLLATE.
Without argument all parts of the locale are printed Without argument all parts of the locale are printed
(this is system dependent). (this is system dependent).
The current language can also be obtained with the The current language can also be obtained with the
|v:lang|, |v:ctype| and |v:lc_time| variables. |v:lang|, |v:ctype|, |v:collate| and |v:lc_time|
variables.
:lan[guage] {name} :lan[guage] {name}
:lan[guage] mes[sages] {name} :lan[guage] mes[sages] {name}
:lan[guage] cty[pe] {name} :lan[guage] cty[pe] {name}
:lan[guage] tim[e] {name} :lan[guage] tim[e] {name}
:lan[guage] col[late] {name}
Set the current language (aka locale) to {name}. Set the current language (aka locale) to {name}.
The locale {name} must be a valid locale on your The locale {name} must be a valid locale on your
system. Some systems accept aliases like "en" or system. Some systems accept aliases like "en" or
@@ -66,7 +71,10 @@ use of "-" and "_".
With the "time" argument the language used for time With the "time" argument the language used for time
and date messages is set. This affects strftime(). and date messages is set. This affects strftime().
This sets $LC_TIME. This sets $LC_TIME.
Without an argument both are set, and additionally With the "collate" argument the language used for the
collation order is set. This affects sorting of
characters. This sets $LC_COLLATE.
Without an argument all are set, and additionally
$LANG is set. $LANG is set.
The LC_NUMERIC value will always be set to "C" so The LC_NUMERIC value will always be set to "C" so
that floating point numbers use '.' as the decimal that floating point numbers use '.' as the decimal

View File

@@ -225,6 +225,7 @@ static struct vimvar {
VV(VV_EVENT, "event", VAR_DICT, VV_RO), VV(VV_EVENT, "event", VAR_DICT, VV_RO),
VV(VV_ECHOSPACE, "echospace", VAR_NUMBER, VV_RO), VV(VV_ECHOSPACE, "echospace", VAR_NUMBER, VV_RO),
VV(VV_ARGV, "argv", VAR_LIST, VV_RO), VV(VV_ARGV, "argv", VAR_LIST, VV_RO),
VV(VV_COLLATE, "collate", VAR_STRING, VV_RO),
VV(VV_EXITING, "exiting", VAR_NUMBER, VV_RO), VV(VV_EXITING, "exiting", VAR_NUMBER, VV_RO),
// Neovim // Neovim
VV(VV_STDERR, "stderr", VAR_NUMBER, VV_RO), VV(VV_STDERR, "stderr", VAR_NUMBER, VV_RO),

View File

@@ -154,6 +154,7 @@ typedef enum {
VV_EVENT, VV_EVENT,
VV_ECHOSPACE, VV_ECHOSPACE,
VV_ARGV, VV_ARGV,
VV_COLLATE,
VV_EXITING, VV_EXITING,
// Neovim // Neovim
VV_STDERR, VV_STDERR,

View File

@@ -3627,6 +3627,14 @@ void set_lang_var(void)
loc = get_locale_val(LC_TIME); loc = get_locale_val(LC_TIME);
# endif # endif
set_vim_var_string(VV_LC_TIME, loc, -1); set_vim_var_string(VV_LC_TIME, loc, -1);
# ifdef HAVE_GET_LOCALE_VAL
loc = get_locale_val(LC_COLLATE);
# else
// setlocale() not supported: use the default value
loc = "C";
# endif
set_vim_var_string(VV_COLLATE, loc, -1);
} }
#ifdef HAVE_WORKING_LIBINTL #ifdef HAVE_WORKING_LIBINTL
@@ -3667,6 +3675,10 @@ void ex_language(exarg_T *eap)
what = LC_TIME; what = LC_TIME;
name = skipwhite(p); name = skipwhite(p);
whatstr = "time "; whatstr = "time ";
} else if (STRNICMP(eap->arg, "collate", p - eap->arg) == 0) {
what = LC_COLLATE;
name = skipwhite(p);
whatstr = "collate ";
} }
} }
@@ -3711,7 +3723,7 @@ void ex_language(exarg_T *eap)
// Reset $LC_ALL, otherwise it would overrule everything. // Reset $LC_ALL, otherwise it would overrule everything.
os_setenv("LC_ALL", "", 1); os_setenv("LC_ALL", "", 1);
if (what != LC_TIME) { if (what != LC_TIME && what != LC_COLLATE) {
// Tell gettext() what to translate to. It apparently doesn't // Tell gettext() what to translate to. It apparently doesn't
// use the currently effective locale. // use the currently effective locale.
if (what == LC_ALL) { if (what == LC_ALL) {
@@ -3726,7 +3738,7 @@ void ex_language(exarg_T *eap)
} }
} }
// Set v:lang, v:lc_time and v:ctype to the final result. // Set v:lang, v:lc_time, v:collate and v:ctype to the final result.
set_lang_var(); set_lang_var();
maketitle(); maketitle();
} }
@@ -3811,12 +3823,15 @@ char_u *get_lang_arg(expand_T *xp, int idx)
if (idx == 2) { if (idx == 2) {
return (char_u *)"time"; return (char_u *)"time";
} }
if (idx == 3) {
return (char_u *)"collate";
}
init_locales(); init_locales();
if (locales == NULL) { if (locales == NULL) {
return NULL; return NULL;
} }
return locales[idx - 3]; return locales[idx - 4];
} }
/// Function given to ExpandGeneric() to obtain the available locales. /// Function given to ExpandGeneric() to obtain the available locales.

View File

@@ -3642,7 +3642,8 @@ const char * set_one_cmd_context(
} else { } else {
if (strncmp(arg, "messages", p - arg) == 0 if (strncmp(arg, "messages", p - arg) == 0
|| strncmp(arg, "ctype", p - arg) == 0 || strncmp(arg, "ctype", p - arg) == 0
|| strncmp(arg, "time", p - arg) == 0) { || strncmp(arg, "time", p - arg) == 0
|| strncmp(arg, "collate", p - arg) == 0) {
xp->xp_context = EXPAND_LOCALES; xp->xp_context = EXPAND_LOCALES;
xp->xp_pattern = skipwhite((const char_u *)p); xp->xp_pattern = skipwhite((const char_u *)p);
} else { } else {

View File

@@ -615,10 +615,20 @@ func Test_cmdline_complete_bang()
endfunc endfunc
funct Test_cmdline_complete_languages() funct Test_cmdline_complete_languages()
let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
call assert_equal(lang, v:lc_time)
let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
call assert_equal(lang, v:ctype)
let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
call assert_equal(lang, v:collate)
let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '') let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
call assert_equal(lang, v:lang)
call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx') call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
call assert_match('^"language .*\<ctype\>.*\<messages\>.*\<time\>', @:) call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
if has('unix') if has('unix')
" TODO: these tests don't work on Windows. lang appears to be 'C' " TODO: these tests don't work on Windows. lang appears to be 'C'
@@ -633,6 +643,9 @@ funct Test_cmdline_complete_languages()
call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx') call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
call assert_match('^"language .*\<' . lang . '\>', @:) call assert_match('^"language .*\<' . lang . '\>', @:)
call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
call assert_match('^"language .*\<' . lang . '\>', @:)
endif endif
endfunc endfunc