* doc/lispref/os.texi (System Environment): Document it.
* lisp/env.el (with-environment-variables): New macro.
if it removed @var{variable} from the environment.
@end deffn
+@defmac with-environment-variables variables body@dots{}
+This macro sets the environment variables in @var{variables}
+temporarily when executing @var{body}. The previous values are
+restored when the form finishes.
+
+@lisp
+(with-environment-variables (("LANG" "C")
+ ("LANGUAGE" "en_US:en"))
+ (call-process "ls" nil t))
+@end lisp
+@end defmac
+
@defvar process-environment
This variable is a list of strings, each describing one environment
variable. The functions @code{getenv} and @code{setenv} work by means
arbitrary-size integers, recursive types, and more. See the Info node
"(elisp) Byte Packing" in the ELisp manual for more details.
++++
+** New macro 'with-environment-variables'.
+This macro allows setting environment variables temporarily when
+executing a form.
+
\f
* Changes in Emacs 28.1 on Non-Free Operating Systems
(message "%s" (if value value "Not set")))
value))
+;;;###autoload
+(defmacro with-environment-variables (variables &rest body)
+ "Set VARIABLES in the environent and execute BODY.
+VARIABLES is a list of variable settings where first element
+should be the name of the variable and the second element should
+be the value.
+
+The previous values will be be restored upon exit."
+ (declare (indent 1) (debug (sexp body)))
+ (unless (consp variables)
+ (error "Invalid VARIABLES: %s" variables))
+ `(let ((process-environment (copy-sequence process-environment)))
+ ,@(mapcar (lambda (elem)
+ `(setenv ,(car elem) ,(cadr elem)))
+ variables)
+ ,@body))
+
(provide 'env)
;;; env.el ends here