;;AUTOLOAD: doregexp
;;AUTODOC: doregexp "matches all regexps in a string"
;; (doregexp (variable regexep string) forms...)
;;
;; doregexp is a convenience macro that will execute forms on each match of 
;; the regular expression regexp in the string
;; variable will be set to the regexp, that can be given as a string.
;;
;; e.g:
;;    (setq price-list "I bought 24 tokens at $2.95 for a total of $70.80!")
;;    (doregexp (re "[$]([0-9]+([.][0-9][0-9])?)" price-list)
;;       (print-format "Found price in dollars: %0\n" (regsub re 1))
;;    )
;;    ==>  Found price in dollars: 2.95
;;         Found price in dollars: 70.80

(defvar Errors:DoRegexp "Bad doregexp syntax: %0")

(defmacrod doregexp (varlist &rest forms &aux 
    var regexp string 
  )
  (if (not (and
	(typep varlist List)
	(= (length varlist) 3)
    ))
    (error 'Errors:DoRegexp varlist)
  )
  (setq var (0 varlist))
  (setq regexp (1 varlist))
  (setq string (2 varlist))

  ;; the resulting function
  (setq R
  `(with (doregexp:offset 0 ,var ,regexp doregexp:string ,string)
      (if (not (typep ,var Regexp)) (setq ,var (regcomp ,var)))
      (while (regexec ,var doregexp:string doregexp:offset)
	,@forms
	(setq doregexp:offset (getn (getn ,var 0) 1))
      )
    )
  )
  R
)
