Merge from emacs-23
[bpt/emacs.git] / lisp / env.el
CommitLineData
55535639 1;;; env.el --- functions to manipulate environment variables
c88ab9ce 2
0d30b337 3;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
5df4f04c 4;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
971571b9 5
e5167999 6;; Maintainer: FSF
d9ecc911 7;; Keywords: processes, unix
bd78fa1d 8;; Package: emacs
e5167999 9
b578f267 10;; This file is part of GNU Emacs.
6449c898 11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
6449c898 16
b578f267
EN
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
6449c898 21
b578f267 22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6449c898 24
d9ecc911
ER
25;;; Commentary:
26
b578f267
EN
27;; UNIX processes inherit a list of name-to-string associations from their
28;; parents called their `environment'; these are commonly used to control
29;; program options. This package permits you to set environment variables
30;; to be passed to any sub-process run under Emacs.
d9ecc911 31
1a90eae6
DL
32;; Note that the environment string `process-environment' is not
33;; decoded, but the args of `setenv' and `getenv' are normally
34;; multibyte text and get coding conversion.
35
e5167999
ER
36;;; Code:
37
5990851d
KL
38(eval-when-compile (require 'cl))
39
99ac138a
RS
40;; History list for environment variable names.
41(defvar read-envvar-name-history nil)
42
43(defun read-envvar-name (prompt &optional mustmatch)
44 "Read environment variable name, prompting with PROMPT.
8b740009
RS
45Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
46If it is also not t, RET does not exit if it does non-null completion."
99ac138a 47 (completing-read prompt
1a90eae6 48 (mapcar (lambda (enventry)
ef651d13
SM
49 (let ((str (substring enventry 0
50 (string-match "=" enventry))))
51 (if (multibyte-string-p str)
52 (decode-coding-string
53 str locale-coding-system t)
54 str)))
5990851d 55 (append process-environment
ef651d13 56 ;;(frame-environment)
de87fb59 57 ))
99ac138a
RS
58 nil mustmatch nil 'read-envvar-name-history))
59
60;; History list for VALUE argument to setenv.
61(defvar setenv-history nil)
62
a4a216c5
GM
63
64(defun substitute-env-vars (string)
65 "Substitute environment variables referred to in STRING.
66`$FOO' where FOO is an environment variable name means to substitute
67the value of that variable. The variable name should be terminated
68with a character not a letter, digit or underscore; otherwise, enclose
cccc806d
RS
69the entire variable name in braces. For instance, in `ab$cd-x',
70`$cd' is treated as an environment variable.
71
72Use `$$' to insert a single dollar sign."
a4a216c5 73 (let ((start 0))
71296446 74 (while (string-match
1a90eae6 75 (eval-when-compile
cccc806d 76 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
1a90eae6
DL
77 (and "${" (submatch (minimal-match (0+ anything))) "}")
78 "$$")))
a4a216c5
GM
79 string start)
80 (cond ((match-beginning 1)
81 (let ((value (getenv (match-string 1 string))))
82 (setq string (replace-match (or value "") t t string)
83 start (+ (match-beginning 0) (length value)))))
84 ((match-beginning 2)
85 (let ((value (getenv (match-string 2 string))))
86 (setq string (replace-match (or value "") t t string)
87 start (+ (match-beginning 0) (length value)))))
88 (t
89 (setq string (replace-match "$" t t string)
90 start (+ (match-beginning 0) 1)))))
91 string))
92
a13f8f50
KL
93
94(defun setenv-internal (env variable value keep-empty)
95 "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY.
96Changes ENV by side-effect, and returns its new value."
97 (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)"))
98 (case-fold-search nil)
99 (scan env)
100 prev found)
101 ;; Handle deletions from the beginning of the list specially.
102 (if (and (null value)
103 (not keep-empty)
104 env
105 (stringp (car env))
106 (string-match pattern (car env)))
107 (cdr env)
108 ;; Try to find existing entry for VARIABLE in ENV.
109 (while (and scan (stringp (car scan)))
110 (when (string-match pattern (car scan))
111 (if value
112 (setcar scan (concat variable "=" value))
113 (if keep-empty
114 (setcar scan variable)
115 (setcdr prev (cdr scan))))
116 (setq found t
117 scan nil))
118 (setq prev scan
119 scan (cdr scan)))
120 (if (and (not found) (or value keep-empty))
121 (cons (if value
122 (concat variable "=" value)
123 variable)
124 env)
125 env))))
126
5990851d 127;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
a4a216c5 128
ef651d13 129(defun setenv (variable &optional value substitute-env-vars)
6449c898 130 "Set the value of the environment variable named VARIABLE to VALUE.
b71038b2 131VARIABLE should be a string. VALUE is optional; if not provided or
17ccbd91 132nil, the environment variable VARIABLE will be removed.
cbfe666b 133
17ccbd91
KL
134Interactively, a prefix argument means to unset the variable, and
135otherwise the current value (if any) of the variable appears at
136the front of the history list when you type in the new value.
137This function always replaces environment variables in the new
138value when called interactively.
99ac138a 139
27bdc650
RS
140SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
141variables in VALUE with `substitute-env-vars', which see.
142This is normally used only for interactive calls.
143
144The return value is the new value of VARIABLE, or nil if
145it was removed from the environment.
146
ef651d13
SM
147This function works by modifying `process-environment'.
148
1a90eae6
DL
149As a special case, setting variable `TZ' calls `set-time-zone-rule' as
150a side-effect."
cbfe666b
RS
151 (interactive
152 (if current-prefix-arg
27bdc650 153 (list (read-envvar-name "Clear environment variable: " 'exact) nil)
2a5becfb
GM
154 (let* ((var (read-envvar-name "Set environment variable: " nil))
155 (value (getenv var)))
156 (when value
4c5f6185 157 (add-to-history 'setenv-history value))
99ac138a 158 ;; Here finally we specify the args to give call setenv with.
71296446 159 (list var
a4a216c5
GM
160 (read-from-minibuffer (format "Set %s to value: " var)
161 nil nil nil 'setenv-history
162 value)
a4a216c5 163 t))))
1a90eae6 164 (if (and (multibyte-string-p variable) locale-coding-system)
1ebb05c4
KH
165 (let ((codings (find-coding-systems-string (concat variable value))))
166 (unless (or (eq 'undecided (car codings))
167 (memq (coding-system-base locale-coding-system) codings))
168 (error "Can't encode `%s=%s' with `locale-coding-system'"
169 variable (or value "")))))
27bdc650
RS
170 (and value
171 substitute-env-vars
172 (setq value (substitute-env-vars value)))
1a90eae6
DL
173 (if (multibyte-string-p variable)
174 (setq variable (encode-coding-string variable locale-coding-system)))
175 (if (and value (multibyte-string-p value))
176 (setq value (encode-coding-string value locale-coding-system)))
6449c898 177 (if (string-match "=" variable)
f105f403 178 (error "Environment variable name `%s' contains `='" variable))
a13f8f50
KL
179 (if (string-equal "TZ" variable)
180 (set-time-zone-rule value))
ef651d13
SM
181 (setq process-environment (setenv-internal process-environment
182 variable value t))
a13f8f50 183 value)
49116ac0 184
da8e8fc1 185(defun getenv (variable &optional frame)
b1e11b4f
GM
186 "Get the value of environment variable VARIABLE.
187VARIABLE should be a string. Value is nil if VARIABLE is undefined in
188the environment. Otherwise, value is a string.
189
da8e8fc1 190If optional parameter FRAME is non-nil, then it should be a
a13f8f50
KL
191frame. This function will look up VARIABLE in its 'environment
192parameter.
f105f403 193
da8e8fc1 194Otherwise, this function searches `process-environment' for
a13f8f50
KL
195VARIABLE. If it is not found there, then it continues the search
196in the environment list of the selected frame."
b1e11b4f 197 (interactive (list (read-envvar-name "Get environment variable: " t)))
1a90eae6
DL
198 (let ((value (getenv-internal (if (multibyte-string-p variable)
199 (encode-coding-string
200 variable locale-coding-system)
36ab8612 201 variable)
263903f7
JB
202 (and frame
203 (assq 'environment
204 (frame-parameters frame))))))
1a90eae6
DL
205 (if (and enable-multibyte-characters value)
206 (setq value (decode-coding-string value locale-coding-system)))
32226619 207 (when (called-interactively-p 'interactive)
b1e11b4f
GM
208 (message "%s" (if value value "Not set")))
209 value))
210
1bbda2d6
NF
211(provide 'env)
212
bb8ef973 213;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
1bbda2d6 214;;; env.el ends here