]> code.delx.au - gnu-emacs-elpa/blob - packages/shell-quasiquote/shell-quasiquote.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / shell-quasiquote / shell-quasiquote.el
1 ;;; shell-quasiquote.el --- Turn s-expressions into shell command strings.
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
6 ;; Keywords: extensions, unix
7 ;; Version: 0
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; "Shell quasiquote" -- turn s-expressions into POSIX shell command strings.
25 ;;
26 ;; Shells other than POSIX sh are not supported.
27 ;;
28 ;; Quoting is automatic and safe against injection.
29 ;;
30 ;; (let ((file1 "file one")
31 ;; (file2 "file two"))
32 ;; (shqq (cp -r ,file1 ,file2 "My Files")))
33 ;; => "cp -r 'file one' 'file two' 'My Files'"
34 ;;
35 ;; You can splice many arguments into place with ,@foo.
36 ;;
37 ;; (let ((files (list "file one" "file two")))
38 ;; (shqq (cp -r ,@files "My Files")))
39 ;; => "cp -r 'file one' 'file two' 'My Files'"
40 ;;
41 ;; Note that the quoting disables a variety of shell expansions like ~/foo,
42 ;; $ENV_VAR, and e.g. {x..y} in GNU Bash.
43 ;;
44 ;; You can use ,,foo to escape the quoting.
45 ;;
46 ;; (let ((files "file1 file2"))
47 ;; (shqq (cp -r ,,files "My Files")))
48 ;; => "cp -r file1 file2 'My Files'"
49 ;;
50 ;; And ,,@foo to splice and escape quoting.
51 ;;
52 ;; (let* ((arglist '("-x 'foo bar' -y baz"))
53 ;; (arglist (append arglist '("-z 'qux fux'"))))
54 ;; (shqq (command ,,@arglist)))
55 ;; => "command -x 'foo bar' -y baz -z 'qux fux'"
56 ;;
57 ;; Neat, eh?
58
59 \f
60 ;;; Code:
61
62 (defun shqq--atom-to-string (atom)
63 (cond
64 ((symbolp atom) (symbol-name atom))
65 ((stringp atom) atom)
66 ((numberp atom) (number-to-string atom))
67 (t (error "Bad shqq atom: %S" atom))))
68
69 (defun shqq--quote-atom (atom)
70 (shell-quote-argument (shqq--atom-to-string atom)))
71 \f
72 (defmacro shqq (parts)
73 "First, PARTS is turned into a list of strings. For this,
74 every element of PARTS must be one of:
75
76 - a symbol, evaluating to its name,
77
78 - a string, evaluating to itself,
79
80 - a number, evaluating to its decimal representation,
81
82 - \",expr\", where EXPR must evaluate to an atom that will be
83 interpreted according to the previous rules,
84
85 - \",@list-expr\", where LIST-EXPR must evaluate to a list whose
86 elements will each be interpreted like the EXPR in an \",EXPR\"
87 form, and spliced into the list of strings,
88
89 - \",,expr\", where EXPR is interpreted like in \",expr\",
90
91 - or \",,@expr\", where EXPR is interpreted like in \",@expr\".
92
93 In the resulting list of strings, all elements except the ones
94 resulting from \",,expr\" and \",,@expr\" forms are quoted for
95 shell grammar.
96
97 Finally, the resulting list of strings is concatenated with
98 separating spaces."
99 (let ((parts
100 (mapcar
101 (lambda (part)
102 (cond
103 ((atom part) (shqq--quote-atom part))
104 ;; We use the match-comma helpers because pcase can't match ,foo.
105 (t (pcase part
106 ;; ,,foo i.e. (, (, foo))
107 (`(,`\, (,`\, ,form)) form)
108 ;; ,,@foo i.e. (, (,@ foo))
109 (`(,`\, (,`\,@ ,form)) `(mapconcat #'identity ,form " "))
110 ;; ,foo
111 (`(,`\, ,form) `(shqq--quote-atom ,form))
112 ;; ,@foo
113 (`,@,form `(mapconcat #'shqq--quote-atom ,form " "))
114 (_
115 (error "Bad shqq part: %S" part))))))
116 parts)))
117 `(mapconcat #'identity (list ,@parts) " ")))
118
119 (provide 'shell-quasiquote)
120 ;; Local Variables:
121 ;; coding: utf-8
122 ;; End:
123 ;;; shell-quasiquote.el ends here