1 ;;; backquote.el --- implement the ` Lisp construct
3 ;; Copyright (C) 1990, 1992, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Rick Sladkey <jrs@world.std.com>
8 ;; Keywords: extensions, internal
10 ;; This file is part of GNU Emacs.
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)
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.
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.
29 ;; When the Lisp reader sees `(...), it generates (\` (...)).
30 ;; When it sees ,... inside such a backquote form, it generates (\, ...).
31 ;; For ,@... it generates (\,@ ...).
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.
42 ;; function and macro versions of backquote-list*
44 (defun backquote-list*-function
(first &rest list
)
45 "Like `list' but the last argument is the tail of the new list.
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.
52 (let* ((rest list
) (newlist (cons first nil
)) (last newlist
))
54 (setcdr last
(cons (car rest
) nil
))
57 (setcdr last
(car rest
))
61 (defmacro backquote-list
*-macro
(first &rest list
)
62 "Like `list' but the last argument is the tail of the new list.
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
))
72 (let* ((second (car list
))
74 (newlist (list 'cons second first
)))
76 (setq newlist
(list 'cons
(car rest
) newlist
)
81 (defalias 'backquote-list
* (symbol-function 'backquote-list
*-macro
))
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.")
88 (defconst backquote-unquote-symbol
'\
,
89 "Symbol used to represent an unquote inside a backquote.")
91 (defconst backquote-splice-symbol
'\
,@
92 "Symbol used to represent a splice inside a backquote.")
94 (defmacro backquote
(structure)
95 "Argument STRUCTURE describes a template to build.
97 The whole structure acts as if it were quoted except for certain
98 places where expressions are evaluated and inserted or spliced in.
102 b => (ba bb bc) ; assume b has this value
103 `(a b c) => (a b c) ; backquote acts like quote
104 `(a ,b c) => (a (ba bb bc) c) ; insert the value of b
105 `(a ,@b c) => (a ba bb bc c) ; splice in the value of b
107 Vectors work just like lists. Nested backquotes are permitted."
108 (cdr (backquote-process structure
)))
110 ;; GNU Emacs has no reader macros
112 (defalias '\
` (symbol-function 'backquote
))
114 ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
115 ;; the backquote-processed structure. 0 => the structure is
116 ;; constant, 1 => to be unquoted, 2 => to be spliced in.
117 ;; The top-level backquote macro just discards the tag.
119 (defun backquote-delay-process (s level
)
120 "Process a (un|back|splice)quote inside a backquote.
121 This simply recurses through the body."
122 (let ((exp (backquote-listify (list (cons 0 (list 'quote
(car s
))))
123 (backquote-process (cdr s
) level
))))
124 (if (eq (car-safe exp
) 'quote
)
125 (cons 0 (list 'quote s
))
128 (defun backquote-process (s &optional level
)
129 "Process the body of a backquote.
130 S is the body. Returns a cons cell whose cdr is piece of code which
131 is the macro-expansion of S, and whose car is a small integer whose value
132 can either indicate that the code is constant (0), or not (1), or returns
133 a list which should be spliced into its environment (2).
134 LEVEL is only used internally and indicates the nesting level:
135 0 (the default) is for the toplevel nested inside a single backquote."
136 (unless level
(setq level
0))
139 (let ((n (backquote-process (append s
()) level
)))
143 ((not (listp (cdr n
)))
144 (list 'vconcat
(cdr n
)))
145 ((eq (nth 1 n
) 'list
)
146 (cons 'vector
(nthcdr 2 n
)))
147 ((eq (nth 1 n
) 'append
)
148 (cons 'vconcat
(nthcdr 2 n
)))
150 (list 'apply
'(function vector
) (cdr n
))))))))
152 (cons 0 (if (or (null s
) (eq s t
) (not (symbolp s
)))
155 ((eq (car s
) backquote-unquote-symbol
)
158 (backquote-delay-process s
(1- level
))))
159 ((eq (car s
) backquote-splice-symbol
)
162 (backquote-delay-process s
(1- level
))))
163 ((eq (car s
) backquote-backquote-symbol
)
164 (backquote-delay-process s
(1+ level
)))
167 item firstlist list lists expression
)
168 ;; Scan this list-level, setting LISTS to a list of forms,
169 ;; each of which produces a list of elements
170 ;; that should go in this level.
171 ;; The order of LISTS is backwards.
172 ;; If there are non-splicing elements (constant or variable)
173 ;; at the beginning, put them in FIRSTLIST,
174 ;; as a list of tagged values (TAG . FORM).
175 ;; If there are any at the end, they go in LIST, likewise.
176 (while (and (consp rest
)
177 ;; Stop if the cdr is an expression inside a backquote or
178 ;; unquote since this needs to go recursively through
179 ;; backquote-process.
180 (not (or (eq (car rest
) backquote-unquote-symbol
)
181 (eq (car rest
) backquote-backquote-symbol
))))
182 (setq item
(backquote-process (car rest
) level
))
185 ;; Put the nonspliced items before the first spliced item
190 ;; Otherwise, put any preceding nonspliced items into LISTS.
192 (push (backquote-listify list
'(0 . nil
)) lists
))
193 (push (cdr item
) lists
)
196 (setq list
(cons item list
))))
197 (setq rest
(cdr rest
)))
198 ;; Handle nonsplicing final elements, and the tail of the list
199 ;; (which remains in REST).
201 (push (backquote-listify list
(backquote-process rest level
))
203 ;; Turn LISTS into a form that produces the combined list.
206 (eq (car-safe (car lists
)) backquote-splice-symbol
))
207 (cons 'append
(nreverse lists
))
209 ;; Tack on any initial elements.
211 (setq expression
(backquote-listify firstlist
(cons 1 expression
))))
212 (if (eq (car-safe expression
) 'quote
)
213 (cons 0 (list 'quote s
))
214 (cons 1 expression
))))))
216 ;; backquote-listify takes (tag . structure) pairs from backquote-process
217 ;; and decides between append, list, backquote-list*, and cons depending
218 ;; on which tags are in the list.
220 (defun backquote-listify (list old-tail
)
221 (let ((heads nil
) (tail (cdr old-tail
)) (list-tail list
) (item nil
))
222 (if (= (car old-tail
) 0)
223 (setq tail
(eval tail
)
225 (while (consp list-tail
)
226 (setq item
(car list-tail
))
227 (setq list-tail
(cdr list-tail
))
228 (if (or heads old-tail
(/= (car item
) 0))
229 (setq heads
(cons (cdr item
) heads
))
230 (setq tail
(cons (eval (cdr item
)) tail
))))
234 (setq tail
(list 'quote tail
)))
236 (let ((use-list* (or (cdr heads
)
237 (and (consp (car heads
))
238 (eq (car (car heads
))
239 backquote-splice-symbol
)))))
240 (cons (if use-list
* 'backquote-list
* 'cons
)
241 (append heads
(list tail
))))
243 (t (cons 'list heads
)))))
245 ;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a
246 ;;; backquote.el ends here