Function: shell--unquote&requote-argument

shell--unquote&requote-argument is a byte-compiled function defined in shell.el.gz.

Signature

(shell--unquote&requote-argument QSTR &optional UPOS)

Source Code

;; Defined in /usr/src/emacs/lisp/shell.el.gz
;;; Basic Procedures

(defun shell--unquote&requote-argument (qstr &optional upos)
  (unless upos (setq upos 0))
  (let* ((qpos 0)
         (dquotes nil)
         (ustrs '())
         (re (concat
              "[\"']"
              "\\|\\$\\(?:\\([[:alpha:]][[:alnum:]]*\\)"
              "\\|{\\(?1:[^{}]+\\)}\\)"
              (when (memq system-type '(ms-dos windows-nt))
                "\\|%\\(?1:[^\\/]*\\)%")
              (when comint-file-name-quote-list
                "\\|\\\\\\(.\\)")))
         (qupos nil)
         (push (lambda (str end)
                 (push str ustrs)
                 (setq upos (- upos (length str)))
                 (unless (or qupos (> upos 0))
                   (setq qupos (if (< end 0) (- end) (+ upos end))))))
         match)
    (while (setq match (string-match re qstr qpos))
      (funcall push (substring qstr qpos match) match)
      (cond
       ((match-beginning 2) (funcall push (match-string 2 qstr) (match-end 0)))
       ((match-beginning 1) (funcall push (getenv (match-string 1 qstr))
                                     (- (match-end 0))))
       ((eq (aref qstr match) ?\") (setq dquotes (not dquotes)))
       ((eq (aref qstr match) ?\')
        (cond
         ;; Treat single quote as text if inside double quotes.
         (dquotes (funcall push "'" (match-end 0)))
         ((< (1+ match) (length qstr))
          (let ((end (string-match "'" qstr (1+ match))))
            (unless end
              (setq end (length qstr))
              (set-match-data (list match (length qstr))))
            (funcall push (substring qstr (1+ match) end) end)))
         ;; Ignore if at the end of string.
         (t nil)))
       (t (error "Unexpected case in shell--unquote&requote-argument!")))
      (setq qpos (match-end 0)))
    (funcall push (substring qstr qpos) (length qstr))
    (list (mapconcat #'identity (nreverse ustrs) "")
          qupos #'comint-quote-filename)))