1 ;;; backquote.el --- implement the ` Lisp construct
3 ;; Copyright (C) 1990, 1992, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005 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 2, 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.")
95 (defmacro backquote
(arg)
96 "Argument STRUCTURE describes a template to build.
98 The whole structure acts as if it were quoted except for certain
99 places where expressions are evaluated and inserted or spliced in.
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
108 Vectors work just like lists. Nested backquotes are permitted."
109 (cdr (backquote-process arg
)))
111 ;; GNU Emacs has no reader macros
114 (defalias '\
` (symbol-function 'backquote
))
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.
121 (defun backquote-process (s)
124 (let ((n (backquote-process (append s
()))))
128 ((not (listp (cdr n
)))
129 (list 'vconcat
(cdr n
)))
130 ((eq (nth 1 n
) 'list
)
131 (cons 'vector
(nthcdr 2 n
)))
132 ((eq (nth 1 n
) 'append
)
133 (cons 'vconcat
(nthcdr 2 n
)))
135 (list 'apply
'(function vector
) (cdr n
))))))))
137 (cons 0 (if (or (null s
) (eq s t
) (not (symbolp s
)))
140 ((eq (car s
) backquote-unquote-symbol
)
142 ((eq (car s
) backquote-splice-symbol
)
144 ((eq (car s
) backquote-backquote-symbol
)
145 (backquote-process (cdr (backquote-process (nth 1 s
)))))
148 item firstlist list lists expression
)
149 ;; Scan this list-level, setting LISTS to a list of forms,
150 ;; each of which produces a list of elements
151 ;; that should go in this level.
152 ;; The order of LISTS is backwards.
153 ;; If there are non-splicing elements (constant or variable)
154 ;; at the beginning, put them in FIRSTLIST,
155 ;; as a list of tagged values (TAG . FORM).
156 ;; If there are any at the end, they go in LIST, likewise.
158 ;; Turn . (, foo) into (,@ foo).
159 (if (eq (car rest
) backquote-unquote-symbol
)
160 (setq rest
(list (list backquote-splice-symbol
(nth 1 rest
)))))
161 (setq item
(backquote-process (car rest
)))
164 ;; Put the nonspliced items before the first spliced item
169 ;; Otherwise, put any preceding nonspliced items into LISTS.
171 (setq lists
(cons (backquote-listify list
'(0 . nil
)) lists
)))
172 (setq lists
(cons (cdr item
) lists
))
175 (setq list
(cons item list
))))
176 (setq rest
(cdr rest
)))
177 ;; Handle nonsplicing final elements, and the tail of the list
178 ;; (which remains in REST).
180 (setq lists
(cons (backquote-listify list
(backquote-process rest
))
182 ;; Turn LISTS into a form that produces the combined list.
185 (eq (car-safe (car lists
)) backquote-splice-symbol
))
186 (cons 'append
(nreverse lists
))
188 ;; Tack on any initial elements.
190 (setq expression
(backquote-listify firstlist
(cons 1 expression
))))
191 (if (eq (car-safe expression
) 'quote
)
192 (cons 0 (list 'quote s
))
193 (cons 1 expression
))))))
195 ;; backquote-listify takes (tag . structure) pairs from backquote-process
196 ;; and decides between append, list, backquote-list*, and cons depending
197 ;; on which tags are in the list.
199 (defun backquote-listify (list old-tail
)
200 (let ((heads nil
) (tail (cdr old-tail
)) (list-tail list
) (item nil
))
201 (if (= (car old-tail
) 0)
202 (setq tail
(eval tail
)
204 (while (consp list-tail
)
205 (setq item
(car list-tail
))
206 (setq list-tail
(cdr list-tail
))
207 (if (or heads old-tail
(/= (car item
) 0))
208 (setq heads
(cons (cdr item
) heads
))
209 (setq tail
(cons (eval (cdr item
)) tail
))))
213 (setq tail
(list 'quote tail
)))
215 (let ((use-list* (or (cdr heads
)
216 (and (consp (car heads
))
217 (eq (car (car heads
))
218 backquote-splice-symbol
)))))
219 (cons (if use-list
* 'backquote-list
* 'cons
)
220 (append heads
(list tail
))))
222 (t (cons 'list heads
)))))
224 ;;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a
225 ;;; backquote.el ends here