1 ;;; backquote.el --- implement the ` Lisp construct
3 ;;; Copyright (C) 1990, 1992, 1994 Free Software Foundation, Inc.
5 ;; Author: Rick Sladkey <jrs@world.std.com>
7 ;; Keywords: extensions, internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This backquote will generate calls to the backquote-list* form.
29 ;; Both a function version and a macro version are included.
30 ;; The macro version is used by default because it is faster
31 ;; and needs no run-time support. It should really be a subr.
37 ;; function and macro versions of backquote-list*
39 (defun backquote-list*-function
(first &rest list
)
40 "Like `list' but the last argument is the tail of the new list.
42 For example (backquote-list* 'a 'b 'c) => (a b . c)"
44 (let* ((rest list
) (newlist (cons first nil
)) (last newlist
))
46 (setcdr last
(cons (car rest
) nil
))
49 (setcdr last
(car rest
))
53 (defmacro backquote-list
*-macro
(first &rest list
)
54 "Like `list' but the last argument is the tail of the new list.
56 For example (backquote-list* 'a 'b 'c) => (a b . c)"
57 (setq list
(reverse (cons first list
))
61 (let* ((second (car list
))
63 (newlist (list 'cons second first
)))
65 (setq newlist
(list 'cons
(car rest
) newlist
)
70 (defalias 'backquote-list
* (symbol-function 'backquote-list
*-macro
))
72 ;; A few advertised variables that control which symbols are used
73 ;; to represent the backquote, unquote, and splice operations.
74 (defconst backquote-backquote-symbol
'\
`
75 "*Symbol used to represent a backquote or nested backquote.")
77 (defconst backquote-unquote-symbol
',
78 "*Symbol used to represent an unquote inside a backquote.")
80 (defconst backquote-splice-symbol
',@
81 "*Symbol used to represent a splice inside a backquote.")
84 (defmacro backquote
(arg)
85 "Argument STRUCTURE describes a template to build.
87 The whole structure acts as if it were quoted except for certain
88 places where expressions are evaluated and inserted or spliced in.
92 b => (ba bb bc) ; assume b has this value
93 `(a b c) => (a b c) ; backquote acts like quote
94 `(a ,b c) => (a (ba bb bc) c) ; insert the value of b
95 `(a ,@b c) => (a ba bb bc c) ; splice in the value of b
97 Vectors work just like lists. Nested backquotes are permitted."
98 (cdr (backquote-process arg
)))
100 ;; GNU Emacs has no reader macros
103 (defalias '\
` (symbol-function 'backquote
))
105 ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
106 ;; the backquote-processed structure. 0 => the structure is
107 ;; constant, 1 => to be unquoted, 2 => to be spliced in.
108 ;; The top-level backquote macro just discards the tag.
110 (defun backquote-process (s)
113 (let ((n (backquote-process (append s
()))))
117 ((eq (nth 1 n
) 'list
)
118 (cons 'vector
(nthcdr 2 n
)))
119 ((eq (nth 1 n
) 'append
)
120 (cons 'vconcat
(nthcdr 2 n
)))
122 (list 'apply
'(function vector
) (cdr n
))))))))
124 (cons 0 (if (or (null s
) (eq s t
) (not (symbolp s
)))
127 ((eq (car s
) backquote-unquote-symbol
)
129 ((eq (car s
) backquote-splice-symbol
)
131 ((eq (car s
) backquote-backquote-symbol
)
132 (backquote-process (cdr (backquote-process (nth 1 s
)))))
135 item firstlist list lists expression
)
136 ;; Scan this list-level, setting LISTS to a list of forms,
137 ;; each of which produces a list of elements
138 ;; that should go in this level.
139 ;; The order of LISTS is backwards.
140 ;; If there are non-splicing elements (constant or variable)
141 ;; at the beginning, put them in FIRSTLIST,
142 ;; as a list of tagged values (TAG . FORM).
143 ;; If there are any at the end, they go in LIST, likewise.
145 ;; Turn . (, foo) into (,@ foo).
146 (if (eq (car rest
) backquote-unquote-symbol
)
147 (setq rest
(list (list backquote-splice-symbol
(nth 1 rest
)))))
148 (setq item
(backquote-process (car rest
)))
151 ;; Put the nonspliced items before the first spliced item
156 ;; Otherwise, put any preceding nonspliced items into LISTS.
158 (setq lists
(cons (backquote-listify list
'(0 . nil
)) lists
)))
159 (setq lists
(cons (cdr item
) lists
))
162 (setq list
(cons item list
))))
163 (setq rest
(cdr rest
)))
164 ;; Handle nonsplicing final elements, and the tail of the list
165 ;; (which remains in REST).
167 (setq lists
(cons (backquote-listify list
(backquote-process rest
))
169 ;; Turn LISTS into a form that produces the combined list.
172 (eq (car-safe (car lists
)) backquote-splice-symbol
))
173 (cons 'append
(nreverse lists
))
175 ;; Tack on any initial elements.
177 (setq expression
(backquote-listify firstlist
(cons 1 expression
))))
178 (if (eq (car-safe expression
) 'quote
)
179 (cons 0 (list 'quote s
))
180 (cons 1 expression
))))))
182 ;; backquote-listify takes (tag . structure) pairs from backquote-process
183 ;; and decides between append, list, backquote-list*, and cons depending
184 ;; on which tags are in the list.
186 (defun backquote-listify (list old-tail
)
187 (let ((heads nil
) (tail (cdr old-tail
)) (list-tail list
) (item nil
))
188 (if (= (car old-tail
) 0)
189 (setq tail
(eval tail
)
191 (while (consp list-tail
)
192 (setq item
(car list-tail
))
193 (setq list-tail
(cdr list-tail
))
194 (if (or heads old-tail
(/= (car item
) 0))
195 (setq heads
(cons (cdr item
) heads
))
196 (setq tail
(cons (eval (cdr item
)) tail
))))
200 (setq tail
(list 'quote tail
)))
202 (let ((use-list* (or (cdr heads
)
203 (and (consp (car heads
))
204 (eq (car (car heads
))
205 backquote-splice-symbol
)))))
206 (cons (if use-list
* 'backquote-list
* 'cons
)
207 (append heads
(list tail
))))
209 (t (cons 'list heads
)))))
211 ;; backquote.el ends here