* lisp/emacs-lisp/package.el (package-generate-autoloads): Load autoloads
[emacs.git] / lisp / env.el
blob5e915eb312696fd0435ed3dbd83e99e4a08b734a
1 ;;; env.el --- functions to manipulate environment variables
3 ;; Copyright (C) 1991, 1994, 2000-2011 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: processes, unix
7 ;; Package: emacs
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 (and frame
202 (assq 'environment
203 (frame-parameters frame))))))
204 (if (and enable-multibyte-characters value)
205 (setq value (decode-coding-string value locale-coding-system)))
206 (when (called-interactively-p 'interactive)
207 (message "%s" (if value value "Not set")))
208 value))
210 (provide 'env)
212 ;;; env.el ends here