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