-(defmacro let-environment (varlist &rest body)
- "Evaluate BODY with environment variables set according to VARLIST.
-The environment variables are then restored to their previous
-values.
-The value of the last form in BODY is returned.
-
-Each element of VARLIST is either a string (which variable is
-then removed from the environment), or a list (NAME
-VALUEFORM) (which sets NAME to the value of VALUEFORM, a string).
-All the VALUEFORMs are evaluated before any variables are set."
- (declare (indent 2))
- (let ((old-env (make-symbol "old-env"))
- (name (make-symbol "name"))
- (value (make-symbol "value"))
- (entry (make-symbol "entry"))
- (frame (make-symbol "frame")))
- `(let ((,frame (selected-frame))
- ,old-env)
- ;; Evaluate VALUEFORMs and replace them in VARLIST with their values.
- (dolist (,entry ,varlist)
- (unless (stringp ,entry)
- (if (cdr (cdr ,entry))
- (error "`let-environment' bindings can have only one value-form"))
- (setcdr ,entry (eval (cadr ,entry)))))
- ;; Set the variables.
- (dolist (,entry ,varlist)
- (let ((,name (if (stringp ,entry) ,entry (car ,entry)))
- (,value (if (consp ,entry) (cdr ,entry))))
- (setq ,old-env (cons (cons ,name (getenv ,name)) ,old-env))
- (setenv ,name ,value)))
- (unwind-protect
- (progn ,@body)
- ;; Restore old values.
- (with-selected-frame (if (frame-live-p ,frame)
- ,frame
- (selected-frame))
- (dolist (,entry ,old-env)
- (setenv (car ,entry) (cdr ,entry))))))))
-