(list-islamic-diary-entries): Doc fix.
[emacs.git] / lisp / env.el
blob4538e2dd715ff3d4125ad3cd561c33f078b50291
1 ;;; env.el --- functions to manipulate environment variables
3 ;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 (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)))
56 (append process-environment
57 ;;(frame-environment)
59 nil mustmatch nil 'read-envvar-name-history))
61 ;; History list for VALUE argument to setenv.
62 (defvar setenv-history nil)
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
70 the entire variable name in braces. For instance, in `ab$cd-x',
71 `$cd' is treated as an environment variable.
73 Use `$$' to insert a single dollar sign."
74 (let ((start 0))
75 (while (string-match
76 (eval-when-compile
77 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
78 (and "${" (submatch (minimal-match (0+ anything))) "}")
79 "$$")))
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)))))
90 (setq string (replace-match "$" t t string)
91 start (+ (match-beginning 0) 1)))))
92 string))
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)
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))))
128 ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
130 (defun setenv (variable &optional value substitute-env-vars)
131 "Set the value of the environment variable named VARIABLE to VALUE.
132 VARIABLE should be a string. VALUE is optional; if not provided or
133 nil, the environment variable VARIABLE will be removed.
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.
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.
145 The return value is the new value of VARIABLE, or nil if
146 it was removed from the environment.
148 This function works by modifying `process-environment'.
150 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
151 a side-effect."
152 (interactive
153 (if current-prefix-arg
154 (list (read-envvar-name "Clear environment variable: " 'exact) nil)
155 (let* ((var (read-envvar-name "Set environment variable: " nil))
156 (value (getenv var)))
157 (when value
158 (add-to-history 'setenv-history value))
159 ;; Here finally we specify the args to give call setenv with.
160 (list var
161 (read-from-minibuffer (format "Set %s to value: " var)
162 nil nil nil 'setenv-history
163 value)
164 t))))
165 (if (and (multibyte-string-p variable) locale-coding-system)
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 "")))))
171 (and value
172 substitute-env-vars
173 (setq value (substitute-env-vars value)))
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)))
178 (if (string-match "=" variable)
179 (error "Environment variable name `%s' contains `='" variable))
180 (if (string-equal "TZ" variable)
181 (set-time-zone-rule value))
182 (setq process-environment (setenv-internal process-environment
183 variable value t))
184 value)
186 (defun getenv (variable &optional frame)
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.
191 If optional parameter FRAME is non-nil, then it should be a
192 frame. This function will look up VARIABLE in its 'environment
193 parameter.
195 Otherwise, this function searches `process-environment' for
196 VARIABLE. If it is not found there, then it continues the search
197 in the environment list of the selected frame."
198 (interactive (list (read-envvar-name "Get environment variable: " t)))
199 (let ((value (getenv-internal (if (multibyte-string-p variable)
200 (encode-coding-string
201 variable locale-coding-system)
202 variable)
203 frame)))
204 (if (and enable-multibyte-characters value)
205 (setq value (decode-coding-string value locale-coding-system)))
206 (when (interactive-p)
207 (message "%s" (if value value "Not set")))
208 value))
210 (defun environment (&optional frame)
211 "Return a list of environment variables with their values.
212 Each entry in the list is a string of the form NAME=VALUE.
214 The returned list can not be used to change environment
215 variables, only read them. See `setenv' to do that.
217 If optional parameter FRAME is non-nil, then it should be a
218 frame. The function returns the environment of that frame.
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.
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'."
227 (let* ((env (append process-environment
228 ;; (frame-environment frame)
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))))
244 ;; Unset variable.
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)))
252 ;; New variable.
253 (setq seen (cons name seen))))
254 (setq prev scan
255 scan (cdr scan))))
256 env))
258 (provide 'env)
260 ;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
261 ;;; env.el ends here