Update GPL to version 2.
[bpt/emacs.git] / lisp / emacs-lisp / backquote.el
1 ;;; New backquote for GNU Emacs.
2 ;;; Copyright (C) 1990, 1992, 1994 Free Software Foundation, Inc.
3
4 ;; Author: Rick Sladkey <jrs@world.std.com>
5 ;; Maintainer: FSF
6 ;; Keywords: extensions, internal
7
8 ;; This file is not part of GNU Emacs but is distributed under
9 ;; the same conditions as GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
24
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:
28
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.
38
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.
43
44 ;;; Code:
45
46 (provide 'backquote)
47
48 ;; function and macro versions of backquote-list*
49
50 (defun backquote-list*-function (first &rest list)
51 "Like `list' but the last argument is the tail of the new list.
52
53 For example (backquote-list* 'a 'b 'c) => (a b . c)"
54 (if list
55 (let* ((rest list) (newlist (cons first nil)) (last newlist))
56 (while (cdr rest)
57 (setcdr last (cons (car rest) nil))
58 (setq last (cdr last)
59 rest (cdr rest)))
60 (setcdr last (car rest))
61 newlist)
62 first))
63
64 (defmacro backquote-list*-macro (first &rest list)
65 "Like `list' but the last argument is the tail of the new list.
66
67 For example (backquote-list* 'a 'b 'c) => (a b . c)"
68 (setq list (reverse (cons first list))
69 first (car list)
70 list (cdr list))
71 (if list
72 (let* ((second (car list))
73 (rest (cdr list))
74 (newlist (list 'cons second first)))
75 (while rest
76 (setq newlist (list 'cons (car rest) newlist)
77 rest (cdr rest)))
78 newlist)
79 first))
80
81 (defalias 'backquote-list* (symbol-function 'backquote-list*-macro))
82
83 ;; A few advertised variables that control which symbols are used
84 ;; to represent the backquote, unquote, and splice operations.
85
86 (defvar backquote-backquote-symbol '\`
87 "*Symbol used to represent a backquote or nested backquote (e.g. `).")
88
89 (defvar backquote-unquote-symbol ',
90 "*Symbol used to represent an unquote (e.g. `,') inside a backquote.")
91
92 (defvar backquote-splice-symbol ',@
93 "*Symbol used to represent a splice (e.g. `,@') inside a backquote.")
94
95 ;;;###autoload
96 (defmacro backquote (arg)
97 "Argument STRUCTURE describes a template to build.
98
99 The whole structure acts as if it were quoted except for certain
100 places where expressions are evaluated and inserted or spliced in.
101
102 For example:
103
104 b => (ba bb bc) ; assume b has this value
105 `(a b c) => (a b c) ; backquote acts like quote
106 `(a (, b) c) => (a (ba bb bc) c) ; insert the value of b
107 `(a (,@ b) c) => (a ba bb bc c) ; splice in the value of b
108
109 Vectors work just like lists. Nested backquotes are permitted."
110 (cdr (backquote-process arg)))
111
112 ;; GNU Emacs has no reader macros
113
114 ;;;###autoload
115 (defalias '\` (symbol-function 'backquote))
116
117 ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
118 ;; the backquote-processed structure. 0 => the structure is
119 ;; constant, 1 => to be unquoted, 2 => to be spliced in.
120 ;; The top-level backquote macro just discards the tag.
121
122 (defun backquote-process (s)
123 (cond
124 ((vectorp s)
125 (let ((n (backquote-process (append s ()))))
126 (if (= (car n) 0)
127 (cons 0 s)
128 (cons 1 (cond
129 ((eq (nth 1 n) 'list)
130 (cons 'vector (nthcdr 2 n)))
131 ((eq (nth 1 n) 'append)
132 (cons 'vconcat (nthcdr 2 n)))
133 (t
134 (list 'apply '(function vector) (cdr n))))))))
135 ((atom s)
136 (cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
137 s
138 (list 'quote s))))
139 ((eq (car s) backquote-unquote-symbol)
140 (cons 1 (nth 1 s)))
141 ((eq (car s) backquote-splice-symbol)
142 (cons 2 (nth 1 s)))
143 ((eq (car s) backquote-backquote-symbol)
144 (backquote-process (cdr (backquote-process (nth 1 s)))))
145 (t
146 (let ((rest s)
147 item firstlist list lists expression)
148 ;; Scan this list-level, setting LISTS to a list of forms,
149 ;; each of which produces a list of elements
150 ;; that should go in this level.
151 ;; The order of LISTS is backwards.
152 ;; If there are non-splicing elements (constant or variable)
153 ;; at the beginning, put them in FIRSTLIST,
154 ;; as a list of tagged values (TAG . FORM).
155 ;; If there are any at the end, they go in LIST, likewise.
156 (while (consp rest)
157 ;; Turn . (, foo) into (,@ foo).
158 (if (eq (car rest) backquote-unquote-symbol)
159 (setq rest (list (list backquote-splice-symbol (nth 1 rest)))))
160 (setq item (backquote-process (car rest)))
161 (cond
162 ((= (car item) 2)
163 ;; Put the nonspliced items before the first spliced item
164 ;; into FIRSTLIST.
165 (if (null lists)
166 (setq firstlist list
167 list nil))
168 ;; Otherwise, put any preceding nonspliced items into LISTS.
169 (if list
170 (setq lists (cons (backquote-listify list '(0 . nil)) lists)))
171 (setq lists (cons (cdr item) lists))
172 (setq list nil))
173 (t
174 (setq list (cons item list))))
175 (setq rest (cdr rest)))
176 ;; Handle nonsplicing final elements, and the tail of the list
177 ;; (which remains in REST).
178 (if (or rest list)
179 (setq lists (cons (backquote-listify list (backquote-process rest))
180 lists)))
181 ;; Turn LISTS into a form that produces the combined list.
182 (setq expression
183 (if (or (cdr lists)
184 (eq (car-safe (car lists)) backquote-splice-symbol))
185 (cons 'append (nreverse lists))
186 (car lists)))
187 ;; Tack on any initial elements.
188 (if firstlist
189 (setq expression (backquote-listify firstlist (cons 1 expression))))
190 (if (eq (car-safe expression) 'quote)
191 (cons 0 (list 'quote s))
192 (cons 1 expression))))))
193
194 ;; backquote-listify takes (tag . structure) pairs from backquote-process
195 ;; and decides between append, list, backquote-list*, and cons depending
196 ;; on which tags are in the list.
197
198 (defun backquote-listify (list old-tail)
199 (let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil))
200 (if (= (car old-tail) 0)
201 (setq tail (eval tail)
202 old-tail nil))
203 (while (consp list-tail)
204 (setq item (car list-tail))
205 (setq list-tail (cdr list-tail))
206 (if (or heads old-tail (/= (car item) 0))
207 (setq heads (cons (cdr item) heads))
208 (setq tail (cons (eval (cdr item)) tail))))
209 (cond
210 (tail
211 (if (null old-tail)
212 (setq tail (list 'quote tail)))
213 (if heads
214 (let ((use-list* (or (cdr heads)
215 (and (consp (car heads))
216 (eq (car (car heads))
217 backquote-splice-symbol)))))
218 (cons (if use-list* 'backquote-list* 'cons)
219 (append heads (list tail))))
220 tail))
221 (t (cons 'list heads)))))
222
223 ;; backquote.el ends here