Add arch taglines
[bpt/emacs.git] / lisp / env.el
CommitLineData
55535639 1;;; env.el --- functions to manipulate environment variables
c88ab9ce 2
1a90eae6 3;; Copyright (C) 1991, 1994, 2000, 2001, 2003 Free Software Foundation, Inc.
971571b9 4
e5167999 5;; Maintainer: FSF
d9ecc911 6;; Keywords: processes, unix
e5167999 7
b578f267 8;; This file is part of GNU Emacs.
6449c898 9
b578f267
EN
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
6449c898 14
b578f267
EN
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
6449c898 19
b578f267
EN
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
6449c898 24
d9ecc911
ER
25;;; Commentary:
26
b578f267
EN
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.
d9ecc911 31
1a90eae6
DL
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
e5167999
ER
36;;; Code:
37
99ac138a
RS
38;; History list for environment variable names.
39(defvar read-envvar-name-history nil)
40
41(defun read-envvar-name (prompt &optional mustmatch)
42 "Read environment variable name, prompting with PROMPT.
8b740009
RS
43Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
44If it is also not t, RET does not exit if it does non-null completion."
99ac138a 45 (completing-read prompt
1a90eae6
DL
46 (mapcar (lambda (enventry)
47 (list (if enable-multibyte-characters
48 (decode-coding-string
49 (substring enventry 0
50 (string-match "=" enventry))
51 locale-coding-system t)
52 (substring enventry 0
53 (string-match "=" enventry)))))
99ac138a
RS
54 process-environment)
55 nil mustmatch nil 'read-envvar-name-history))
56
57;; History list for VALUE argument to setenv.
58(defvar setenv-history nil)
59
a4a216c5
GM
60
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
64the value of that variable. The variable name should be terminated
65with a character not a letter, digit or underscore; otherwise, enclose
cccc806d
RS
66the entire variable name in braces. For instance, in `ab$cd-x',
67`$cd' is treated as an environment variable.
68
69Use `$$' to insert a single dollar sign."
a4a216c5 70 (let ((start 0))
71296446 71 (while (string-match
1a90eae6 72 (eval-when-compile
cccc806d 73 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
1a90eae6
DL
74 (and "${" (submatch (minimal-match (0+ anything))) "}")
75 "$$")))
a4a216c5
GM
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)))))
85 (t
86 (setq string (replace-match "$" t t string)
87 start (+ (match-beginning 0) 1)))))
88 string))
89
1a90eae6 90;; Fixme: Should `process-environment' be recoded if LC_CTYPE &c is set?
a4a216c5
GM
91
92(defun setenv (variable &optional value unset substitute-env-vars)
6449c898 93 "Set the value of the environment variable named VARIABLE to VALUE.
b71038b2
JB
94VARIABLE should be a string. VALUE is optional; if not provided or
95nil, the environment variable VARIABLE will be removed. UNSET
a4a216c5
GM
96if non-nil means to remove VARIABLE from the environment.
97SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
98variables in VALUE with `substitute-env-vars', where see.
99Value is the new value if VARIABLE, or nil if removed from the
100environment.
cbfe666b
RS
101
102Interactively, a prefix argument means to unset the variable.
99ac138a
RS
103Interactively, the current value (if any) of the variable
104appears at the front of the history list when you type in the new value.
a4a216c5 105Interactively, always replace environment variables in the new value.
99ac138a 106
1a90eae6
DL
107This function works by modifying `process-environment'.
108
109As a special case, setting variable `TZ' calls `set-time-zone-rule' as
110a side-effect."
cbfe666b
RS
111 (interactive
112 (if current-prefix-arg
8b740009 113 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
2a5becfb
GM
114 (let* ((var (read-envvar-name "Set environment variable: " nil))
115 (value (getenv var)))
116 (when value
117 (push value setenv-history))
99ac138a 118 ;; Here finally we specify the args to give call setenv with.
71296446 119 (list var
a4a216c5
GM
120 (read-from-minibuffer (format "Set %s to value: " var)
121 nil nil nil 'setenv-history
122 value)
71296446 123 nil
a4a216c5 124 t))))
1a90eae6 125 (if (and (multibyte-string-p variable) locale-coding-system)
1ebb05c4
KH
126 (let ((codings (find-coding-systems-string (concat variable value))))
127 (unless (or (eq 'undecided (car codings))
128 (memq (coding-system-base locale-coding-system) codings))
129 (error "Can't encode `%s=%s' with `locale-coding-system'"
130 variable (or value "")))))
b71038b2 131 (if unset
a4a216c5
GM
132 (setq value nil)
133 (if substitute-env-vars
134 (setq value (substitute-env-vars value))))
1a90eae6
DL
135 (if (multibyte-string-p variable)
136 (setq variable (encode-coding-string variable locale-coding-system)))
137 (if (and value (multibyte-string-p value))
138 (setq value (encode-coding-string value locale-coding-system)))
6449c898 139 (if (string-match "=" variable)
1bbda2d6 140 (error "Environment variable name `%s' contains `='" variable)
971571b9 141 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
7e68de56 142 (case-fold-search nil)
cbfe666b
RS
143 (scan process-environment)
144 found)
7fd81709
RS
145 (if (string-equal "TZ" variable)
146 (set-time-zone-rule value))
cbfe666b
RS
147 (while scan
148 (cond ((string-match pattern (car scan))
149 (setq found t)
150 (if (eq nil value)
1a90eae6
DL
151 (setq process-environment (delq (car scan)
152 process-environment))
cbfe666b
RS
153 (setcar scan (concat variable "=" value)))
154 (setq scan nil)))
155 (setq scan (cdr scan)))
156 (or found
157 (if value
a3cda273 158 (setq process-environment
cbfe666b 159 (cons (concat variable "=" value)
a4a216c5
GM
160 process-environment))))))
161 value)
49116ac0 162
b1e11b4f
GM
163(defun getenv (variable)
164 "Get the value of environment variable VARIABLE.
165VARIABLE should be a string. Value is nil if VARIABLE is undefined in
166the environment. Otherwise, value is a string.
167
168This function consults the variable `process-environment'
169for its value."
170 (interactive (list (read-envvar-name "Get environment variable: " t)))
1a90eae6
DL
171 (let ((value (getenv-internal (if (multibyte-string-p variable)
172 (encode-coding-string
173 variable locale-coding-system)
174 variable))))
175 (if (and enable-multibyte-characters value)
176 (setq value (decode-coding-string value locale-coding-system)))
b1e11b4f
GM
177 (when (interactive-p)
178 (message "%s" (if value value "Not set")))
179 value))
180
1bbda2d6
NF
181(provide 'env)
182
ab5796a9 183;;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
1bbda2d6 184;;; env.el ends here