(pop-up-frame-function): Remove choice nil since it
[emacs.git] / lisp / emacs-lisp / backquote.el
blobbc3292085f3889a08681e971ef7e2b31197cd93d
1 ;;; backquote.el --- implement the ` Lisp construct
3 ;; Copyright (C) 1990, 1992, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Rick Sladkey <jrs@world.std.com>
7 ;; Maintainer: FSF
8 ;; Keywords: extensions, internal
10 ;; This file is part of GNU Emacs.
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 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; When the Lisp reader sees `(...), it generates (\` (...)).
28 ;; When it sees ,... inside such a backquote form, it generates (\, ...).
29 ;; For ,@... it generates (\,@ ...).
31 ;; This backquote will generate calls to the backquote-list* form.
32 ;; Both a function version and a macro version are included.
33 ;; The macro version is used by default because it is faster
34 ;; and needs no run-time support. It should really be a subr.
36 ;;; Code:
38 (provide 'backquote)
40 ;; function and macro versions of backquote-list*
42 (defun backquote-list*-function (first &rest list)
43 "Like `list' but the last argument is the tail of the new list.
45 For example (backquote-list* 'a 'b 'c) => (a b . c)"
46 ;; The recursive solution is much nicer:
47 ;; (if list (cons first (apply 'backquote-list*-function list)) first))
48 ;; but Emacs is not very good at efficiently processing recursion.
49 (if list
50 (let* ((rest list) (newlist (cons first nil)) (last newlist))
51 (while (cdr rest)
52 (setcdr last (cons (car rest) nil))
53 (setq last (cdr last)
54 rest (cdr rest)))
55 (setcdr last (car rest))
56 newlist)
57 first))
59 (defmacro backquote-list*-macro (first &rest list)
60 "Like `list' but the last argument is the tail of the new list.
62 For example (backquote-list* 'a 'b 'c) => (a b . c)"
63 ;; The recursive solution is much nicer:
64 ;; (if list (list 'cons first (cons 'backquote-list*-macro list)) first))
65 ;; but Emacs is not very good at efficiently processing such things.
66 (setq list (nreverse (cons first list))
67 first (car list)
68 list (cdr list))
69 (if list
70 (let* ((second (car list))
71 (rest (cdr list))
72 (newlist (list 'cons second first)))
73 (while rest
74 (setq newlist (list 'cons (car rest) newlist)
75 rest (cdr rest)))
76 newlist)
77 first))
79 (defalias 'backquote-list* (symbol-function 'backquote-list*-macro))
81 ;; A few advertised variables that control which symbols are used
82 ;; to represent the backquote, unquote, and splice operations.
83 (defconst backquote-backquote-symbol '\`
84 "Symbol used to represent a backquote or nested backquote.")
86 (defconst backquote-unquote-symbol '\,
87 "Symbol used to represent an unquote inside a backquote.")
89 (defconst backquote-splice-symbol '\,@
90 "Symbol used to represent a splice inside a backquote.")
92 (defmacro backquote (structure)
93 "Argument STRUCTURE describes a template to build.
95 The whole structure acts as if it were quoted except for certain
96 places where expressions are evaluated and inserted or spliced in.
98 For example:
100 b => (ba bb bc) ; assume b has this value
101 `(a b c) => (a b c) ; backquote acts like quote
102 `(a ,b c) => (a (ba bb bc) c) ; insert the value of b
103 `(a ,@b c) => (a ba bb bc c) ; splice in the value of b
105 Vectors work just like lists. Nested backquotes are permitted."
106 (cdr (backquote-process structure)))
108 ;; GNU Emacs has no reader macros
110 (defalias '\` (symbol-function 'backquote))
112 ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
113 ;; the backquote-processed structure. 0 => the structure is
114 ;; constant, 1 => to be unquoted, 2 => to be spliced in.
115 ;; The top-level backquote macro just discards the tag.
117 (defun backquote-delay-process (s level)
118 "Process a (un|back|splice)quote inside a backquote.
119 This simply recurses through the body."
120 (let ((exp (backquote-listify (list (cons 0 (list 'quote (car s))))
121 (backquote-process (cdr s) level))))
122 (if (eq (car-safe exp) 'quote)
123 (cons 0 (list 'quote s))
124 (cons 1 exp))))
126 (defun backquote-process (s &optional level)
127 "Process the body of a backquote.
128 S is the body. Returns a cons cell whose cdr is piece of code which
129 is the macro-expansion of S, and whose car is a small integer whose value
130 can either indicate that the code is constant (0), or not (1), or returns
131 a list which should be spliced into its environment (2).
132 LEVEL is only used internally and indicates the nesting level:
133 0 (the default) is for the toplevel nested inside a single backquote."
134 (unless level (setq level 0))
135 (cond
136 ((vectorp s)
137 (let ((n (backquote-process (append s ()) level)))
138 (if (= (car n) 0)
139 (cons 0 s)
140 (cons 1 (cond
141 ((not (listp (cdr n)))
142 (list 'vconcat (cdr n)))
143 ((eq (nth 1 n) 'list)
144 (cons 'vector (nthcdr 2 n)))
145 ((eq (nth 1 n) 'append)
146 (cons 'vconcat (nthcdr 2 n)))
148 (list 'apply '(function vector) (cdr n))))))))
149 ((atom s)
150 (cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
152 (list 'quote s))))
153 ((eq (car s) backquote-unquote-symbol)
154 (if (<= level 0)
155 (cons 1 (nth 1 s))
156 (backquote-delay-process s (1- level))))
157 ((eq (car s) backquote-splice-symbol)
158 (if (<= level 0)
159 (cons 2 (nth 1 s))
160 (backquote-delay-process s (1- level))))
161 ((eq (car s) backquote-backquote-symbol)
162 (backquote-delay-process s (1+ level)))
164 (let ((rest s)
165 item firstlist list lists expression)
166 ;; Scan this list-level, setting LISTS to a list of forms,
167 ;; each of which produces a list of elements
168 ;; that should go in this level.
169 ;; The order of LISTS is backwards.
170 ;; If there are non-splicing elements (constant or variable)
171 ;; at the beginning, put them in FIRSTLIST,
172 ;; as a list of tagged values (TAG . FORM).
173 ;; If there are any at the end, they go in LIST, likewise.
174 (while (and (consp rest)
175 ;; Stop if the cdr is an expression inside a backquote or
176 ;; unquote since this needs to go recursively through
177 ;; backquote-process.
178 (not (or (eq (car rest) backquote-unquote-symbol)
179 (eq (car rest) backquote-backquote-symbol))))
180 (setq item (backquote-process (car rest) level))
181 (cond
182 ((= (car item) 2)
183 ;; Put the nonspliced items before the first spliced item
184 ;; into FIRSTLIST.
185 (if (null lists)
186 (setq firstlist list
187 list nil))
188 ;; Otherwise, put any preceding nonspliced items into LISTS.
189 (if list
190 (push (backquote-listify list '(0 . nil)) lists))
191 (push (cdr item) lists)
192 (setq list nil))
194 (setq list (cons item list))))
195 (setq rest (cdr rest)))
196 ;; Handle nonsplicing final elements, and the tail of the list
197 ;; (which remains in REST).
198 (if (or rest list)
199 (push (backquote-listify list (backquote-process rest level))
200 lists))
201 ;; Turn LISTS into a form that produces the combined list.
202 (setq expression
203 (if (or (cdr lists)
204 (eq (car-safe (car lists)) backquote-splice-symbol))
205 (cons 'append (nreverse lists))
206 (car lists)))
207 ;; Tack on any initial elements.
208 (if firstlist
209 (setq expression (backquote-listify firstlist (cons 1 expression))))
210 (if (eq (car-safe expression) 'quote)
211 (cons 0 (list 'quote s))
212 (cons 1 expression))))))
214 ;; backquote-listify takes (tag . structure) pairs from backquote-process
215 ;; and decides between append, list, backquote-list*, and cons depending
216 ;; on which tags are in the list.
218 (defun backquote-listify (list old-tail)
219 (let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil))
220 (if (= (car old-tail) 0)
221 (setq tail (eval tail)
222 old-tail nil))
223 (while (consp list-tail)
224 (setq item (car list-tail))
225 (setq list-tail (cdr list-tail))
226 (if (or heads old-tail (/= (car item) 0))
227 (setq heads (cons (cdr item) heads))
228 (setq tail (cons (eval (cdr item)) tail))))
229 (cond
230 (tail
231 (if (null old-tail)
232 (setq tail (list 'quote tail)))
233 (if heads
234 (let ((use-list* (or (cdr heads)
235 (and (consp (car heads))
236 (eq (car (car heads))
237 backquote-splice-symbol)))))
238 (cons (if use-list* 'backquote-list* 'cons)
239 (append heads (list tail))))
240 tail))
241 (t (cons 'list heads)))))
243 ;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a
244 ;;; backquote.el ends here