Add 2010 to copyright years.
[bpt/emacs.git] / lisp / emacs-lisp / backquote.el
index 54fcfc3..998cee1 100644 (file)
@@ -1,7 +1,7 @@
 ;;; backquote.el --- implement the ` Lisp construct
 
 ;; Copyright (C) 1990, 1992, 1994, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007 Free Software Foundation, Inc.
+;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 
 ;; Author: Rick Sladkey <jrs@world.std.com>
 ;; Maintainer: FSF
@@ -9,10 +9,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; 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
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) 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
@@ -20,9 +20,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -85,14 +83,13 @@ For example (backquote-list* 'a 'b 'c) => (a b . c)"
 (defconst backquote-backquote-symbol '\`
   "Symbol used to represent a backquote or nested backquote.")
 
-(defconst backquote-unquote-symbol ',
+(defconst backquote-unquote-symbol '\,
   "Symbol used to represent an unquote inside a backquote.")
 
-(defconst backquote-splice-symbol ',@
+(defconst backquote-splice-symbol '\,@
   "Symbol used to represent a splice inside a backquote.")
 
-;;;###autoload
-(defmacro backquote (arg)
+(defmacro backquote (structure)
   "Argument STRUCTURE describes a template to build.
 
 The whole structure acts as if it were quoted except for certain
@@ -106,11 +103,10 @@ b              => (ba bb bc)              ; assume b has this value
 `(a ,@b c)     => (a ba bb bc c)       ; splice in the value of b
 
 Vectors work just like lists.  Nested backquotes are permitted."
-  (cdr (backquote-process arg)))
+  (cdr (backquote-process structure)))
 
 ;; GNU Emacs has no reader macros
 
-;;;###autoload
 (defalias '\` (symbol-function 'backquote))
 
 ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
@@ -118,10 +114,27 @@ Vectors work just like lists.  Nested backquotes are permitted."
 ;; constant, 1 => to be unquoted, 2 => to be spliced in.
 ;; The top-level backquote macro just discards the tag.
 
-(defun backquote-process (s)
+(defun backquote-delay-process (s level)
+  "Process a (un|back|splice)quote inside a backquote.
+This simply recurses through the body."
+  (let ((exp (backquote-listify (list (cons 0 (list 'quote (car s))))
+                                (backquote-process (cdr s) level))))
+    (if (eq (car-safe exp) 'quote)
+        (cons 0 (list 'quote s))
+      (cons 1 exp))))
+
+(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))
   (cond
    ((vectorp s)
-    (let ((n (backquote-process (append s ()))))
+    (let ((n (backquote-process (append s ()) level)))
       (if (= (car n) 0)
          (cons 0 s)
        (cons 1 (cond
@@ -138,11 +151,15 @@ Vectors work just like lists.  Nested backquotes are permitted."
                s
              (list 'quote s))))
    ((eq (car s) backquote-unquote-symbol)
-    (cons 1 (nth 1 s)))
+    (if (<= level 0)
+        (cons 1 (nth 1 s))
+      (backquote-delay-process s (1- level))))
    ((eq (car s) backquote-splice-symbol)
-    (cons 2 (nth 1 s)))
+    (if (<= level 0)
+        (cons 2 (nth 1 s))
+      (backquote-delay-process s (1- level))))
    ((eq (car s) backquote-backquote-symbol)
-    (backquote-process (cdr (backquote-process (nth 1 s)))))
+      (backquote-delay-process s (1+ level)))
    (t
     (let ((rest s)
          item firstlist list lists expression)
@@ -154,11 +171,13 @@ Vectors work just like lists.  Nested backquotes are permitted."
       ;; 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.
-      (while (consp rest)
-       ;; Turn . (, foo) into (,@ foo).
-       (if (eq (car rest) backquote-unquote-symbol)
-           (setq rest (list (list backquote-splice-symbol (nth 1 rest)))))
-       (setq item (backquote-process (car rest)))
+      (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))
        (cond
         ((= (car item) 2)
          ;; Put the nonspliced items before the first spliced item
@@ -168,8 +187,8 @@ Vectors work just like lists.  Nested backquotes are permitted."
                    list nil))
          ;; Otherwise, put any preceding nonspliced items into LISTS.
          (if list
-             (setq lists (cons (backquote-listify list '(0 . nil)) lists)))
-         (setq lists (cons (cdr item) lists))
+             (push (backquote-listify list '(0 . nil)) lists))
+         (push (cdr item) lists)
          (setq list nil))
         (t
          (setq list (cons item list))))
@@ -177,8 +196,8 @@ Vectors work just like lists.  Nested backquotes are permitted."
       ;; Handle nonsplicing final elements, and the tail of the list
       ;; (which remains in REST).
       (if (or rest list)
-         (setq lists (cons (backquote-listify list (backquote-process rest))
-                           lists)))
+         (push (backquote-listify list (backquote-process rest level))
+                lists))
       ;; Turn LISTS into a form that produces the combined list.
       (setq expression
            (if (or (cdr lists)
@@ -221,5 +240,5 @@ Vectors work just like lists.  Nested backquotes are permitted."
        tail))
      (t (cons 'list heads)))))
 
-;;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a
+;; arch-tag: 1a26206a-6b5e-4c56-8e24-2eef0f7e0e7a
 ;;; backquote.el ends here