;;AUTOLOAD: trap-error
;;AUTODOC: trap-error "catches (traps) silently errors in statements"
;;(trap-error errorcode forms...)
;;
;;Traps the error "errorcode" (symbol, must be quoted) in the forms. returns 
;;the result of the evaluation of the last form if the error did not occur or
;;() if it occurs. "errorcode" can be 'ALL meaning trap any error. If an error
;; occurs in the form, the rest of the forms are not evaluated
;;The variable trap-error:caught is set to nil if no error, or to a list of the
;;errorcode and the rest of the error-specific parameters in case of errors.
;;
;;Example:
;;    (setq fd (trap-error 'Errors:ErrorOpeningFile (open "foo")))
;;    (if (not fd) (setq fd (trap-error 'Errors:ErrorOpeningFile (open "bar"))))
;;    (if (not fd) (print "Cannot open foo nor bar\n"))

(defunq trap-error (trap-error:tag &rest trap-error:body)
  (setq trap-error:tag (eval trap-error:tag))
  (setq trap-error:caught ())
  (with (*error-correctors* (+
	(list
	  (eval `(lambda (errorcode &rest args)
	      (if (or (= ',trap-error:tag errorcode) (= ',trap-error:tag 'ALL))
		(progn
		  (setq trap-error:caught `(,errorcode ,@trap-error:body))
		  (throw 'trap-error ())
		)
		:nil))))
	*error-correctors* 
    ))
    (catch 'trap-error (apply progn trap-error:body))
))
