unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob deb58c2b479ea61e408cedf62e2a1b6ba9d5b1f5 5164 bytes (raw)
name: packages/shell-quasiquote/shell-quasiquote.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 
;;; shell-quasiquote.el --- Turn s-expressions into shell command strings.

;; Copyright (C) 2015  Free Software Foundation, Inc.

;; Author: Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
;; Keywords: extensions, unix
;; URL: https://github.com/TaylanUB/emacs-shell-quasiquote

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; "Shell quasiquote" -- turn s-expressions into shell command strings.
;;
;; Quoting is automatic for POSIX shells.
;;
;;   (let ((file1 "file one")
;;         (file2 "file two"))
;;     (shqq (cp -r ,file1 ,file2 "My Files")))
;;       => "cp -r 'file one' 'file two' 'My Files'"
;;
;; You can splice many arguments into place with ,@foo.
;;
;;   (let ((files (list "file one" "file two")))
;;     (shqq (cp -r ,@files "My Files")))
;;       => "cp -r 'file one' 'file two' 'My Files'"
;;
;; Note that the quoting disables a variety of shell expansions like ~/foo,
;; $ENV_VAR, and e.g. {x..y} in GNU Bash.
;;
;; You can use ,,foo to escape the quoting.
;;
;;   (let ((files "file1 file2"))
;;     (shqq (cp -r ,,files "My Files")))
;;       => "cp -r file1 file2 'My Files'"
;;
;; And ,,@foo to splice and escape quoting.
;;
;;   (let* ((arglist '("-x 'foo bar' -y baz"))
;;          (arglist (append arglist '("-z 'qux fux'"))))
;;     (shqq (command ,,@arglist)))
;;       => "command -x 'foo bar' -y baz -z 'qux fux'"
;;
;; Neat, eh?

\f
;;; Code:

;;; Like `shell-quote-argument', but much simpler in implementation.
(defun shqq--quote-string (string)
  (concat "'" (replace-regexp-in-string "'" "'\\\\''" string) "'"))

(defun shqq--atom-to-string (atom)
  (cond
   ((symbolp atom) (symbol-name atom))
   ((stringp atom) atom)
   ((numberp atom) (number-to-string atom))
   (t (error "Bad shqq atom: %S" atom))))

(defun shqq--quote-atom (atom)
  (shqq--quote-string (shqq--atom-to-string atom)))

(defun shqq--match-comma (form)
  "Matches FORM against ,foo i.e. (\, foo) and returns foo.
Returns nil if FORM didn't match.  You can't disambiguate between
FORM matching ,nil and not matching."
  (if (and (consp form)
           (eq '\, (car form))
           (consp (cdr form))
           (null (cddr form)))
      (cadr form)))

(defun shqq--match-comma2 (form)
  "Matches FORM against ,,foo i.e. (\, (\, foo)) and returns foo.
Returns nil if FORM didn't match.  You can't disambiguate between
FORM matching ,,nil and not matching."
  (if (and (consp form)
           (eq '\, (car form))
           (consp (cdr form))
           (null (cddr form)))
      (shqq--match-comma (cadr form))))

\f
(defmacro shqq (parts)
  "First, PARTS is turned into a list of strings.  For this,
every element of PARTS must be one of:

- a symbol, evaluating to its name,

- a string, evaluating to itself,

- a number, evaluating to its decimal representation,

- \",expr\", where EXPR must evaluate to an atom that will be
  interpreted according to the previous rules,

- \",@list-expr\", where LIST-EXPR must evaluate to a list whose
  elements will each be interpreted like the EXPR in an \",EXPR\"
  form, and spliced into the list of strings,

- \",,expr\", where EXPR is interpreted like in \",expr\",

- or \",,@expr\", where EXPR is interpreted like in \",@expr\".

In the resulting list of strings, all elements except the ones
resulting from \",,expr\" and \",,@expr\" forms are quoted for
shell grammar.

Finally, the resulting list of strings is concatenated with
separating spaces."
  (let ((parts
         (mapcar
          (lambda (part)
            (cond
             ((atom part) (shqq--quote-atom part))
             ;; We use the match-comma helpers because pcase can't match ,foo.
             (t (pcase part
                  ;; ,,foo i.e. (, (, foo))
                  ((pred shqq--match-comma2)
                   (shqq--match-comma2 part))
                  ;; ,,@foo i.e. (, (,@ foo))
                  ((and (pred shqq--match-comma)
                        (let `,@,form (shqq--match-comma part)))
                   `(mapconcat #'identity ,form " "))
                  ;; ,foo
                  ;; Insert redundant 'and x' to work around debbugs#18554.
                  ((and x (pred shqq--match-comma))
                   `(shqq--quote-atom ,(shqq--match-comma part)))
                  ;; ,@foo
                  (`,@,form
                   `(mapconcat #'shqq--quote-atom ,form " "))
                  (_
                   (error "Bad shqq part: %S" part))))))
          parts)))
    `(mapconcat #'identity (list ,@parts) " ")))

(provide 'shell-quasiquote)
;;; shell-quasiquote.el ends here

debug log:

solving deb58c2 ...
found deb58c2 in https://yhetil.org/emacs-devel/87si59wj42.fsf@T420.taylan/

applying [1/1] https://yhetil.org/emacs-devel/87si59wj42.fsf@T420.taylan/
diff --git a/packages/shell-quasiquote/shell-quasiquote.el b/packages/shell-quasiquote/shell-quasiquote.el
new file mode 100644
index 0000000..deb58c2

Checking patch packages/shell-quasiquote/shell-quasiquote.el...
Applied patch packages/shell-quasiquote/shell-quasiquote.el cleanly.

index at:
100644 deb58c2b479ea61e408cedf62e2a1b6ba9d5b1f5	packages/shell-quasiquote/shell-quasiquote.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).