Skip to content

Commit

Permalink
refactor(lang): reduce scope of HAVE_WORKING_LIBINTL #ifdefs
Browse files Browse the repository at this point in the history
A lot of code inside HAVE_WORKING_LIBINTL doesn't really depend on a
"working libintl". For instance ex_language is also used for ":lang collate"
and ":lang time".

Also ":lang C" should not fail just because translations aren't available (it
just means use the default text).

References:

neovim#1 2d00ead
separate ifdefs for locale and gettext got merged togheter.

neovim@8253e29
Unmotivated switcharoo of get_mess_env() logic. If available, get_locale_val(LC_MESSAGES) is the correct implementation.
  • Loading branch information
bfredl committed Oct 14, 2023
1 parent 37da0bc commit aa6a3a1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
5 changes: 1 addition & 4 deletions src/nvim/cmdexpand.c
Original file line number Diff line number Diff line change
Expand Up @@ -2116,10 +2116,9 @@ static const char *set_context_by_cmdname(const char *cmd, cmdidx_T cmdidx, expa
set_context_in_runtime_cmd(xp, arg);
break;

#ifdef HAVE_WORKING_LIBINTL
case CMD_language:
return set_context_in_lang_cmd(xp, arg);
#endif

case CMD_profile:
set_context_in_profile_cmd(xp, arg);
break;
Expand Down Expand Up @@ -2620,10 +2619,8 @@ static int ExpandOther(char *pat, expand_T *xp, regmatch_T *rmp, char ***matches
{ EXPAND_AUGROUP, expand_get_augroup_name, true, false },
{ EXPAND_SIGN, get_sign_name, true, true },
{ EXPAND_PROFILE, get_profile_name, true, true },
#ifdef HAVE_WORKING_LIBINTL
{ EXPAND_LANGUAGE, get_lang_arg, true, false },
{ EXPAND_LOCALES, get_locales, true, false },
#endif
{ EXPAND_ENV_VARS, get_env_name, true, true },
{ EXPAND_USER, get_users, true, false },
{ EXPAND_ARGLIST, get_arglist_name, true, false },
Expand Down
4 changes: 0 additions & 4 deletions src/nvim/ex_docmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ struct dbg_stuff {
# include "ex_docmd.c.generated.h"
#endif

#ifndef HAVE_WORKING_LIBINTL
# define ex_language ex_ni
#endif

// Declare cmdnames[].
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ex_cmds_defs.generated.h"
Expand Down
2 changes: 1 addition & 1 deletion src/nvim/gettext.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# undef setlocale
# endif
#else
# define _(x) (x)
# define _(x) ((char *)(x))
# define N_(x) x
# define NGETTEXT(x, xs, n) ((n) == 1 ? (x) : (xs))
# define bindtextdomain(x, y) // empty
Expand Down
30 changes: 11 additions & 19 deletions src/nvim/os/lang.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ char *get_mess_lang(void)
return is_valid_mess_lang(p) ? p : NULL;
}

// Complicated #if; matches with where get_mess_env() is used below.
#ifdef HAVE_WORKING_LIBINTL
/// Get the language used for messages from the environment.
static char *get_mess_env(void)
{
// Complicated code; matches the behavior of how get_mess_env() was used
#ifdef LC_MESSAGES
return get_locale_val(LC_MESSAGES);
#else
// In Windows LC_MESSAGES is not defined, emulate the env behavior.
char *p;

p = (char *)os_getenv("LC_ALL");
Expand All @@ -84,16 +87,16 @@ static char *get_mess_env(void)
if (p == NULL) {
p = (char *)os_getenv("LANG");
if (p != NULL && ascii_isdigit(*p)) {
p = NULL; // ignore something like "1043"
p = NULL; // ignore something like "1043"
}
if (p == NULL) {
p = get_locale_val(LC_CTYPE);
}
}
}
return p;
}
#endif
}

/// Set the "v:lang" variable according to the current locale setting.
/// Also do "v:lc_time"and "v:ctype".
Expand All @@ -106,14 +109,7 @@ void set_lang_var(void)

// When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall
// back to LC_CTYPE if it's empty.
#ifdef HAVE_WORKING_LIBINTL
loc = get_mess_env();
#elif defined(LC_MESSAGES)
loc = get_locale_val(LC_MESSAGES);
#else
// In Windows LC_MESSAGES is not defined fallback to LC_CTYPE
loc = get_locale_val(LC_CTYPE);
#endif
set_vim_var_string(VV_LANG, loc, -1);

loc = get_locale_val(LC_TIME);
Expand Down Expand Up @@ -145,8 +141,6 @@ void init_locale(void)
TIME_MSG("locale set");
}

#ifdef HAVE_WORKING_LIBINTL

/// ":language": Set the language (locale).
///
/// @param eap
Expand Down Expand Up @@ -250,7 +244,7 @@ void ex_language(exarg_T *eap)

static char **locales = NULL; // Array of all available locales

# ifndef MSWIN
#ifndef MSWIN
static bool did_init_locales = false;

/// @return an array of strings for all available locales + NULL for the
Expand Down Expand Up @@ -285,19 +279,19 @@ static char **find_locales(void)
((char **)locales_ga.ga_data)[locales_ga.ga_len] = NULL;
return locales_ga.ga_data;
}
# endif
#endif

/// Lazy initialization of all available locales.
static void init_locales(void)
{
# ifndef MSWIN
#ifndef MSWIN
if (did_init_locales) {
return;
}

did_init_locales = true;
locales = find_locales();
# endif
#endif
}

# if defined(EXITFREE)
Expand Down Expand Up @@ -348,8 +342,6 @@ char *get_locales(expand_T *xp, int idx)
return locales[idx];
}

#endif

void lang_init(void)
{
#ifdef __APPLE__
Expand Down

0 comments on commit aa6a3a1

Please sign in to comment.