[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 7cb2944 1/2: Add new macro with-environment-variables
From: |
Lars Ingebrigtsen |
Subject: |
master 7cb2944 1/2: Add new macro with-environment-variables |
Date: |
Sun, 26 Sep 2021 02:29:49 -0400 (EDT) |
branch: master
commit 7cb29440433bda1ad8defad70cbd43fb2f9f4d1f
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>
Add new macro with-environment-variables
* doc/lispref/os.texi (System Environment): Document it.
* lisp/env.el (with-environment-variables): New macro.
---
doc/lispref/os.texi | 12 ++++++++++++
etc/NEWS | 5 +++++
lisp/env.el | 17 +++++++++++++++++
3 files changed, 34 insertions(+)
diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
index 658f742..a34c01c 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 d77d341..bc468c6 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 83f43d1..31a728c 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