1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-29 07:58:28 +00:00

Add new macro with-environment-variables

* doc/lispref/os.texi (System Environment): Document it.

* lisp/env.el (with-environment-variables): New macro.
This commit is contained in:
Lars Ingebrigtsen 2021-09-26 08:27:51 +02:00
parent 43ae8c828d
commit 7cb2944043
3 changed files with 34 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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