Commit | Line | Data |
---|---|---|
be010748 | 1 | ;;; backquote.el -- implement the ` Lisp construct |
b578f267 | 2 | |
d733c5ec | 3 | ;;; Copyright (C) 1990, 1992, 1994 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 | |
288f95bd | 28 | ;; This backquote will generate calls to the backquote-list* form. |
41ea659a RS |
29 | ;; Both a function version and a macro version are included. |
30 | ;; The macro version is used by default because it is faster | |
31 | ;; and needs no run-time support. It should really be a subr. | |
c440e42b | 32 | |
e5167999 | 33 | ;;; Code: |
c440e42b | 34 | |
49116ac0 JB |
35 | (provide 'backquote) |
36 | ||
288f95bd | 37 | ;; function and macro versions of backquote-list* |
41ea659a | 38 | |
288f95bd | 39 | (defun backquote-list*-function (first &rest list) |
41ea659a RS |
40 | "Like `list' but the last argument is the tail of the new list. |
41 | ||
288f95bd | 42 | For example (backquote-list* 'a 'b 'c) => (a b . c)" |
41ea659a RS |
43 | (if list |
44 | (let* ((rest list) (newlist (cons first nil)) (last newlist)) | |
45 | (while (cdr rest) | |
46 | (setcdr last (cons (car rest) nil)) | |
47 | (setq last (cdr last) | |
48 | rest (cdr rest))) | |
49 | (setcdr last (car rest)) | |
50 | newlist) | |
51 | first)) | |
52 | ||
288f95bd RS |
53 | (defmacro backquote-list*-macro (first &rest list) |
54 | "Like `list' but the last argument is the tail of the new list. | |
41ea659a | 55 | |
288f95bd | 56 | For example (backquote-list* 'a 'b 'c) => (a b . c)" |
41ea659a RS |
57 | (setq list (reverse (cons first list)) |
58 | first (car list) | |
59 | list (cdr list)) | |
60 | (if list | |
61 | (let* ((second (car list)) | |
62 | (rest (cdr list)) | |
63 | (newlist (list 'cons second first))) | |
64 | (while rest | |
65 | (setq newlist (list 'cons (car rest) newlist) | |
66 | rest (cdr rest))) | |
67 | newlist) | |
68 | first)) | |
69 | ||
c9c7f2c4 | 70 | (defalias 'backquote-list* (symbol-function 'backquote-list*-macro)) |
41ea659a RS |
71 | |
72 | ;; A few advertised variables that control which symbols are used | |
73 | ;; to represent the backquote, unquote, and splice operations. | |
74 | ||
1df90f8f | 75 | (defvar backquote-backquote-symbol '\` |
41ea659a RS |
76 | "*Symbol used to represent a backquote or nested backquote (e.g. `).") |
77 | ||
78 | (defvar backquote-unquote-symbol ', | |
288f95bd | 79 | "*Symbol used to represent an unquote (e.g. `,') inside a backquote.") |
41ea659a RS |
80 | |
81 | (defvar backquote-splice-symbol ',@ | |
44c48a83 | 82 | "*Symbol used to represent a splice (e.g. `,@') inside a backquote.") |
41ea659a | 83 | |
325e3af2 | 84 | ;;;###autoload |
41ea659a RS |
85 | (defmacro backquote (arg) |
86 | "Argument STRUCTURE describes a template to build. | |
87 | ||
88 | The whole structure acts as if it were quoted except for certain | |
89 | places where expressions are evaluated and inserted or spliced in. | |
90 | ||
91 | For example: | |
92 | ||
1df90f8f RS |
93 | b => (ba bb bc) ; assume b has this value |
94 | `(a b c) => (a b c) ; backquote acts like quote | |
2b1c5e12 RS |
95 | `(a ,b c) => (a (ba bb bc) c) ; insert the value of b |
96 | `(a ,@b c) => (a ba bb bc c) ; splice in the value of b | |
41ea659a | 97 | |
288f95bd RS |
98 | Vectors work just like lists. Nested backquotes are permitted." |
99 | (cdr (backquote-process arg))) | |
41ea659a RS |
100 | |
101 | ;; GNU Emacs has no reader macros | |
102 | ||
325e3af2 | 103 | ;;;###autoload |
1df90f8f | 104 | (defalias '\` (symbol-function 'backquote)) |
41ea659a | 105 | |
288f95bd | 106 | ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and |
41ea659a RS |
107 | ;; the backquote-processed structure. 0 => the structure is |
108 | ;; constant, 1 => to be unquoted, 2 => to be spliced in. | |
109 | ;; The top-level backquote macro just discards the tag. | |
110 | ||
288f95bd | 111 | (defun backquote-process (s) |
41ea659a RS |
112 | (cond |
113 | ((vectorp s) | |
288f95bd | 114 | (let ((n (backquote-process (append s ())))) |
41ea659a RS |
115 | (if (= (car n) 0) |
116 | (cons 0 s) | |
117 | (cons 1 (cond | |
118 | ((eq (nth 1 n) 'list) | |
119 | (cons 'vector (nthcdr 2 n))) | |
120 | ((eq (nth 1 n) 'append) | |
121 | (cons 'vconcat (nthcdr 2 n))) | |
122 | (t | |
123 | (list 'apply '(function vector) (cdr n)))))))) | |
124 | ((atom s) | |
125 | (cons 0 (if (or (null s) (eq s t) (not (symbolp s))) | |
126 | s | |
127 | (list 'quote s)))) | |
128 | ((eq (car s) backquote-unquote-symbol) | |
129 | (cons 1 (nth 1 s))) | |
130 | ((eq (car s) backquote-splice-symbol) | |
131 | (cons 2 (nth 1 s))) | |
132 | ((eq (car s) backquote-backquote-symbol) | |
288f95bd | 133 | (backquote-process (cdr (backquote-process (nth 1 s))))) |
41ea659a | 134 | (t |
cd320f32 RS |
135 | (let ((rest s) |
136 | item firstlist list lists expression) | |
137 | ;; Scan this list-level, setting LISTS to a list of forms, | |
138 | ;; each of which produces a list of elements | |
139 | ;; that should go in this level. | |
140 | ;; The order of LISTS is backwards. | |
141 | ;; If there are non-splicing elements (constant or variable) | |
142 | ;; at the beginning, put them in FIRSTLIST, | |
143 | ;; as a list of tagged values (TAG . FORM). | |
144 | ;; If there are any at the end, they go in LIST, likewise. | |
41ea659a | 145 | (while (consp rest) |
cd320f32 | 146 | ;; Turn . (, foo) into (,@ foo). |
41ea659a RS |
147 | (if (eq (car rest) backquote-unquote-symbol) |
148 | (setq rest (list (list backquote-splice-symbol (nth 1 rest))))) | |
288f95bd | 149 | (setq item (backquote-process (car rest))) |
41ea659a RS |
150 | (cond |
151 | ((= (car item) 2) | |
1d08cb50 RS |
152 | ;; Put the nonspliced items before the first spliced item |
153 | ;; into FIRSTLIST. | |
154 | (if (null lists) | |
41ea659a RS |
155 | (setq firstlist list |
156 | list nil)) | |
1d08cb50 | 157 | ;; Otherwise, put any preceding nonspliced items into LISTS. |
41ea659a | 158 | (if list |
288f95bd | 159 | (setq lists (cons (backquote-listify list '(0 . nil)) lists))) |
41ea659a RS |
160 | (setq lists (cons (cdr item) lists)) |
161 | (setq list nil)) | |
162 | (t | |
163 | (setq list (cons item list)))) | |
164 | (setq rest (cdr rest))) | |
cd320f32 RS |
165 | ;; Handle nonsplicing final elements, and the tail of the list |
166 | ;; (which remains in REST). | |
41ea659a | 167 | (if (or rest list) |
288f95bd RS |
168 | (setq lists (cons (backquote-listify list (backquote-process rest)) |
169 | lists))) | |
cd320f32 RS |
170 | ;; Turn LISTS into a form that produces the combined list. |
171 | (setq expression | |
41ea659a | 172 | (if (or (cdr lists) |
cd320f32 | 173 | (eq (car-safe (car lists)) backquote-splice-symbol)) |
41ea659a RS |
174 | (cons 'append (nreverse lists)) |
175 | (car lists))) | |
cd320f32 | 176 | ;; Tack on any initial elements. |
41ea659a | 177 | (if firstlist |
cd320f32 RS |
178 | (setq expression (backquote-listify firstlist (cons 1 expression)))) |
179 | (if (eq (car-safe expression) 'quote) | |
41ea659a | 180 | (cons 0 (list 'quote s)) |
cd320f32 | 181 | (cons 1 expression)))))) |
41ea659a | 182 | |
288f95bd RS |
183 | ;; backquote-listify takes (tag . structure) pairs from backquote-process |
184 | ;; and decides between append, list, backquote-list*, and cons depending | |
41ea659a RS |
185 | ;; on which tags are in the list. |
186 | ||
288f95bd | 187 | (defun backquote-listify (list old-tail) |
41ea659a RS |
188 | (let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil)) |
189 | (if (= (car old-tail) 0) | |
190 | (setq tail (eval tail) | |
191 | old-tail nil)) | |
192 | (while (consp list-tail) | |
193 | (setq item (car list-tail)) | |
194 | (setq list-tail (cdr list-tail)) | |
195 | (if (or heads old-tail (/= (car item) 0)) | |
196 | (setq heads (cons (cdr item) heads)) | |
197 | (setq tail (cons (eval (cdr item)) tail)))) | |
198 | (cond | |
199 | (tail | |
200 | (if (null old-tail) | |
201 | (setq tail (list 'quote tail))) | |
202 | (if heads | |
203 | (let ((use-list* (or (cdr heads) | |
204 | (and (consp (car heads)) | |
205 | (eq (car (car heads)) | |
206 | backquote-splice-symbol))))) | |
288f95bd | 207 | (cons (if use-list* 'backquote-list* 'cons) |
41ea659a RS |
208 | (append heads (list tail)))) |
209 | tail)) | |
210 | (t (cons 'list heads))))) | |
211 | ||
212 | ;; backquote.el ends here |