Fix up autoloads in vc-rcs.el and vc-sccs.el.
[emacs.git] / lisp / emacs-lisp / backquote.el
blob5cecbcd4335b88cee25191fae58680933a60d107
1 ;;; backquote.el --- implement the ` Lisp construct
3 ;; Copyright (C) 1990, 1992, 1994, 2001-2014 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Rick Sladkey <jrs@world.std.com>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: extensions, internal
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; When the Lisp reader sees `(...), it generates (\` (...)).
29 ;; When it sees ,... inside such a backquote form, it generates (\, ...).
30 ;; For ,@... it generates (\,@ ...).
32 ;; This backquote will generate calls to the backquote-list* form.
33 ;; Both a function version and a macro version are included.
34 ;; The macro version is used by default because it is faster
35 ;; and needs no run-time support. It should really be a subr.
37 ;;; Code:
39 (provide 'backquote)
41 ;; function and macro versions of backquote-list*
43 (defun backquote-list*-function (first &rest list)
44 "Like `list' but the last argument is the tail of the new list.
46 For example (backquote-list* 'a 'b 'c) => (a b . c)"
47 ;; The recursive solution is much nicer:
48 ;; (if list (cons first (apply 'backquote-list*-function list)) first))
49 ;; but Emacs is not very good at efficiently processing recursion.
50 (if list
51 (let* ((rest list) (newlist (cons first nil)) (last newlist))
52 (while (cdr rest)
53 (setcdr last (cons (car rest) nil))
54 (setq last (cdr last)
55 rest (cdr rest)))
56 (setcdr last (car rest))
57 newlist)
58 first))
60 (defmacro backquote-list*-macro (first &rest list)
61 "Like `list' but the last argument is the tail of the new list.
63 For example (backquote-list* 'a 'b 'c) => (a b . c)"
64 ;; The recursive solution is much nicer:
65 ;; (if list (list 'cons first (cons 'backquote-list*-macro list)) first))
66 ;; but Emacs is not very good at efficiently processing such things.
67 (setq list (nreverse (cons first list))
68 first (car list)
69 list (cdr list))
70 (if list
71 (let* ((second (car list))
72 (rest (cdr list))
73 (newlist (list 'cons second first)))
74 (while rest
75 (setq newlist (list 'cons (car rest) newlist)
76 rest (cdr rest)))
77 newlist)
78 first))
80 (defalias 'backquote-list* (symbol-function 'backquote-list*-macro))
82 ;; A few advertised variables that control which symbols are used
83 ;; to represent the backquote, unquote, and splice operations.
84 (defconst backquote-backquote-symbol '\`
85 "Symbol used to represent a backquote or nested backquote.")
87 (defconst backquote-unquote-symbol '\,
88 "Symbol used to represent an unquote inside a backquote.")
90 (defconst backquote-splice-symbol '\,@
91 "Symbol used to represent a splice inside a backquote.")
93 (defmacro backquote (structure)
94 "Argument STRUCTURE describes a template to build.
96 The whole structure acts as if it were quoted except for certain
97 places where expressions are evaluated and inserted or spliced in.
99 For example:
101 b => (ba bb bc) ; assume b has this value
102 `(a b c) => (a b c) ; backquote acts like quote
103 `(a ,b c) => (a (ba bb bc) c) ; insert the value of b
104 `(a ,@b c) => (a ba bb bc c) ; splice in the value of b
106 Vectors work just like lists. Nested backquotes are permitted."
107 (cdr (backquote-process structure)))
109 ;; GNU Emacs has no reader macros
111 (defalias '\` (symbol-function 'backquote))
113 ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
114 ;; the backquote-processed structure. 0 => the structure is
115 ;; constant, 1 => to be unquoted, 2 => to be spliced in.
116 ;; The top-level backquote macro just discards the tag.
118 (defun backquote-delay-process (s level)
119 "Process a (un|back|splice)quote inside a backquote.
120 This simply recurses through the body."
121 (let ((exp (backquote-listify (list (cons 0 (list 'quote (car s))))
122 (backquote-process (cdr s) level))))
123 (if (eq (car-safe exp) 'quote)
124 (cons 0 (list 'quote s))
125 (cons 1 exp))))
127 (defun backquote-process (s &optional level)
128 "Process the body of a backquote.
129 S is the body. Returns a cons cell whose cdr is piece of code which
130 is the macro-expansion of S, and whose car is a small integer whose value
131 can either indicate that the code is constant (0), or not (1), or returns
132 a list which should be spliced into its environment (2).
133 LEVEL is only used internally and indicates the nesting level:
134 0 (the default) is for the toplevel nested inside a single backquote."
135 (unless level (setq level 0))
136 (cond
137 ((vectorp s)
138 (let ((n (backquote-process (append s ()) level)))
139 (if (= (car n) 0)
140 (cons 0 s)
141 (cons 1 (cond
142 ((not (listp (cdr n)))
143 (list 'vconcat (cdr n)))
144 ((eq (nth 1 n) 'list)
145 (cons 'vector (nthcdr 2 n)))
146 ((eq (nth 1 n) 'append)
147 (cons 'vconcat (nthcdr 2 n)))
149 (list 'apply '(function vector) (cdr n))))))))
150 ((atom s)
151 ;; FIXME: Use macroexp-quote!
152 (cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
154 (list 'quote s))))
155 ((eq (car s) backquote-unquote-symbol)
156 (if (<= level 0)
157 (cond
158 ((> (length s) 2)
159 ;; We could support it with: (cons 2 `(list . ,(cdr s)))
160 ;; But let's not encourage such uses.
161 (error "Multiple args to , are not supported: %S" s))
162 (t (cons (if (eq (car-safe (nth 1 s)) 'quote) 0 1)
163 (nth 1 s))))
164 (backquote-delay-process s (1- level))))
165 ((eq (car s) backquote-splice-symbol)
166 (if (<= level 0)
167 (if (> (length s) 2)
168 ;; (cons 2 `(append . ,(cdr s)))
169 (error "Multiple args to ,@ are not supported: %S" s)
170 (cons 2 (nth 1 s)))
171 (backquote-delay-process s (1- level))))
172 ((eq (car s) backquote-backquote-symbol)
173 (backquote-delay-process s (1+ level)))
175 (let ((rest s)
176 item firstlist list lists expression)
177 ;; Scan this list-level, setting LISTS to a list of forms,
178 ;; each of which produces a list of elements
179 ;; that should go in this level.
180 ;; The order of LISTS is backwards.
181 ;; If there are non-splicing elements (constant or variable)
182 ;; at the beginning, put them in FIRSTLIST,
183 ;; as a list of tagged values (TAG . FORM).
184 ;; If there are any at the end, they go in LIST, likewise.
185 (while (and (consp rest)
186 ;; Stop if the cdr is an expression inside a backquote or
187 ;; unquote since this needs to go recursively through
188 ;; backquote-process.
189 (not (or (eq (car rest) backquote-unquote-symbol)
190 (eq (car rest) backquote-backquote-symbol))))
191 (setq item (backquote-process (car rest) level))
192 (cond
193 ((= (car item) 2)
194 ;; Put the nonspliced items before the first spliced item
195 ;; into FIRSTLIST.
196 (if (null lists)
197 (setq firstlist list
198 list nil))
199 ;; Otherwise, put any preceding nonspliced items into LISTS.
200 (if list
201 (push (backquote-listify list '(0 . nil)) lists))
202 (push (cdr item) lists)
203 (setq list nil))
205 (setq list (cons item list))))
206 (setq rest (cdr rest)))
207 ;; Handle nonsplicing final elements, and the tail of the list
208 ;; (which remains in REST).
209 (if (or rest list)
210 (push (backquote-listify list (backquote-process rest level))
211 lists))
212 ;; Turn LISTS into a form that produces the combined list.
213 (setq expression
214 (if (or (cdr lists)
215 (eq (car-safe (car lists)) backquote-splice-symbol))
216 (cons 'append (nreverse lists))
217 (car lists)))
218 ;; Tack on any initial elements.
219 (if firstlist
220 (setq expression (backquote-listify firstlist (cons 1 expression))))
221 (cons (if (eq (car-safe expression) 'quote) 0 1) expression)))))
223 ;; backquote-listify takes (tag . structure) pairs from backquote-process
224 ;; and decides between append, list, backquote-list*, and cons depending
225 ;; on which tags are in the list.
227 (defun backquote-listify (list old-tail)
228 (let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil))
229 (if (= (car old-tail) 0)
230 (setq tail (eval tail)
231 old-tail nil))
232 (while (consp list-tail)
233 (setq item (car list-tail))
234 (setq list-tail (cdr list-tail))
235 (if (or heads old-tail (/= (car item) 0))
236 (setq heads (cons (cdr item) heads))
237 (setq tail (cons (eval (cdr item)) tail))))
238 (cond
239 (tail
240 (if (null old-tail)
241 (setq tail (list 'quote tail)))
242 (if heads
243 (let ((use-list* (or (cdr heads)
244 (and (consp (car heads))
245 (eq (car (car heads))
246 backquote-splice-symbol)))))
247 (cons (if use-list* 'backquote-list* 'cons)
248 (append heads (list tail))))
249 tail))
250 (t (cons 'list heads)))))
252 ;;; backquote.el ends here