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