backquote.el 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ;;; backquote.el --- implement the ` Lisp construct
  2. ;; Copyright (C) 1990, 1992, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Rick Sladkey <jrs@world.std.com>
  4. ;; Maintainer: FSF
  5. ;; Keywords: extensions, internal
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; When the Lisp reader sees `(...), it generates (\` (...)).
  20. ;; When it sees ,... inside such a backquote form, it generates (\, ...).
  21. ;; For ,@... it generates (\,@ ...).
  22. ;; This backquote will generate calls to the backquote-list* form.
  23. ;; Both a function version and a macro version are included.
  24. ;; The macro version is used by default because it is faster
  25. ;; and needs no run-time support. It should really be a subr.
  26. ;;; Code:
  27. (provide 'backquote)
  28. ;; function and macro versions of backquote-list*
  29. (defun backquote-list*-function (first &rest list)
  30. "Like `list' but the last argument is the tail of the new list.
  31. For example (backquote-list* 'a 'b 'c) => (a b . c)"
  32. ;; The recursive solution is much nicer:
  33. ;; (if list (cons first (apply 'backquote-list*-function list)) first))
  34. ;; but Emacs is not very good at efficiently processing recursion.
  35. (if list
  36. (let* ((rest list) (newlist (cons first nil)) (last newlist))
  37. (while (cdr rest)
  38. (setcdr last (cons (car rest) nil))
  39. (setq last (cdr last)
  40. rest (cdr rest)))
  41. (setcdr last (car rest))
  42. newlist)
  43. first))
  44. (defmacro backquote-list*-macro (first &rest list)
  45. "Like `list' but the last argument is the tail of the new list.
  46. For example (backquote-list* 'a 'b 'c) => (a b . c)"
  47. ;; The recursive solution is much nicer:
  48. ;; (if list (list 'cons first (cons 'backquote-list*-macro list)) first))
  49. ;; but Emacs is not very good at efficiently processing such things.
  50. (setq list (nreverse (cons first list))
  51. first (car list)
  52. list (cdr list))
  53. (if list
  54. (let* ((second (car list))
  55. (rest (cdr list))
  56. (newlist (list 'cons second first)))
  57. (while rest
  58. (setq newlist (list 'cons (car rest) newlist)
  59. rest (cdr rest)))
  60. newlist)
  61. first))
  62. (defalias 'backquote-list* (symbol-function 'backquote-list*-macro))
  63. ;; A few advertised variables that control which symbols are used
  64. ;; to represent the backquote, unquote, and splice operations.
  65. (defconst backquote-backquote-symbol '\`
  66. "Symbol used to represent a backquote or nested backquote.")
  67. (defconst backquote-unquote-symbol '\,
  68. "Symbol used to represent an unquote inside a backquote.")
  69. (defconst backquote-splice-symbol '\,@
  70. "Symbol used to represent a splice inside a backquote.")
  71. (defmacro backquote (structure)
  72. "Argument STRUCTURE describes a template to build.
  73. The whole structure acts as if it were quoted except for certain
  74. places where expressions are evaluated and inserted or spliced in.
  75. For example:
  76. b => (ba bb bc) ; assume b has this value
  77. `(a b c) => (a b c) ; backquote acts like quote
  78. `(a ,b c) => (a (ba bb bc) c) ; insert the value of b
  79. `(a ,@b c) => (a ba bb bc c) ; splice in the value of b
  80. Vectors work just like lists. Nested backquotes are permitted."
  81. (cdr (backquote-process structure)))
  82. ;; GNU Emacs has no reader macros
  83. (defalias '\` (symbol-function 'backquote))
  84. ;; backquote-process returns a dotted-pair of a tag (0, 1, or 2) and
  85. ;; the backquote-processed structure. 0 => the structure is
  86. ;; constant, 1 => to be unquoted, 2 => to be spliced in.
  87. ;; The top-level backquote macro just discards the tag.
  88. (defun backquote-delay-process (s level)
  89. "Process a (un|back|splice)quote inside a backquote.
  90. This simply recurses through the body."
  91. (let ((exp (backquote-listify (list (cons 0 (list 'quote (car s))))
  92. (backquote-process (cdr s) level))))
  93. (if (eq (car-safe exp) 'quote)
  94. (cons 0 (list 'quote s))
  95. (cons 1 exp))))
  96. (defun backquote-process (s &optional level)
  97. "Process the body of a backquote.
  98. S is the body. Returns a cons cell whose cdr is piece of code which
  99. is the macro-expansion of S, and whose car is a small integer whose value
  100. can either indicate that the code is constant (0), or not (1), or returns
  101. a list which should be spliced into its environment (2).
  102. LEVEL is only used internally and indicates the nesting level:
  103. 0 (the default) is for the toplevel nested inside a single backquote."
  104. (unless level (setq level 0))
  105. (cond
  106. ((vectorp s)
  107. (let ((n (backquote-process (append s ()) level)))
  108. (if (= (car n) 0)
  109. (cons 0 s)
  110. (cons 1 (cond
  111. ((not (listp (cdr n)))
  112. (list 'vconcat (cdr n)))
  113. ((eq (nth 1 n) 'list)
  114. (cons 'vector (nthcdr 2 n)))
  115. ((eq (nth 1 n) 'append)
  116. (cons 'vconcat (nthcdr 2 n)))
  117. (t
  118. (list 'apply '(function vector) (cdr n))))))))
  119. ((atom s)
  120. (cons 0 (if (or (null s) (eq s t) (not (symbolp s)))
  121. s
  122. (list 'quote s))))
  123. ((eq (car s) backquote-unquote-symbol)
  124. (if (<= level 0)
  125. (cons 1 (nth 1 s))
  126. (backquote-delay-process s (1- level))))
  127. ((eq (car s) backquote-splice-symbol)
  128. (if (<= level 0)
  129. (cons 2 (nth 1 s))
  130. (backquote-delay-process s (1- level))))
  131. ((eq (car s) backquote-backquote-symbol)
  132. (backquote-delay-process s (1+ level)))
  133. (t
  134. (let ((rest s)
  135. item firstlist list lists expression)
  136. ;; Scan this list-level, setting LISTS to a list of forms,
  137. ;; each of which produces a list of elements
  138. ;; that should go in this level.
  139. ;; The order of LISTS is backwards.
  140. ;; If there are non-splicing elements (constant or variable)
  141. ;; at the beginning, put them in FIRSTLIST,
  142. ;; as a list of tagged values (TAG . FORM).
  143. ;; If there are any at the end, they go in LIST, likewise.
  144. (while (and (consp rest)
  145. ;; Stop if the cdr is an expression inside a backquote or
  146. ;; unquote since this needs to go recursively through
  147. ;; backquote-process.
  148. (not (or (eq (car rest) backquote-unquote-symbol)
  149. (eq (car rest) backquote-backquote-symbol))))
  150. (setq item (backquote-process (car rest) level))
  151. (cond
  152. ((= (car item) 2)
  153. ;; Put the nonspliced items before the first spliced item
  154. ;; into FIRSTLIST.
  155. (if (null lists)
  156. (setq firstlist list
  157. list nil))
  158. ;; Otherwise, put any preceding nonspliced items into LISTS.
  159. (if list
  160. (push (backquote-listify list '(0 . nil)) lists))
  161. (push (cdr item) lists)
  162. (setq list nil))
  163. (t
  164. (setq list (cons item list))))
  165. (setq rest (cdr rest)))
  166. ;; Handle nonsplicing final elements, and the tail of the list
  167. ;; (which remains in REST).
  168. (if (or rest list)
  169. (push (backquote-listify list (backquote-process rest level))
  170. lists))
  171. ;; Turn LISTS into a form that produces the combined list.
  172. (setq expression
  173. (if (or (cdr lists)
  174. (eq (car-safe (car lists)) backquote-splice-symbol))
  175. (cons 'append (nreverse lists))
  176. (car lists)))
  177. ;; Tack on any initial elements.
  178. (if firstlist
  179. (setq expression (backquote-listify firstlist (cons 1 expression))))
  180. (if (eq (car-safe expression) 'quote)
  181. (cons 0 (list 'quote s))
  182. (cons 1 expression))))))
  183. ;; backquote-listify takes (tag . structure) pairs from backquote-process
  184. ;; and decides between append, list, backquote-list*, and cons depending
  185. ;; on which tags are in the list.
  186. (defun backquote-listify (list old-tail)
  187. (let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil))
  188. (if (= (car old-tail) 0)
  189. (setq tail (eval tail)
  190. old-tail nil))
  191. (while (consp list-tail)
  192. (setq item (car list-tail))
  193. (setq list-tail (cdr list-tail))
  194. (if (or heads old-tail (/= (car item) 0))
  195. (setq heads (cons (cdr item) heads))
  196. (setq tail (cons (eval (cdr item)) tail))))
  197. (cond
  198. (tail
  199. (if (null old-tail)
  200. (setq tail (list 'quote tail)))
  201. (if heads
  202. (let ((use-list* (or (cdr heads)
  203. (and (consp (car heads))
  204. (eq (car (car heads))
  205. backquote-splice-symbol)))))
  206. (cons (if use-list* 'backquote-list* 'cons)
  207. (append heads (list tail))))
  208. tail))
  209. (t (cons 'list heads)))))
  210. ;;; backquote.el ends here