robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#if defined(__APPLE__ )&& !defined(ZIG_BUILD ) 2# define Boolean CFBoolean // Avoid conflict with API's Boolean 3# define FileInfo CSFileInfo // Avoid conflict with API's Fileinfo 4# include <CoreServices/CoreServices.h> 5 6# undef Boolean 7# undef FileInfo 8#endif 9 10#include <locale.h> 11#include <stdbool.h> 12#include <stdio.h> 13 14#include "auto/config.h" 15#include "nvim/ascii_defs.h" 16#include "nvim/buffer.h" 17#include "nvim/charset.h" 18#include "nvim/cmdexpand_defs.h" 19#include "nvim/eval/vars.h" 20#include "nvim/ex_cmds_defs.h" 21#include "nvim/garray.h" 22#include "nvim/gettext_defs.h" 23#include "nvim/globals.h" 24#include "nvim/macros_defs.h" 25#include "nvim/memory.h" 26#include "nvim/message.h" 27#include "nvim/option.h" 28#include "nvim/os/lang.h" 29#include "nvim/os/os.h" 30#include "nvim/os/shell.h" 31#include "nvim/path.h" 32#include "nvim/profile.h" 33#include "nvim/vim_defs.h" 34 35#include "os/lang.c.generated.h" 36 37static char * get_locale_val (int what ) 38{ 39// Obtain the locale value from the libraries. 40char * loc = setlocale (what ,NULL ); 41 42return loc ; 43} 44 45/// @return true when "lang" starts with a valid language name. 46/// Rejects NULL, empty string, "C", "C.UTF-8" and others. 47static bool is_valid_mess_lang (const char * lang ) 48{ 49return lang != NULL && ASCII_ISALPHA (lang [0 ])&& ASCII_ISALPHA (lang [1 ]); 50} 51 52/// Obtain the current messages language. Used to set the default for 53/// 'helplang'. May return NULL or an empty string. 54char * get_mess_lang (void ) 55{ 56char * p ; 57 58#ifdef LC_MESSAGES 59p = get_locale_val (LC_MESSAGES ); 60#else 61// This is necessary for Win32, where LC_MESSAGES is not defined and $LANG 62// may be set to the LCID number. LC_COLLATE is the best guess, LC_TIME 63// and LC_MONETARY may be set differently for a Japanese working in the 64// US. 65p = get_locale_val (LC_COLLATE ); 66#endif 67return is_valid_mess_lang (p ) ?p :NULL ; 68} 69 70/// Get the language used for messages from the environment. 71/// The function may be using NameBuff. 72/// 73/// This uses LC_MESSAGES when available, which it is for most systems we build for 74/// except for windows. Then fallback to get the value from the environment 75/// ourselves, and use LC_CTYPE as a last resort. 76static char * get_mess_env (void ) 77{ 78#ifdef LC_MESSAGES 79return get_locale_val (LC_MESSAGES ); 80#else 81char * p = os_getenv_noalloc ("LC_ALL" ); 82if (p != NULL ) { 83return p ; 84 } 85 86p = os_getenv_noalloc ("LC_MESSAGES" ); 87if (p != NULL ) { 88return p ; 89 } 90 91p = os_getenv_noalloc ("LANG" ); 92if (p != NULL && ascii_isdigit (* p )) { 93p = NULL ;// ignore something like "1043" 94 } 95if (p == NULL ) { 96p = get_locale_val (LC_CTYPE ); 97 } 98return p ; 99#endif 100} 101 102/// Set the "v:lang" variable according to the current locale setting. 103/// Also do "v:lc_time"and "v:ctype". 104void set_lang_var (void ) 105{ 106const char * loc = get_locale_val (LC_CTYPE ); 107set_vim_var_string (VV_CTYPE ,loc ,-1 ); 108 109loc = get_mess_env (); 110set_vim_var_string (VV_LANG ,loc ,-1 ); 111 112loc = get_locale_val (LC_TIME ); 113set_vim_var_string (VV_LC_TIME ,loc ,-1 ); 114 115loc = get_locale_val (LC_COLLATE ); 116set_vim_var_string (VV_COLLATE ,loc ,-1 ); 117} 118 119/// Setup to use the current locale (for ctype() and many other things). 120void init_locale (void ) 121{ 122setlocale (LC_ALL ,"" ); 123 124#ifdef LC_NUMERIC 125// Make sure strtod() uses a decimal point, not a comma. 126setlocale (LC_NUMERIC ,"C" ); 127#endif 128 129char localepath [MAXPATHL ]= {0 }; 130snprintf (localepath ,sizeof (localepath ),"%s" ,get_vim_var_str (VV_PROGPATH )); 131char * tail = path_tail_with_sep (localepath ); 132* tail = NUL ; 133tail = path_tail (localepath ); 134xstrlcpy (tail ,"share/locale" , 135sizeof (localepath )- (size_t )(tail - localepath )); 136bindtextdomain (PROJECT_NAME ,localepath ); 137textdomain (PROJECT_NAME ); 138TIME_MSG ("locale set" ); 139} 140 141/// ":language": Set the language (locale). 142/// 143/// @param eap 144void ex_language (exarg_T * eap ) 145{ 146char * loc ; 147int what = LC_ALL ; 148char * whatstr = "" ; 149#ifdef LC_MESSAGES 150# define VIM_LC_MESSAGES LC_MESSAGES 151#else 152# define VIM_LC_MESSAGES 6789 153#endif 154 155char * name = eap -> arg ; 156 157// Check for "messages {name}", "ctype {name}" or "time {name}" argument. 158// Allow abbreviation, but require at least 3 characters to avoid 159// confusion with a two letter language name "me" or "ct". 160char * p = skiptowhite (eap -> arg ); 161if ((* p == NUL || ascii_iswhite (* p ))&& p - eap -> arg >=3 ) { 162if (STRNICMP (eap -> arg ,"messages" , (size_t )(p - eap -> arg ))== 0 ) { 163what = VIM_LC_MESSAGES ; 164name = skipwhite (p ); 165whatstr = "messages " ; 166 }else if (STRNICMP (eap -> arg ,"ctype" , (size_t )(p - eap -> arg ))== 0 ) { 167what = LC_CTYPE ; 168name = skipwhite (p ); 169whatstr = "ctype " ; 170 }else if (STRNICMP (eap -> arg ,"time" , (size_t )(p - eap -> arg ))== 0 ) { 171what = LC_TIME ; 172name = skipwhite (p ); 173whatstr = "time " ; 174 }else if (STRNICMP (eap -> arg ,"collate" , (size_t )(p - eap -> arg ))== 0 ) { 175what = LC_COLLATE ; 176name = skipwhite (p ); 177whatstr = "collate " ; 178 } 179 } 180 181if (* name == NUL ) { 182if (what == VIM_LC_MESSAGES ) { 183p = get_mess_env (); 184 }else { 185p = setlocale (what ,NULL ); 186 } 187if (p == NULL || * p == NUL ) { 188p = "Unknown" ; 189 } 190smsg (0 ,_ ("Current %slanguage: \"%s\"" ),whatstr ,p ); 191 }else { 192#ifndef LC_MESSAGES 193if (what == VIM_LC_MESSAGES ) { 194loc = "" ; 195 }else { 196#endif 197loc = setlocale (what ,name ); 198#ifdef LC_NUMERIC 199// Make sure strtod() uses a decimal point, not a comma. 200setlocale (LC_NUMERIC ,"C" ); 201#endif 202#ifndef LC_MESSAGES 203 } 204#endif 205if (loc == NULL ) { 206semsg (_ ("E197: Cannot set language to \"%s\"" ),name ); 207 }else { 208#ifdef HAVE_NL_MSG_CAT_CNTR 209// Need to do this for GNU gettext, otherwise cached translations 210// will be used again. 211extern int _nl_msg_cat_cntr ;// NOLINT(bugprone-reserved-identifier) 212 213_nl_msg_cat_cntr ++ ; 214#endif 215// Reset $LC_ALL, otherwise it would overrule everything. 216os_setenv ("LC_ALL" ,"" ,1 ); 217 218if (what != LC_TIME && what != LC_COLLATE ) { 219// Tell gettext() what to translate to. It apparently doesn't 220// use the currently effective locale. 221if (what == LC_ALL ) { 222os_setenv ("LANG" ,name ,1 ); 223 224// Clear $LANGUAGE because GNU gettext uses it. 225os_setenv ("LANGUAGE" ,"" ,1 ); 226 } 227if (what != LC_CTYPE ) { 228os_setenv ("LC_MESSAGES" ,name ,1 ); 229set_helplang_default (name ); 230 } 231 } 232 233// Set v:lang, v:lc_time, v:collate and v:ctype to the final result. 234set_lang_var (); 235maketitle (); 236 } 237 } 238 239// TODO(bfredl): isident=@ should not depend on the locale 240buf_init_isk_chartab (curbuf ); 241init_isf_chartab (); 242init_isi_chartab (); 243} 244 245static char ** locales = NULL ;// Array of all available locales 246 247#ifndef MSWIN 248static bool did_init_locales = false; 249 250/// @return an array of strings for all available locales + NULL for the 251/// last element or, 252/// NULL in case of error. 253static char ** find_locales (void ) 254{ 255garray_T locales_ga ; 256char * saveptr = NULL ; 257 258// Find all available locales by running command "locale -a". If this 259// doesn't work we won't have completion. 260char * locale_a = get_cmd_output ("locale -a" ,NULL ,kShellOptSilent ,NULL ); 261if (locale_a == NULL ) { 262return NULL ; 263 } 264ga_init (& locales_ga ,sizeof (char * ),20 ); 265 266// Transform locale_a string where each locale is separated by "\n" 267// into an array of locale strings. 268char * loc = os_strtok (locale_a ,"\n" ,& saveptr ); 269 270while (loc != NULL ) { 271loc = xstrdup (loc ); 272GA_APPEND (char * ,& locales_ga ,loc ); 273loc = os_strtok (NULL ,"\n" ,& saveptr ); 274 } 275xfree (locale_a ); 276// Guarantee that .ga_data is NULL terminated 277ga_grow (& locales_ga ,1 ); 278 ((char ** )locales_ga .ga_data )[locales_ga .ga_len ]= NULL ; 279return locales_ga .ga_data ; 280} 281#endif 282 283/// Lazy initialization of all available locales. 284static void init_locales (void ) 285{ 286#ifndef MSWIN 287if (did_init_locales ) { 288return ; 289 } 290 291did_init_locales = true; 292locales = find_locales (); 293#endif 294} 295 296#ifdef EXITFREE 297void free_locales (void ) 298{ 299if (locales == NULL ) { 300return ; 301 } 302 303for (int i = 0 ;locales [i ]!= NULL ;i ++ ) { 304xfree (locales [i ]); 305 } 306XFREE_CLEAR (locales ); 307} 308#endif 309 310/// Function given to ExpandGeneric() to obtain the possible arguments of the 311/// ":language" command. 312char * get_lang_arg (expand_T * xp ,int idx ) 313{ 314if (idx == 0 ) { 315return "messages" ; 316 } 317if (idx == 1 ) { 318return "ctype" ; 319 } 320if (idx == 2 ) { 321return "time" ; 322 } 323if (idx == 3 ) { 324return "collate" ; 325 } 326 327init_locales (); 328if (locales == NULL ) { 329return NULL ; 330 } 331return locales [idx - 4 ]; 332} 333 334/// Function given to ExpandGeneric() to obtain the available locales. 335char * get_locales (expand_T * xp ,int idx ) 336{ 337init_locales (); 338if (locales == NULL ) { 339return NULL ; 340 } 341return locales [idx ]; 342} 343 344void lang_init (void ) 345{ 346#if defined(__APPLE__ ) 347if (!os_env_exists ("LANG" , true)) { 348# if defined(ZIG_BUILD ) 349// fuck it just use the objectively correct values: 350os_setenv ("LANG" ,"C.UTF-8" , true); 351setlocale (LC_ALL ,"C.UTF-8" ); 352// Make sure strtod() uses a decimal point, not a comma. 353setlocale (LC_NUMERIC ,"C" ); 354# else 355char buf [50 ]= {0 }; 356 357// $LANG is not set, either because it was unset or Nvim was started 358// from the Dock. Query the system locale. 359if (LocaleRefGetPartString (NULL , 360kLocaleLanguageMask |kLocaleLanguageVariantMask | 361kLocaleRegionMask |kLocaleRegionVariantMask , 362sizeof (buf )- 10 ,buf )== noErr && * buf ) { 363if (strcasestr (buf ,"utf-8" )== NULL ) { 364xstrlcat (buf ,".UTF-8" ,sizeof (buf )); 365 } 366os_setenv ("LANG" ,buf , true); 367setlocale (LC_ALL ,"" ); 368// Make sure strtod() uses a decimal point, not a comma. 369setlocale (LC_NUMERIC ,"C" ); 370 }else { 371ELOG ("$LANG is empty and the macOS primary language cannot be inferred." ); 372 } 373# endif 374 } 375#endif 376}