]> git.eshelyaron.com Git - emacs.git/commitdiff
(shell-quote-wildcard-pattern): New function.
authorEli Zaretskii <eliz@gnu.org>
Sun, 30 Jul 2000 13:27:46 +0000 (13:27 +0000)
committerEli Zaretskii <eliz@gnu.org>
Sun, 30 Jul 2000 13:27:46 +0000 (13:27 +0000)
(insert-directory): Call it.  Only prepend "\" to command on Unix
and GNU/Linux systems.

lisp/ChangeLog
lisp/files.el

index 9c9b04e440254f73bd348a23d93b8768f849c1b5..99f458e5566781e5dadfcecfa6e009d4c406b7ea 100644 (file)
@@ -1,3 +1,9 @@
+2000-07-30  Eli Zaretskii  <eliz@is.elta.co.il>
+
+       * files.el (shell-quote-wildcard-pattern): New function.
+       (insert-directory): Call it.  Only prepend "\" to command on Unix
+       and GNU/Linux systems.
+
 2000-07-30  Gerd Moellmann  <gerd@gnu.org>
 
        * eshell/esh-groups.el: Change custom :link file names
index 0da412b91a23d16492fbcacf400762eeea5dc636..bb17d5ca13b469418eda73d3e8ca0b841501627c 100644 (file)
@@ -3411,6 +3411,50 @@ and `list-directory-verbose-switches'."
        (let ((wildcard (not (file-directory-p dirname))))
          (insert-directory dirname switches wildcard (not wildcard)))))))
 
+(defun shell-quote-wildcard-pattern (pattern)
+  "Quote characters special to the shell in PATTERN, leave wildcards alone.
+
+PATTERN is assumed to represent a file-name wildcard suitable for the
+underlying filesystem.  For Unix and GNU/Linux, the characters from the
+set [ \\t\\n;<>&|()#$] are quoted with a backslash; for DOS/Windows, all
+the parts of the pattern which don't include wildcard characters are
+quoted with double quotes.
+Existing quote characters in PATTERN are left alone, so you can pass
+PATTERN that already quotes some of the special characters."
+  (save-match-data
+    (cond
+     ((memq system-type '(ms-dos windows-nt))
+      ;; DOS/Windows don't allow `"' in file names.  So if the
+      ;; argument has quotes, we can safely assume it is already
+      ;; quoted by the caller.
+      (if (or (string-match "[\"]" pattern)
+             ;; We quote [&()#$'] in case their shell is a port of a
+             ;; Unixy shell.  We quote [,=+] because stock DOS and
+             ;; Windows shells require that in some cases, such as
+             ;; passing arguments to batch files that use positional
+             ;; arguments like %1.
+             (not (string-match "[ \t;&()#$',=+]" pattern)))
+         pattern
+       (let ((result "\"")
+             (beg 0)
+             end)
+         (while (string-match "[*?]+" pattern beg)
+           (setq end (match-beginning 0)
+                 result (concat result (substring pattern beg end)
+                                "\""
+                                (substring pattern end (match-end 0))
+                                "\"")
+                 beg (match-end 0)))
+         (concat result (substring pattern beg) "\""))))
+     (t
+      (let ((beg 0))
+       (while (string-match "[ \t\n;<>&|()#$]" pattern beg)
+         (setq pattern
+               (concat (substring pattern 0 (match-beginning 0))
+                       "\\"
+                       (substring pattern (match-beginning 0)))
+               beg (1+ (match-end 0)))))))))
+
 (defvar insert-directory-program "ls"
   "Absolute or relative name of the `ls' program used by `insert-directory'.")
 
@@ -3461,30 +3505,28 @@ If WILDCARD, it also runs the shell specified by `shell-file-name'."
                (if wildcard
                    ;; Run ls in the directory of the file pattern we asked for
                    (let ((default-directory (file-name-directory file))
-                         (pattern (file-name-nondirectory file))
-                         (beg 0))
-                     ;; Quote some characters that have special
-                     ;; meanings in shells; but don't quote the
-                     ;; wildcards--we want them to be special.
-                     ;; We also currently don't quote the quoting
-                     ;; characters in case people want to use them
-                     ;; explicitly to quote wildcard characters.
-                     (while (string-match "[ \t\n;<>&|()#$]" pattern beg)
-                       (setq pattern
-                             (concat (substring pattern 0 (match-beginning 0))
-                                     "\\"
-                                     (substring pattern (match-beginning 0)))
-                             beg (1+ (match-end 0))))
+                         (pattern (file-name-nondirectory file)))
                      (call-process
                        shell-file-name nil t nil
-                       "-c" (concat "\\" ; Disregard shell aliases!
+                       "-c" (concat (if (memq system-type '(ms-dos windows-nt))
+                                       ""
+                                     "\\") ; Disregard Unix shell aliases!
                                     insert-directory-program
                                     " -d "
                                     (if (stringp switches)
                                         switches
                                         (mapconcat 'identity switches " "))
                                     " -- "
-                                    pattern)))
+                                   ;; Quote some characters that have
+                                   ;; special meanings in shells; but
+                                   ;; don't quote the wildcards--we
+                                   ;; want them to be special.  We
+                                   ;; also currently don't quote the
+                                   ;; quoting characters in case
+                                   ;; people want to use them
+                                   ;; explicitly to quote wildcard
+                                   ;; characters.
+                                    (shell-quote-wildcard-pattern pattern))))
                  ;; SunOS 4.1.3, SVr4 and others need the "." to list the
                  ;; directory if FILE is a symbolic link.
                  (apply 'call-process