Commit | Line | Data |
---|---|---|
b035a678 | 1 | ;;; backquote.el --- implement the ` Lisp construct |
b578f267 | 2 | |
6b61353c | 3 | ;; Copyright (C) 1990, 92, 1994, 2001, 2004 Free Software Foundation, Inc. |
c0274f38 | 4 | |
41ea659a RS |
5 | ;; Author: Rick Sladkey <jrs@world.std.com> |
6 | ;; Maintainer: FSF | |
7 | ;; Keywords: extensions, internal | |
9750e079 | 8 | |
be010748 | 9 | ;; This file is part of GNU Emacs. |
c440e42b RS |
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 | |
7c938215 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
c440e42b RS |
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 | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
c440e42b | 25 | |
be010748 | 26 | ;;; Commentary: |
41ea659a | 27 | |
574cb02e RS |
28 | ;; When the Lisp reader sees `(...), it generates (\` (...)). |
29 | ;; When it sees ,... inside such a backquote form, it generates (\, ...). | |
30 | ;; For ,@... it generates (\,@ ...). | |
31 | ||
288f95bd | 32 | ;; This backquote will generate calls to the backquote-list* form. |
41ea659a RS |
33 | ;; Both a function version and a macro version are included. |
34 | ;; The macro version is used by default because it is faster | |
35 | ;; and needs no run-time support. It should really be a subr. | |
c440e42b | 36 | |
e5167999 | 37 | ;;; Code: |
c440e42b | 38 | |
49116ac0 JB |
39 | (provide 'backquote) |
40 | ||
288f95bd | 41 | ;; function and macro versions of backquote-list* |
41ea659a | 42 | |
288f95bd | 43 | (defun backquote-list*-function (first &rest list) |
41ea659a RS |
44 | "Like `list' but the last argument is the tail of the new list. |
45 | ||
288f95bd | 46 | For example (backquote-list* 'a 'b 'c) => (a b . c)" |
6b61353c KH |
47 | ;; The recursive solution is much nicer: |
48 | ;; (if list (cons first (apply 'backquote-list*-function list)) first)) | |
49 | ;; but Emacs is not very good at efficiently processing recursion. | |
41ea659a RS |
50 | (if list |
51 | (let* ((rest list) (newlist (cons first nil)) (last newlist)) | |
52 | (while (cdr rest) | |
53 | (setcdr last (cons (car rest) nil)) | |
54 | (setq last (cdr last) | |
55 | rest (cdr rest))) | |
56 | (setcdr last (car rest)) | |
57 | newlist) | |
58 | first)) | |
59 | ||
288f95bd RS |
60 | (defmacro backquote-list*-macro (first &rest list) |
61 | "Like `list' but the last argument is the tail of the new list. | |
41ea659a | 62 | |
288f95bd | 63 | For example (backquote-list* 'a 'b 'c) => (a b . c)" |
6b61353c KH |
64 | ;; The recursive solution is much nicer: |
65 | ;; (if list (list 'cons first (cons 'backquote-list*-macro list)) first)) | |
66 | ;; but Emacs is not very good at efficiently processing such things. | |
67 | (setq list (nreverse (cons first list)) | |
41ea659a RS |
68 | first (car list) |
69 | list (cdr list)) | |
70 | (if list | |
71 | (let* ((second (car list)) | |
72 | (rest (cdr list)) | |
73 | (newlist (list 'cons second first))) | |
74 | (while rest | |
75 | (setq newlist (list 'cons (car rest) newlist) | |
76 | rest (cdr rest))) | |
77 | newlist) | |
78 | first)) | |
79 | ||
c9c7f2c4 | 80 | (defalias 'backquote-list* (symbol-function 'backquote-list*-macro)) |
41ea659a RS |
81 | |
82 | ;; A few advertised variables that control which symbols are used | |
83 | ;; to represent the backquote, unquote, and splice operations. | |
4c548238 | 84 | (defconst backquote-backquote-symbol '\` |
cdab3e50 | 85 | "Symbol used to represent a backquote or nested backquote.") |
4c548238 DL |
86 | |
87 | (defconst backquote-unquote-symbol ', | |
cdab3e50 | 88 | "Symbol used to represent an unquote inside a backquote.") |
4c548238 DL |
89 | |
90 | (defconst backquote-splice-symbol ',@ | |
cdab3e50 | 91 | "Symbol used to represent a splice inside a backquote.") |
41ea659a | 92 | |
325e3af2 | 93 | ;;;###autoload |
41ea659a RS |
94 | (defmacro backquote (arg) |
95 | "Argument STRUCTURE describes a template to build. | |
96 | ||
97 | The whole structure acts as if it were quoted except for certain | |
98 | places where expressions are evaluated and inserted or spliced in. | |
99 | ||
100 | For example: | |
101 | ||
1df90f8f RS |
102 | b => (ba bb bc) ; assume b has this value |
103 | `(a b c) => (a b c) ; backquote acts like quote | |
2b1c5e12 RS |
104 | `(a ,b c) => (a (ba bb bc) c) ; insert the value of b |
105 | `(a ,@b c) => (a ba bb bc c) ; splice in the value of b | |
41ea659a | 106 | |
288f95bd RS |
107 | Vectors work just like lists. Nested backquotes are permitted." |
108 | (cdr (backquote-process arg))) | |
41ea659a RS |
109 | |
110 | ;; GNU Emacs has no reader macros | |
111 | ||
325e3af2 | 112 | ;;;###autoload |
1df90f8f | 113 | (defalias '\` (symbol-function 'backquote)) |
41ea659a | 114 | |
288f95bd | 115 | ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and |
41ea659a RS |
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 | ||
288f95bd | 120 | (defun backquote-process (s) |
41ea659a RS |
121 | (cond |
122 | ((vectorp s) | |
288f95bd | 123 | (let ((n (backquote-process (append s ())))) |
41ea659a RS |
124 | (if (= (car n) 0) |
125 | (cons 0 s) | |
126 | (cons 1 (cond | |
ea4a56de RS |
127 | ((not (listp (cdr n))) |
128 | (list 'vconcat (cdr n))) | |
41ea659a RS |
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) | |
288f95bd | 144 | (backquote-process (cdr (backquote-process (nth 1 s))))) |
41ea659a | 145 | (t |
cd320f32 RS |
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. | |
a1506d29 | 151 | ;; The order of LISTS is backwards. |
cd320f32 RS |
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. | |
41ea659a | 156 | (while (consp rest) |
cd320f32 | 157 | ;; Turn . (, foo) into (,@ foo). |
41ea659a RS |
158 | (if (eq (car rest) backquote-unquote-symbol) |
159 | (setq rest (list (list backquote-splice-symbol (nth 1 rest))))) | |
288f95bd | 160 | (setq item (backquote-process (car rest))) |
41ea659a RS |
161 | (cond |
162 | ((= (car item) 2) | |
1d08cb50 RS |
163 | ;; Put the nonspliced items before the first spliced item |
164 | ;; into FIRSTLIST. | |
165 | (if (null lists) | |
41ea659a RS |
166 | (setq firstlist list |
167 | list nil)) | |
1d08cb50 | 168 | ;; Otherwise, put any preceding nonspliced items into LISTS. |
41ea659a | 169 | (if list |
288f95bd | 170 | (setq lists (cons (backquote-listify list '(0 . nil)) lists))) |
41ea659a RS |
171 | (setq lists (cons (cdr item) lists)) |
172 | (setq list nil)) | |
173 | (t | |
174 | (setq list (cons item list)))) | |
175 | (setq rest (cdr rest))) | |
cd320f32 RS |
176 | ;; Handle nonsplicing final elements, and the tail of the list |
177 | ;; (which remains in REST). | |
41ea659a | 178 | (if (or rest list) |
288f95bd RS |
179 | (setq lists (cons (backquote-listify list (backquote-process rest)) |
180 | lists))) | |
a1506d29 | 181 | ;; Turn LISTS into a form that produces the combined list. |
cd320f32 | 182 | (setq expression |
41ea659a | 183 | (if (or (cdr lists) |
cd320f32 | 184 | (eq (car-safe (car lists)) backquote-splice-symbol)) |
41ea659a RS |
185 | (cons 'append (nreverse lists)) |
186 | (car lists))) | |
cd320f32 | 187 | ;; Tack on any initial elements. |
41ea659a | 188 | (if firstlist |
cd320f32 RS |
189 | (setq expression (backquote-listify firstlist (cons 1 expression)))) |
190 | (if (eq (car-safe expression) 'quote) | |
41ea659a | 191 | (cons 0 (list 'quote s)) |
cd320f32 | 192 | (cons 1 expression)))))) |
41ea659a | 193 | |
288f95bd RS |
194 | ;; backquote-listify takes (tag . structure) pairs from backquote-process |
195 | ;; and decides between append, list, backquote-list*, and cons depending | |
41ea659a RS |
196 | ;; on which tags are in the list. |
197 | ||
288f95bd | 198 | (defun backquote-listify (list old-tail) |
41ea659a RS |
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))))) | |
288f95bd | 218 | (cons (if use-list* 'backquote-list* 'cons) |
41ea659a RS |
219 | (append heads (list tail)))) |
220 | tail)) | |
221 | (t (cons 'list heads))))) | |
222 | ||
6b61353c | 223 | ;;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a |
55535639 | 224 | ;;; backquote.el ends here |