Subject: bug#51321: 29.0.50; date in modelines

From: Alan Third <[email-masked]>
Date: Mon, 1 Nov 2021 21:17:36 +0000

On Mon, Nov 01, 2021 at 04:34:58PM +0200, Eli Zaretskii wrote:
> > From: Filipp Gunbin <[email-masked]>
> > Cc: [email-masked],  [email-masked],  [email-masked],  [email-masked],
> >   [email-masked],  [email-masked]
> > Date: Mon, 01 Nov 2021 17:21:36 +0300
> > 
> > >> So maybe we should check that the locale is valid before setting it...
> > >
> > > "Valid" in what sense?  Is that locale not installed on your system,
> > > or are you saying that it cannot exist?
> > 
> > I think in the sense of 'locale -a | grep ru' in this case:
> > 
> > ru_RU.ISO8859-5
> > ru_RU.CP866
> > ru_RU.CP1251
> > ru_RU.UTF-8
> > ru_RU.KOI8-R
> > ru_RU
> > 
> > Maybe it could exist, but it doesn't on macOS.
> > 
> > > (And why does the warning come from Bash when it was Emacs who sets
> > > the locale?)
> > 
> > Forgot to tell - this output shows up when running shell command from
> > emacs (M-! or similar).
> 
> Hmm... actually, why do we call setenv instead of calling setlocale
> directly?  Doesn't macOS support setlocale?  AFAIU, calling setlocale
> would then only affect Emacs itself, not its sub-processes.

The point of this is to affect Emacs's sub-processes.

I know almost nothing about the locale, but from reading the man pages
I can't understand why we need to set LC_ALL when we're already
setting LANG. Isn't LANG the fallback if LC_ALL isn't set?

Anyway, I think we can use setlocale to test if the locale string is
valid, but I don't know if there are any other problems with that.
Would we want to reset it after testing?

I suppose it might also be preferable to not even try doing this if
Emacs was run from a terminal since the original problem really only
manifests when Emacs is run from the GUI.

Fillip, can you please try this:

modified   src/nsterm.m
@@ -543,10 +543,13 @@ - (NSColor *)colorUsingDefaultColorSpace
       NSString *localeID = [NSString stringWithFormat:@"%@.UTF-8",
                                      [locale localeIdentifier]];
 
-      /* Set LANG and LC_ALL to locale, but not if the variables are
-         already set.  */
-      setenv("LANG", [localeID UTF8String], 0);
-      setenv("LC_ALL", [localeID UTF8String], 0);
+      if (!isatty (STDIN_FILENO) && setlocale (LC_ALL, [localeID UTF8String]))
+        {
+          /* Set LANG and LC_ALL to locale, but not if the variables are
+             already set.  */
+          setenv("LANG", [localeID UTF8String], 0);
+          setenv("LC_ALL", [localeID UTF8String], 0);
+        }
     }
   @catch (NSException *e)
     {

-- 
Alan Third