Terminology cleanup.
[emacs.git] / lisp / env.el
blob128228be3dbc2d75c165479c7ca6222ba762938d
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.
6 ;; Maintainer: FSF
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)
14 ;; any later version.
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.
26 ;;; Commentary:
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.
37 ;;; Code:
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
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)
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."
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)))))
91 (setq string (replace-match "$" t t string)
92 start (+ (match-beginning 0) 1)))))
93 string))
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)
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))))
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
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 (setq process-environment (setenv-internal process-environment
196 variable value nil)))
197 value)
199 (defun getenv (variable &optional frame)
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.
204 If optional parameter FRAME is non-nil, then it should be a
205 frame. This function will look up VARIABLE in its 'environment
206 parameter.
208 Otherwise, this function searches `process-environment' for
209 VARIABLE. If it is not found there, then it continues the search
210 in the environment list of the selected frame."
211 (interactive (list (read-envvar-name "Get environment variable: " t)))
212 (let ((value (getenv-internal (if (multibyte-string-p variable)
213 (encode-coding-string
214 variable locale-coding-system)
215 variable)
216 frame)))
217 (if (and enable-multibyte-characters value)
218 (setq value (decode-coding-string value locale-coding-system)))
219 (when (interactive-p)
220 (message "%s" (if value value "Not set")))
221 value))
223 (defun environment (&optional frame)
224 "Return a list of environment variables with their values.
225 Each entry in the list is a string of the form NAME=VALUE.
227 The returned list can not be used to change environment
228 variables, only read them. See `setenv' to do that.
230 If optional parameter FRAME is non-nil, then it should be a
231 frame. The function returns the environment of that frame.
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.
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'."
240 (let* ((env (append process-environment
241 ;; (frame-parameter (frame-with-environment frame)
242 ;; 'environment)
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))))
258 ;; Unset variable.
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)))
266 ;; New variable.
267 (setq seen (cons name seen))))
268 (setq prev scan
269 scan (cdr scan))))
270 env))
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.
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))))))))
311 (provide 'env)
313 ;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
314 ;;; env.el ends here