1996-05-20 21:07:10 +00:00
|
|
|
|
;;; backquote.el --- implement the ` Lisp construct
|
1996-01-14 07:34:30 +00:00
|
|
|
|
|
2018-01-01 00:21:42 -08:00
|
|
|
|
;; Copyright (C) 1990, 1992, 1994, 2001-2018 Free Software Foundation,
|
2013-01-01 09:11:05 +00:00
|
|
|
|
;; Inc.
|
1992-05-30 23:54:21 +00:00
|
|
|
|
|
1994-03-06 19:39:10 +00:00
|
|
|
|
;; Author: Rick Sladkey <jrs@world.std.com>
|
2014-02-09 17:34:22 -08:00
|
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
1994-03-06 19:39:10 +00:00
|
|
|
|
;; Keywords: extensions, internal
|
2010-08-29 12:17:13 -04:00
|
|
|
|
;; Package: emacs
|
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
|
|
|
|
|
2008-05-06 03:21:21 +00:00
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1991-01-31 21:18:19 +00:00
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 03:21:21 +00:00
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
1991-01-31 21:18:19 +00:00
|
|
|
|
|
|
|
|
|
;; 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
|
2017-09-13 15:52:52 -07:00
|
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
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.
|
|
|
|
|
|
2015-11-17 15:28:50 -08: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
|
|
|
|
|
2015-11-17 15:28:50 -08: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
|
|
|
|
|
2007-08-23 18:39:20 +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
|
|
|
|
|
2007-08-23 18:39:20 +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
|
|
|
|
|
2007-11-17 02:49:49 +00:00
|
|
|
|
(defmacro backquote (structure)
|
1994-03-06 19:39:10 +00:00
|
|
|
|
"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
|
2015-05-28 00:44:32 -07:00
|
|
|
|
\\=`(a b c) => (a b c) ; backquote acts like quote
|
|
|
|
|
\\=`(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."
|
2007-11-17 02:49:49 +00:00
|
|
|
|
(cdr (backquote-process structure)))
|
1994-03-06 19:39:10 +00:00
|
|
|
|
|
|
|
|
|
;; GNU Emacs has no reader macros
|
|
|
|
|
|
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.
|
|
|
|
|
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(defun backquote-delay-process (s level)
|
|
|
|
|
"Process a (un|back|splice)quote inside a backquote.
|
|
|
|
|
This simply recurses through the body."
|
2007-08-21 19:09:25 +00:00
|
|
|
|
(let ((exp (backquote-listify (list (cons 0 (list 'quote (car s))))
|
|
|
|
|
(backquote-process (cdr s) level))))
|
2015-01-30 16:00:29 -05:00
|
|
|
|
(cons (if (eq (car-safe exp) 'quote) 0 1) exp)))
|
2007-08-10 06:52:30 +00:00
|
|
|
|
|
|
|
|
|
(defun backquote-process (s &optional level)
|
|
|
|
|
"Process the body of a backquote.
|
|
|
|
|
S is the body. Returns a cons cell whose cdr is piece of code which
|
|
|
|
|
is the macro-expansion of S, and whose car is a small integer whose value
|
|
|
|
|
can either indicate that the code is constant (0), or not (1), or returns
|
|
|
|
|
a list which should be spliced into its environment (2).
|
|
|
|
|
LEVEL is only used internally and indicates the nesting level:
|
|
|
|
|
0 (the default) is for the toplevel nested inside a single backquote."
|
|
|
|
|
(unless level (setq level 0))
|
1994-03-06 19:39:10 +00:00
|
|
|
|
(cond
|
|
|
|
|
((vectorp s)
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(let ((n (backquote-process (append s ()) level)))
|
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)
|
2014-11-15 23:59:50 -05:00
|
|
|
|
;; FIXME: Use macroexp-quote!
|
1994-03-06 19:39:10 +00:00
|
|
|
|
(cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
|
|
|
|
|
s
|
|
|
|
|
(list 'quote s))))
|
|
|
|
|
((eq (car s) backquote-unquote-symbol)
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(if (<= level 0)
|
2014-11-15 23:59:50 -05:00
|
|
|
|
(cond
|
|
|
|
|
((> (length s) 2)
|
|
|
|
|
;; We could support it with: (cons 2 `(list . ,(cdr s)))
|
|
|
|
|
;; But let's not encourage such uses.
|
|
|
|
|
(error "Multiple args to , are not supported: %S" s))
|
|
|
|
|
(t (cons (if (eq (car-safe (nth 1 s)) 'quote) 0 1)
|
|
|
|
|
(nth 1 s))))
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(backquote-delay-process s (1- level))))
|
1994-03-06 19:39:10 +00:00
|
|
|
|
((eq (car s) backquote-splice-symbol)
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(if (<= level 0)
|
2013-10-08 00:30:31 -04:00
|
|
|
|
(if (> (length s) 2)
|
|
|
|
|
;; (cons 2 `(append . ,(cdr s)))
|
|
|
|
|
(error "Multiple args to ,@ are not supported: %S" s)
|
|
|
|
|
(cons 2 (nth 1 s)))
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(backquote-delay-process s (1- level))))
|
1994-03-06 19:39:10 +00:00
|
|
|
|
((eq (car s) backquote-backquote-symbol)
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(backquote-delay-process s (1+ level)))
|
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.
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(while (and (consp rest)
|
|
|
|
|
;; Stop if the cdr is an expression inside a backquote or
|
|
|
|
|
;; unquote since this needs to go recursively through
|
|
|
|
|
;; backquote-process.
|
|
|
|
|
(not (or (eq (car rest) backquote-unquote-symbol)
|
|
|
|
|
(eq (car rest) backquote-backquote-symbol))))
|
|
|
|
|
(setq item (backquote-process (car rest) level))
|
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
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(push (backquote-listify list '(0 . nil)) lists))
|
|
|
|
|
(push (cdr item) lists)
|
1994-03-06 19:39:10 +00:00
|
|
|
|
(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)
|
2007-08-10 06:52:30 +00:00
|
|
|
|
(push (backquote-listify list (backquote-process rest level))
|
|
|
|
|
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))))
|
2014-11-15 23:59:50 -05:00
|
|
|
|
(cons (if (eq (car-safe expression) 'quote) 0 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)))))
|
|
|
|
|
|
2017-01-23 19:00:49 +00:00
|
|
|
|
|
|
|
|
|
;; Give `,' and `,@' documentation strings which can be examined by C-h f.
|
|
|
|
|
(put '\, 'function-documentation
|
|
|
|
|
"See `\\=`' (also `pcase') for the usage of `,'.")
|
|
|
|
|
(put '\, 'reader-construct t)
|
|
|
|
|
|
|
|
|
|
(put '\,@ 'function-documentation
|
|
|
|
|
"See `\\=`' for the usage of `,@'.")
|
|
|
|
|
(put '\,@ 'reader-construct t)
|
|
|
|
|
|
2001-07-15 16:15:35 +00:00
|
|
|
|
;;; backquote.el ends here
|