Comment change.
[bpt/emacs.git] / lisp / emacs-lisp / backquote.el
1 ;;; backquote.el -- implement the ` Lisp construct
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 part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
23
24 ;;; Commentary:
25
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.
30
31 ;;; Code:
32
33 (provide 'backquote)
34
35 ;; function and macro versions of backquote-list*
36
37 (defun backquote-list*-function (first &rest list)
38 "Like `list' but the last argument is the tail of the new list.
39
40 For example (backquote-list* 'a 'b 'c) => (a b . c)"
41 (if list
42 (let* ((rest list) (newlist (cons first nil)) (last newlist))
43 (while (cdr rest)
44 (setcdr last (cons (car rest) nil))
45 (setq last (cdr last)
46 rest (cdr rest)))
47 (setcdr last (car rest))
48 newlist)
49 first))
50
51 (defmacro backquote-list*-macro (first &rest list)
52 "Like `list' but the last argument is the tail of the new list.
53
54 For example (backquote-list* 'a 'b 'c) => (a b . c)"
55 (setq list (reverse (cons first list))
56 first (car list)
57 list (cdr list))
58 (if list
59 (let* ((second (car list))
60 (rest (cdr list))
61 (newlist (list 'cons second first)))
62 (while rest
63 (setq newlist (list 'cons (car rest) newlist)
64 rest (cdr rest)))
65 newlist)
66 first))
67
68 (defalias 'backquote-list* (symbol-function 'backquote-list*-macro))
69
70 ;; A few advertised variables that control which symbols are used
71 ;; to represent the backquote, unquote, and splice operations.
72
73 (defvar backquote-backquote-symbol '\`
74 "*Symbol used to represent a backquote or nested backquote (e.g. `).")
75
76 (defvar backquote-unquote-symbol ',
77 "*Symbol used to represent an unquote (e.g. `,') inside a backquote.")
78
79 (defvar backquote-splice-symbol ',@
80 "*Symbol used to represent a splice (e.g. `,@') inside a backquote.")
81
82 ;;;###autoload
83 (defmacro backquote (arg)
84 "Argument STRUCTURE describes a template to build.
85
86 The whole structure acts as if it were quoted except for certain
87 places where expressions are evaluated and inserted or spliced in.
88
89 For example:
90
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
95
96 Vectors work just like lists. Nested backquotes are permitted."
97 (cdr (backquote-process arg)))
98
99 ;; GNU Emacs has no reader macros
100
101 ;;;###autoload
102 (defalias '\` (symbol-function 'backquote))
103
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.
108
109 (defun backquote-process (s)
110 (cond
111 ((vectorp s)
112 (let ((n (backquote-process (append s ()))))
113 (if (= (car n) 0)
114 (cons 0 s)
115 (cons 1 (cond
116 ((eq (nth 1 n) 'list)
117 (cons 'vector (nthcdr 2 n)))
118 ((eq (nth 1 n) 'append)
119 (cons 'vconcat (nthcdr 2 n)))
120 (t
121 (list 'apply '(function vector) (cdr n))))))))
122 ((atom s)
123 (cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
124 s
125 (list 'quote s))))
126 ((eq (car s) backquote-unquote-symbol)
127 (cons 1 (nth 1 s)))
128 ((eq (car s) backquote-splice-symbol)
129 (cons 2 (nth 1 s)))
130 ((eq (car s) backquote-backquote-symbol)
131 (backquote-process (cdr (backquote-process (nth 1 s)))))
132 (t
133 (let ((rest 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.
143 (while (consp rest)
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)))
148 (cond
149 ((= (car item) 2)
150 ;; Put the nonspliced items before the first spliced item
151 ;; into FIRSTLIST.
152 (if (null lists)
153 (setq firstlist list
154 list nil))
155 ;; Otherwise, put any preceding nonspliced items into LISTS.
156 (if list
157 (setq lists (cons (backquote-listify list '(0 . nil)) lists)))
158 (setq lists (cons (cdr item) lists))
159 (setq list nil))
160 (t
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).
165 (if (or rest list)
166 (setq lists (cons (backquote-listify list (backquote-process rest))
167 lists)))
168 ;; Turn LISTS into a form that produces the combined list.
169 (setq expression
170 (if (or (cdr lists)
171 (eq (car-safe (car lists)) backquote-splice-symbol))
172 (cons 'append (nreverse lists))
173 (car lists)))
174 ;; Tack on any initial elements.
175 (if firstlist
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))))))
180
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.
184
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)
189 old-tail nil))
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))))
196 (cond
197 (tail
198 (if (null old-tail)
199 (setq tail (list 'quote tail)))
200 (if heads
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))))
207 tail))
208 (t (cons 'list heads)))))
209
210 ;; backquote.el ends here