Subject: bug#64791: 30.0.50; [PATCH] Fix dired mismatch on some filenames

From: Stefan Kangas <[email-masked]>
Date: Sun, 24 Sep 2023 05:34:20 -0700

Eli Zaretskii <[email-masked]> writes:

>> I guess we'd have to set it in something like `normal-top-level'.
>
> We have a machinery for delaying the initialization to startup time:
> custom-initialize-delay.

TIL, thanks.  How does the attached patch look?

It bootstraps and seems to work on macOS, at least.
From 7a183ab0a6908030cbeee9301429920406465470 Mon Sep 17 00:00:00 2001
From: Stefan Kangas <[email-masked]>
Date: Sun, 24 Sep 2023 09:52:27 +0200
Subject: [PATCH] Make insert-directory-program a defcustom and use "gls" on
 *BSD

* lisp/files.el (insert-directory-program): Change into defcustom.
Default to using "gls" on *BSD and macOS.  (Bug#64791)
---
 etc/NEWS      |  8 ++++++++
 lisp/files.el | 19 +++++++++++++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 53c8451dc19..6400fb59255 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -144,6 +144,14 @@ can use this to distinguish between buffers visiting files with the
 same base name that belong to different projects by using the provided
 transform function 'project-uniquify-dirname-transform'.
 
+** 'insert-directory-program' is now a defcustom.
+
+** 'insert-directory-program' prefers "gls" on *BSD and macOS.
+On *BSD and macOS systems, it now defaults to using the "gls"
+executable, if it exists.  This should remove the need to change its
+value when installing GNU coreutils using something like ports or
+Homebrew.
+
 ** cl-print
 
 *** You can expand the "..." truncation everywhere.
diff --git a/lisp/files.el b/lisp/files.el
index 9d766683d26..d305c51748c 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -7727,10 +7727,25 @@ shell-quote-wildcard-pattern
       pattern))))

-(defvar insert-directory-program (purecopy "ls")
+(defcustom insert-directory-program
+  (if (and (memq system-type '(berkeley-unix darwin))
+           (executable-find "gls"))
+      (purecopy "gls")
+    (purecopy "ls"))
   "Absolute or relative name of the `ls'-like program.
 This is used by `insert-directory' and `dired-insert-directory'
-\(thus, also by `dired').")
+(thus, also by `dired').  This should point to GNU ls, or another
+version of ls that supports the \"--dired\" flag.
+
+On GNU/Linux and other capable systems, the default is \"ls\".
+
+On *BSD and macOS systems, the default \"ls\" does not support
+the \"--dired\" flag.  Therefore, the default is to use the
+\"gls\" executable on such machines, if it exists."
+  :group 'dired
+  :type 'string
+  :initialize #'custom-initialize-delay
+  :version "30.1")
 
 (defcustom directory-free-space-program (purecopy "df")
   "Program to get the amount of free space on a file system.
-- 
2.42.0