38412
|
1 ;;; env.el --- functions to manipulate environment variables
|
658
|
2
|
64762
|
3 ;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
|
106815
|
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
1185
|
5
|
807
|
6 ;; Maintainer: FSF
|
2315
|
7 ;; Keywords: processes, unix
|
110015
|
8 ;; Package: emacs
|
807
|
9
|
14169
|
10 ;; This file is part of GNU Emacs.
|
489
|
11
|
94678
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
|
14169
|
13 ;; it under the terms of the GNU General Public License as published by
|
94678
|
14 ;; the Free Software Foundation, either version 3 of the License, or
|
|
15 ;; (at your option) any later version.
|
489
|
16
|
14169
|
17 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 ;; GNU General Public License for more details.
|
489
|
21
|
14169
|
22 ;; You should have received a copy of the GNU General Public License
|
94678
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
489
|
24
|
2315
|
25 ;;; Commentary:
|
|
26
|
14169
|
27 ;; UNIX processes inherit a list of name-to-string associations from their
|
|
28 ;; parents called their `environment'; these are commonly used to control
|
|
29 ;; program options. This package permits you to set environment variables
|
|
30 ;; to be passed to any sub-process run under Emacs.
|
2315
|
31
|
49968
|
32 ;; Note that the environment string `process-environment' is not
|
|
33 ;; decoded, but the args of `setenv' and `getenv' are normally
|
|
34 ;; multibyte text and get coding conversion.
|
|
35
|
807
|
36 ;;; Code:
|
|
37
|
83425
|
38 (eval-when-compile (require 'cl))
|
|
39
|
9220
|
40 ;; History list for environment variable names.
|
|
41 (defvar read-envvar-name-history nil)
|
|
42
|
|
43 (defun read-envvar-name (prompt &optional mustmatch)
|
|
44 "Read environment variable name, prompting with PROMPT.
|
9345
|
45 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
|
|
46 If it is also not t, RET does not exit if it does non-null completion."
|
9220
|
47 (completing-read prompt
|
49968
|
48 (mapcar (lambda (enventry)
|
85142
|
49 (let ((str (substring enventry 0
|
|
50 (string-match "=" enventry))))
|
|
51 (if (multibyte-string-p str)
|
|
52 (decode-coding-string
|
|
53 str locale-coding-system t)
|
|
54 str)))
|
83425
|
55 (append process-environment
|
85142
|
56 ;;(frame-environment)
|
83640
|
57 ))
|
9220
|
58 nil mustmatch nil 'read-envvar-name-history))
|
|
59
|
|
60 ;; History list for VALUE argument to setenv.
|
|
61 (defvar setenv-history nil)
|
|
62
|
39554
|
63
|
|
64 (defun substitute-env-vars (string)
|
|
65 "Substitute environment variables referred to in STRING.
|
|
66 `$FOO' where FOO is an environment variable name means to substitute
|
|
67 the value of that variable. The variable name should be terminated
|
|
68 with a character not a letter, digit or underscore; otherwise, enclose
|
51279
|
69 the entire variable name in braces. For instance, in `ab$cd-x',
|
|
70 `$cd' is treated as an environment variable.
|
|
71
|
|
72 Use `$$' to insert a single dollar sign."
|
39554
|
73 (let ((start 0))
|
49588
|
74 (while (string-match
|
49968
|
75 (eval-when-compile
|
51279
|
76 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
|
49968
|
77 (and "${" (submatch (minimal-match (0+ anything))) "}")
|
|
78 "$$")))
|
39554
|
79 string start)
|
|
80 (cond ((match-beginning 1)
|
|
81 (let ((value (getenv (match-string 1 string))))
|
|
82 (setq string (replace-match (or value "") t t string)
|
|
83 start (+ (match-beginning 0) (length value)))))
|
|
84 ((match-beginning 2)
|
|
85 (let ((value (getenv (match-string 2 string))))
|
|
86 (setq string (replace-match (or value "") t t string)
|
|
87 start (+ (match-beginning 0) (length value)))))
|
|
88 (t
|
|
89 (setq string (replace-match "$" t t string)
|
|
90 start (+ (match-beginning 0) 1)))))
|
|
91 string))
|
|
92
|
|
93
|
83529
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
94 (defun setenv-internal (env variable value keep-empty)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
95 "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY.
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
96 Changes ENV by side-effect, and returns its new value."
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
97 (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)"))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
98 (case-fold-search nil)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
99 (scan env)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
100 prev found)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
101 ;; Handle deletions from the beginning of the list specially.
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
102 (if (and (null value)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
103 (not keep-empty)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
104 env
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
105 (stringp (car env))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
106 (string-match pattern (car env)))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
107 (cdr env)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
108 ;; Try to find existing entry for VARIABLE in ENV.
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
109 (while (and scan (stringp (car scan)))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
110 (when (string-match pattern (car scan))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
111 (if value
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
112 (setcar scan (concat variable "=" value))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
113 (if keep-empty
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
114 (setcar scan variable)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
115 (setcdr prev (cdr scan))))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
116 (setq found t
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
117 scan nil))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
118 (setq prev scan
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
119 scan (cdr scan)))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
120 (if (and (not found) (or value keep-empty))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
121 (cons (if value
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
122 (concat variable "=" value)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
123 variable)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
124 env)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
125 env))))
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
126
|
83425
|
127 ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
|
39554
|
128
|
70094
|
129 (defun setenv (variable &optional value substitute-env-vars)
|
489
|
130 "Set the value of the environment variable named VARIABLE to VALUE.
|
50873
|
131 VARIABLE should be a string. VALUE is optional; if not provided or
|
70094
|
132 nil, the environment variable VARIABLE will be removed.
|
8004
|
133
|
83430
|
134 Interactively, a prefix argument means to unset the variable, and
|
|
135 otherwise the current value (if any) of the variable appears at
|
|
136 the front of the history list when you type in the new value.
|
|
137 This function always replaces environment variables in the new
|
|
138 value when called interactively.
|
9220
|
139
|
70094
|
140 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
|
|
141 variables in VALUE with `substitute-env-vars', which see.
|
|
142 This is normally used only for interactive calls.
|
|
143
|
|
144 The return value is the new value of VARIABLE, or nil if
|
|
145 it was removed from the environment.
|
|
146
|
49968
|
147 This function works by modifying `process-environment'.
|
|
148
|
|
149 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
|
|
150 a side-effect."
|
8004
|
151 (interactive
|
|
152 (if current-prefix-arg
|
70094
|
153 (list (read-envvar-name "Clear environment variable: " 'exact) nil)
|
39126
|
154 (let* ((var (read-envvar-name "Set environment variable: " nil))
|
|
155 (value (getenv var)))
|
|
156 (when value
|
70417
|
157 (add-to-history 'setenv-history value))
|
9220
|
158 ;; Here finally we specify the args to give call setenv with.
|
49588
|
159 (list var
|
39554
|
160 (read-from-minibuffer (format "Set %s to value: " var)
|
|
161 nil nil nil 'setenv-history
|
|
162 value)
|
|
163 t))))
|
49968
|
164 (if (and (multibyte-string-p variable) locale-coding-system)
|
49977
|
165 (let ((codings (find-coding-systems-string (concat variable value))))
|
|
166 (unless (or (eq 'undecided (car codings))
|
|
167 (memq (coding-system-base locale-coding-system) codings))
|
|
168 (error "Can't encode `%s=%s' with `locale-coding-system'"
|
|
169 variable (or value "")))))
|
70094
|
170 (and value
|
|
171 substitute-env-vars
|
|
172 (setq value (substitute-env-vars value)))
|
49968
|
173 (if (multibyte-string-p variable)
|
|
174 (setq variable (encode-coding-string variable locale-coding-system)))
|
|
175 (if (and value (multibyte-string-p value))
|
|
176 (setq value (encode-coding-string value locale-coding-system)))
|
489
|
177 (if (string-match "=" variable)
|
83421
bb2edc915032
Implement automatic terminal-local environment variables via `local-environment-variables'.
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
178 (error "Environment variable name `%s' contains `='" variable))
|
83529
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
179 (if (string-equal "TZ" variable)
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
180 (set-time-zone-rule value))
|
85142
|
181 (setq process-environment (setenv-internal process-environment
|
|
182 variable value t))
|
39554
|
183 value)
|
584
|
184
|
83427
|
185 (defun getenv (variable &optional frame)
|
28917
|
186 "Get the value of environment variable VARIABLE.
|
|
187 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
|
|
188 the environment. Otherwise, value is a string.
|
|
189
|
83427
|
190 If optional parameter FRAME is non-nil, then it should be a
|
83529
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
191 frame. This function will look up VARIABLE in its 'environment
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
192 parameter.
|
83421
bb2edc915032
Implement automatic terminal-local environment variables via `local-environment-variables'.
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
193
|
83427
|
194 Otherwise, this function searches `process-environment' for
|
83529
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
195 VARIABLE. If it is not found there, then it continues the search
|
0d9e16eab053
Rework environment variable support. (Reported by Kalle Olavi Niemitalo and Noah Friedman.)
Karoly Lorentey <lorentey@elte.hu>
diff
changeset
|
196 in the environment list of the selected frame."
|
28917
|
197 (interactive (list (read-envvar-name "Get environment variable: " t)))
|
49968
|
198 (let ((value (getenv-internal (if (multibyte-string-p variable)
|
|
199 (encode-coding-string
|
|
200 variable locale-coding-system)
|
83594
|
201 variable)
|
101930
a7b014166b41
* env.el (getenv): When FRAME is non-nil, pass the frame environment
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
202 (and frame
|
a7b014166b41
* env.el (getenv): When FRAME is non-nil, pass the frame environment
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
203 (assq 'environment
|
a7b014166b41
* env.el (getenv): When FRAME is non-nil, pass the frame environment
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
204 (frame-parameters frame))))))
|
49968
|
205 (if (and enable-multibyte-characters value)
|
|
206 (setq value (decode-coding-string value locale-coding-system)))
|
105372
|
207 (when (called-interactively-p 'interactive)
|
28917
|
208 (message "%s" (if value value "Not set")))
|
|
209 value))
|
|
210
|
2403
|
211 (provide 'env)
|
|
212
|
83797
|
213 ;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
|
2403
|
214 ;;; env.el ends here
|