Function: shell-quote-wildcard-pattern
shell-quote-wildcard-pattern is a byte-compiled function defined in
files.el.gz.
Signature
(shell-quote-wildcard-pattern PATTERN)
Documentation
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, each character from the set [ \t\n;<>&|()`'"#$] is quoted with a backslash; for DOS/Windows, all the parts of the pattern that don't include wildcard characters are quoted with double quotes.
This function leaves alone existing quote characters (\\ on Unix and " on Windows), so PATTERN can use them to quote wildcard characters that need to be passed verbatim to shell commands.
Source Code
;; Defined in /usr/src/emacs/lisp/files.el.gz
(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, each character from the
set [ \\t\\n;<>&|()\\=`\\='\"#$] is quoted with a backslash; for DOS/Windows, all
the parts of the pattern that don't include wildcard characters are
quoted with double quotes.
This function leaves alone existing quote characters (\\ on Unix and \"
on Windows), so PATTERN can use them to quote wildcard characters that
need to be passed verbatim to shell commands."
(save-match-data
(cond
((memq system-type '(ms-dos windows-nt cygwin))
;; 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-search "\"" 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)))))
pattern))))