From: Lars Ingebrigtsen Date: Sun, 26 Sep 2021 06:27:51 +0000 (+0200) Subject: Add new macro with-environment-variables X-Git-Tag: emacs-28.0.90~619 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=7cb29440433bda1ad8defad70cbd43fb2f9f4d1f;p=emacs.git Add new macro with-environment-variables * doc/lispref/os.texi (System Environment): Document it. * lisp/env.el (with-environment-variables): New macro. --- diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi index 658f742c418..a34c01c81aa 100644 --- a/doc/lispref/os.texi +++ b/doc/lispref/os.texi @@ -1042,6 +1042,18 @@ that variable with @code{let} is also reasonable practice. 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 diff --git a/etc/NEWS b/etc/NEWS index d77d34160b2..bc468c6df4c 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -4417,6 +4417,11 @@ obsoletes the old data layout specifications. It supports 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. + * Changes in Emacs 28.1 on Non-Free Operating Systems diff --git a/lisp/env.el b/lisp/env.el index 83f43d1006b..31a728c0e56 100644 --- a/lisp/env.el +++ b/lisp/env.el @@ -218,6 +218,23 @@ in the environment list of the selected frame." (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