(vc-bzr-registered): Use \0 instead of literal NULs.
[bpt/emacs.git] / lisp / emacs-lisp / backquote.el
CommitLineData
b035a678 1;;; backquote.el --- implement the ` Lisp construct
b578f267 2
3731a850 3;; Copyright (C) 1990, 1992, 1994, 2001, 2002, 2003, 2004,
f0fa15c5 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
c0274f38 5
41ea659a
RS
6;; Author: Rick Sladkey <jrs@world.std.com>
7;; Maintainer: FSF
8;; Keywords: extensions, internal
9750e079 9
be010748 10;; This file is part of GNU Emacs.
c440e42b
RS
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
e0085d62 14;; the Free Software Foundation; either version 3, or (at your option)
c440e42b
RS
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267 23;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
c440e42b 26
be010748 27;;; Commentary:
41ea659a 28
574cb02e
RS
29;; When the Lisp reader sees `(...), it generates (\` (...)).
30;; When it sees ,... inside such a backquote form, it generates (\, ...).
31;; For ,@... it generates (\,@ ...).
32
288f95bd 33;; This backquote will generate calls to the backquote-list* form.
41ea659a
RS
34;; Both a function version and a macro version are included.
35;; The macro version is used by default because it is faster
36;; and needs no run-time support. It should really be a subr.
c440e42b 37
e5167999 38;;; Code:
c440e42b 39
49116ac0
JB
40(provide 'backquote)
41
288f95bd 42;; function and macro versions of backquote-list*
41ea659a 43
288f95bd 44(defun backquote-list*-function (first &rest list)
41ea659a
RS
45 "Like `list' but the last argument is the tail of the new list.
46
288f95bd 47For example (backquote-list* 'a 'b 'c) => (a b . c)"
1de9630d
SM
48 ;; The recursive solution is much nicer:
49 ;; (if list (cons first (apply 'backquote-list*-function list)) first))
50 ;; but Emacs is not very good at efficiently processing recursion.
41ea659a
RS
51 (if list
52 (let* ((rest list) (newlist (cons first nil)) (last newlist))
53 (while (cdr rest)
54 (setcdr last (cons (car rest) nil))
55 (setq last (cdr last)
56 rest (cdr rest)))
57 (setcdr last (car rest))
58 newlist)
59 first))
60
288f95bd
RS
61(defmacro backquote-list*-macro (first &rest list)
62 "Like `list' but the last argument is the tail of the new list.
41ea659a 63
288f95bd 64For example (backquote-list* 'a 'b 'c) => (a b . c)"
1de9630d
SM
65 ;; The recursive solution is much nicer:
66 ;; (if list (list 'cons first (cons 'backquote-list*-macro list)) first))
67 ;; but Emacs is not very good at efficiently processing such things.
68 (setq list (nreverse (cons first list))
41ea659a
RS
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
c9c7f2c4 81(defalias 'backquote-list* (symbol-function 'backquote-list*-macro))
41ea659a
RS
82
83;; A few advertised variables that control which symbols are used
84;; to represent the backquote, unquote, and splice operations.
4c548238 85(defconst backquote-backquote-symbol '\`
cdab3e50 86 "Symbol used to represent a backquote or nested backquote.")
4c548238
DL
87
88(defconst backquote-unquote-symbol ',
cdab3e50 89 "Symbol used to represent an unquote inside a backquote.")
4c548238
DL
90
91(defconst backquote-splice-symbol ',@
cdab3e50 92 "Symbol used to represent a splice inside a backquote.")
41ea659a 93
325e3af2 94;;;###autoload
41ea659a
RS
95(defmacro backquote (arg)
96 "Argument STRUCTURE describes a template to build.
97
98The whole structure acts as if it were quoted except for certain
99places where expressions are evaluated and inserted or spliced in.
100
101For example:
102
1df90f8f
RS
103b => (ba bb bc) ; assume b has this value
104`(a b c) => (a b c) ; backquote acts like quote
2b1c5e12
RS
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
41ea659a 107
288f95bd
RS
108Vectors work just like lists. Nested backquotes are permitted."
109 (cdr (backquote-process arg)))
41ea659a
RS
110
111;; GNU Emacs has no reader macros
112
325e3af2 113;;;###autoload
1df90f8f 114(defalias '\` (symbol-function 'backquote))
41ea659a 115
288f95bd 116;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
41ea659a
RS
117;; the backquote-processed structure. 0 => the structure is
118;; constant, 1 => to be unquoted, 2 => to be spliced in.
119;; The top-level backquote macro just discards the tag.
120
288f95bd 121(defun backquote-process (s)
41ea659a
RS
122 (cond
123 ((vectorp s)
288f95bd 124 (let ((n (backquote-process (append s ()))))
41ea659a
RS
125 (if (= (car n) 0)
126 (cons 0 s)
127 (cons 1 (cond
ea4a56de
RS
128 ((not (listp (cdr n)))
129 (list 'vconcat (cdr n)))
41ea659a
RS
130 ((eq (nth 1 n) 'list)
131 (cons 'vector (nthcdr 2 n)))
132 ((eq (nth 1 n) 'append)
133 (cons 'vconcat (nthcdr 2 n)))
134 (t
135 (list 'apply '(function vector) (cdr n))))))))
136 ((atom s)
137 (cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
138 s
139 (list 'quote s))))
140 ((eq (car s) backquote-unquote-symbol)
141 (cons 1 (nth 1 s)))
142 ((eq (car s) backquote-splice-symbol)
143 (cons 2 (nth 1 s)))
144 ((eq (car s) backquote-backquote-symbol)
288f95bd 145 (backquote-process (cdr (backquote-process (nth 1 s)))))
41ea659a 146 (t
cd320f32
RS
147 (let ((rest s)
148 item firstlist list lists expression)
149 ;; Scan this list-level, setting LISTS to a list of forms,
150 ;; each of which produces a list of elements
151 ;; that should go in this level.
a1506d29 152 ;; The order of LISTS is backwards.
cd320f32
RS
153 ;; If there are non-splicing elements (constant or variable)
154 ;; at the beginning, put them in FIRSTLIST,
155 ;; as a list of tagged values (TAG . FORM).
156 ;; If there are any at the end, they go in LIST, likewise.
41ea659a 157 (while (consp rest)
cd320f32 158 ;; Turn . (, foo) into (,@ foo).
41ea659a
RS
159 (if (eq (car rest) backquote-unquote-symbol)
160 (setq rest (list (list backquote-splice-symbol (nth 1 rest)))))
288f95bd 161 (setq item (backquote-process (car rest)))
41ea659a
RS
162 (cond
163 ((= (car item) 2)
1d08cb50
RS
164 ;; Put the nonspliced items before the first spliced item
165 ;; into FIRSTLIST.
166 (if (null lists)
41ea659a
RS
167 (setq firstlist list
168 list nil))
1d08cb50 169 ;; Otherwise, put any preceding nonspliced items into LISTS.
41ea659a 170 (if list
288f95bd 171 (setq lists (cons (backquote-listify list '(0 . nil)) lists)))
41ea659a
RS
172 (setq lists (cons (cdr item) lists))
173 (setq list nil))
174 (t
175 (setq list (cons item list))))
176 (setq rest (cdr rest)))
cd320f32
RS
177 ;; Handle nonsplicing final elements, and the tail of the list
178 ;; (which remains in REST).
41ea659a 179 (if (or rest list)
288f95bd
RS
180 (setq lists (cons (backquote-listify list (backquote-process rest))
181 lists)))
a1506d29 182 ;; Turn LISTS into a form that produces the combined list.
cd320f32 183 (setq expression
41ea659a 184 (if (or (cdr lists)
cd320f32 185 (eq (car-safe (car lists)) backquote-splice-symbol))
41ea659a
RS
186 (cons 'append (nreverse lists))
187 (car lists)))
cd320f32 188 ;; Tack on any initial elements.
41ea659a 189 (if firstlist
cd320f32
RS
190 (setq expression (backquote-listify firstlist (cons 1 expression))))
191 (if (eq (car-safe expression) 'quote)
41ea659a 192 (cons 0 (list 'quote s))
cd320f32 193 (cons 1 expression))))))
41ea659a 194
288f95bd
RS
195;; backquote-listify takes (tag . structure) pairs from backquote-process
196;; and decides between append, list, backquote-list*, and cons depending
41ea659a
RS
197;; on which tags are in the list.
198
288f95bd 199(defun backquote-listify (list old-tail)
41ea659a
RS
200 (let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil))
201 (if (= (car old-tail) 0)
202 (setq tail (eval tail)
203 old-tail nil))
204 (while (consp list-tail)
205 (setq item (car list-tail))
206 (setq list-tail (cdr list-tail))
207 (if (or heads old-tail (/= (car item) 0))
208 (setq heads (cons (cdr item) heads))
209 (setq tail (cons (eval (cdr item)) tail))))
210 (cond
211 (tail
212 (if (null old-tail)
213 (setq tail (list 'quote tail)))
214 (if heads
215 (let ((use-list* (or (cdr heads)
216 (and (consp (car heads))
217 (eq (car (car heads))
218 backquote-splice-symbol)))))
288f95bd 219 (cons (if use-list* 'backquote-list* 'cons)
41ea659a
RS
220 (append heads (list tail))))
221 tail))
222 (t (cons 'list heads)))))
223
ab5796a9 224;;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a
55535639 225;;; backquote.el ends here