Function: setenv
setenv is an interactive and byte-compiled function defined in
env.el.gz.
Signature
(setenv VARIABLE &optional VALUE SUBSTITUTE-ENV-VARS)
Documentation
Set the value of the environment variable named VARIABLE to VALUE.
VARIABLE should be a string. VALUE is optional; if not provided or nil, the environment variable VARIABLE will be removed.
Interactively, a prefix argument means to unset the variable, and otherwise the current value (if any) of the variable appears at the front of the history list when you type in the new value. This function always replaces environment variables in the new value when called interactively.
SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
variables in VALUE with substitute-env-vars, which see.
This is normally used only for interactive calls.
The return value is the new value of VARIABLE, or nil if it was removed from the environment.
This function works by modifying process-environment.
As a special case, setting variable TZ calls set-time-zone-rule as
a side-effect.
Probably introduced at or before Emacs version 19.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/env.el.gz
;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
(defun setenv (variable &optional value substitute-env-vars)
"Set the value of the environment variable named VARIABLE to VALUE.
VARIABLE should be a string. VALUE is optional; if not provided or
nil, the environment variable VARIABLE will be removed.
Interactively, a prefix argument means to unset the variable, and
otherwise the current value (if any) of the variable appears at
the front of the history list when you type in the new value.
This function always replaces environment variables in the new
value when called interactively.
SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
variables in VALUE with `substitute-env-vars', which see.
This is normally used only for interactive calls.
The return value is the new value of VARIABLE, or nil if
it was removed from the environment.
This function works by modifying `process-environment'.
As a special case, setting variable `TZ' calls `set-time-zone-rule' as
a side-effect."
(interactive
(if current-prefix-arg
(list (read-envvar-name "Clear environment variable: " 'exact) nil)
(let* ((var (read-envvar-name "Set environment variable: " nil))
(value (getenv var)))
(when value
(add-to-history 'setenv-history value))
;; Here finally we specify the args to give call setenv with.
(list var
(read-from-minibuffer (format "Set %s to value: " var)
nil nil nil 'setenv-history
value)
t))))
(if (and (multibyte-string-p variable) locale-coding-system)
(let ((codings (find-coding-systems-string (concat variable value))))
(unless (or (eq 'undecided (car codings))
(memq (coding-system-base locale-coding-system) codings))
(error "Can't encode `%s=%s' with `locale-coding-system'"
variable (or value "")))))
(and value
substitute-env-vars
(setq value (substitute-env-vars value)))
(if (multibyte-string-p variable)
(setq variable (encode-coding-string variable locale-coding-system)))
(if (and value (multibyte-string-p value))
(setq value (encode-coding-string value locale-coding-system)))
(if (string-search "=" variable)
(error "Environment variable name `%s' contains `='" variable))
(if (string-equal "TZ" variable)
(set-time-zone-rule value))
(setq process-environment (setenv-internal process-environment
variable value t))
value)