+(defun replace-match-string-symbols (n)
+ "Process a list (and any sub-lists), expanding certain symbols.
+Symbol Expands To
+N (match-string N) (where N is a string of digits)
+#N (string-to-number (match-string N))
+& (match-string 0)
+#& (string-to-number (match-string 0))
+
+Note that these symbols must be preceeded by a backslash in order to
+type them."
+ (while n
+ (cond
+ ((consp (car n))
+ (replace-match-string-symbols (car n))) ;Process sub-list
+ ((symbolp (car n))
+ (let ((name (symbol-name (car n))))
+ (cond
+ ((string-match "^[0-9]+$" name)
+ (setcar n (list 'match-string (string-to-number name))))
+ ((string-match "^#[0-9]+$" name)
+ (setcar n (list 'string-to-number
+ (list 'match-string
+ (string-to-number (substring name 1))))))
+ ((string= "&" name)
+ (setcar n '(match-string 0)))
+ ((string= "#&" name)
+ (setcar n '(string-to-number (match-string 0))))))))
+ (setq n (cdr n))))
+
+(defun replace-eval-replacement (expression replace-count)
+ (let ((replacement (eval expression)))
+ (if (stringp replacement)
+ replacement
+ (prin1-to-string replacement t))))
+
+(defun replace-loop-through-replacements (data replace-count)
+ ;; DATA is a vector contaning the following values:
+ ;; 0 next-rotate-count
+ ;; 1 repeat-count
+ ;; 2 next-replacement
+ ;; 3 replacements
+ (if (= (aref data 0) replace-count)
+ (progn
+ (aset data 0 (+ replace-count (aref data 1)))
+ (let ((next (cdr (aref data 2))))
+ (aset data 2 (if (consp next) next (aref data 3))))))
+ (car (aref data 2)))
+
+(defun perform-replace (from-string replacements start end