* nsterm.m (ns_draw_window_cursor): Draw BAR_CURSOR correct for R2L.
[bpt/emacs.git] / lisp / env.el
CommitLineData
55535639 1;;; env.el --- functions to manipulate environment variables
c88ab9ce 2
0d30b337 3;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
114f9c96 4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
971571b9 5
e5167999 6;; Maintainer: FSF
d9ecc911 7;; Keywords: processes, unix
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
5990851d
KL
37(eval-when-compile (require 'cl))
38
99ac138a
RS
39;; History list for environment variable names.
40(defvar read-envvar-name-history nil)
41
42(defun read-envvar-name (prompt &optional mustmatch)
43 "Read environment variable name, prompting with PROMPT.
8b740009
RS
44Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
45If it is also not t, RET does not exit if it does non-null completion."
99ac138a 46 (completing-read prompt
1a90eae6 47 (mapcar (lambda (enventry)
ef651d13
SM
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)))
5990851d 54 (append process-environment
ef651d13 55 ;;(frame-environment)
de87fb59 56 ))
99ac138a
RS
57 nil mustmatch nil 'read-envvar-name-history))
58
59;; History list for VALUE argument to setenv.
60(defvar setenv-history nil)
61
a4a216c5
GM
62
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
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.
70
71Use `$$' to insert a single dollar sign."
a4a216c5 72 (let ((start 0))
71296446 73 (while (string-match
1a90eae6 74 (eval-when-compile
cccc806d 75 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
1a90eae6
DL
76 (and "${" (submatch (minimal-match (0+ anything))) "}")
77 "$$")))
a4a216c5
GM
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)))))
87 (t
88 (setq string (replace-match "$" t t string)
89 start (+ (match-beginning 0) 1)))))
90 string))
91
a13f8f50
KL
92
93(defun setenv-internal (env variable value keep-empty)
94 "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY.
95Changes 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)
103 env
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))))
125
5990851d 126;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
a4a216c5 127
ef651d13 128(defun setenv (variable &optional value substitute-env-vars)
6449c898 129 "Set the value of the environment variable named VARIABLE to VALUE.
b71038b2 130VARIABLE should be a string. VALUE is optional; if not provided or
17ccbd91 131nil, the environment variable VARIABLE will be removed.
cbfe666b 132
17ccbd91
KL
133Interactively, a prefix argument means to unset the variable, and
134otherwise the current value (if any) of the variable appears at
135the front of the history list when you type in the new value.
136This function always replaces environment variables in the new
137value when called interactively.
99ac138a 138
27bdc650
RS
139SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
140variables in VALUE with `substitute-env-vars', which see.
141This is normally used only for interactive calls.
142
143The return value is the new value of VARIABLE, or nil if
144it was removed from the environment.
145
ef651d13
SM
146This function works by modifying `process-environment'.
147
1a90eae6
DL
148As a special case, setting variable `TZ' calls `set-time-zone-rule' as
149a side-effect."
cbfe666b
RS
150 (interactive
151 (if current-prefix-arg
27bdc650 152 (list (read-envvar-name "Clear environment variable: " 'exact) nil)
2a5becfb
GM
153 (let* ((var (read-envvar-name "Set environment variable: " nil))
154 (value (getenv var)))
155 (when value
4c5f6185 156 (add-to-history 'setenv-history value))
99ac138a 157 ;; Here finally we specify the args to give call setenv with.
71296446 158 (list var
a4a216c5
GM
159 (read-from-minibuffer (format "Set %s to value: " var)
160 nil nil nil 'setenv-history
161 value)
a4a216c5 162 t))))
1a90eae6 163 (if (and (multibyte-string-p variable) locale-coding-system)
1ebb05c4
KH
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 "")))))
27bdc650
RS
169 (and value
170 substitute-env-vars
171 (setq value (substitute-env-vars value)))
1a90eae6
DL
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)))
6449c898 176 (if (string-match "=" variable)
f105f403 177 (error "Environment variable name `%s' contains `='" variable))
a13f8f50
KL
178 (if (string-equal "TZ" variable)
179 (set-time-zone-rule value))
ef651d13
SM
180 (setq process-environment (setenv-internal process-environment
181 variable value t))
a13f8f50 182 value)
49116ac0 183
da8e8fc1 184(defun getenv (variable &optional frame)
b1e11b4f
GM
185 "Get the value of environment variable VARIABLE.
186VARIABLE should be a string. Value is nil if VARIABLE is undefined in
187the environment. Otherwise, value is a string.
188
da8e8fc1 189If optional parameter FRAME is non-nil, then it should be a
a13f8f50
KL
190frame. This function will look up VARIABLE in its 'environment
191parameter.
f105f403 192
da8e8fc1 193Otherwise, this function searches `process-environment' for
a13f8f50
KL
194VARIABLE. If it is not found there, then it continues the search
195in the environment list of the selected frame."
b1e11b4f 196 (interactive (list (read-envvar-name "Get environment variable: " t)))
1a90eae6
DL
197 (let ((value (getenv-internal (if (multibyte-string-p variable)
198 (encode-coding-string
199 variable locale-coding-system)
36ab8612 200 variable)
263903f7
JB
201 (and frame
202 (assq 'environment
203 (frame-parameters frame))))))
1a90eae6
DL
204 (if (and enable-multibyte-characters value)
205 (setq value (decode-coding-string value locale-coding-system)))
32226619 206 (when (called-interactively-p 'interactive)
b1e11b4f
GM
207 (message "%s" (if value value "Not set")))
208 value))
209
1bbda2d6
NF
210(provide 'env)
211
bb8ef973 212;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
1bbda2d6 213;;; env.el ends here