Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; env.el --- functions to manipulate environment variables |
c88ab9ce | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004, |
d7a0267c | 4 | ;; 2005, 2006, 2007 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 | |
b4aa6026 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
b578f267 | 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 |
46 | Optional second arg MUSTMATCH, if non-nil, means require existing envvar name. |
47 | If 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 | 57 | (append process-environment |
de87fb59 DN |
58 | nil ;;(frame-parameter (frame-with-environment) 'environment) |
59 | )) | |
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 | |
69 | the value of that variable. The variable name should be terminated | |
70 | with a character not a letter, digit or underscore; otherwise, enclose | |
cccc806d RS |
71 | the entire variable name in braces. For instance, in `ab$cd-x', |
72 | `$cd' is treated as an environment variable. | |
73 | ||
74 | Use `$$' 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 | ||
a13f8f50 KL |
95 | |
96 | (defun setenv-internal (env variable value keep-empty) | |
97 | "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY. | |
98 | Changes ENV by side-effect, and returns its new value." | |
99 | (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)")) | |
100 | (case-fold-search nil) | |
101 | (scan env) | |
102 | prev found) | |
103 | ;; Handle deletions from the beginning of the list specially. | |
104 | (if (and (null value) | |
105 | (not keep-empty) | |
106 | env | |
107 | (stringp (car env)) | |
108 | (string-match pattern (car env))) | |
109 | (cdr env) | |
110 | ;; Try to find existing entry for VARIABLE in ENV. | |
111 | (while (and scan (stringp (car scan))) | |
112 | (when (string-match pattern (car scan)) | |
113 | (if value | |
114 | (setcar scan (concat variable "=" value)) | |
115 | (if keep-empty | |
116 | (setcar scan variable) | |
117 | (setcdr prev (cdr scan)))) | |
118 | (setq found t | |
119 | scan nil)) | |
120 | (setq prev scan | |
121 | scan (cdr scan))) | |
122 | (if (and (not found) (or value keep-empty)) | |
123 | (cons (if value | |
124 | (concat variable "=" value) | |
125 | variable) | |
126 | env) | |
127 | env)))) | |
128 | ||
5990851d | 129 | ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set? |
a4a216c5 | 130 | |
447b0165 | 131 | (defun setenv (variable &optional value substitute-env-vars frame) |
6449c898 | 132 | "Set the value of the environment variable named VARIABLE to VALUE. |
b71038b2 | 133 | VARIABLE should be a string. VALUE is optional; if not provided or |
17ccbd91 | 134 | nil, the environment variable VARIABLE will be removed. |
cbfe666b | 135 | |
17ccbd91 KL |
136 | Interactively, a prefix argument means to unset the variable, and |
137 | otherwise the current value (if any) of the variable appears at | |
138 | the front of the history list when you type in the new value. | |
139 | This function always replaces environment variables in the new | |
140 | value when called interactively. | |
99ac138a | 141 | |
27bdc650 RS |
142 | SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment |
143 | variables in VALUE with `substitute-env-vars', which see. | |
144 | This is normally used only for interactive calls. | |
145 | ||
a13f8f50 KL |
146 | If optional parameter FRAME is non-nil, this function modifies |
147 | only the frame-local value of VARIABLE on FRAME, ignoring | |
148 | `process-environment'. Note that frames on the same terminal | |
149 | device usually share their environment, so calling `setenv' on | |
150 | one of them affects the others as well. | |
151 | ||
152 | If FRAME is nil, `setenv' changes the global value of VARIABLE by | |
153 | modifying `process-environment'. Note that the global value | |
154 | overrides any frame-local values. | |
155 | ||
27bdc650 RS |
156 | The return value is the new value of VARIABLE, or nil if |
157 | it was removed from the environment. | |
158 | ||
1a90eae6 DL |
159 | As a special case, setting variable `TZ' calls `set-time-zone-rule' as |
160 | a side-effect." | |
cbfe666b RS |
161 | (interactive |
162 | (if current-prefix-arg | |
27bdc650 | 163 | (list (read-envvar-name "Clear environment variable: " 'exact) nil) |
2a5becfb GM |
164 | (let* ((var (read-envvar-name "Set environment variable: " nil)) |
165 | (value (getenv var))) | |
166 | (when value | |
4c5f6185 | 167 | (add-to-history 'setenv-history value)) |
99ac138a | 168 | ;; Here finally we specify the args to give call setenv with. |
71296446 | 169 | (list var |
a4a216c5 GM |
170 | (read-from-minibuffer (format "Set %s to value: " var) |
171 | nil nil nil 'setenv-history | |
172 | value) | |
a4a216c5 | 173 | t)))) |
1a90eae6 | 174 | (if (and (multibyte-string-p variable) locale-coding-system) |
1ebb05c4 KH |
175 | (let ((codings (find-coding-systems-string (concat variable value)))) |
176 | (unless (or (eq 'undecided (car codings)) | |
177 | (memq (coding-system-base locale-coding-system) codings)) | |
178 | (error "Can't encode `%s=%s' with `locale-coding-system'" | |
179 | variable (or value ""))))) | |
27bdc650 RS |
180 | (and value |
181 | substitute-env-vars | |
182 | (setq value (substitute-env-vars value))) | |
1a90eae6 DL |
183 | (if (multibyte-string-p variable) |
184 | (setq variable (encode-coding-string variable locale-coding-system))) | |
185 | (if (and value (multibyte-string-p value)) | |
186 | (setq value (encode-coding-string value locale-coding-system))) | |
6449c898 | 187 | (if (string-match "=" variable) |
f105f403 | 188 | (error "Environment variable name `%s' contains `='" variable)) |
a13f8f50 KL |
189 | (if (string-equal "TZ" variable) |
190 | (set-time-zone-rule value)) | |
191 | (if (null frame) | |
192 | (setq process-environment (setenv-internal process-environment | |
193 | variable value t)) | |
da8e8fc1 | 194 | (setq frame (frame-with-environment frame)) |
473a8c58 SM |
195 | (setq process-environment (setenv-internal process-environment |
196 | variable value nil))) | |
a13f8f50 | 197 | value) |
49116ac0 | 198 | |
da8e8fc1 | 199 | (defun getenv (variable &optional frame) |
b1e11b4f GM |
200 | "Get the value of environment variable VARIABLE. |
201 | VARIABLE should be a string. Value is nil if VARIABLE is undefined in | |
202 | the environment. Otherwise, value is a string. | |
203 | ||
da8e8fc1 | 204 | If optional parameter FRAME is non-nil, then it should be a |
a13f8f50 KL |
205 | frame. This function will look up VARIABLE in its 'environment |
206 | parameter. | |
f105f403 | 207 | |
da8e8fc1 | 208 | Otherwise, this function searches `process-environment' for |
a13f8f50 KL |
209 | VARIABLE. If it is not found there, then it continues the search |
210 | in the environment list of the selected frame." | |
b1e11b4f | 211 | (interactive (list (read-envvar-name "Get environment variable: " t))) |
1a90eae6 DL |
212 | (let ((value (getenv-internal (if (multibyte-string-p variable) |
213 | (encode-coding-string | |
214 | variable locale-coding-system) | |
36ab8612 MB |
215 | variable) |
216 | frame))) | |
1a90eae6 DL |
217 | (if (and enable-multibyte-characters value) |
218 | (setq value (decode-coding-string value locale-coding-system))) | |
b1e11b4f GM |
219 | (when (interactive-p) |
220 | (message "%s" (if value value "Not set"))) | |
221 | value)) | |
222 | ||
36ab8612 | 223 | (defun environment (&optional frame) |
5990851d KL |
224 | "Return a list of environment variables with their values. |
225 | Each entry in the list is a string of the form NAME=VALUE. | |
226 | ||
227 | The returned list can not be used to change environment | |
228 | variables, only read them. See `setenv' to do that. | |
229 | ||
36ab8612 MB |
230 | If optional parameter FRAME is non-nil, then it should be a |
231 | frame. The function returns the environment of that frame. | |
232 | ||
a13f8f50 KL |
233 | The list is constructed by concatenating the elements of |
234 | `process-environment' and the 'environment parameter of the | |
235 | selected frame, and removing duplicated and empty values. | |
5990851d KL |
236 | |
237 | Non-ASCII characters are encoded according to the initial value of | |
238 | `locale-coding-system', i.e. the elements must normally be decoded for use. | |
239 | See `setenv' and `getenv'." | |
a13f8f50 | 240 | (let* ((env (append process-environment |
de87fb59 DN |
241 | ;; (frame-parameter (frame-with-environment frame) |
242 | ;; 'environment) | |
a13f8f50 KL |
243 | nil)) |
244 | (scan env) | |
245 | prev seen) | |
246 | ;; Remove unset variables from the beginning of the list. | |
247 | (while (and env | |
248 | (or (not (stringp (car env))) | |
249 | (not (string-match "=" (car env))))) | |
250 | (or (member (car env) seen) | |
251 | (setq seen (cons (car env) seen))) | |
252 | (setq env (cdr env) | |
253 | scan env)) | |
254 | (let (name) | |
255 | (while scan | |
256 | (cond ((or (not (stringp (car scan))) | |
257 | (not (string-match "=" (car scan)))) | |
5990851d | 258 | ;; Unset variable. |
a13f8f50 KL |
259 | (or (member (car scan) seen) |
260 | (setq seen (cons (car scan) seen))) | |
261 | (setcdr prev (cdr scan))) | |
262 | ((member (setq name (substring (car scan) 0 (string-match "=" (car scan)))) seen) | |
263 | ;; Duplicated variable. | |
264 | (setcdr prev (cdr scan))) | |
5990851d KL |
265 | (t |
266 | ;; New variable. | |
a13f8f50 KL |
267 | (setq seen (cons name seen)))) |
268 | (setq prev scan | |
269 | scan (cdr scan)))) | |
5990851d KL |
270 | env)) |
271 | ||
272 | (defmacro let-environment (varlist &rest body) | |
273 | "Evaluate BODY with environment variables set according to VARLIST. | |
274 | The environment variables are then restored to their previous | |
275 | values. | |
276 | The value of the last form in BODY is returned. | |
277 | ||
278 | Each element of VARLIST is either a string (which variable is | |
279 | then removed from the environment), or a list (NAME | |
280 | VALUEFORM) (which sets NAME to the value of VALUEFORM, a string). | |
281 | All the VALUEFORMs are evaluated before any variables are set." | |
282 | (declare (indent 2)) | |
283 | (let ((old-env (make-symbol "old-env")) | |
284 | (name (make-symbol "name")) | |
285 | (value (make-symbol "value")) | |
286 | (entry (make-symbol "entry")) | |
287 | (frame (make-symbol "frame"))) | |
288 | `(let ((,frame (selected-frame)) | |
289 | ,old-env) | |
290 | ;; Evaluate VALUEFORMs and replace them in VARLIST with their values. | |
291 | (dolist (,entry ,varlist) | |
292 | (unless (stringp ,entry) | |
293 | (if (cdr (cdr ,entry)) | |
294 | (error "`let-environment' bindings can have only one value-form")) | |
295 | (setcdr ,entry (eval (cadr ,entry))))) | |
296 | ;; Set the variables. | |
297 | (dolist (,entry ,varlist) | |
298 | (let ((,name (if (stringp ,entry) ,entry (car ,entry))) | |
299 | (,value (if (consp ,entry) (cdr ,entry)))) | |
300 | (setq ,old-env (cons (cons ,name (getenv ,name)) ,old-env)) | |
301 | (setenv ,name ,value))) | |
302 | (unwind-protect | |
303 | (progn ,@body) | |
304 | ;; Restore old values. | |
305 | (with-selected-frame (if (frame-live-p ,frame) | |
306 | ,frame | |
307 | (selected-frame)) | |
308 | (dolist (,entry ,old-env) | |
309 | (setenv (car ,entry) (cdr ,entry)))))))) | |
310 | ||
1bbda2d6 NF |
311 | (provide 'env) |
312 | ||
bb8ef973 | 313 | ;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8 |
1bbda2d6 | 314 | ;;; env.el ends here |