feat(lib): backport with-environment-variables from 28.1

And alias letenv! to it. Not sure if I want to deprecate it though,
given the length of the macro it's linked to...
This commit is contained in:
Henrik Lissner 2022-08-24 21:36:24 +02:00
parent efa3e9791f
commit 65c86ea3ff
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -198,13 +198,8 @@ TRIGGER-HOOK is a list of quoted hooks and/or sharp-quoted functions."
(buffer-file-name)
((error "Cannot get this file-path"))))
(defmacro letenv! (envvars &rest body)
"Lexically bind ENVVARS in BODY, like `let' but for `process-environment'."
(declare (indent 1))
`(let ((process-environment (copy-sequence process-environment)))
,@(cl-loop for (var val) in envvars
collect `(setenv ,var ,val))
,@body))
;; REVIEW Should I deprecate this? The macro's name is so long...
(defalias 'letenv! 'with-environment-variables)
(defmacro letf! (bindings &rest body)
"Temporarily rebind function, macros, and advice in BODY.
@ -827,5 +822,23 @@ inserted before contatenating."
collect (match-string 1 str)))
"/")))
;; Introduced in 28.1
(unless (fboundp 'with-environment-variables)
(defmacro with-environment-variables (variables &rest body)
"Set VARIABLES in the environment and execute BODY.
VARIABLES is a list of variable settings of the form (VAR VALUE),
where VAR is the name of the variable (a string) and VALUE
is its value (also a string).
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 'doom-lib)
;;; doom-lib.el ends here