1;;; backquote.el --- implement the ` Lisp construct
2
3;; Copyright (C) 1990, 1992, 1994, 2001, 2002, 2003, 2004,
4;;   2005, 2006, 2007 Free Software Foundation, Inc.
5
6;; Author: Rick Sladkey <jrs@world.std.com>
7;; Maintainer: FSF
8;; Keywords: extensions, internal
9
10;; This file is part of GNU Emacs.
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
14;; the Free Software Foundation; either version 2, or (at your option)
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
23;; along with GNU Emacs; see the file COPYING.  If not, write to the
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
26
27;;; Commentary:
28
29;; When the Lisp reader sees `(...), it generates (\` (...)).
30;; When it sees ,... inside such a backquote form, it generates (\, ...).
31;; For ,@... it generates (\,@ ...).
32
33;; This backquote will generate calls to the backquote-list* form.
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.
37
38;;; Code:
39
40(provide 'backquote)
41
42;; function and macro versions of backquote-list*
43
44(defun backquote-list*-function (first &rest list)
45  "Like `list' but the last argument is the tail of the new list.
46
47For example (backquote-list* 'a 'b 'c) => (a b . c)"
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.
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
61(defmacro backquote-list*-macro (first &rest list)
62  "Like `list' but the last argument is the tail of the new list.
63
64For example (backquote-list* 'a 'b 'c) => (a b . c)"
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))
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(defalias '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(defconst backquote-backquote-symbol '\`
86  "Symbol used to represent a backquote or nested backquote.")
87
88(defconst backquote-unquote-symbol ',
89  "Symbol used to represent an unquote inside a backquote.")
90
91(defconst backquote-splice-symbol ',@
92  "Symbol used to represent a splice inside a backquote.")
93
94;;;###autoload
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
103b              => (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
108Vectors work just like lists.  Nested backquotes are permitted."
109  (cdr (backquote-process arg)))
110
111;; GNU Emacs has no reader macros
112
113;;;###autoload
114(defalias '\` (symbol-function 'backquote))
115
116;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
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
121(defun backquote-process (s)
122  (cond
123   ((vectorp s)
124    (let ((n (backquote-process (append s ()))))
125      (if (= (car n) 0)
126	  (cons 0 s)
127	(cons 1 (cond
128		 ((not (listp (cdr n)))
129		  (list 'vconcat (cdr n)))
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)
145    (backquote-process (cdr (backquote-process (nth 1 s)))))
146   (t
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.
152      ;; The order of LISTS is backwards.
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.
157      (while (consp rest)
158	;; Turn . (, foo) into (,@ foo).
159	(if (eq (car rest) backquote-unquote-symbol)
160	    (setq rest (list (list backquote-splice-symbol (nth 1 rest)))))
161	(setq item (backquote-process (car rest)))
162	(cond
163	 ((= (car item) 2)
164	  ;; Put the nonspliced items before the first spliced item
165	  ;; into FIRSTLIST.
166	  (if (null lists)
167	      (setq firstlist list
168		    list nil))
169	  ;; Otherwise, put any preceding nonspliced items into LISTS.
170	  (if list
171	      (setq lists (cons (backquote-listify list '(0 . nil)) lists)))
172	  (setq lists (cons (cdr item) lists))
173	  (setq list nil))
174	 (t
175	  (setq list (cons item list))))
176	(setq rest (cdr rest)))
177      ;; Handle nonsplicing final elements, and the tail of the list
178      ;; (which remains in REST).
179      (if (or rest list)
180	  (setq lists (cons (backquote-listify list (backquote-process rest))
181			    lists)))
182      ;; Turn LISTS into a form that produces the combined list.
183      (setq expression
184	    (if (or (cdr lists)
185		    (eq (car-safe (car lists)) backquote-splice-symbol))
186		(cons 'append (nreverse lists))
187	      (car lists)))
188      ;; Tack on any initial elements.
189      (if firstlist
190	  (setq expression (backquote-listify firstlist (cons 1 expression))))
191      (if (eq (car-safe expression) 'quote)
192	  (cons 0 (list 'quote s))
193	(cons 1 expression))))))
194
195;; backquote-listify takes (tag . structure) pairs from backquote-process
196;; and decides between append, list, backquote-list*, and cons depending
197;; on which tags are in the list.
198
199(defun backquote-listify (list old-tail)
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)))))
219	    (cons (if use-list* 'backquote-list* 'cons)
220		  (append heads (list tail))))
221	tail))
222     (t (cons 'list heads)))))
223
224;;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a
225;;; backquote.el ends here
226