Subject: bug#10607: bug-10607

From: Charles El Hourani <[email-masked]>
Date: Tue, 4 Apr 2023 21:52:39 +0300

Here is a way to solve the issue by calling `ls` directly, without
re-implementing the "-b" functionality of ls in elisp.
From 22962ffd84370ac05017ed05cca88286d010aa0e Mon Sep 17 00:00:00 2001
From: Charlie El Hourani <[email-masked]>
Date: Tue, 4 Apr 2023 21:26:07 +0300
Subject: [PATCH] Fix dired goto file when -b is provided to ls (bug#10607)

This fixes the goto file in dired mode for:
- files containing a control character
- and when dired uses ls with the "-b" flag

The goto file function calls 'ls' to give it the escaped name.
---
 lisp/dired.el            | 16 +++++++++-------
 test/lisp/dired-tests.el | 22 ++++++++++++++++++++++
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/lisp/dired.el b/lisp/dired.el
index 8e3244356fe..a3117d8e553 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -3522,13 +3522,15 @@ dired-goto-file-1
   (let (str)
     (setq str (string-replace "\^m" "\\^m"  file))
     (setq str (string-replace "\\" "\\\\" str))
-    (and (dired-switches-escape-p dired-actual-switches)
-	 (string-match-p "[ \t\n]" str)
-	 ;; FIXME: to fix this for embedded control characters etc, we
-	 ;; should escape everything that `ls -b' does.
-	 (setq str (string-replace " " "\\ "  str)
-	       str (string-replace "\t" "\\t" str)
-	       str (string-replace "\n" "\\n" str)))
+    (and (not (featurep 'ls-lisp))
+         (dired-switches-escape-p dired-actual-switches)
+         (let ((escaped-full-name
+                (with-output-to-string
+                  (call-process "ls" nil (list standard-output nil) nil
+                                "-bd" full-name))))
+           (when (not (string-blank-p escaped-full-name))
+             (setq str (file-name-nondirectory
+                        (car (split-string escaped-full-name)))))))
     (let ((found nil)
 	  ;; filenames are preceded by SPC, this makes the search faster
 	  ;; (e.g. for the filename "-").
diff --git a/test/lisp/dired-tests.el b/test/lisp/dired-tests.el
index 347bdfc0d7b..c26ff8ec9ea 100644
--- a/test/lisp/dired-tests.el
+++ b/test/lisp/dired-tests.el
@@ -31,6 +31,28 @@ dired-autoload
     (symbol-function
      'dired-do-relsymlink))))
 
+(ert-deftest dired-test-bug10607 ()
+  "Test for https://debbugs.gnu.org/10607 ."
+  (let* ((dired-listing-switches "-alb")
+         (prefix "ab")
+         (prefixed-filename (make-temp-file prefix))
+         (regular-filename (make-temp-file ""))
+         buffers)
+    (push (dired (file-name-directory prefixed-filename)) buffers)
+    (unwind-protect
+        (progn
+          (dired-goto-file prefixed-filename)
+          (when (not (featurep 'ls-lisp))
+            (should (equal (dired-file-name-at-point)
+                           prefixed-filename)))
+          (dired-goto-file regular-filename)
+          (should (equal (dired-file-name-at-point)
+                         regular-filename)))
+      (dolist (buf buffers)
+        (when (buffer-live-p buf) (kill-buffer buf)))
+      (delete-file prefixed-filename)
+      (delete-file regular-filename))))
+
 (ert-deftest dired-test-bug22694 ()
   "Test for https://debbugs.gnu.org/22694 ."
   (let* ((dir       (expand-file-name "bug22694" default-directory))
-- 
2.34.1