1 ;;; New backquote for GNU Emacs.
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 not part of GNU Emacs but is distributed under
9 ;; the same conditions as 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 1, 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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; This is a new backquote for GNU Emacs written by
26 ;; Rick Sladkey <jrs@world.std.com>. It has the following
27 ;; features compared to the version 18 backquote:
29 ;; Correctly handles nested backquotes.
30 ;; Correctly handles constants after a splice.
31 ;; Correctly handles top-level atoms and unquotes.
32 ;; Correctly handles unquote after dot.
33 ;; Understands vectors.
34 ;; Minimizes gratuitous consing.
35 ;; Faster operation with simpler semantics.
36 ;; Generates faster run-time expressions.
37 ;; One third fewer calories than our regular beer.
39 ;; This backquote will generate calls to the backquote-list* form.
40 ;; Both a function version and a macro version are included.
41 ;; The macro version is used by default because it is faster
42 ;; and needs no run-time support. It should really be a subr.
48 ;; function and macro versions of backquote-list*
50 (defun backquote-list*-function (first &rest list)
51 "Like `list' but the last argument is the tail of the new list.
53 For example (backquote-list* 'a 'b 'c) => (a b . c)"
55 (let* ((rest list) (newlist (cons first nil)) (last newlist))
57 (setcdr last (cons (car rest) nil))
60 (setcdr last (car rest))
64 (defmacro backquote-list*-macro (first &rest list)
65 "Like `list' but the last argument is the tail of the new list.
67 For example (backquote-list* 'a 'b 'c) => (a b . c)"
68 (setq list (reverse (cons first list))
72 (let* ((second (car list))
74 (newlist (list 'cons second first)))
76 (setq newlist (list 'cons (car rest) newlist)
81 (fset '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.
86 (defvar backquote-backquote-symbol '`
87 "*Symbol used to represent a backquote or nested backquote (e.g. `).")
89 (defvar backquote-unquote-symbol ',
90 "*Symbol used to represent an unquote (e.g. `,') inside a backquote.")
92 (defvar backquote-splice-symbol ',@
93 "*Symbol used to represent a splice (e.g. `,'@) 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
113 (fset backquote-backquote-symbol (symbol-function 'backquote))
115 ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
116 ;; the backquote-processed structure. 0 => the structure is
117 ;; constant, 1 => to be unquoted, 2 => to be spliced in.
118 ;; The top-level backquote macro just discards the tag.
120 (defun backquote-process (s)
123 (let ((n (backquote-process (append s ()))))
127 ((eq (nth 1 n) 'list)
128 (cons 'vector (nthcdr 2 n)))
129 ((eq (nth 1 n) 'append)
130 (cons 'vconcat (nthcdr 2 n)))
132 (list 'apply '(function vector) (cdr n))))))))
134 (cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
137 ((eq (car s) backquote-unquote-symbol)
139 ((eq (car s) backquote-splice-symbol)
141 ((eq (car s) backquote-backquote-symbol)
142 (backquote-process (cdr (backquote-process (nth 1 s)))))
145 item firstlist list lists expression)
146 ;; Scan this list-level, setting LISTS to a list of forms,
147 ;; each of which produces a list of elements
148 ;; that should go in this level.
149 ;; The order of LISTS is backwards.
150 ;; If there are non-splicing elements (constant or variable)
151 ;; at the beginning, put them in FIRSTLIST,
152 ;; as a list of tagged values (TAG . FORM).
153 ;; If there are any at the end, they go in LIST, likewise.
155 ;; Turn . (, foo) into (,@ foo).
156 (if (eq (car rest) backquote-unquote-symbol)
157 (setq rest (list (list backquote-splice-symbol (nth 1 rest)))))
158 (setq item (backquote-process (car rest)))
165 (setq lists (cons (backquote-listify list '(0 . nil)) lists)))
166 (setq lists (cons (cdr item) lists))
169 (setq list (cons item list))))
170 (setq rest (cdr rest)))
171 ;; Handle nonsplicing final elements, and the tail of the list
172 ;; (which remains in REST).
174 (setq lists (cons (backquote-listify list (backquote-process rest))
176 ;; Turn LISTS into a form that produces the combined list.
179 (eq (car-safe (car lists)) backquote-splice-symbol))
180 (cons 'append (nreverse lists))
182 ;; Tack on any initial elements.
184 (setq expression (backquote-listify firstlist (cons 1 expression))))
185 (if (eq (car-safe expression) 'quote)
186 (cons 0 (list 'quote s))
187 (cons 1 expression))))))
189 ;; backquote-listify takes (tag . structure) pairs from backquote-process
190 ;; and decides between append, list, backquote-list*, and cons depending
191 ;; on which tags are in the list.
193 (defun backquote-listify (list old-tail)
194 (let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil))
195 (if (= (car old-tail) 0)
196 (setq tail (eval tail)
198 (while (consp list-tail)
199 (setq item (car list-tail))
200 (setq list-tail (cdr list-tail))
201 (if (or heads old-tail (/= (car item) 0))
202 (setq heads (cons (cdr item) heads))
203 (setq tail (cons (eval (cdr item)) tail))))
207 (setq tail (list 'quote tail)))
209 (let ((use-list* (or (cdr heads)
210 (and (consp (car heads))
211 (eq (car (car heads))
212 backquote-splice-symbol)))))
213 (cons (if use-list* 'backquote-list* 'cons)
214 (append heads (list tail))))
216 (t (cons 'list heads)))))
218 ;; backquote.el ends here