* keyboard.c (init_kboard): Now static. Add arg
[bpt/emacs.git] / lisp / env.el
CommitLineData
55535639 1;;; env.el --- functions to manipulate environment variables
c88ab9ce 2
ab422c4d 3;; Copyright (C) 1991, 1994, 2000-2013 Free Software Foundation, Inc.
971571b9 4
e5167999 5;; Maintainer: FSF
d9ecc911 6;; Keywords: processes, unix
bd78fa1d 7;; Package: emacs
e5167999 8
b578f267 9;; This file is part of GNU Emacs.
6449c898 10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
6449c898 15
b578f267
EN
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.
6449c898 20
b578f267 21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6449c898 23
d9ecc911
ER
24;;; Commentary:
25
b578f267
EN
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.
d9ecc911 30
1a90eae6
DL
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.
34
e5167999
ER
35;;; Code:
36
99ac138a
RS
37;; History list for environment variable names.
38(defvar read-envvar-name-history nil)
39
40(defun read-envvar-name (prompt &optional mustmatch)
41 "Read environment variable name, prompting with PROMPT.
8b740009
RS
42Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
43If it is also not t, RET does not exit if it does non-null completion."
99ac138a 44 (completing-read prompt
1a90eae6 45 (mapcar (lambda (enventry)
ef651d13
SM
46 (let ((str (substring enventry 0
47 (string-match "=" enventry))))
48 (if (multibyte-string-p str)
49 (decode-coding-string
50 str locale-coding-system t)
51 str)))
5990851d 52 (append process-environment
ef651d13 53 ;;(frame-environment)
de87fb59 54 ))
99ac138a
RS
55 nil mustmatch nil 'read-envvar-name-history))
56
57;; History list for VALUE argument to setenv.
58(defvar setenv-history nil)
59
3b11e6ac 60(defconst env--substitute-vars-regexp
67dd8ad1 61 "\\$\\(?:\\(?1:[[:alnum:]_]+\\)\\|{\\(?1:[^{}]+\\)}\\|\\$\\)")
a4a216c5 62
3b11e6ac 63(defun substitute-env-vars (string &optional only-defined)
a4a216c5
GM
64 "Substitute environment variables referred to in STRING.
65`$FOO' where FOO is an environment variable name means to substitute
66the value of that variable. The variable name should be terminated
67with a character not a letter, digit or underscore; otherwise, enclose
cccc806d
RS
68the entire variable name in braces. For instance, in `ab$cd-x',
69`$cd' is treated as an environment variable.
3b11e6ac
SM
70If ONLY-DEFINED is nil, references to undefined environment variables
71are replaced by the empty string; if it is non-nil, they are left unchanged.
cccc806d
RS
72
73Use `$$' to insert a single dollar sign."
a4a216c5 74 (let ((start 0))
3b11e6ac 75 (while (string-match env--substitute-vars-regexp string start)
a4a216c5
GM
76 (cond ((match-beginning 1)
77 (let ((value (getenv (match-string 1 string))))
3b11e6ac
SM
78 (if (and (null value) only-defined)
79 (setq start (match-end 0))
a4a216c5 80 (setq string (replace-match (or value "") t t string)
3b11e6ac 81 start (+ (match-beginning 0) (length value))))))
a4a216c5
GM
82 (t
83 (setq string (replace-match "$" t t string)
84 start (+ (match-beginning 0) 1)))))
85 string))
86
a13f8f50
KL
87
88(defun setenv-internal (env variable value keep-empty)
89 "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY.
90Changes ENV by side-effect, and returns its new value."
91 (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)"))
92 (case-fold-search nil)
93 (scan env)
94 prev found)
95 ;; Handle deletions from the beginning of the list specially.
96 (if (and (null value)
97 (not keep-empty)
98 env
99 (stringp (car env))
100 (string-match pattern (car env)))
101 (cdr env)
102 ;; Try to find existing entry for VARIABLE in ENV.
103 (while (and scan (stringp (car scan)))
104 (when (string-match pattern (car scan))
105 (if value
106 (setcar scan (concat variable "=" value))
107 (if keep-empty
108 (setcar scan variable)
109 (setcdr prev (cdr scan))))
110 (setq found t
111 scan nil))
112 (setq prev scan
113 scan (cdr scan)))
114 (if (and (not found) (or value keep-empty))
115 (cons (if value
116 (concat variable "=" value)
117 variable)
118 env)
119 env))))
120
5990851d 121;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
a4a216c5 122
ef651d13 123(defun setenv (variable &optional value substitute-env-vars)
6449c898 124 "Set the value of the environment variable named VARIABLE to VALUE.
b71038b2 125VARIABLE should be a string. VALUE is optional; if not provided or
17ccbd91 126nil, the environment variable VARIABLE will be removed.
cbfe666b 127
17ccbd91
KL
128Interactively, a prefix argument means to unset the variable, and
129otherwise the current value (if any) of the variable appears at
130the front of the history list when you type in the new value.
131This function always replaces environment variables in the new
132value when called interactively.
99ac138a 133
27bdc650
RS
134SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
135variables in VALUE with `substitute-env-vars', which see.
136This is normally used only for interactive calls.
137
138The return value is the new value of VARIABLE, or nil if
139it was removed from the environment.
140
ef651d13
SM
141This function works by modifying `process-environment'.
142
1a90eae6
DL
143As a special case, setting variable `TZ' calls `set-time-zone-rule' as
144a side-effect."
cbfe666b
RS
145 (interactive
146 (if current-prefix-arg
27bdc650 147 (list (read-envvar-name "Clear environment variable: " 'exact) nil)
2a5becfb
GM
148 (let* ((var (read-envvar-name "Set environment variable: " nil))
149 (value (getenv var)))
150 (when value
4c5f6185 151 (add-to-history 'setenv-history value))
99ac138a 152 ;; Here finally we specify the args to give call setenv with.
71296446 153 (list var
a4a216c5
GM
154 (read-from-minibuffer (format "Set %s to value: " var)
155 nil nil nil 'setenv-history
156 value)
a4a216c5 157 t))))
1a90eae6 158 (if (and (multibyte-string-p variable) locale-coding-system)
1ebb05c4
KH
159 (let ((codings (find-coding-systems-string (concat variable value))))
160 (unless (or (eq 'undecided (car codings))
161 (memq (coding-system-base locale-coding-system) codings))
162 (error "Can't encode `%s=%s' with `locale-coding-system'"
163 variable (or value "")))))
27bdc650
RS
164 (and value
165 substitute-env-vars
166 (setq value (substitute-env-vars value)))
1a90eae6
DL
167 (if (multibyte-string-p variable)
168 (setq variable (encode-coding-string variable locale-coding-system)))
169 (if (and value (multibyte-string-p value))
170 (setq value (encode-coding-string value locale-coding-system)))
6449c898 171 (if (string-match "=" variable)
f105f403 172 (error "Environment variable name `%s' contains `='" variable))
a13f8f50
KL
173 (if (string-equal "TZ" variable)
174 (set-time-zone-rule value))
ef651d13
SM
175 (setq process-environment (setenv-internal process-environment
176 variable value t))
a13f8f50 177 value)
49116ac0 178
da8e8fc1 179(defun getenv (variable &optional frame)
b1e11b4f
GM
180 "Get the value of environment variable VARIABLE.
181VARIABLE should be a string. Value is nil if VARIABLE is undefined in
182the environment. Otherwise, value is a string.
183
da8e8fc1 184If optional parameter FRAME is non-nil, then it should be a
3b11e6ac 185frame. This function will look up VARIABLE in its `environment'
a13f8f50 186parameter.
f105f403 187
da8e8fc1 188Otherwise, this function searches `process-environment' for
a13f8f50
KL
189VARIABLE. If it is not found there, then it continues the search
190in the environment list of the selected frame."
b1e11b4f 191 (interactive (list (read-envvar-name "Get environment variable: " t)))
1a90eae6
DL
192 (let ((value (getenv-internal (if (multibyte-string-p variable)
193 (encode-coding-string
194 variable locale-coding-system)
36ab8612 195 variable)
263903f7
JB
196 (and frame
197 (assq 'environment
198 (frame-parameters frame))))))
1a90eae6
DL
199 (if (and enable-multibyte-characters value)
200 (setq value (decode-coding-string value locale-coding-system)))
32226619 201 (when (called-interactively-p 'interactive)
b1e11b4f
GM
202 (message "%s" (if value value "Not set")))
203 value))
204
1bbda2d6
NF
205(provide 'env)
206
207;;; env.el ends here