Subject: bug#65908: 29.1.50; Emacs 29 regresses on macOS

From: Gerd Möllmann <[email-masked]>
Date: Thu, 05 Oct 2023 10:52:21 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: Gerd Möllmann <[email-masked]>
>> Cc: Stefan Kangas <[email-masked]>,  Gregory Heytings
>>  <[email-masked]>,  [email-masked],  Eli Zaretskii
>>  <[email-masked]>,  [email-masked]
>> Date: Thu, 05 Oct 2023 07:55:55 +0200
>>
>> When I start 058c012f73d4abe014ace44b46c23babd48aebbc by double-clicking
>> Emacs.app, then M-x shell, I get
>>
>> $ locale
>> LANG=""
>> LC_COLLATE="C"
>> LC_CTYPE="C"
>> LC_MESSAGES="C"
>> LC_MONETARY="C"
>> LC_NUMERIC="C"
>> LC_TIME="C"
>> LC_ALL=
>>
>> That can't be right, or is it?
>>
>> The same, one commit before that:
>>
>> $ locale
>> LANG="en_DE.UTF-8"
>> LC_COLLATE="C"
>> LC_CTYPE="C"
>> LC_MESSAGES="C"
>> LC_MONETARY="C"
>> LC_NUMERIC="C"
>> LC_TIME="C"
>> LC_ALL=
>>
>> At least LANG looks correct to me (I'm using an English UI, in DE.).
>> The LC_* are odd, though.
>
> Emacs forces LC_ALL= and LC_NUMERIC=C, to properly localize the
> initial error messages and format numbers as in the "C" locale, see
> 'main' (which also has an NS-specific code).

Thanks for the hint, Eli.

I've meanwhile found out something.  Not yet sure what to make of this,
but anyway:

1. In ns_init_locale, [locale localeIdentifier] returns "en_DE" on my
system, which is, I think, a consequence of my system settings, as I
mentioned (English UI in Germany).

2. man setlocale(3) says this

     An argument of "" will
     determine the name of the new locale taking into account the environment
     variables LANG and LC_*.

So, the setlocale (LC_ALL, "") in main should initialize LC_* according
to LANG, if set.  (Another question might be if that is what was
intended here, when I read the comment above that code, but be that as
it may.)

And another thing - any setlocale in ns_init_locale is moot because it
will be undone in main.

4. When I setenv LANG="en_DE.UTF-8" in ns_init_locale, the setlocale
(LC_ALL, "") returns NULL, indicating that LANG is invalid.  And, indeed

  $ locale -a | grep de
  de_CH
  de_DE.UTF-8
  de_AT.ISO8859-1
  de_AT.UTF-8
  de_AT.ISO8859-15
  de_DE.ISO8859-15
  de_CH.UTF-8
  de_CH.ISO8859-15
  de_DE.ISO8859-1
  de_CH.ISO8859-1
  de_AT
  de_DE

  There is no en_DE...

5. When I don't use localeIndentifier, but "en_US.UTF-8", for instance,
   the setlocale (LC_ALL, "") in main works like expected, and in a shell
   buffer:

  $ ~/emacs/master/src/ > locale
  LANG="en_US.UTF-8"
  LC_COLLATE="en_US.UTF-8"
  LC_CTYPE="UTF-8"
  LC_MESSAGES="en_US.UTF-8"
  LC_MONETARY="en_US.UTF-8"
  LC_NUMERIC="en_US.UTF-8"
  LC_TIME="en_US.UTF-8"
  LC_ALL=

That's all not very nice :-).

Conclusion so far, for me,

- don't selocale in ns_init_locale, only setenv LANG, or set LC_CTYPE,
  maybe.
- localeIndentifier is wrong for mixed cases like "en_DE".