]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/backquote.el
Update copyright.
[gnu-emacs] / 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 1, 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 (fset '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 (defmacro backquote (arg)
96 "Argument STRUCTURE describes a template to build.
97
98 The whole structure acts as if it were quoted except for certain
99 places where expressions are evaluated and inserted or spliced in.
100
101 For example:
102
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
107
108 Vectors work just like lists. Nested backquotes are permitted."
109 (cdr (backquote-process arg)))
110
111 ;; GNU Emacs has no reader macros
112
113 (fset backquote-backquote-symbol (symbol-function 'backquote))
114
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.
119
120 (defun backquote-process (s)
121 (cond
122 ((vectorp s)
123 (let ((n (backquote-process (append s ()))))
124 (if (= (car n) 0)
125 (cons 0 s)
126 (cons 1 (cond
127 ((eq (nth 1 n) 'list)
128 (cons 'vector (nthcdr 2 n)))
129 ((eq (nth 1 n) 'append)
130 (cons 'vconcat (nthcdr 2 n)))
131 (t
132 (list 'apply '(function vector) (cdr n))))))))
133 ((atom s)
134 (cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
135 s
136 (list 'quote s))))
137 ((eq (car s) backquote-unquote-symbol)
138 (cons 1 (nth 1 s)))
139 ((eq (car s) backquote-splice-symbol)
140 (cons 2 (nth 1 s)))
141 ((eq (car s) backquote-backquote-symbol)
142 (backquote-process (cdr (backquote-process (nth 1 s)))))
143 (t
144 (let ((rest 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.
154 (while (consp rest)
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)))
159 (cond
160 ((= (car item) 2)
161 (if (null firstlist)
162 (setq firstlist list
163 list nil))
164 (if list
165 (setq lists (cons (backquote-listify list '(0 . nil)) lists)))
166 (setq lists (cons (cdr item) lists))
167 (setq list nil))
168 (t
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).
173 (if (or rest list)
174 (setq lists (cons (backquote-listify list (backquote-process rest))
175 lists)))
176 ;; Turn LISTS into a form that produces the combined list.
177 (setq expression
178 (if (or (cdr lists)
179 (eq (car-safe (car lists)) backquote-splice-symbol))
180 (cons 'append (nreverse lists))
181 (car lists)))
182 ;; Tack on any initial elements.
183 (if firstlist
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))))))
188
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.
192
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)
197 old-tail nil))
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))))
204 (cond
205 (tail
206 (if (null old-tail)
207 (setq tail (list 'quote tail)))
208 (if heads
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))))
215 tail))
216 (t (cons 'list heads)))))
217
218 ;; backquote.el ends here