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, |
409cc4a3 | 4 | ;; 2005, 2006, 2007, 2008 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 | 49 | (mapcar (lambda (enventry) |
ef651d13 SM |
50 | (let ((str (substring enventry 0 |
51 | (string-match "=" enventry)))) | |
52 | (if (multibyte-string-p str) | |
53 | (decode-coding-string | |
54 | str locale-coding-system t) | |
55 | str))) | |
5990851d | 56 | (append process-environment |
ef651d13 | 57 | ;;(frame-environment) |
de87fb59 | 58 | )) |
99ac138a RS |
59 | nil mustmatch nil 'read-envvar-name-history)) |
60 | ||
61 | ;; History list for VALUE argument to setenv. | |
62 | (defvar setenv-history nil) | |
63 | ||
a4a216c5 GM |
64 | |
65 | (defun substitute-env-vars (string) | |
66 | "Substitute environment variables referred to in STRING. | |
67 | `$FOO' where FOO is an environment variable name means to substitute | |
68 | the value of that variable. The variable name should be terminated | |
69 | with a character not a letter, digit or underscore; otherwise, enclose | |
cccc806d RS |
70 | the entire variable name in braces. For instance, in `ab$cd-x', |
71 | `$cd' is treated as an environment variable. | |
72 | ||
73 | Use `$$' to insert a single dollar sign." | |
a4a216c5 | 74 | (let ((start 0)) |
71296446 | 75 | (while (string-match |
1a90eae6 | 76 | (eval-when-compile |
cccc806d | 77 | (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]")))) |
1a90eae6 DL |
78 | (and "${" (submatch (minimal-match (0+ anything))) "}") |
79 | "$$"))) | |
a4a216c5 GM |
80 | string start) |
81 | (cond ((match-beginning 1) | |
82 | (let ((value (getenv (match-string 1 string)))) | |
83 | (setq string (replace-match (or value "") t t string) | |
84 | start (+ (match-beginning 0) (length value))))) | |
85 | ((match-beginning 2) | |
86 | (let ((value (getenv (match-string 2 string)))) | |
87 | (setq string (replace-match (or value "") t t string) | |
88 | start (+ (match-beginning 0) (length value))))) | |
89 | (t | |
90 | (setq string (replace-match "$" t t string) | |
91 | start (+ (match-beginning 0) 1))))) | |
92 | string)) | |
93 | ||
a13f8f50 KL |
94 | |
95 | (defun setenv-internal (env variable value keep-empty) | |
96 | "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY. | |
97 | Changes ENV by side-effect, and returns its new value." | |
98 | (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)")) | |
99 | (case-fold-search nil) | |
100 | (scan env) | |
101 | prev found) | |
102 | ;; Handle deletions from the beginning of the list specially. | |
103 | (if (and (null value) | |
104 | (not keep-empty) | |
105 | env | |
106 | (stringp (car env)) | |
107 | (string-match pattern (car env))) | |
108 | (cdr env) | |
109 | ;; Try to find existing entry for VARIABLE in ENV. | |
110 | (while (and scan (stringp (car scan))) | |
111 | (when (string-match pattern (car scan)) | |
112 | (if value | |
113 | (setcar scan (concat variable "=" value)) | |
114 | (if keep-empty | |
115 | (setcar scan variable) | |
116 | (setcdr prev (cdr scan)))) | |
117 | (setq found t | |
118 | scan nil)) | |
119 | (setq prev scan | |
120 | scan (cdr scan))) | |
121 | (if (and (not found) (or value keep-empty)) | |
122 | (cons (if value | |
123 | (concat variable "=" value) | |
124 | variable) | |
125 | env) | |
126 | env)))) | |
127 | ||
5990851d | 128 | ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set? |
a4a216c5 | 129 | |
ef651d13 | 130 | (defun setenv (variable &optional value substitute-env-vars) |
6449c898 | 131 | "Set the value of the environment variable named VARIABLE to VALUE. |
b71038b2 | 132 | VARIABLE should be a string. VALUE is optional; if not provided or |
17ccbd91 | 133 | nil, the environment variable VARIABLE will be removed. |
cbfe666b | 134 | |
17ccbd91 KL |
135 | Interactively, a prefix argument means to unset the variable, and |
136 | otherwise the current value (if any) of the variable appears at | |
137 | the front of the history list when you type in the new value. | |
138 | This function always replaces environment variables in the new | |
139 | value when called interactively. | |
99ac138a | 140 | |
27bdc650 RS |
141 | SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment |
142 | variables in VALUE with `substitute-env-vars', which see. | |
143 | This is normally used only for interactive calls. | |
144 | ||
145 | The return value is the new value of VARIABLE, or nil if | |
146 | it was removed from the environment. | |
147 | ||
ef651d13 SM |
148 | This function works by modifying `process-environment'. |
149 | ||
1a90eae6 DL |
150 | As a special case, setting variable `TZ' calls `set-time-zone-rule' as |
151 | a side-effect." | |
cbfe666b RS |
152 | (interactive |
153 | (if current-prefix-arg | |
27bdc650 | 154 | (list (read-envvar-name "Clear environment variable: " 'exact) nil) |
2a5becfb GM |
155 | (let* ((var (read-envvar-name "Set environment variable: " nil)) |
156 | (value (getenv var))) | |
157 | (when value | |
4c5f6185 | 158 | (add-to-history 'setenv-history value)) |
99ac138a | 159 | ;; Here finally we specify the args to give call setenv with. |
71296446 | 160 | (list var |
a4a216c5 GM |
161 | (read-from-minibuffer (format "Set %s to value: " var) |
162 | nil nil nil 'setenv-history | |
163 | value) | |
a4a216c5 | 164 | t)))) |
1a90eae6 | 165 | (if (and (multibyte-string-p variable) locale-coding-system) |
1ebb05c4 KH |
166 | (let ((codings (find-coding-systems-string (concat variable value)))) |
167 | (unless (or (eq 'undecided (car codings)) | |
168 | (memq (coding-system-base locale-coding-system) codings)) | |
169 | (error "Can't encode `%s=%s' with `locale-coding-system'" | |
170 | variable (or value ""))))) | |
27bdc650 RS |
171 | (and value |
172 | substitute-env-vars | |
173 | (setq value (substitute-env-vars value))) | |
1a90eae6 DL |
174 | (if (multibyte-string-p variable) |
175 | (setq variable (encode-coding-string variable locale-coding-system))) | |
176 | (if (and value (multibyte-string-p value)) | |
177 | (setq value (encode-coding-string value locale-coding-system))) | |
6449c898 | 178 | (if (string-match "=" variable) |
f105f403 | 179 | (error "Environment variable name `%s' contains `='" variable)) |
a13f8f50 KL |
180 | (if (string-equal "TZ" variable) |
181 | (set-time-zone-rule value)) | |
ef651d13 SM |
182 | (setq process-environment (setenv-internal process-environment |
183 | variable value t)) | |
a13f8f50 | 184 | value) |
49116ac0 | 185 | |
da8e8fc1 | 186 | (defun getenv (variable &optional frame) |
b1e11b4f GM |
187 | "Get the value of environment variable VARIABLE. |
188 | VARIABLE should be a string. Value is nil if VARIABLE is undefined in | |
189 | the environment. Otherwise, value is a string. | |
190 | ||
da8e8fc1 | 191 | If optional parameter FRAME is non-nil, then it should be a |
a13f8f50 KL |
192 | frame. This function will look up VARIABLE in its 'environment |
193 | parameter. | |
f105f403 | 194 | |
da8e8fc1 | 195 | Otherwise, this function searches `process-environment' for |
a13f8f50 KL |
196 | VARIABLE. If it is not found there, then it continues the search |
197 | in the environment list of the selected frame." | |
b1e11b4f | 198 | (interactive (list (read-envvar-name "Get environment variable: " t))) |
1a90eae6 DL |
199 | (let ((value (getenv-internal (if (multibyte-string-p variable) |
200 | (encode-coding-string | |
201 | variable locale-coding-system) | |
36ab8612 MB |
202 | variable) |
203 | frame))) | |
1a90eae6 DL |
204 | (if (and enable-multibyte-characters value) |
205 | (setq value (decode-coding-string value locale-coding-system))) | |
b1e11b4f GM |
206 | (when (interactive-p) |
207 | (message "%s" (if value value "Not set"))) | |
208 | value)) | |
209 | ||
36ab8612 | 210 | (defun environment (&optional frame) |
5990851d KL |
211 | "Return a list of environment variables with their values. |
212 | Each entry in the list is a string of the form NAME=VALUE. | |
213 | ||
214 | The returned list can not be used to change environment | |
215 | variables, only read them. See `setenv' to do that. | |
216 | ||
36ab8612 MB |
217 | If optional parameter FRAME is non-nil, then it should be a |
218 | frame. The function returns the environment of that frame. | |
219 | ||
a13f8f50 KL |
220 | The list is constructed by concatenating the elements of |
221 | `process-environment' and the 'environment parameter of the | |
222 | selected frame, and removing duplicated and empty values. | |
5990851d KL |
223 | |
224 | Non-ASCII characters are encoded according to the initial value of | |
225 | `locale-coding-system', i.e. the elements must normally be decoded for use. | |
226 | See `setenv' and `getenv'." | |
a13f8f50 | 227 | (let* ((env (append process-environment |
ef651d13 | 228 | ;; (frame-environment frame) |
a13f8f50 KL |
229 | nil)) |
230 | (scan env) | |
231 | prev seen) | |
232 | ;; Remove unset variables from the beginning of the list. | |
233 | (while (and env | |
234 | (or (not (stringp (car env))) | |
235 | (not (string-match "=" (car env))))) | |
236 | (or (member (car env) seen) | |
237 | (setq seen (cons (car env) seen))) | |
238 | (setq env (cdr env) | |
239 | scan env)) | |
240 | (let (name) | |
241 | (while scan | |
242 | (cond ((or (not (stringp (car scan))) | |
243 | (not (string-match "=" (car scan)))) | |
5990851d | 244 | ;; Unset variable. |
a13f8f50 KL |
245 | (or (member (car scan) seen) |
246 | (setq seen (cons (car scan) seen))) | |
247 | (setcdr prev (cdr scan))) | |
248 | ((member (setq name (substring (car scan) 0 (string-match "=" (car scan)))) seen) | |
249 | ;; Duplicated variable. | |
250 | (setcdr prev (cdr scan))) | |
5990851d KL |
251 | (t |
252 | ;; New variable. | |
a13f8f50 KL |
253 | (setq seen (cons name seen)))) |
254 | (setq prev scan | |
255 | scan (cdr scan)))) | |
5990851d KL |
256 | env)) |
257 | ||
1bbda2d6 NF |
258 | (provide 'env) |
259 | ||
bb8ef973 | 260 | ;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8 |
1bbda2d6 | 261 | ;;; env.el ends here |