Use make_formatted_string to avoid double length calculation.
[emacs.git] / lisp / env.el
blobd0d8ed0b998356cd1b3cf3073df85bbb8c2428d6
1 ;;; env.el --- functions to manipulate environment variables
3 ;; Copyright (C) 1991, 1994, 2000-2012 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 ;; History list for environment variable names.
38 (defvar read-envvar-name-history nil)
40 (defun read-envvar-name (prompt &optional mustmatch)
41 "Read environment variable name, prompting with PROMPT.
42 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
43 If it is also not t, RET does not exit if it does non-null completion."
44 (completing-read prompt
45 (mapcar (lambda (enventry)
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)))
52 (append process-environment
53 ;;(frame-environment)
55 nil mustmatch nil 'read-envvar-name-history))
57 ;; History list for VALUE argument to setenv.
58 (defvar setenv-history nil)
61 (defun substitute-env-vars (string)
62 "Substitute environment variables referred to in STRING.
63 `$FOO' where FOO is an environment variable name means to substitute
64 the value of that variable. The variable name should be terminated
65 with a character not a letter, digit or underscore; otherwise, enclose
66 the entire variable name in braces. For instance, in `ab$cd-x',
67 `$cd' is treated as an environment variable.
69 Use `$$' to insert a single dollar sign."
70 (let ((start 0))
71 (while (string-match
72 (eval-when-compile
73 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
74 (and "${" (submatch (minimal-match (0+ anything))) "}")
75 "$$")))
76 string start)
77 (cond ((match-beginning 1)
78 (let ((value (getenv (match-string 1 string))))
79 (setq string (replace-match (or value "") t t string)
80 start (+ (match-beginning 0) (length value)))))
81 ((match-beginning 2)
82 (let ((value (getenv (match-string 2 string))))
83 (setq string (replace-match (or value "") t t string)
84 start (+ (match-beginning 0) (length value)))))
86 (setq string (replace-match "$" t t string)
87 start (+ (match-beginning 0) 1)))))
88 string))
91 (defun setenv-internal (env variable value keep-empty)
92 "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY.
93 Changes ENV by side-effect, and returns its new value."
94 (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)"))
95 (case-fold-search nil)
96 (scan env)
97 prev found)
98 ;; Handle deletions from the beginning of the list specially.
99 (if (and (null value)
100 (not keep-empty)
102 (stringp (car env))
103 (string-match pattern (car env)))
104 (cdr env)
105 ;; Try to find existing entry for VARIABLE in ENV.
106 (while (and scan (stringp (car scan)))
107 (when (string-match pattern (car scan))
108 (if value
109 (setcar scan (concat variable "=" value))
110 (if keep-empty
111 (setcar scan variable)
112 (setcdr prev (cdr scan))))
113 (setq found t
114 scan nil))
115 (setq prev scan
116 scan (cdr scan)))
117 (if (and (not found) (or value keep-empty))
118 (cons (if value
119 (concat variable "=" value)
120 variable)
121 env)
122 env))))
124 ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
126 (defun setenv (variable &optional value substitute-env-vars)
127 "Set the value of the environment variable named VARIABLE to VALUE.
128 VARIABLE should be a string. VALUE is optional; if not provided or
129 nil, the environment variable VARIABLE will be removed.
131 Interactively, a prefix argument means to unset the variable, and
132 otherwise the current value (if any) of the variable appears at
133 the front of the history list when you type in the new value.
134 This function always replaces environment variables in the new
135 value when called interactively.
137 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
138 variables in VALUE with `substitute-env-vars', which see.
139 This is normally used only for interactive calls.
141 The return value is the new value of VARIABLE, or nil if
142 it was removed from the environment.
144 This function works by modifying `process-environment'.
146 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
147 a side-effect."
148 (interactive
149 (if current-prefix-arg
150 (list (read-envvar-name "Clear environment variable: " 'exact) nil)
151 (let* ((var (read-envvar-name "Set environment variable: " nil))
152 (value (getenv var)))
153 (when value
154 (add-to-history 'setenv-history value))
155 ;; Here finally we specify the args to give call setenv with.
156 (list var
157 (read-from-minibuffer (format "Set %s to value: " var)
158 nil nil nil 'setenv-history
159 value)
160 t))))
161 (if (and (multibyte-string-p variable) locale-coding-system)
162 (let ((codings (find-coding-systems-string (concat variable value))))
163 (unless (or (eq 'undecided (car codings))
164 (memq (coding-system-base locale-coding-system) codings))
165 (error "Can't encode `%s=%s' with `locale-coding-system'"
166 variable (or value "")))))
167 (and value
168 substitute-env-vars
169 (setq value (substitute-env-vars value)))
170 (if (multibyte-string-p variable)
171 (setq variable (encode-coding-string variable locale-coding-system)))
172 (if (and value (multibyte-string-p value))
173 (setq value (encode-coding-string value locale-coding-system)))
174 (if (string-match "=" variable)
175 (error "Environment variable name `%s' contains `='" variable))
176 (if (string-equal "TZ" variable)
177 (set-time-zone-rule value))
178 (setq process-environment (setenv-internal process-environment
179 variable value t))
180 value)
182 (defun getenv (variable &optional frame)
183 "Get the value of environment variable VARIABLE.
184 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
185 the environment. Otherwise, value is a string.
187 If optional parameter FRAME is non-nil, then it should be a
188 frame. This function will look up VARIABLE in its 'environment
189 parameter.
191 Otherwise, this function searches `process-environment' for
192 VARIABLE. If it is not found there, then it continues the search
193 in the environment list of the selected frame."
194 (interactive (list (read-envvar-name "Get environment variable: " t)))
195 (let ((value (getenv-internal (if (multibyte-string-p variable)
196 (encode-coding-string
197 variable locale-coding-system)
198 variable)
199 (and frame
200 (assq 'environment
201 (frame-parameters frame))))))
202 (if (and enable-multibyte-characters value)
203 (setq value (decode-coding-string value locale-coding-system)))
204 (when (called-interactively-p 'interactive)
205 (message "%s" (if value value "Not set")))
206 value))
208 (provide 'env)
210 ;;; env.el ends here