Subject: bug#71935: split-string-and-unquote mishandles dired-listing-switches with '

From: Juri Linkov <[email-masked]>
Date: Fri, 05 Jul 2024 09:48:39 +0300

>> > @@ -8195,9 +8193,11 @@ insert-directory
>> > -			       (if (stringp switches)
>> > -				   switches
>> > -				 (mapconcat #'identity switches " "))
>> > +			       (mapconcat #'shell-quote-argument
>> > +                                          (if (stringp switches)
>> > +				              (split-string-and-unquote switches)
>> > +                                            switches)
>> > +                                          " ")
>>
>> Thanks, that's what I had in mind.  Please install on the emacs-30
>> branch.
>
> On second thought: could there be options that include shell
> wildcards, which therefore should not be quoted?  If so, perhaps
> instead of shell-quote-argument we should use
> shell-quote-wildcard-pattern?

Indeed, there are ls switches that use wildcards, e.g.
‘--hide=PATTERN’ and ‘--ignore=PATTERN’.  But it seems
they are ignored anyway while using wildcards with 'ls -d',
so I can't test them.  What I can confirm only is that with

1. (setopt dired-listing-switches "-al --block-size='1 --ignore=system*")
2. C-x d /tmp/s*

the switches are correctly quoted by 'shell-quote-wildcard-pattern':

"ls -d --dired -N -al --block-size=\\'1 --ignore=system* -- s*"

diff --git a/lisp/files.el b/lisp/files.el
index 042b8e2d515..e69dfb22a5f 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -8195,9 +8193,15 @@ insert-directory
                                 "\\") ; Disregard Unix shell aliases!
                               insert-directory-program
                               " -d "
-                              (if (stringp switches)
-                                  switches
-                                (mapconcat #'identity switches " "))
+                              ;; Quote switches that require quoting
+                              ;; such as "--block-size='1".  But don't
+                              ;; quote switches that use patterns
+                              ;; such as "--ignore=PATTERN" (bug#71935).
+                              (mapconcat #'shell-quote-wildcard-pattern
+                                         (if (stringp switches)
+                                             (split-string-and-unquote switches)
+                                           switches)
+                                         " ")
                               " -- "
                               ;; Quote some characters that have
                               ;; special meanings in shells; but