1 ;;; env.el --- functions to manipulate environment variables
3 ;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
7 ;; Keywords: processes, unix
9 ;; This file is part of GNU Emacs.
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 3, or (at your option)
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.
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
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
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.
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.
39 (eval-when-compile (require 'cl
))
41 ;; History list for environment variable names.
42 (defvar read-envvar-name-history nil
)
44 (defun read-envvar-name (prompt &optional mustmatch
)
45 "Read environment variable name, prompting with PROMPT.
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."
48 (completing-read prompt
49 (mapcar (lambda (enventry)
50 (list (if enable-multibyte-characters
53 (string-match "=" enventry
))
54 locale-coding-system t
)
56 (string-match "=" enventry
)))))
57 (append process-environment
58 nil
;;(frame-parameter (frame-with-environment) 'environment)
60 nil mustmatch nil
'read-envvar-name-history
))
62 ;; History list for VALUE argument to setenv.
63 (defvar setenv-history nil
)
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
71 the entire variable name in braces. For instance, in `ab$cd-x',
72 `$cd' is treated as an environment variable.
74 Use `$$' to insert a single dollar sign."
78 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
79 (and "${" (submatch (minimal-match (0+ anything
))) "}")
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
)))))
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
)))))
91 (setq string
(replace-match "$" t t string
)
92 start
(+ (match-beginning 0) 1)))))
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
)
103 ;; Handle deletions from the beginning of the list specially.
104 (if (and (null value
)
108 (string-match pattern
(car 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
))
114 (setcar scan
(concat variable
"=" value
))
116 (setcar scan variable
)
117 (setcdr prev
(cdr scan
))))
122 (if (and (not found
) (or value keep-empty
))
124 (concat variable
"=" value
)
129 ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
131 (defun setenv (variable &optional value substitute-env-vars frame
)
132 "Set the value of the environment variable named VARIABLE to VALUE.
133 VARIABLE should be a string. VALUE is optional; if not provided or
134 nil, the environment variable VARIABLE will be removed.
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.
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.
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.
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.
156 The return value is the new value of VARIABLE, or nil if
157 it was removed from the environment.
159 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
162 (if current-prefix-arg
163 (list (read-envvar-name "Clear environment variable: " 'exact
) nil
)
164 (let* ((var (read-envvar-name "Set environment variable: " nil
))
165 (value (getenv var
)))
167 (add-to-history 'setenv-history value
))
168 ;; Here finally we specify the args to give call setenv with.
170 (read-from-minibuffer (format "Set %s to value: " var
)
171 nil nil nil
'setenv-history
174 (if (and (multibyte-string-p variable
) locale-coding-system
)
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
"")))))
182 (setq value
(substitute-env-vars value
)))
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
)))
187 (if (string-match "=" variable
)
188 (error "Environment variable name `%s' contains `='" variable
))
189 (if (string-equal "TZ" variable
)
190 (set-time-zone-rule value
))
192 (setq process-environment
(setenv-internal process-environment
194 (setq frame
(frame-with-environment frame
))
196 ((string-equal "DISPLAY" variable
)
197 (set-frame-parameter frame
'display-environment-variable value
))
199 (setq process-environment
(setenv-internal process-environment
200 variable value nil
)))))
203 (defun getenv (variable &optional frame
)
204 "Get the value of environment variable VARIABLE.
205 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
206 the environment. Otherwise, value is a string.
208 If optional parameter FRAME is non-nil, then it should be a
209 frame. This function will look up VARIABLE in its 'environment
212 Otherwise, this function searches `process-environment' for
213 VARIABLE. If it is not found there, then it continues the search
214 in the environment list of the selected frame."
215 (interactive (list (read-envvar-name "Get environment variable: " t
)))
216 (let ((value (getenv-internal (if (multibyte-string-p variable
)
217 (encode-coding-string
218 variable locale-coding-system
)
221 (if (and enable-multibyte-characters value
)
222 (setq value
(decode-coding-string value locale-coding-system
)))
223 (when (interactive-p)
224 (message "%s" (if value value
"Not set")))
227 (defun environment (&optional frame
)
228 "Return a list of environment variables with their values.
229 Each entry in the list is a string of the form NAME=VALUE.
231 The returned list can not be used to change environment
232 variables, only read them. See `setenv' to do that.
234 If optional parameter FRAME is non-nil, then it should be a
235 frame. The function returns the environment of that frame.
237 The list is constructed by concatenating the elements of
238 `process-environment' and the 'environment parameter of the
239 selected frame, and removing duplicated and empty values.
241 Non-ASCII characters are encoded according to the initial value of
242 `locale-coding-system', i.e. the elements must normally be decoded for use.
243 See `setenv' and `getenv'."
244 (let* ((env (append process-environment
245 ;; (frame-parameter (frame-with-environment frame)
250 ;; Remove unset variables from the beginning of the list.
252 (or (not (stringp (car env
)))
253 (not (string-match "=" (car env
)))))
254 (or (member (car env
) seen
)
255 (setq seen
(cons (car env
) seen
)))
260 (cond ((or (not (stringp (car scan
)))
261 (not (string-match "=" (car scan
))))
263 (or (member (car scan
) seen
)
264 (setq seen
(cons (car scan
) seen
)))
265 (setcdr prev
(cdr scan
)))
266 ((member (setq name
(substring (car scan
) 0 (string-match "=" (car scan
)))) seen
)
267 ;; Duplicated variable.
268 (setcdr prev
(cdr scan
)))
271 (setq seen
(cons name seen
))))
276 (defmacro let-environment
(varlist &rest body
)
277 "Evaluate BODY with environment variables set according to VARLIST.
278 The environment variables are then restored to their previous
280 The value of the last form in BODY is returned.
282 Each element of VARLIST is either a string (which variable is
283 then removed from the environment), or a list (NAME
284 VALUEFORM) (which sets NAME to the value of VALUEFORM, a string).
285 All the VALUEFORMs are evaluated before any variables are set."
287 (let ((old-env (make-symbol "old-env"))
288 (name (make-symbol "name"))
289 (value (make-symbol "value"))
290 (entry (make-symbol "entry"))
291 (frame (make-symbol "frame")))
292 `(let ((,frame
(selected-frame))
294 ;; Evaluate VALUEFORMs and replace them in VARLIST with their values.
295 (dolist (,entry
,varlist
)
296 (unless (stringp ,entry
)
297 (if (cdr (cdr ,entry
))
298 (error "`let-environment' bindings can have only one value-form"))
299 (setcdr ,entry
(eval (cadr ,entry
)))))
300 ;; Set the variables.
301 (dolist (,entry
,varlist
)
302 (let ((,name
(if (stringp ,entry
) ,entry
(car ,entry
)))
303 (,value
(if (consp ,entry
) (cdr ,entry
))))
304 (setq ,old-env
(cons (cons ,name
(getenv ,name
)) ,old-env
))
305 (setenv ,name
,value
)))
308 ;; Restore old values.
309 (with-selected-frame (if (frame-live-p ,frame
)
312 (dolist (,entry
,old-env
)
313 (setenv (car ,entry
) (cdr ,entry
))))))))
317 ;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8