emacs-31 4f36e9e8855: File name with newline: require 'ls' --dired switch

Stephen Berman via Mailing list for Emacs changes <[email protected]> Thu, 30 Jul 2026 17:13:39 -0400 (EDT)
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: emacs-31
commit 4f36e9e885535c97b1f9e9dcdd4c82b59860988b
Author: Stephen Berman <[email protected]>
Commit: Stephen Berman <[email protected]>

    File name with newline: require 'ls' --dired switch
    
    File name with newline: require 'ls' --dired switch
    
    Previously the handling of newlines in file names tested for the
    -b switch, but on systems such as Solaris 'ls' takes -b but not
    --dired and the latter is needed to set the correct bounds of file
    names containing a newline.
    
    * lisp/dired.el (dired--check-use-ls-dired): New function.
    (dired-internal-noselect, dired-insert-directory)
    (dired--set-auto-toggle-b-switch): Use it.
    (dired--remove-b-switch): Delete.
    
    * test/lisp/dired-tests.el (dired-test-filename-with-newline-1)
    (dired-test-filename-with-newline-2): Use the new function to skip
    tests when 'ls' does not support the --dired switch (bug#81481).
---
 lisp/dired.el            | 24 +++++++++++++-----------
 test/lisp/dired-tests.el | 14 ++++++--------
 2 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/lisp/dired.el b/lisp/dired.el
index 1b88f2eab36..61d27919c51 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -1504,7 +1504,10 @@ The return value is the target column for the file names."
                 (dired-switches-escape-p dired-listing-switches)
                 (dired-switches-escape-p dired-actual-switches))
       (when (and (dired--filename-with-newline-p)
-                 (dired--ls-accept-b-switch-p))
+                 ;; Can't use `dired-use-ls-dired' because users could
+                 ;; set it to `t' even though their `ls' does not
+                 ;; support "--dired".
+                 (dired--check-use-ls-dired))
         (dired--display-filename-with-newline-warning buffer)))
     (set-buffer old-buf)
     buffer))
@@ -1794,6 +1797,10 @@ BEG..END is the line where the file info is located."
   "Return non-nil if the string SWITCHES contains -R or --recursive."
   (dired-check-switches switches "R" "recursive"))
 
+(defun dired--check-use-ls-dired ()
+  "Return non-nil if `ls' supports the \"--dired\" switch."
+  (eq 0 (call-process insert-directory-program nil nil nil "--dired" "-N")))
+
 (defun dired-insert-directory (dir switches &optional file-list wildcard hdr)
   "Insert a directory listing of DIR, Dired style.
 Use SWITCHES to make the listings.
@@ -1813,11 +1820,7 @@ If HDR is non-nil, insert a header line with the directory name."
          (not (bound-and-true-p eshell-ls-use-in-dired))
 	 (or remotep
              (if (eq dired-use-ls-dired 'unspecified)
-		 ;; Check whether "ls --dired" gives exit code 0, and
-		 ;; save the answer in `dired-use-ls-dired'.
-		 (or (setq dired-use-ls-dired
-			   (eq 0 (call-process insert-directory-program
-                                               nil nil nil "--dired" "-N")))
+		 (or (setq dired-use-ls-dired (dired--check-use-ls-dired))
 		     (progn
 		       (message "ls does not support --dired -N; \
 see `dired-use-ls-dired' for more details.")
@@ -4078,10 +4081,6 @@ newline character (regardless of whether Dired displays the character as
 a literal newline or as \"\\n\")."
   (directory-files default-directory nil "\n"))
 
-(defun dired--ls-accept-b-switch-p ()
-  "Return non-nil if the `ls' used by Dired accepts the `b' switch."
-  (eq 0 (call-process insert-directory-program nil nil nil "-b")))
-
 (defun dired--remove-b-switch ()
   "Remove all variants of the `b' switch from `dired-actual-switches'.
 This removes not only all occurrences of the short form `-b' but also
@@ -4116,7 +4115,10 @@ otherwise remove the `b' switch unless it is in
 (defun dired--set-auto-toggle-b-switch (symbol value)
   "The :set function for user option `dired-auto-toggle-b-switch'."
   (custom-set-default symbol value)
-  (when (dired--ls-accept-b-switch-p)
+  (when
+      ;; Can't use `dired-use-ls-dired' because users could set it to
+      ;; `t' even though their `ls' does not support "--dired".
+      (dired--check-use-ls-dired)
     (if value
         (add-hook 'post-command-hook #'dired--toggle-b-switch nil t)
       (remove-hook 'post-command-hook #'dired--toggle-b-switch t))
diff --git a/test/lisp/dired-tests.el b/test/lisp/dired-tests.el
index 65ca58cc705..47ead6a30b3 100644
--- a/test/lisp/dired-tests.el
+++ b/test/lisp/dired-tests.el
@@ -660,10 +660,9 @@ The current directory at call time should not affect the result (Bug#50630)."
 
 (ert-deftest dired-test-filename-with-newline-1 () ; bug#79528, bug#80499
   "Test handling of file name with literal embedded newline."
-  ;; File names with embedded newlines are not allowed on MS-Windows and
-  ;; MS-DOS.
-  (skip-when (memq system-type '(windows-nt ms-dos)))
-  (skip-unless (dired--ls-accept-b-switch-p))
+  ;; The handling of file names with embedded newlines requires an `ls'
+  ;; that supports the "--dired" switch.
+  (skip-unless (dired--check-use-ls-dired))
   (with-current-buffer "*Messages*"
     (let ((inhibit-read-only t))
       (erase-buffer)))
@@ -696,10 +695,9 @@ The current directory at call time should not affect the result (Bug#50630)."
 
 (ert-deftest dired-test-filename-with-newline-2 () ; bug#79528, bug#80499
   "Test handling of file name with embedded newline using `b' switch."
-  ;; File names with embedded newlines are not allowed on MS-Windows and
-  ;; MS-DOS.
-  (skip-when (memq system-type '(windows-nt ms-dos)))
-  (skip-unless (dired--ls-accept-b-switch-p))
+  ;; The handling of file names with embedded newlines requires an `ls'
+  ;; that supports the "--dired" switch.
+  (skip-unless (dired--check-use-ls-dired))
   (with-current-buffer "*Messages*"
     (let ((inhibit-read-only t))
       (erase-buffer)))