* src/casetab.c (init_casetab_once): Fix last change.
[emacs.git] / lisp / env.el
blob5618404cb6735e826fde1ca574f2d957cc149e28
1 ;;; env.el --- functions to manipulate environment variables
3 ;; Copyright (C) 1991, 1994, 2000-2013 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)
60 (defconst env--substitute-vars-regexp
61 "\\$\\(?:\\(?1:[[:alnum:]_]+\\)\\|{\\(?1:[^{}]+\\)}\\|\\$\\)")
63 (defun substitute-env-vars (string &optional only-defined)
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.
70 If ONLY-DEFINED is nil, references to undefined environment variables
71 are replaced by the empty string; if it is non-nil, they are left unchanged.
73 Use `$$' to insert a single dollar sign."
74 (let ((start 0))
75 (while (string-match env--substitute-vars-regexp string start)
76 (cond ((match-beginning 1)
77 (let ((value (getenv (match-string 1 string))))
78 (if (and (null value) only-defined)
79 (setq start (match-end 0))
80 (setq string (replace-match (or value "") t t string)
81 start (+ (match-beginning 0) (length value))))))
83 (setq string (replace-match "$" t t string)
84 start (+ (match-beginning 0) 1)))))
85 string))
88 (defun setenv-internal (env variable value keep-empty)
89 "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY.
90 Changes 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))))
121 ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set?
123 (defun setenv (variable &optional value substitute-env-vars)
124 "Set the value of the environment variable named VARIABLE to VALUE.
125 VARIABLE should be a string. VALUE is optional; if not provided or
126 nil, the environment variable VARIABLE will be removed.
128 Interactively, a prefix argument means to unset the variable, and
129 otherwise the current value (if any) of the variable appears at
130 the front of the history list when you type in the new value.
131 This function always replaces environment variables in the new
132 value when called interactively.
134 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
135 variables in VALUE with `substitute-env-vars', which see.
136 This is normally used only for interactive calls.
138 The return value is the new value of VARIABLE, or nil if
139 it was removed from the environment.
141 This function works by modifying `process-environment'.
143 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
144 a side-effect."
145 (interactive
146 (if current-prefix-arg
147 (list (read-envvar-name "Clear environment variable: " 'exact) nil)
148 (let* ((var (read-envvar-name "Set environment variable: " nil))
149 (value (getenv var)))
150 (when value
151 (add-to-history 'setenv-history value))
152 ;; Here finally we specify the args to give call setenv with.
153 (list var
154 (read-from-minibuffer (format "Set %s to value: " var)
155 nil nil nil 'setenv-history
156 value)
157 t))))
158 (if (and (multibyte-string-p variable) locale-coding-system)
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 "")))))
164 (and value
165 substitute-env-vars
166 (setq value (substitute-env-vars value)))
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)))
171 (if (string-match "=" variable)
172 (error "Environment variable name `%s' contains `='" variable))
173 (if (string-equal "TZ" variable)
174 (set-time-zone-rule value))
175 (setq process-environment (setenv-internal process-environment
176 variable value t))
177 value)
179 (defun getenv (variable &optional frame)
180 "Get the value of environment variable VARIABLE.
181 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
182 the environment. Otherwise, value is a string.
184 If optional parameter FRAME is non-nil, then it should be a
185 frame. This function will look up VARIABLE in its `environment'
186 parameter.
188 Otherwise, this function searches `process-environment' for
189 VARIABLE. If it is not found there, then it continues the search
190 in the environment list of the selected frame."
191 (interactive (list (read-envvar-name "Get environment variable: " t)))
192 (let ((value (getenv-internal (if (multibyte-string-p variable)
193 (encode-coding-string
194 variable locale-coding-system)
195 variable)
196 (and frame
197 (assq 'environment
198 (frame-parameters frame))))))
199 (if (and enable-multibyte-characters value)
200 (setq value (decode-coding-string value locale-coding-system)))
201 (when (called-interactively-p 'interactive)
202 (message "%s" (if value value "Not set")))
203 value))
205 (provide 'env)
207 ;;; env.el ends here