Next: Q5.1.6, Previous: Q5.1.4, Up: Miscellaneous
setq
?You will typically defvar
your global variable to a default
value, and use setq
to set it later.
It is never a good practice to setq
user variables (like
case-fold-search
, etc.), as it ignores the user’s choice
unconditionally. Note that defvar
doesn’t change the value of a
variable if it was bound previously. If you wish to change a
user-variable temporarily, use let
:
(let ((case-fold-search nil)) ... ; code with searches that must be case-sensitive ...)
You will notice the user-variables by their docstrings beginning with an asterisk (a convention).
Bind them with let
, which will unbind them (or restore their
previous value, if they were bound) after exiting from the let
form. Change the value of local variables with setq
or whatever
you like (e.g. incf
, setf
and such). The let
form
can even return one of its local variables.
Typical usage:
;; iterate through the elements of the list returned by ;; `hairy-function-that-returns-list' (let ((l (hairy-function-that-returns-list))) (while l ... do something with (car l) ... (setq l (cdr l))))
Another typical usage includes building a value simply to work with it.
;; Build the mode keymap out of the key-translation-alist (let ((inbox (file-truename (expand-file-name box))) (i 0)) ... code dealing with inbox ... inbox)
This piece of code uses the local variable inbox
, which becomes
unbound (or regains old value) after exiting the form. The form also
returns the value of inbox
, which can be reused, for instance:
(setq foo-processed-inbox (let .....))
Next: Q5.1.6, Previous: Q5.1.4, Up: Miscellaneous