* nsterm.m (ns_draw_window_cursor): Draw BAR_CURSOR correct for R2L.
[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,
114f9c96 4;; 2005, 2006, 2007, 2008, 2009, 2010 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 11
d6cba7ae 12;; GNU Emacs is free software: you can redistribute it and/or modify
c440e42b 13;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
c440e42b
RS
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
d6cba7ae 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c440e42b 24
be010748 25;;; Commentary:
41ea659a 26
574cb02e
RS
27;; When the Lisp reader sees `(...), it generates (\` (...)).
28;; When it sees ,... inside such a backquote form, it generates (\, ...).
29;; For ,@... it generates (\,@ ...).
30
288f95bd 31;; This backquote will generate calls to the backquote-list* form.
41ea659a
RS
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.
c440e42b 35
e5167999 36;;; Code:
c440e42b 37
49116ac0
JB
38(provide 'backquote)
39
288f95bd 40;; function and macro versions of backquote-list*
41ea659a 41
288f95bd 42(defun backquote-list*-function (first &rest list)
41ea659a
RS
43 "Like `list' but the last argument is the tail of the new list.
44
288f95bd 45For example (backquote-list* 'a 'b 'c) => (a b . c)"
1de9630d
SM
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.
41ea659a
RS
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))
58
288f95bd
RS
59(defmacro backquote-list*-macro (first &rest list)
60 "Like `list' but the last argument is the tail of the new list.
41ea659a 61
288f95bd 62For example (backquote-list* 'a 'b 'c) => (a b . c)"
1de9630d
SM
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))
41ea659a
RS
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))
78
c9c7f2c4 79(defalias 'backquote-list* (symbol-function 'backquote-list*-macro))
41ea659a
RS
80
81;; A few advertised variables that control which symbols are used
82;; to represent the backquote, unquote, and splice operations.
4c548238 83(defconst backquote-backquote-symbol '\`
cdab3e50 84 "Symbol used to represent a backquote or nested backquote.")
4c548238 85
727fb8cc 86(defconst backquote-unquote-symbol '\,
cdab3e50 87 "Symbol used to represent an unquote inside a backquote.")
4c548238 88
727fb8cc 89(defconst backquote-splice-symbol '\,@
cdab3e50 90 "Symbol used to represent a splice inside a backquote.")
41ea659a 91
82bb5643 92(defmacro backquote (structure)
41ea659a
RS
93 "Argument STRUCTURE describes a template to build.
94
95The whole structure acts as if it were quoted except for certain
96places where expressions are evaluated and inserted or spliced in.
97
98For example:
99
1df90f8f
RS
100b => (ba bb bc) ; assume b has this value
101`(a b c) => (a b c) ; backquote acts like quote
2b1c5e12
RS
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
41ea659a 104
288f95bd 105Vectors work just like lists. Nested backquotes are permitted."
82bb5643 106 (cdr (backquote-process structure)))
41ea659a
RS
107
108;; GNU Emacs has no reader macros
109
1df90f8f 110(defalias '\` (symbol-function 'backquote))
41ea659a 111
288f95bd 112;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
41ea659a
RS
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.
116
3527bdcc
SM
117(defun backquote-delay-process (s level)
118 "Process a (un|back|splice)quote inside a backquote.
119This simply recurses through the body."
5d3440f4
SM
120 (let ((exp (backquote-listify (list (cons 0 (list 'quote (car s))))
121 (backquote-process (cdr s) level))))
3527bdcc
SM
122 (if (eq (car-safe exp) 'quote)
123 (cons 0 (list 'quote s))
124 (cons 1 exp))))
125
126(defun backquote-process (s &optional level)
127 "Process the body of a backquote.
128S is the body. Returns a cons cell whose cdr is piece of code which
129is the macro-expansion of S, and whose car is a small integer whose value
130can either indicate that the code is constant (0), or not (1), or returns
131a list which should be spliced into its environment (2).
132LEVEL is only used internally and indicates the nesting level:
1330 (the default) is for the toplevel nested inside a single backquote."
134 (unless level (setq level 0))
41ea659a
RS
135 (cond
136 ((vectorp s)
3527bdcc 137 (let ((n (backquote-process (append s ()) level)))
41ea659a
RS
138 (if (= (car n) 0)
139 (cons 0 s)
140 (cons 1 (cond
ea4a56de
RS
141 ((not (listp (cdr n)))
142 (list 'vconcat (cdr n)))
41ea659a
RS
143 ((eq (nth 1 n) 'list)
144 (cons 'vector (nthcdr 2 n)))
145 ((eq (nth 1 n) 'append)
146 (cons 'vconcat (nthcdr 2 n)))
147 (t
148 (list 'apply '(function vector) (cdr n))))))))
149 ((atom s)
150 (cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
151 s
152 (list 'quote s))))
153 ((eq (car s) backquote-unquote-symbol)
3527bdcc
SM
154 (if (<= level 0)
155 (cons 1 (nth 1 s))
156 (backquote-delay-process s (1- level))))
41ea659a 157 ((eq (car s) backquote-splice-symbol)
3527bdcc
SM
158 (if (<= level 0)
159 (cons 2 (nth 1 s))
160 (backquote-delay-process s (1- level))))
41ea659a 161 ((eq (car s) backquote-backquote-symbol)
3527bdcc 162 (backquote-delay-process s (1+ level)))
41ea659a 163 (t
cd320f32
RS
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.
a1506d29 169 ;; The order of LISTS is backwards.
cd320f32
RS
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.
3527bdcc
SM
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))
41ea659a
RS
181 (cond
182 ((= (car item) 2)
1d08cb50
RS
183 ;; Put the nonspliced items before the first spliced item
184 ;; into FIRSTLIST.
185 (if (null lists)
41ea659a
RS
186 (setq firstlist list
187 list nil))
1d08cb50 188 ;; Otherwise, put any preceding nonspliced items into LISTS.
41ea659a 189 (if list
3527bdcc
SM
190 (push (backquote-listify list '(0 . nil)) lists))
191 (push (cdr item) lists)
41ea659a
RS
192 (setq list nil))
193 (t
194 (setq list (cons item list))))
195 (setq rest (cdr rest)))
cd320f32
RS
196 ;; Handle nonsplicing final elements, and the tail of the list
197 ;; (which remains in REST).
41ea659a 198 (if (or rest list)
3527bdcc
SM
199 (push (backquote-listify list (backquote-process rest level))
200 lists))
a1506d29 201 ;; Turn LISTS into a form that produces the combined list.
cd320f32 202 (setq expression
41ea659a 203 (if (or (cdr lists)
cd320f32 204 (eq (car-safe (car lists)) backquote-splice-symbol))
41ea659a
RS
205 (cons 'append (nreverse lists))
206 (car lists)))
cd320f32 207 ;; Tack on any initial elements.
41ea659a 208 (if firstlist
cd320f32
RS
209 (setq expression (backquote-listify firstlist (cons 1 expression))))
210 (if (eq (car-safe expression) 'quote)
41ea659a 211 (cons 0 (list 'quote s))
cd320f32 212 (cons 1 expression))))))
41ea659a 213
288f95bd
RS
214;; backquote-listify takes (tag . structure) pairs from backquote-process
215;; and decides between append, list, backquote-list*, and cons depending
41ea659a
RS
216;; on which tags are in the list.
217
288f95bd 218(defun backquote-listify (list old-tail)
41ea659a
RS
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)))))
288f95bd 238 (cons (if use-list* 'backquote-list* 'cons)
41ea659a
RS
239 (append heads (list tail))))
240 tail))
241 (t (cons 'list heads)))))
242
3527bdcc 243;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a
55535639 244;;; backquote.el ends here