]> code.delx.au - gnu-emacs/blob - lisp/env.el
Merge from emacs--devo--0
[gnu-emacs] / lisp / env.el
1 ;;; env.el --- functions to manipulate environment variables
2
3 ;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: processes, unix
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.
32
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
37 ;;; Code:
38
39 (eval-when-compile (require 'cl))
40
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.
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
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)))))
57 (append process-environment
58 nil ;;(frame-parameter (frame-with-environment) 'environment)
59 ))
60 nil mustmatch nil 'read-envvar-name-history))
61
62 ;; History list for VALUE argument to setenv.
63 (defvar setenv-history nil)
64
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
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."
75 (let ((start 0))
76 (while (string-match
77 (eval-when-compile
78 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
79 (and "${" (submatch (minimal-match (0+ anything))) "}")
80 "$$")))
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
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
129 ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
130
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.
135
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.
141
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
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
156 The return value is the new value of VARIABLE, or nil if
157 it was removed from the environment.
158
159 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
160 a side-effect."
161 (interactive
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)))
166 (when value
167 (add-to-history 'setenv-history value))
168 ;; Here finally we specify the args to give call setenv with.
169 (list var
170 (read-from-minibuffer (format "Set %s to value: " var)
171 nil nil nil 'setenv-history
172 value)
173 t))))
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 "")))))
180 (and value
181 substitute-env-vars
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))
191 (if (null frame)
192 (setq process-environment (setenv-internal process-environment
193 variable value t))
194 (setq frame (frame-with-environment frame))
195 (cond
196 ((string-equal "TERM" variable)
197 (set-frame-parameter frame 'term-environment-variable value))
198 ((string-equal "DISPLAY" variable)
199 (set-frame-parameter frame 'display-environment-variable value))
200 (t
201 (setq process-environment (setenv-internal process-environment
202 variable value nil)))))
203 value)
204
205 (defun getenv (variable &optional frame)
206 "Get the value of environment variable VARIABLE.
207 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
208 the environment. Otherwise, value is a string.
209
210 If optional parameter FRAME is non-nil, then it should be a
211 frame. This function will look up VARIABLE in its 'environment
212 parameter.
213
214 Otherwise, this function searches `process-environment' for
215 VARIABLE. If it is not found there, then it continues the search
216 in the environment list of the selected frame."
217 (interactive (list (read-envvar-name "Get environment variable: " t)))
218 (let ((value (getenv-internal (if (multibyte-string-p variable)
219 (encode-coding-string
220 variable locale-coding-system)
221 variable)
222 frame)))
223 (if (and enable-multibyte-characters value)
224 (setq value (decode-coding-string value locale-coding-system)))
225 (when (interactive-p)
226 (message "%s" (if value value "Not set")))
227 value))
228
229 (defun environment (&optional frame)
230 "Return a list of environment variables with their values.
231 Each entry in the list is a string of the form NAME=VALUE.
232
233 The returned list can not be used to change environment
234 variables, only read them. See `setenv' to do that.
235
236 If optional parameter FRAME is non-nil, then it should be a
237 frame. The function returns the environment of that frame.
238
239 The list is constructed by concatenating the elements of
240 `process-environment' and the 'environment parameter of the
241 selected frame, and removing duplicated and empty values.
242
243 Non-ASCII characters are encoded according to the initial value of
244 `locale-coding-system', i.e. the elements must normally be decoded for use.
245 See `setenv' and `getenv'."
246 (let* ((env (append process-environment
247 ;; (frame-parameter (frame-with-environment frame)
248 ;; 'environment)
249 nil))
250 (scan env)
251 prev seen)
252 ;; Remove unset variables from the beginning of the list.
253 (while (and env
254 (or (not (stringp (car env)))
255 (not (string-match "=" (car env)))))
256 (or (member (car env) seen)
257 (setq seen (cons (car env) seen)))
258 (setq env (cdr env)
259 scan env))
260 (let (name)
261 (while scan
262 (cond ((or (not (stringp (car scan)))
263 (not (string-match "=" (car scan))))
264 ;; Unset variable.
265 (or (member (car scan) seen)
266 (setq seen (cons (car scan) seen)))
267 (setcdr prev (cdr scan)))
268 ((member (setq name (substring (car scan) 0 (string-match "=" (car scan)))) seen)
269 ;; Duplicated variable.
270 (setcdr prev (cdr scan)))
271 (t
272 ;; New variable.
273 (setq seen (cons name seen))))
274 (setq prev scan
275 scan (cdr scan))))
276 env))
277
278 (defmacro let-environment (varlist &rest body)
279 "Evaluate BODY with environment variables set according to VARLIST.
280 The environment variables are then restored to their previous
281 values.
282 The value of the last form in BODY is returned.
283
284 Each element of VARLIST is either a string (which variable is
285 then removed from the environment), or a list (NAME
286 VALUEFORM) (which sets NAME to the value of VALUEFORM, a string).
287 All the VALUEFORMs are evaluated before any variables are set."
288 (declare (indent 2))
289 (let ((old-env (make-symbol "old-env"))
290 (name (make-symbol "name"))
291 (value (make-symbol "value"))
292 (entry (make-symbol "entry"))
293 (frame (make-symbol "frame")))
294 `(let ((,frame (selected-frame))
295 ,old-env)
296 ;; Evaluate VALUEFORMs and replace them in VARLIST with their values.
297 (dolist (,entry ,varlist)
298 (unless (stringp ,entry)
299 (if (cdr (cdr ,entry))
300 (error "`let-environment' bindings can have only one value-form"))
301 (setcdr ,entry (eval (cadr ,entry)))))
302 ;; Set the variables.
303 (dolist (,entry ,varlist)
304 (let ((,name (if (stringp ,entry) ,entry (car ,entry)))
305 (,value (if (consp ,entry) (cdr ,entry))))
306 (setq ,old-env (cons (cons ,name (getenv ,name)) ,old-env))
307 (setenv ,name ,value)))
308 (unwind-protect
309 (progn ,@body)
310 ;; Restore old values.
311 (with-selected-frame (if (frame-live-p ,frame)
312 ,frame
313 (selected-frame))
314 (dolist (,entry ,old-env)
315 (setenv (car ,entry) (cdr ,entry))))))))
316
317 (provide 'env)
318
319 ;;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
320 ;;; env.el ends here