(nnimap-retrieve-headers-progress): Don't use nnimap-demule
[emacs.git] / lisp / env.el
blob06ee533d74c092f84140ecd5135beb52304162bd
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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
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.
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.
35 ;;; Code:
37 (eval-when-compile (require 'cl))
39 ;; History list for environment variable names.
40 (defvar read-envvar-name-history nil)
42 (defun read-envvar-name (prompt &optional mustmatch)
43 "Read environment variable name, prompting with PROMPT.
44 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
45 If it is also not t, RET does not exit if it does non-null completion."
46 (completing-read prompt
47 (mapcar (lambda (enventry)
48 (let ((str (substring enventry 0
49 (string-match "=" enventry))))
50 (if (multibyte-string-p str)
51 (decode-coding-string
52 str locale-coding-system t)
53 str)))
54 (append process-environment
55 ;;(frame-environment)
57 nil mustmatch nil 'read-envvar-name-history))
59 ;; History list for VALUE argument to setenv.
60 (defvar setenv-history nil)
63 (defun substitute-env-vars (string)
64 "Substitute environment variables referred to in STRING.
65 `$FOO' where FOO is an environment variable name means to substitute
66 the value of that variable. The variable name should be terminated
67 with a character not a letter, digit or underscore; otherwise, enclose
68 the entire variable name in braces. For instance, in `ab$cd-x',
69 `$cd' is treated as an environment variable.
71 Use `$$' to insert a single dollar sign."
72 (let ((start 0))
73 (while (string-match
74 (eval-when-compile
75 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
76 (and "${" (submatch (minimal-match (0+ anything))) "}")
77 "$$")))
78 string start)
79 (cond ((match-beginning 1)
80 (let ((value (getenv (match-string 1 string))))
81 (setq string (replace-match (or value "") t t string)
82 start (+ (match-beginning 0) (length value)))))
83 ((match-beginning 2)
84 (let ((value (getenv (match-string 2 string))))
85 (setq string (replace-match (or value "") t t string)
86 start (+ (match-beginning 0) (length value)))))
88 (setq string (replace-match "$" t t string)
89 start (+ (match-beginning 0) 1)))))
90 string))
93 (defun setenv-internal (env variable value keep-empty)
94 "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY.
95 Changes ENV by side-effect, and returns its new value."
96 (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)"))
97 (case-fold-search nil)
98 (scan env)
99 prev found)
100 ;; Handle deletions from the beginning of the list specially.
101 (if (and (null value)
102 (not keep-empty)
104 (stringp (car env))
105 (string-match pattern (car env)))
106 (cdr env)
107 ;; Try to find existing entry for VARIABLE in ENV.
108 (while (and scan (stringp (car scan)))
109 (when (string-match pattern (car scan))
110 (if value
111 (setcar scan (concat variable "=" value))
112 (if keep-empty
113 (setcar scan variable)
114 (setcdr prev (cdr scan))))
115 (setq found t
116 scan nil))
117 (setq prev scan
118 scan (cdr scan)))
119 (if (and (not found) (or value keep-empty))
120 (cons (if value
121 (concat variable "=" value)
122 variable)
123 env)
124 env))))
126 ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
128 (defun setenv (variable &optional value substitute-env-vars)
129 "Set the value of the environment variable named VARIABLE to VALUE.
130 VARIABLE should be a string. VALUE is optional; if not provided or
131 nil, the environment variable VARIABLE will be removed.
133 Interactively, a prefix argument means to unset the variable, and
134 otherwise the current value (if any) of the variable appears at
135 the front of the history list when you type in the new value.
136 This function always replaces environment variables in the new
137 value when called interactively.
139 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
140 variables in VALUE with `substitute-env-vars', which see.
141 This is normally used only for interactive calls.
143 The return value is the new value of VARIABLE, or nil if
144 it was removed from the environment.
146 This function works by modifying `process-environment'.
148 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
149 a side-effect."
150 (interactive
151 (if current-prefix-arg
152 (list (read-envvar-name "Clear environment variable: " 'exact) nil)
153 (let* ((var (read-envvar-name "Set environment variable: " nil))
154 (value (getenv var)))
155 (when value
156 (add-to-history 'setenv-history value))
157 ;; Here finally we specify the args to give call setenv with.
158 (list var
159 (read-from-minibuffer (format "Set %s to value: " var)
160 nil nil nil 'setenv-history
161 value)
162 t))))
163 (if (and (multibyte-string-p variable) locale-coding-system)
164 (let ((codings (find-coding-systems-string (concat variable value))))
165 (unless (or (eq 'undecided (car codings))
166 (memq (coding-system-base locale-coding-system) codings))
167 (error "Can't encode `%s=%s' with `locale-coding-system'"
168 variable (or value "")))))
169 (and value
170 substitute-env-vars
171 (setq value (substitute-env-vars value)))
172 (if (multibyte-string-p variable)
173 (setq variable (encode-coding-string variable locale-coding-system)))
174 (if (and value (multibyte-string-p value))
175 (setq value (encode-coding-string value locale-coding-system)))
176 (if (string-match "=" variable)
177 (error "Environment variable name `%s' contains `='" variable))
178 (if (string-equal "TZ" variable)
179 (set-time-zone-rule value))
180 (setq process-environment (setenv-internal process-environment
181 variable value t))
182 value)
184 (defun getenv (variable &optional frame)
185 "Get the value of environment variable VARIABLE.
186 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
187 the environment. Otherwise, value is a string.
189 If optional parameter FRAME is non-nil, then it should be a
190 frame. This function will look up VARIABLE in its 'environment
191 parameter.
193 Otherwise, this function searches `process-environment' for
194 VARIABLE. If it is not found there, then it continues the search
195 in the environment list of the selected frame."
196 (interactive (list (read-envvar-name "Get environment variable: " t)))
197 (let ((value (getenv-internal (if (multibyte-string-p variable)
198 (encode-coding-string
199 variable locale-coding-system)
200 variable)
201 frame)))
202 (if (and enable-multibyte-characters value)
203 (setq value (decode-coding-string value locale-coding-system)))
204 (when (interactive-p)
205 (message "%s" (if value value "Not set")))
206 value))
208 (defun environment (&optional frame)
209 "Return a list of environment variables with their values.
210 Each entry in the list is a string of the form NAME=VALUE.
212 The returned list can not be used to change environment
213 variables, only read them. See `setenv' to do that.
215 If optional parameter FRAME is non-nil, then it should be a
216 frame. The function returns the environment of that frame.
218 The list is constructed by concatenating the elements of
219 `process-environment' and the 'environment parameter of the
220 selected frame, and removing duplicated and empty values.
222 Non-ASCII characters are encoded according to the initial value of
223 `locale-coding-system', i.e. the elements must normally be decoded for use.
224 See `setenv' and `getenv'."
225 (let* ((env (append process-environment
226 ;; (frame-environment frame)
227 nil))
228 (scan env)
229 prev seen)
230 ;; Remove unset variables from the beginning of the list.
231 (while (and env
232 (or (not (stringp (car env)))
233 (not (string-match "=" (car env)))))
234 (or (member (car env) seen)
235 (setq seen (cons (car env) seen)))
236 (setq env (cdr env)
237 scan env))
238 (let (name)
239 (while scan
240 (cond ((or (not (stringp (car scan)))
241 (not (string-match "=" (car scan))))
242 ;; Unset variable.
243 (or (member (car scan) seen)
244 (setq seen (cons (car scan) seen)))
245 (setcdr prev (cdr scan)))
246 ((member (setq name (substring (car scan) 0 (string-match "=" (car scan)))) seen)
247 ;; Duplicated variable.
248 (setcdr prev (cdr scan)))
250 ;; New variable.
251 (setq seen (cons name seen))))
252 (setq prev scan
253 scan (cdr scan))))
254 env))
256 (provide 'env)
258 ;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
259 ;;; env.el ends here