1 ;;; env.el --- functions to manipulate environment variables
3 ;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
7 ;; Keywords: processes, unix
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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; UNIX processes inherit a list of name-to-string associations from their
29 ;; parents called their `environment'; these are commonly used to control
30 ;; program options. This package permits you to set environment variables
31 ;; to be passed to any sub-process run under Emacs.
33 ;; Note that the environment string `process-environment' is not
34 ;; decoded, but the args of `setenv' and `getenv' are normally
35 ;; multibyte text and get coding conversion.
39 ;; History list for environment variable names.
40 (defvar read-envvar-name-history nil
)
42 (defun read-envvar-name (prompt &optional mustmatch
)
43 "Read environment variable name, prompting with PROMPT.
44 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
45 If it is also not t, RET does not exit if it does non-null completion."
46 (completing-read prompt
47 (mapcar (lambda (enventry)
48 (list (if enable-multibyte-characters
51 (string-match "=" enventry
))
52 locale-coding-system t
)
54 (string-match "=" enventry
)))))
56 nil mustmatch nil
'read-envvar-name-history
))
58 ;; History list for VALUE argument to setenv.
59 (defvar setenv-history nil
)
62 (defun substitute-env-vars (string)
63 "Substitute environment variables referred to in STRING.
64 `$FOO' where FOO is an environment variable name means to substitute
65 the value of that variable. The variable name should be terminated
66 with a character not a letter, digit or underscore; otherwise, enclose
67 the entire variable name in braces. For instance, in `ab$cd-x',
68 `$cd' is treated as an environment variable.
70 Use `$$' to insert a single dollar sign."
74 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
75 (and "${" (submatch (minimal-match (0+ anything
))) "}")
78 (cond ((match-beginning 1)
79 (let ((value (getenv (match-string 1 string
))))
80 (setq string
(replace-match (or value
"") t t string
)
81 start
(+ (match-beginning 0) (length value
)))))
83 (let ((value (getenv (match-string 2 string
))))
84 (setq string
(replace-match (or value
"") t t string
)
85 start
(+ (match-beginning 0) (length value
)))))
87 (setq string
(replace-match "$" t t string
)
88 start
(+ (match-beginning 0) 1)))))
91 ;; Fixme: Should `process-environment' be recoded if LC_CTYPE &c is set?
93 (defun setenv (variable &optional value substitute-env-vars
)
94 "Set the value of the environment variable named VARIABLE to VALUE.
95 VARIABLE should be a string. VALUE is optional; if not provided or
96 nil, the environment variable VARIABLE will be removed.
98 Interactively, a prefix argument means to unset the variable.
99 Interactively, the current value (if any) of the variable
100 appears at the front of the history list when you type in the new value.
101 Interactively, always replace environment variables in the new value.
103 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
104 variables in VALUE with `substitute-env-vars', which see.
105 This is normally used only for interactive calls.
107 The return value is the new value of VARIABLE, or nil if
108 it was removed from the environment.
110 This function works by modifying `process-environment'.
112 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
115 (if current-prefix-arg
116 (list (read-envvar-name "Clear environment variable: " 'exact
) nil
)
117 (let* ((var (read-envvar-name "Set environment variable: " nil
))
118 (value (getenv var
)))
120 (add-to-history 'setenv-history value
))
121 ;; Here finally we specify the args to give call setenv with.
123 (read-from-minibuffer (format "Set %s to value: " var
)
124 nil nil nil
'setenv-history
127 (if (and (multibyte-string-p variable
) locale-coding-system
)
128 (let ((codings (find-coding-systems-string (concat variable value
))))
129 (unless (or (eq 'undecided
(car codings
))
130 (memq (coding-system-base locale-coding-system
) codings
))
131 (error "Can't encode `%s=%s' with `locale-coding-system'"
132 variable
(or value
"")))))
135 (setq value
(substitute-env-vars value
)))
136 (if (multibyte-string-p variable
)
137 (setq variable
(encode-coding-string variable locale-coding-system
)))
138 (if (and value
(multibyte-string-p value
))
139 (setq value
(encode-coding-string value locale-coding-system
)))
140 (if (string-match "=" variable
)
141 (error "Environment variable name `%s' contains `='" variable
)
142 (let ((pattern (concat "\\`" (regexp-quote (concat variable
"="))))
143 (case-fold-search nil
)
144 (scan process-environment
)
146 (if (string-equal "TZ" variable
)
147 (set-time-zone-rule value
))
149 (cond ((string-match pattern
(car scan
))
152 (setq process-environment
(delq (car scan
)
153 process-environment
))
154 (setcar scan
(concat variable
"=" value
)))
156 (setq scan
(cdr scan
)))
159 (setq process-environment
160 (cons (concat variable
"=" value
)
161 process-environment
))))))
164 (defun getenv (variable)
165 "Get the value of environment variable VARIABLE.
166 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
167 the environment. Otherwise, value is a string.
169 This function consults the variable `process-environment'
171 (interactive (list (read-envvar-name "Get environment variable: " t
)))
172 (let ((value (getenv-internal (if (multibyte-string-p variable
)
173 (encode-coding-string
174 variable locale-coding-system
)
176 (if (and enable-multibyte-characters value
)
177 (setq value
(decode-coding-string value locale-coding-system
)))
178 (when (interactive-p)
179 (message "%s" (if value value
"Not set")))
184 ;;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8