1996-05-20 21:07:10 +00:00
|
|
|
;;; backquote.el --- implement the ` Lisp construct
|
1996-01-14 07:34:30 +00:00
|
|
|
|
2005-08-06 17:48:15 +00:00
|
|
|
;; Copyright (C) 1990, 1992, 1994, 2001, 2002, 2003, 2004,
|
2007-01-21 02:48:43 +00:00
|
|
|
;; 2005, 2006, 2007 Free Software Foundation, Inc.
|
1992-05-30 23:54:21 +00:00
|
|
|
|
1994-03-06 19:39:10 +00:00
|
|
|
;; Author: Rick Sladkey <jrs@world.std.com>
|
|
|
|
;; Maintainer: FSF
|
|
|
|
;; Keywords: extensions, internal
|
1992-07-22 04:22:30 +00:00
|
|
|
|
1995-10-30 17:35:01 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
1991-01-31 21:18:19 +00:00
|
|
|
|
|
|
|
;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
1995-06-15 20:42:24 +00:00
|
|
|
;; the Free Software Foundation; either version 2, or (at your option)
|
1991-01-31 21:18:19 +00:00
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
1996-01-14 07:34:30 +00:00
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
2005-07-04 17:55:18 +00:00
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
1991-01-31 21:18:19 +00:00
|
|
|
|
1995-10-30 17:35:01 +00:00
|
|
|
;;; Commentary:
|
1994-03-06 19:39:10 +00:00
|
|
|
|
2002-11-06 05:07:33 +00:00
|
|
|
;; When the Lisp reader sees `(...), it generates (\` (...)).
|
|
|
|
;; When it sees ,... inside such a backquote form, it generates (\, ...).
|
|
|
|
;; For ,@... it generates (\,@ ...).
|
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
;; This backquote will generate calls to the backquote-list* form.
|
1994-03-06 19:39:10 +00:00
|
|
|
;; Both a function version and a macro version are included.
|
|
|
|
;; The macro version is used by default because it is faster
|
|
|
|
;; and needs no run-time support. It should really be a subr.
|
1991-01-31 21:18:19 +00:00
|
|
|
|
1992-07-16 21:47:34 +00:00
|
|
|
;;; Code:
|
1991-01-31 21:18:19 +00:00
|
|
|
|
1992-03-16 20:39:07 +00:00
|
|
|
(provide 'backquote)
|
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
;; function and macro versions of backquote-list*
|
1994-03-06 19:39:10 +00:00
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
(defun backquote-list*-function (first &rest list)
|
1994-03-06 19:39:10 +00:00
|
|
|
"Like `list' but the last argument is the tail of the new list.
|
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
For example (backquote-list* 'a 'b 'c) => (a b . c)"
|
2004-03-22 15:17:01 +00:00
|
|
|
;; The recursive solution is much nicer:
|
|
|
|
;; (if list (cons first (apply 'backquote-list*-function list)) first))
|
|
|
|
;; but Emacs is not very good at efficiently processing recursion.
|
1994-03-06 19:39:10 +00:00
|
|
|
(if list
|
|
|
|
(let* ((rest list) (newlist (cons first nil)) (last newlist))
|
|
|
|
(while (cdr rest)
|
|
|
|
(setcdr last (cons (car rest) nil))
|
|
|
|
(setq last (cdr last)
|
|
|
|
rest (cdr rest)))
|
|
|
|
(setcdr last (car rest))
|
|
|
|
newlist)
|
|
|
|
first))
|
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
(defmacro backquote-list*-macro (first &rest list)
|
|
|
|
"Like `list' but the last argument is the tail of the new list.
|
1994-03-06 19:39:10 +00:00
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
For example (backquote-list* 'a 'b 'c) => (a b . c)"
|
2004-03-22 15:17:01 +00:00
|
|
|
;; The recursive solution is much nicer:
|
|
|
|
;; (if list (list 'cons first (cons 'backquote-list*-macro list)) first))
|
|
|
|
;; but Emacs is not very good at efficiently processing such things.
|
|
|
|
(setq list (nreverse (cons first list))
|
1994-03-06 19:39:10 +00:00
|
|
|
first (car list)
|
|
|
|
list (cdr list))
|
|
|
|
(if list
|
|
|
|
(let* ((second (car list))
|
|
|
|
(rest (cdr list))
|
|
|
|
(newlist (list 'cons second first)))
|
|
|
|
(while rest
|
|
|
|
(setq newlist (list 'cons (car rest) newlist)
|
|
|
|
rest (cdr rest)))
|
|
|
|
newlist)
|
|
|
|
first))
|
|
|
|
|
1994-05-07 00:18:35 +00:00
|
|
|
(defalias 'backquote-list* (symbol-function 'backquote-list*-macro))
|
1994-03-06 19:39:10 +00:00
|
|
|
|
|
|
|
;; A few advertised variables that control which symbols are used
|
|
|
|
;; to represent the backquote, unquote, and splice operations.
|
2000-01-08 15:19:21 +00:00
|
|
|
(defconst backquote-backquote-symbol '\`
|
2001-09-03 07:56:39 +00:00
|
|
|
"Symbol used to represent a backquote or nested backquote.")
|
2000-01-08 15:19:21 +00:00
|
|
|
|
|
|
|
(defconst backquote-unquote-symbol ',
|
2001-09-03 07:56:39 +00:00
|
|
|
"Symbol used to represent an unquote inside a backquote.")
|
2000-01-08 15:19:21 +00:00
|
|
|
|
|
|
|
(defconst backquote-splice-symbol ',@
|
2001-09-03 07:56:39 +00:00
|
|
|
"Symbol used to represent a splice inside a backquote.")
|
1994-03-06 19:39:10 +00:00
|
|
|
|
1994-05-03 23:36:52 +00:00
|
|
|
;;;###autoload
|
1994-03-06 19:39:10 +00:00
|
|
|
(defmacro backquote (arg)
|
|
|
|
"Argument STRUCTURE describes a template to build.
|
|
|
|
|
|
|
|
The whole structure acts as if it were quoted except for certain
|
|
|
|
places where expressions are evaluated and inserted or spliced in.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
1995-05-04 20:44:55 +00:00
|
|
|
b => (ba bb bc) ; assume b has this value
|
|
|
|
`(a b c) => (a b c) ; backquote acts like quote
|
1995-07-19 03:42:12 +00:00
|
|
|
`(a ,b c) => (a (ba bb bc) c) ; insert the value of b
|
|
|
|
`(a ,@b c) => (a ba bb bc c) ; splice in the value of b
|
1994-03-06 19:39:10 +00:00
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
Vectors work just like lists. Nested backquotes are permitted."
|
|
|
|
(cdr (backquote-process arg)))
|
1994-03-06 19:39:10 +00:00
|
|
|
|
|
|
|
;; GNU Emacs has no reader macros
|
|
|
|
|
1994-05-03 23:36:52 +00:00
|
|
|
;;;###autoload
|
1995-05-04 20:44:55 +00:00
|
|
|
(defalias '\` (symbol-function 'backquote))
|
1994-03-06 19:39:10 +00:00
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
|
1994-03-06 19:39:10 +00:00
|
|
|
;; the backquote-processed structure. 0 => the structure is
|
|
|
|
;; constant, 1 => to be unquoted, 2 => to be spliced in.
|
|
|
|
;; The top-level backquote macro just discards the tag.
|
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
(defun backquote-process (s)
|
1994-03-06 19:39:10 +00:00
|
|
|
(cond
|
|
|
|
((vectorp s)
|
1994-03-06 19:43:23 +00:00
|
|
|
(let ((n (backquote-process (append s ()))))
|
1994-03-06 19:39:10 +00:00
|
|
|
(if (= (car n) 0)
|
|
|
|
(cons 0 s)
|
|
|
|
(cons 1 (cond
|
2001-06-13 15:30:35 +00:00
|
|
|
((not (listp (cdr n)))
|
|
|
|
(list 'vconcat (cdr n)))
|
1994-03-06 19:39:10 +00:00
|
|
|
((eq (nth 1 n) 'list)
|
|
|
|
(cons 'vector (nthcdr 2 n)))
|
|
|
|
((eq (nth 1 n) 'append)
|
|
|
|
(cons 'vconcat (nthcdr 2 n)))
|
|
|
|
(t
|
|
|
|
(list 'apply '(function vector) (cdr n))))))))
|
|
|
|
((atom s)
|
|
|
|
(cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
|
|
|
|
s
|
|
|
|
(list 'quote s))))
|
|
|
|
((eq (car s) backquote-unquote-symbol)
|
|
|
|
(cons 1 (nth 1 s)))
|
|
|
|
((eq (car s) backquote-splice-symbol)
|
|
|
|
(cons 2 (nth 1 s)))
|
|
|
|
((eq (car s) backquote-backquote-symbol)
|
1994-03-06 19:43:23 +00:00
|
|
|
(backquote-process (cdr (backquote-process (nth 1 s)))))
|
1994-03-06 19:39:10 +00:00
|
|
|
(t
|
1994-04-12 17:45:53 +00:00
|
|
|
(let ((rest s)
|
|
|
|
item firstlist list lists expression)
|
|
|
|
;; Scan this list-level, setting LISTS to a list of forms,
|
|
|
|
;; each of which produces a list of elements
|
|
|
|
;; that should go in this level.
|
2003-02-04 13:24:35 +00:00
|
|
|
;; The order of LISTS is backwards.
|
1994-04-12 17:45:53 +00:00
|
|
|
;; If there are non-splicing elements (constant or variable)
|
|
|
|
;; at the beginning, put them in FIRSTLIST,
|
|
|
|
;; as a list of tagged values (TAG . FORM).
|
|
|
|
;; If there are any at the end, they go in LIST, likewise.
|
1994-03-06 19:39:10 +00:00
|
|
|
(while (consp rest)
|
1994-04-12 17:45:53 +00:00
|
|
|
;; Turn . (, foo) into (,@ foo).
|
1994-03-06 19:39:10 +00:00
|
|
|
(if (eq (car rest) backquote-unquote-symbol)
|
|
|
|
(setq rest (list (list backquote-splice-symbol (nth 1 rest)))))
|
1994-03-06 19:43:23 +00:00
|
|
|
(setq item (backquote-process (car rest)))
|
1994-03-06 19:39:10 +00:00
|
|
|
(cond
|
|
|
|
((= (car item) 2)
|
1994-06-22 15:30:42 +00:00
|
|
|
;; Put the nonspliced items before the first spliced item
|
|
|
|
;; into FIRSTLIST.
|
|
|
|
(if (null lists)
|
1994-03-06 19:39:10 +00:00
|
|
|
(setq firstlist list
|
|
|
|
list nil))
|
1994-06-22 15:30:42 +00:00
|
|
|
;; Otherwise, put any preceding nonspliced items into LISTS.
|
1994-03-06 19:39:10 +00:00
|
|
|
(if list
|
1994-03-06 19:43:23 +00:00
|
|
|
(setq lists (cons (backquote-listify list '(0 . nil)) lists)))
|
1994-03-06 19:39:10 +00:00
|
|
|
(setq lists (cons (cdr item) lists))
|
|
|
|
(setq list nil))
|
|
|
|
(t
|
|
|
|
(setq list (cons item list))))
|
|
|
|
(setq rest (cdr rest)))
|
1994-04-12 17:45:53 +00:00
|
|
|
;; Handle nonsplicing final elements, and the tail of the list
|
|
|
|
;; (which remains in REST).
|
1994-03-06 19:39:10 +00:00
|
|
|
(if (or rest list)
|
1994-03-06 19:43:23 +00:00
|
|
|
(setq lists (cons (backquote-listify list (backquote-process rest))
|
|
|
|
lists)))
|
2003-02-04 13:24:35 +00:00
|
|
|
;; Turn LISTS into a form that produces the combined list.
|
1994-04-12 17:45:53 +00:00
|
|
|
(setq expression
|
1994-03-06 19:39:10 +00:00
|
|
|
(if (or (cdr lists)
|
1994-04-12 17:45:53 +00:00
|
|
|
(eq (car-safe (car lists)) backquote-splice-symbol))
|
1994-03-06 19:39:10 +00:00
|
|
|
(cons 'append (nreverse lists))
|
|
|
|
(car lists)))
|
1994-04-12 17:45:53 +00:00
|
|
|
;; Tack on any initial elements.
|
1994-03-06 19:39:10 +00:00
|
|
|
(if firstlist
|
1994-04-12 17:45:53 +00:00
|
|
|
(setq expression (backquote-listify firstlist (cons 1 expression))))
|
|
|
|
(if (eq (car-safe expression) 'quote)
|
1994-03-06 19:39:10 +00:00
|
|
|
(cons 0 (list 'quote s))
|
1994-04-12 17:45:53 +00:00
|
|
|
(cons 1 expression))))))
|
1994-03-06 19:39:10 +00:00
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
;; backquote-listify takes (tag . structure) pairs from backquote-process
|
|
|
|
;; and decides between append, list, backquote-list*, and cons depending
|
1994-03-06 19:39:10 +00:00
|
|
|
;; on which tags are in the list.
|
|
|
|
|
1994-03-06 19:43:23 +00:00
|
|
|
(defun backquote-listify (list old-tail)
|
1994-03-06 19:39:10 +00:00
|
|
|
(let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil))
|
|
|
|
(if (= (car old-tail) 0)
|
|
|
|
(setq tail (eval tail)
|
|
|
|
old-tail nil))
|
|
|
|
(while (consp list-tail)
|
|
|
|
(setq item (car list-tail))
|
|
|
|
(setq list-tail (cdr list-tail))
|
|
|
|
(if (or heads old-tail (/= (car item) 0))
|
|
|
|
(setq heads (cons (cdr item) heads))
|
|
|
|
(setq tail (cons (eval (cdr item)) tail))))
|
|
|
|
(cond
|
|
|
|
(tail
|
|
|
|
(if (null old-tail)
|
|
|
|
(setq tail (list 'quote tail)))
|
|
|
|
(if heads
|
|
|
|
(let ((use-list* (or (cdr heads)
|
|
|
|
(and (consp (car heads))
|
|
|
|
(eq (car (car heads))
|
|
|
|
backquote-splice-symbol)))))
|
1994-03-06 19:43:23 +00:00
|
|
|
(cons (if use-list* 'backquote-list* 'cons)
|
1994-03-06 19:39:10 +00:00
|
|
|
(append heads (list tail))))
|
|
|
|
tail))
|
|
|
|
(t (cons 'list heads)))))
|
|
|
|
|
2003-09-01 15:45:59 +00:00
|
|
|
;;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a
|
2001-07-15 16:15:35 +00:00
|
|
|
;;; backquote.el ends here
|