1 ;;; backquote.el -- implement the ` Lisp construct
2 ;;; Copyright (C) 1990, 1992, 1994 Free Software Foundation, Inc.
4 ;; Author: Rick Sladkey <jrs@world.std.com>
6 ;; Keywords: extensions, internal
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This backquote will generate calls to the backquote-list* form.
27 ;; Both a function version and a macro version are included.
28 ;; The macro version is used by default because it is faster
29 ;; and needs no run-time support. It should really be a subr.
35 ;; function and macro versions of backquote-list*
37 (defun backquote-list*-function
(first &rest list
)
38 "Like `list' but the last argument is the tail of the new list.
40 For example (backquote-list* 'a 'b 'c) => (a b . c)"
42 (let* ((rest list
) (newlist (cons first nil
)) (last newlist
))
44 (setcdr last
(cons (car rest
) nil
))
47 (setcdr last
(car rest
))
51 (defmacro backquote-list
*-macro
(first &rest list
)
52 "Like `list' but the last argument is the tail of the new list.
54 For example (backquote-list* 'a 'b 'c) => (a b . c)"
55 (setq list
(reverse (cons first list
))
59 (let* ((second (car list
))
61 (newlist (list 'cons second first
)))
63 (setq newlist
(list 'cons
(car rest
) newlist
)
68 (defalias 'backquote-list
* (symbol-function 'backquote-list
*-macro
))
70 ;; A few advertised variables that control which symbols are used
71 ;; to represent the backquote, unquote, and splice operations.
73 (defvar backquote-backquote-symbol
'\
`
74 "*Symbol used to represent a backquote or nested backquote (e.g. `).")
76 (defvar backquote-unquote-symbol
',
77 "*Symbol used to represent an unquote (e.g. `,') inside a backquote.")
79 (defvar backquote-splice-symbol
',@
80 "*Symbol used to represent a splice (e.g. `,@') inside a backquote.")
83 (defmacro backquote
(arg)
84 "Argument STRUCTURE describes a template to build.
86 The whole structure acts as if it were quoted except for certain
87 places where expressions are evaluated and inserted or spliced in.
91 b => (ba bb bc) ; assume b has this value
92 `(a b c) => (a b c) ; backquote acts like quote
93 `(a ,b c) => (a (ba bb bc) c) ; insert the value of b
94 `(a ,@b c) => (a ba bb bc c) ; splice in the value of b
96 Vectors work just like lists. Nested backquotes are permitted."
97 (cdr (backquote-process arg
)))
99 ;; GNU Emacs has no reader macros
102 (defalias '\
` (symbol-function 'backquote
))
104 ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
105 ;; the backquote-processed structure. 0 => the structure is
106 ;; constant, 1 => to be unquoted, 2 => to be spliced in.
107 ;; The top-level backquote macro just discards the tag.
109 (defun backquote-process (s)
112 (let ((n (backquote-process (append s
()))))
116 ((eq (nth 1 n
) 'list
)
117 (cons 'vector
(nthcdr 2 n
)))
118 ((eq (nth 1 n
) 'append
)
119 (cons 'vconcat
(nthcdr 2 n
)))
121 (list 'apply
'(function vector
) (cdr n
))))))))
123 (cons 0 (if (or (null s
) (eq s t
) (not (symbolp s
)))
126 ((eq (car s
) backquote-unquote-symbol
)
128 ((eq (car s
) backquote-splice-symbol
)
130 ((eq (car s
) backquote-backquote-symbol
)
131 (backquote-process (cdr (backquote-process (nth 1 s
)))))
134 item firstlist list lists expression
)
135 ;; Scan this list-level, setting LISTS to a list of forms,
136 ;; each of which produces a list of elements
137 ;; that should go in this level.
138 ;; The order of LISTS is backwards.
139 ;; If there are non-splicing elements (constant or variable)
140 ;; at the beginning, put them in FIRSTLIST,
141 ;; as a list of tagged values (TAG . FORM).
142 ;; If there are any at the end, they go in LIST, likewise.
144 ;; Turn . (, foo) into (,@ foo).
145 (if (eq (car rest
) backquote-unquote-symbol
)
146 (setq rest
(list (list backquote-splice-symbol
(nth 1 rest
)))))
147 (setq item
(backquote-process (car rest
)))
150 ;; Put the nonspliced items before the first spliced item
155 ;; Otherwise, put any preceding nonspliced items into LISTS.
157 (setq lists
(cons (backquote-listify list
'(0 . nil
)) lists
)))
158 (setq lists
(cons (cdr item
) lists
))
161 (setq list
(cons item list
))))
162 (setq rest
(cdr rest
)))
163 ;; Handle nonsplicing final elements, and the tail of the list
164 ;; (which remains in REST).
166 (setq lists
(cons (backquote-listify list
(backquote-process rest
))
168 ;; Turn LISTS into a form that produces the combined list.
171 (eq (car-safe (car lists
)) backquote-splice-symbol
))
172 (cons 'append
(nreverse lists
))
174 ;; Tack on any initial elements.
176 (setq expression
(backquote-listify firstlist
(cons 1 expression
))))
177 (if (eq (car-safe expression
) 'quote
)
178 (cons 0 (list 'quote s
))
179 (cons 1 expression
))))))
181 ;; backquote-listify takes (tag . structure) pairs from backquote-process
182 ;; and decides between append, list, backquote-list*, and cons depending
183 ;; on which tags are in the list.
185 (defun backquote-listify (list old-tail
)
186 (let ((heads nil
) (tail (cdr old-tail
)) (list-tail list
) (item nil
))
187 (if (= (car old-tail
) 0)
188 (setq tail
(eval tail
)
190 (while (consp list-tail
)
191 (setq item
(car list-tail
))
192 (setq list-tail
(cdr list-tail
))
193 (if (or heads old-tail
(/= (car item
) 0))
194 (setq heads
(cons (cdr item
) heads
))
195 (setq tail
(cons (eval (cdr item
)) tail
))))
199 (setq tail
(list 'quote tail
)))
201 (let ((use-list* (or (cdr heads
)
202 (and (consp (car heads
))
203 (eq (car (car heads
))
204 backquote-splice-symbol
)))))
205 (cons (if use-list
* 'backquote-list
* 'cons
)
206 (append heads
(list tail
))))
208 (t (cons 'list heads
)))))
210 ;; backquote.el ends here