(rmail-make-basic-summary-line): Show 14 chars before @ and 11 after.
[bpt/emacs.git] / lisp / emacs-lisp / backquote.el
CommitLineData
41ea659a 1;;; New backquote for GNU Emacs.
d733c5ec 2;;; Copyright (C) 1990, 1992, 1994 Free Software Foundation, Inc.
c0274f38 3
41ea659a
RS
4;; Author: Rick Sladkey <jrs@world.std.com>
5;; Maintainer: FSF
6;; Keywords: extensions, internal
9750e079 7
41ea659a
RS
8;; This file is not part of GNU Emacs but is distributed under
9;; the same conditions as 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
41ea659a 13;; the Free Software Foundation; either version 1, 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
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
41ea659a
RS
25;; This is a new backquote for GNU Emacs written by
26;; Rick Sladkey <jrs@world.std.com>. It has the following
27;; features compared to the version 18 backquote:
28
29;; Correctly handles nested backquotes.
30;; Correctly handles constants after a splice.
31;; Correctly handles top-level atoms and unquotes.
32;; Correctly handles unquote after dot.
33;; Understands vectors.
34;; Minimizes gratuitous consing.
35;; Faster operation with simpler semantics.
36;; Generates faster run-time expressions.
37;; One third fewer calories than our regular beer.
38
288f95bd 39;; This backquote will generate calls to the backquote-list* form.
41ea659a
RS
40;; Both a function version and a macro version are included.
41;; The macro version is used by default because it is faster
42;; and needs no run-time support. It should really be a subr.
c440e42b 43
e5167999 44;;; Code:
c440e42b 45
49116ac0
JB
46(provide 'backquote)
47
288f95bd 48;; function and macro versions of backquote-list*
41ea659a 49
288f95bd 50(defun backquote-list*-function (first &rest list)
41ea659a
RS
51 "Like `list' but the last argument is the tail of the new list.
52
288f95bd 53For example (backquote-list* 'a 'b 'c) => (a b . c)"
41ea659a
RS
54 (if list
55 (let* ((rest list) (newlist (cons first nil)) (last newlist))
56 (while (cdr rest)
57 (setcdr last (cons (car rest) nil))
58 (setq last (cdr last)
59 rest (cdr rest)))
60 (setcdr last (car rest))
61 newlist)
62 first))
63
288f95bd
RS
64(defmacro backquote-list*-macro (first &rest list)
65 "Like `list' but the last argument is the tail of the new list.
41ea659a 66
288f95bd 67For example (backquote-list* 'a 'b 'c) => (a b . c)"
41ea659a
RS
68 (setq list (reverse (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
288f95bd 81(fset 'backquote-list* (symbol-function 'backquote-list*-macro))
41ea659a
RS
82
83;; A few advertised variables that control which symbols are used
84;; to represent the backquote, unquote, and splice operations.
85
325e3af2 86;;;###autoload
41ea659a
RS
87(defvar backquote-backquote-symbol '`
88 "*Symbol used to represent a backquote or nested backquote (e.g. `).")
89
90(defvar backquote-unquote-symbol ',
288f95bd 91 "*Symbol used to represent an unquote (e.g. `,') inside a backquote.")
41ea659a
RS
92
93(defvar backquote-splice-symbol ',@
288f95bd 94 "*Symbol used to represent a splice (e.g. `,'@) inside a backquote.")
41ea659a 95
325e3af2 96;;;###autoload
41ea659a
RS
97(defmacro backquote (arg)
98 "Argument STRUCTURE describes a template to build.
99
100The whole structure acts as if it were quoted except for certain
101places where expressions are evaluated and inserted or spliced in.
102
103For example:
104
105b => (ba bb bc) ; assume b has this value
106\(` (a b c)) => (a b c) ; backquote acts like quote
107\(` (a (, b) c)) => (a (ba bb bc) c) ; insert the value of b
108\(` (a (,@ b) c)) => (a ba bb bc c) ; splice in the value of b
109
288f95bd
RS
110Vectors work just like lists. Nested backquotes are permitted."
111 (cdr (backquote-process arg)))
41ea659a
RS
112
113;; GNU Emacs has no reader macros
114
325e3af2 115;;;###autoload
41ea659a
RS
116(fset backquote-backquote-symbol (symbol-function 'backquote))
117
288f95bd 118;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
41ea659a
RS
119;; the backquote-processed structure. 0 => the structure is
120;; constant, 1 => to be unquoted, 2 => to be spliced in.
121;; The top-level backquote macro just discards the tag.
122
288f95bd 123(defun backquote-process (s)
41ea659a
RS
124 (cond
125 ((vectorp s)
288f95bd 126 (let ((n (backquote-process (append s ()))))
41ea659a
RS
127 (if (= (car n) 0)
128 (cons 0 s)
129 (cons 1 (cond
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)
288f95bd 145 (backquote-process (cdr (backquote-process (nth 1 s)))))
41ea659a 146 (t
cd320f32
RS
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.
41ea659a 157 (while (consp rest)
cd320f32 158 ;; Turn . (, foo) into (,@ foo).
41ea659a
RS
159 (if (eq (car rest) backquote-unquote-symbol)
160 (setq rest (list (list backquote-splice-symbol (nth 1 rest)))))
288f95bd 161 (setq item (backquote-process (car rest)))
41ea659a
RS
162 (cond
163 ((= (car item) 2)
164 (if (null firstlist)
165 (setq firstlist list
166 list nil))
167 (if list
288f95bd 168 (setq lists (cons (backquote-listify list '(0 . nil)) lists)))
41ea659a
RS
169 (setq lists (cons (cdr item) lists))
170 (setq list nil))
171 (t
172 (setq list (cons item list))))
173 (setq rest (cdr rest)))
cd320f32
RS
174 ;; Handle nonsplicing final elements, and the tail of the list
175 ;; (which remains in REST).
41ea659a 176 (if (or rest list)
288f95bd
RS
177 (setq lists (cons (backquote-listify list (backquote-process rest))
178 lists)))
cd320f32
RS
179 ;; Turn LISTS into a form that produces the combined list.
180 (setq expression
41ea659a 181 (if (or (cdr lists)
cd320f32 182 (eq (car-safe (car lists)) backquote-splice-symbol))
41ea659a
RS
183 (cons 'append (nreverse lists))
184 (car lists)))
cd320f32 185 ;; Tack on any initial elements.
41ea659a 186 (if firstlist
cd320f32
RS
187 (setq expression (backquote-listify firstlist (cons 1 expression))))
188 (if (eq (car-safe expression) 'quote)
41ea659a 189 (cons 0 (list 'quote s))
cd320f32 190 (cons 1 expression))))))
41ea659a 191
288f95bd
RS
192;; backquote-listify takes (tag . structure) pairs from backquote-process
193;; and decides between append, list, backquote-list*, and cons depending
41ea659a
RS
194;; on which tags are in the list.
195
288f95bd 196(defun backquote-listify (list old-tail)
41ea659a
RS
197 (let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil))
198 (if (= (car old-tail) 0)
199 (setq tail (eval tail)
200 old-tail nil))
201 (while (consp list-tail)
202 (setq item (car list-tail))
203 (setq list-tail (cdr list-tail))
204 (if (or heads old-tail (/= (car item) 0))
205 (setq heads (cons (cdr item) heads))
206 (setq tail (cons (eval (cdr item)) tail))))
207 (cond
208 (tail
209 (if (null old-tail)
210 (setq tail (list 'quote tail)))
211 (if heads
212 (let ((use-list* (or (cdr heads)
213 (and (consp (car heads))
214 (eq (car (car heads))
215 backquote-splice-symbol)))))
288f95bd 216 (cons (if use-list* 'backquote-list* 'cons)
41ea659a
RS
217 (append heads (list tail))))
218 tail))
219 (t (cons 'list heads)))))
220
221;; backquote.el ends here