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

From: Stefan Kangas <[email-masked]>
Date: Tue, 5 Sep 2023 15:27:48 -0700

Eli Zaretskii <[email-masked]> writes:

> Actually, I take this back: this defvar will be computed when files.el
> is loaded, which is at dump time.  So this is not TRT, since the
> conditions and the environment at dump time could be very different
> from those at run time.  It could be even on another machine.

Yes, right.  That's no good.

> So I once again suggest to make this a defcustom.  This has two
> advantages:
>
>  . it will be evaluated at Emacs startup, not at dump time

Agreed.  Let's change it into a defcustom.

>  . it will allow users to customize the value and thus avoid costly
>    search  for the program and prevent finding the wrong version of
>    'ls'

Perhaps this part is okay though, given that the search should only
affect users on macOS and *BSD (who presumably will want this anyways)?

Proposed patch attached.
From 43ff9396fdc6b5643a72fdb76eca10067762b334 Mon Sep 17 00:00:00 2001
From: Stefan Kangas <[email-masked]>
Date: Wed, 6 Sep 2023 00:16:03 +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 | 11 +++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index a68e67e0694..52648400261 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -128,6 +128,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 4188615e490..ef5c6a96b55 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -7723,10 +7723,17 @@ 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')."
+  :group 'dired
+  :type 'string
+  :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