;;AUTOLOAD: macroexpand
;;AUTODOC: macroexpand "expands a macro call without evaluating the result"
;;(macroexpand macro-call)
;;
;; If macro-call is a expression calling a macro, then macroexpand returns the 
;; result of the evaluation
;;e.g.:
;;     (defmacro position (e l) `(seek ,l ,e))
;;     (macroexpand '(position element thelist))
;;               ==> (seek thelist element)
;; Useful for testing macros
;;
;; Note: this corresponds actually to the macroexpand-1 function of 
;; Common Lisp, as it only dereferences once.

(defun macroexpand (expr &aux func)
  (if (and 
      (typep expr List) 
      (or (typep (setq func (0 expr)) Macro)
	(and (typep (0 expr) Atom) (typep (setq func (eval (0 expr))) Macro))
    ))
    (progn
      (type-replace func FExpr)
      (unwind-protect (eval expr)
	(type-replace func Macro)
))))
))

;; alias for CL compat
;;AUTOLOAD: macroexpand-1

(setq macroexpand-1 macroexpand)
