unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Alan Third <alan@idiocy.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: larsi@gnus.org, 51832@debbugs.gnu.org,
	Philipp <p.stephani2@gmail.com>,
	tor.a.s.kringeland@ntnu.no
Subject: bug#51832: Piping unicode text in `shell-command'
Date: Sun, 14 Nov 2021 12:31:36 +0000	[thread overview]
Message-ID: <YZEBqFv8o4sjYKtJ@idiocy.org> (raw)
In-Reply-To: <83czn3uhtd.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1923 bytes --]

On Sun, Nov 14, 2021 at 12:56:14PM +0200, Eli Zaretskii wrote:
> > From: Philipp <p.stephani2@gmail.com>
> > Date: Sun, 14 Nov 2021 11:41:38 +0100
> > Cc: Lars Ingebrigtsen <larsi@gnus.org>,
> >  tor.a.s.kringeland@ntnu.no,
> >  51832@debbugs.gnu.org,
> >  alan@idiocy.org
> > 
> > > I asked once why we push LANG into the environment, instead of calling
> > > setlocale, which would only affect Emacs.  I don't think I saw an
> > > answer to that question, or did I miss it?
> > > 
> > 
> > AIUI the intention is that this should affect subprocesses started from Emacs.  At least that's how I interpret the comment
> > 
> > /* macOS doesn't set any environment variables for the locale when run
> >    from the GUI. Get the locale from the OS and set LANG.  */
> 
> Why is that needed?
> 
> And if it is needed, how come we are setting LANG to an invalid locale
> and the system somehow sets it to the correct locale?

macOS itself doesn't set any locale related environment variables, any
application that is running UNIX style commands is expected to set
them itself. The UNIX commands don't themselves pick up the locale
from the system, they rely on the environment variables.

In other words, as with anything UNIXy on macOS, it's a badly thought
out mess.

It seems suspicious to me that we've had this code since Emacs 26, but
only in the last few weeks we've had two complaints about it. Having
dug out my Mac I can't convince it to show any of the errors that have
been reported, so I suspect either the latest version of macOS has
made the locale handling much more strict or has removed a lot of
locales.

I've attached a patch that may do something towards preventing this
problem but ultimately this is a convenience to give a best guess at
choosing the correct dictionary, date format, etc. If we can't easily
fix it then we can drop it and tell people to set it in their init.el
themselves.

-- 
Alan Third

[-- Attachment #2: 0001-Only-set-LANG-if-the-ID-is-valid.patch --]
[-- Type: text/x-diff, Size: 2059 bytes --]

From ff67f1cbee3c0b1fd5b1a0d725e40158190cfe55 Mon Sep 17 00:00:00 2001
From: Alan Third <alan@idiocy.org>
Date: Sun, 14 Nov 2021 11:32:54 +0000
Subject: [PATCH] Only set LANG if the ID is valid

* src/nsterm.m (ns_init_locale): Check the provided locale identifier
is available before trying to use it.
---
 src/nsterm.m | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index 1f17a30272..566537e8a1 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -535,21 +535,25 @@ - (NSColor *)colorUsingDefaultColorSpace
 
   NSTRACE ("ns_init_locale");
 
-  @try
+  if ([[NSLocale availableLocaleIdentifiers]
+        containsObject:[locale localeIdentifier]])
     {
-      /* It seems macOS should probably use UTF-8 everywhere.
-         'localeIdentifier' does not specify the encoding, and I can't
-         find any way to get the OS to tell us which encoding to use,
-         so hard-code '.UTF-8'.  */
-      NSString *localeID = [NSString stringWithFormat:@"%@.UTF-8",
-                                     [locale localeIdentifier]];
-
-      /* Set LANG to locale, but not if LANG is already set.  */
-      setenv("LANG", [localeID UTF8String], 0);
-    }
-  @catch (NSException *e)
-    {
-      NSLog (@"Locale detection failed: %@: %@", [e name], [e reason]);
+      @try
+        {
+          /* It seems macOS should probably use UTF-8 everywhere.
+             'localeIdentifier' does not specify the encoding, and I can't
+             find any way to get the OS to tell us which encoding to use,
+             so hard-code '.UTF-8'.  */
+          NSString *localeID = [NSString stringWithFormat:@"%@.UTF-8",
+                                         [locale localeIdentifier]];
+
+          /* Set LANG to locale, but not if LANG is already set.  */
+          setenv("LANG", [localeID UTF8String], 0);
+        }
+      @catch (NSException *e)
+        {
+          NSLog (@"Locale detection failed: %@: %@", [e name], [e reason]);
+        }
     }
 }
 
-- 
2.33.0


  parent reply	other threads:[~2021-11-14 12:31 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-14  3:10 bug#51832: Piping unicode text in `shell-command' Tor Kringeland
2021-11-14  7:26 ` Eli Zaretskii
2021-11-14  7:53   ` Lars Ingebrigtsen
2021-11-14  8:13     ` Eli Zaretskii
2021-11-14  8:18       ` Lars Ingebrigtsen
2021-11-14  8:25         ` Eli Zaretskii
2021-11-14  9:19           ` Lars Ingebrigtsen
2021-11-14  9:32           ` Lars Ingebrigtsen
2021-11-14  9:46             ` Lars Ingebrigtsen
2021-11-14 10:31               ` Eli Zaretskii
2021-11-14 10:41                 ` Philipp
2021-11-14 10:56                   ` Eli Zaretskii
2021-11-14 11:20                     ` Lars Ingebrigtsen
2021-11-14 11:48                       ` Philipp
2021-11-14 12:16                       ` Eli Zaretskii
2021-11-14 12:31                     ` Alan Third [this message]
2021-11-14 13:41                       ` Lars Ingebrigtsen
2021-11-14 14:23                         ` Philipp
2021-11-14 14:28                           ` Lars Ingebrigtsen
2021-11-14 15:20                             ` Alan Third
2021-11-14 15:29                               ` Lars Ingebrigtsen
2021-11-16 20:52                                 ` Alan Third
2022-09-20 13:24                                   ` Lars Ingebrigtsen
2021-11-14 15:01                           ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YZEBqFv8o4sjYKtJ@idiocy.org \
    --to=alan@idiocy.org \
    --cc=51832@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=larsi@gnus.org \
    --cc=p.stephani2@gmail.com \
    --cc=tor.a.s.kringeland@ntnu.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).