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