Initialize Vprocess_environment to nil.
[bpt/emacs.git] / lisp / env.el
CommitLineData
55535639 1;;; env.el --- functions to manipulate environment variables
c88ab9ce 2
0d30b337
TTN
3;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
4;; 2005 Free Software Foundation, Inc.
971571b9 5
e5167999 6;; Maintainer: FSF
d9ecc911 7;; Keywords: processes, unix
e5167999 8
b578f267 9;; This file is part of GNU Emacs.
6449c898 10
b578f267
EN
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
6449c898 15
b578f267
EN
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
6449c898 20
b578f267
EN
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
6449c898 25
d9ecc911
ER
26;;; Commentary:
27
b578f267
EN
28;; UNIX processes inherit a list of name-to-string associations from their
29;; parents called their `environment'; these are commonly used to control
30;; program options. This package permits you to set environment variables
31;; to be passed to any sub-process run under Emacs.
d9ecc911 32
1a90eae6
DL
33;; Note that the environment string `process-environment' is not
34;; decoded, but the args of `setenv' and `getenv' are normally
35;; multibyte text and get coding conversion.
36
e5167999
ER
37;;; Code:
38
5990851d
KL
39(eval-when-compile (require 'cl))
40
99ac138a
RS
41;; History list for environment variable names.
42(defvar read-envvar-name-history nil)
43
44(defun read-envvar-name (prompt &optional mustmatch)
45 "Read environment variable name, prompting with PROMPT.
8b740009
RS
46Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
47If it is also not t, RET does not exit if it does non-null completion."
99ac138a 48 (completing-read prompt
1a90eae6
DL
49 (mapcar (lambda (enventry)
50 (list (if enable-multibyte-characters
51 (decode-coding-string
52 (substring enventry 0
53 (string-match "=" enventry))
54 locale-coding-system t)
55 (substring enventry 0
56 (string-match "=" enventry)))))
5990851d
KL
57 (append process-environment
58 (terminal-parameter nil 'environment)
59 global-environment))
99ac138a
RS
60 nil mustmatch nil 'read-envvar-name-history))
61
62;; History list for VALUE argument to setenv.
63(defvar setenv-history nil)
64
a4a216c5
GM
65
66(defun substitute-env-vars (string)
67 "Substitute environment variables referred to in STRING.
68`$FOO' where FOO is an environment variable name means to substitute
69the value of that variable. The variable name should be terminated
70with a character not a letter, digit or underscore; otherwise, enclose
cccc806d
RS
71the entire variable name in braces. For instance, in `ab$cd-x',
72`$cd' is treated as an environment variable.
73
74Use `$$' to insert a single dollar sign."
a4a216c5 75 (let ((start 0))
71296446 76 (while (string-match
1a90eae6 77 (eval-when-compile
cccc806d 78 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
1a90eae6
DL
79 (and "${" (submatch (minimal-match (0+ anything))) "}")
80 "$$")))
a4a216c5
GM
81 string start)
82 (cond ((match-beginning 1)
83 (let ((value (getenv (match-string 1 string))))
84 (setq string (replace-match (or value "") t t string)
85 start (+ (match-beginning 0) (length value)))))
86 ((match-beginning 2)
87 (let ((value (getenv (match-string 2 string))))
88 (setq string (replace-match (or value "") t t string)
89 start (+ (match-beginning 0) (length value)))))
90 (t
91 (setq string (replace-match "$" t t string)
92 start (+ (match-beginning 0) 1)))))
93 string))
94
5990851d 95;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
a4a216c5 96
f105f403 97(defun setenv (variable &optional value unset substitute-env-vars terminal)
6449c898 98 "Set the value of the environment variable named VARIABLE to VALUE.
b71038b2
JB
99VARIABLE should be a string. VALUE is optional; if not provided or
100nil, the environment variable VARIABLE will be removed. UNSET
a4a216c5
GM
101if non-nil means to remove VARIABLE from the environment.
102SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
103variables in VALUE with `substitute-env-vars', where see.
104Value is the new value if VARIABLE, or nil if removed from the
105environment.
cbfe666b
RS
106
107Interactively, a prefix argument means to unset the variable.
99ac138a
RS
108Interactively, the current value (if any) of the variable
109appears at the front of the history list when you type in the new value.
a4a216c5 110Interactively, always replace environment variables in the new value.
99ac138a 111
5990851d
KL
112If VARIABLE is set in `process-environment', then this function
113modifies its value there. Otherwise, this function works by
114modifying either `global-environment' or the environment
115belonging to the terminal device of the selected frame, depending
116on the value of `local-environment-variables'.
117
f105f403
KL
118If optional parameter TERMINAL is non-nil, then it should be a
119terminal id or a frame. If the specified terminal device has its own
120set of environment variables, this function will modify VAR in it.
121
1a90eae6
DL
122As a special case, setting variable `TZ' calls `set-time-zone-rule' as
123a side-effect."
cbfe666b
RS
124 (interactive
125 (if current-prefix-arg
8b740009 126 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
2a5becfb
GM
127 (let* ((var (read-envvar-name "Set environment variable: " nil))
128 (value (getenv var)))
129 (when value
130 (push value setenv-history))
99ac138a 131 ;; Here finally we specify the args to give call setenv with.
71296446 132 (list var
a4a216c5
GM
133 (read-from-minibuffer (format "Set %s to value: " var)
134 nil nil nil 'setenv-history
135 value)
71296446 136 nil
a4a216c5 137 t))))
1a90eae6 138 (if (and (multibyte-string-p variable) locale-coding-system)
1ebb05c4
KH
139 (let ((codings (find-coding-systems-string (concat variable value))))
140 (unless (or (eq 'undecided (car codings))
141 (memq (coding-system-base locale-coding-system) codings))
142 (error "Can't encode `%s=%s' with `locale-coding-system'"
143 variable (or value "")))))
b71038b2 144 (if unset
a4a216c5
GM
145 (setq value nil)
146 (if substitute-env-vars
147 (setq value (substitute-env-vars value))))
1a90eae6
DL
148 (if (multibyte-string-p variable)
149 (setq variable (encode-coding-string variable locale-coding-system)))
150 (if (and value (multibyte-string-p value))
151 (setq value (encode-coding-string value locale-coding-system)))
6449c898 152 (if (string-match "=" variable)
f105f403 153 (error "Environment variable name `%s' contains `='" variable))
5990851d
KL
154 (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)"))
155 (case-fold-search nil)
156 (terminal-env (terminal-parameter terminal 'environment))
157 (scan process-environment)
158 found)
f105f403
KL
159 (if (string-equal "TZ" variable)
160 (set-time-zone-rule value))
5990851d
KL
161 (block nil
162 ;; Look for an existing entry for VARIABLE; try `process-environment' first.
163 (while (and scan (stringp (car scan)))
164 (when (string-match pattern (car scan))
165 (if value
166 (setcar scan (concat variable "=" value))
167 ;; Leave unset variables in `process-environment',
168 ;; otherwise the overridden value in `global-environment'
169 ;; or terminal-env would become unmasked.
170 (setcar scan variable))
171 (return value))
172 (setq scan (cdr scan)))
173
174 ;; Look in the local or global environment, whichever is relevant.
175 (let ((local-var-p (and terminal-env
176 (or terminal
177 (eq t local-environment-variables)
178 (member variable local-environment-variables)))))
179 (setq scan (if local-var-p
180 terminal-env
181 global-environment))
182 (while scan
183 (when (string-match pattern (car scan))
184 (if value
185 (setcar scan (concat variable "=" value))
186 (if local-var-p
187 (set-terminal-parameter terminal 'environment
188 (delq (car scan) terminal-env))
189 (setq global-environment (delq (car scan) global-environment)))
190 (return value)))
191 (setq scan (cdr scan)))
192
193 ;; VARIABLE is not in any environment list.
f105f403
KL
194 (if value
195 (if local-var-p
196 (set-terminal-parameter nil 'environment
197 (cons (concat variable "=" value)
5990851d
KL
198 terminal-env))
199 (setq global-environment
cbfe666b 200 (cons (concat variable "=" value)
5990851d
KL
201 global-environment))))
202 (return value)))))
49116ac0 203
f105f403 204(defun getenv (variable &optional terminal)
b1e11b4f
GM
205 "Get the value of environment variable VARIABLE.
206VARIABLE should be a string. Value is nil if VARIABLE is undefined in
207the environment. Otherwise, value is a string.
208
f105f403
KL
209If optional parameter TERMINAL is non-nil, then it should be a
210terminal id or a frame. If the specified terminal device has its own
5990851d
KL
211set of environment variables, this function will look up VARIABLE in
212it.
f105f403 213
5990851d
KL
214Otherwise, this function searches `process-environment' for VARIABLE.
215If it was not found there, then it continues the search in either
216`global-environment' or the local environment list of the current
217terminal device, depending on the value of
218`local-environment-variables'."
b1e11b4f 219 (interactive (list (read-envvar-name "Get environment variable: " t)))
1a90eae6
DL
220 (let ((value (getenv-internal (if (multibyte-string-p variable)
221 (encode-coding-string
222 variable locale-coding-system)
223 variable))))
224 (if (and enable-multibyte-characters value)
225 (setq value (decode-coding-string value locale-coding-system)))
b1e11b4f
GM
226 (when (interactive-p)
227 (message "%s" (if value value "Not set")))
228 value))
229
5990851d
KL
230(defun environment ()
231 "Return a list of environment variables with their values.
232Each entry in the list is a string of the form NAME=VALUE.
233
234The returned list can not be used to change environment
235variables, only read them. See `setenv' to do that.
236
237The list is constructed from elements of `process-environment',
238`global-environment' and the local environment list of the
239current terminal, as specified by `local-environment-variables'.
240
241Non-ASCII characters are encoded according to the initial value of
242`locale-coding-system', i.e. the elements must normally be decoded for use.
243See `setenv' and `getenv'."
244 (let ((env (cond ((or (not local-environment-variables)
245 (not (terminal-parameter nil 'environment)))
246 (append process-environment global-environment nil))
247 ((consp local-environment-variables)
248 (let ((e (reverse process-environment)))
249 (dolist (entry local-environment-variables)
250 (setq e (cons (getenv entry) e)))
251 (append (nreverse e) global-environment nil)))
252 (t
253 (append process-environment (terminal-parameter nil 'environment) nil))))
254 scan seen)
255 ;; Find the first valid entry in env.
256 (while (and env (stringp (car env))
257 (or (not (string-match "=" (car env)))
258 (member (substring (car env) 0 (string-match "=" (car env))) seen)))
259 (setq seen (cons (car env) seen)
260 env (cdr env)))
261 (setq scan env)
262 (while (and (cdr scan) (stringp (cadr scan)))
263 (let* ((match (string-match "=" (cadr scan)))
264 (name (substring (cadr scan) 0 match)))
265 (cond ((not match)
266 ;; Unset variable.
267 (setq seen (cons name seen))
268 (setcdr scan (cddr scan)))
269 ((member name seen)
270 ;; Duplicate variable.
271 (setcdr scan (cddr scan)))
272 (t
273 ;; New variable.
274 (setq seen (cons name seen)
275 scan (cdr scan))))))
276 env))
277
278(defmacro let-environment (varlist &rest body)
279 "Evaluate BODY with environment variables set according to VARLIST.
280The environment variables are then restored to their previous
281values.
282The value of the last form in BODY is returned.
283
284Each element of VARLIST is either a string (which variable is
285then removed from the environment), or a list (NAME
286VALUEFORM) (which sets NAME to the value of VALUEFORM, a string).
287All the VALUEFORMs are evaluated before any variables are set."
288 (declare (indent 2))
289 (let ((old-env (make-symbol "old-env"))
290 (name (make-symbol "name"))
291 (value (make-symbol "value"))
292 (entry (make-symbol "entry"))
293 (frame (make-symbol "frame")))
294 `(let ((,frame (selected-frame))
295 ,old-env)
296 ;; Evaluate VALUEFORMs and replace them in VARLIST with their values.
297 (dolist (,entry ,varlist)
298 (unless (stringp ,entry)
299 (if (cdr (cdr ,entry))
300 (error "`let-environment' bindings can have only one value-form"))
301 (setcdr ,entry (eval (cadr ,entry)))))
302 ;; Set the variables.
303 (dolist (,entry ,varlist)
304 (let ((,name (if (stringp ,entry) ,entry (car ,entry)))
305 (,value (if (consp ,entry) (cdr ,entry))))
306 (setq ,old-env (cons (cons ,name (getenv ,name)) ,old-env))
307 (setenv ,name ,value)))
308 (unwind-protect
309 (progn ,@body)
310 ;; Restore old values.
311 (with-selected-frame (if (frame-live-p ,frame)
312 ,frame
313 (selected-frame))
314 (dolist (,entry ,old-env)
315 (setenv (car ,entry) (cdr ,entry))))))))
316
1bbda2d6
NF
317(provide 'env)
318
ab5796a9 319;;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
1bbda2d6 320;;; env.el ends here