I have problems to get into Lisp. I assume I miss the correct terms to search for. So I try to "speak in Python".
#!/usr/bin/env python3
foo = 7
backuped_foo = foo
# temporary change the value of foo
foo = 323
# restore the original foo value
foo = backuped_foo
So I would I do this in Emacs Lisp?
EDIT: I am not interested in lexial scoping or other concepts for a solution. I am only interested in an nearly exact technical copy of that python code into Lisp.
This is inspired by another answer:
(setq foo 7) ;; original value
(setq backuped_foo foo) ;; remember it for later restore
(setq foo 4373) ;; temporary value
;; ...
;; do a lot of stuff where a lexical scoope does not help
;; ...
(setq foo backuped_foo) ;; restore the original value
At the end foo==7
(do not know how to express this in Lisp) so everything is fine. But I am not sure if this is a good and Lisp-like way.
In real I need this to modify a global variable (gc-cons-threshold
) while a script (init.el
) is running. When the script is finished I need to restore the original value.
(let ...)
statement as shown in the answer from @db48xlet
to rebind dynamic variables too. Just do(let ((gc-cons-threshhold 9999999)) …)
...
are.(let ((gc-cons-threshhold 9999999)) (load "my-init.el"))
. No need to physically have so many lines there.(defun my-300-lines-of-stuff () …)
, and then call it inside the let:(let ((gc-cons-threshhold 9999999)) (my-300-lines-of-stuff))
.