(describe-char-unicode-data): New dummy definition.
[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
71296446 66the entire variable name in braces. Use `$$' to insert a single
a4a216c5
GM
67dollar sign."
68 (let ((start 0))
71296446 69 (while (string-match
1a90eae6
DL
70 (eval-when-compile
71 (rx (or (and "$" (submatch (1+ (regexp "[:alnum:]_"))))
72 (and "${" (submatch (minimal-match (0+ anything))) "}")
73 "$$")))
a4a216c5
GM
74 string start)
75 (cond ((match-beginning 1)
76 (let ((value (getenv (match-string 1 string))))
77 (setq string (replace-match (or value "") t t string)
78 start (+ (match-beginning 0) (length value)))))
79 ((match-beginning 2)
80 (let ((value (getenv (match-string 2 string))))
81 (setq string (replace-match (or value "") t t string)
82 start (+ (match-beginning 0) (length value)))))
83 (t
84 (setq string (replace-match "$" t t string)
85 start (+ (match-beginning 0) 1)))))
86 string))
87
1a90eae6 88;; Fixme: Should `process-environment' be recoded if LC_CTYPE &c is set?
a4a216c5
GM
89
90(defun setenv (variable &optional value unset substitute-env-vars)
6449c898 91 "Set the value of the environment variable named VARIABLE to VALUE.
b71038b2
JB
92VARIABLE should be a string. VALUE is optional; if not provided or
93nil, the environment variable VARIABLE will be removed. UNSET
a4a216c5
GM
94if non-nil means to remove VARIABLE from the environment.
95SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
96variables in VALUE with `substitute-env-vars', where see.
97Value is the new value if VARIABLE, or nil if removed from the
98environment.
cbfe666b
RS
99
100Interactively, a prefix argument means to unset the variable.
99ac138a
RS
101Interactively, the current value (if any) of the variable
102appears at the front of the history list when you type in the new value.
a4a216c5 103Interactively, always replace environment variables in the new value.
99ac138a 104
1a90eae6
DL
105This function works by modifying `process-environment'.
106
107As a special case, setting variable `TZ' calls `set-time-zone-rule' as
108a side-effect."
cbfe666b
RS
109 (interactive
110 (if current-prefix-arg
8b740009 111 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
2a5becfb
GM
112 (let* ((var (read-envvar-name "Set environment variable: " nil))
113 (value (getenv var)))
114 (when value
115 (push value setenv-history))
99ac138a 116 ;; Here finally we specify the args to give call setenv with.
71296446 117 (list var
a4a216c5
GM
118 (read-from-minibuffer (format "Set %s to value: " var)
119 nil nil nil 'setenv-history
120 value)
71296446 121 nil
a4a216c5 122 t))))
1a90eae6 123 (if (and (multibyte-string-p variable) locale-coding-system)
1ebb05c4
KH
124 (let ((codings (find-coding-systems-string (concat variable value))))
125 (unless (or (eq 'undecided (car codings))
126 (memq (coding-system-base locale-coding-system) codings))
127 (error "Can't encode `%s=%s' with `locale-coding-system'"
128 variable (or value "")))))
b71038b2 129 (if unset
a4a216c5
GM
130 (setq value nil)
131 (if substitute-env-vars
132 (setq value (substitute-env-vars value))))
1a90eae6
DL
133 (if (multibyte-string-p variable)
134 (setq variable (encode-coding-string variable locale-coding-system)))
135 (if (and value (multibyte-string-p value))
136 (setq value (encode-coding-string value locale-coding-system)))
6449c898 137 (if (string-match "=" variable)
1bbda2d6 138 (error "Environment variable name `%s' contains `='" variable)
971571b9 139 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
7e68de56 140 (case-fold-search nil)
cbfe666b
RS
141 (scan process-environment)
142 found)
7fd81709
RS
143 (if (string-equal "TZ" variable)
144 (set-time-zone-rule value))
cbfe666b
RS
145 (while scan
146 (cond ((string-match pattern (car scan))
147 (setq found t)
148 (if (eq nil value)
1a90eae6
DL
149 (setq process-environment (delq (car scan)
150 process-environment))
cbfe666b
RS
151 (setcar scan (concat variable "=" value)))
152 (setq scan nil)))
153 (setq scan (cdr scan)))
154 (or found
155 (if value
a3cda273 156 (setq process-environment
cbfe666b 157 (cons (concat variable "=" value)
a4a216c5
GM
158 process-environment))))))
159 value)
49116ac0 160
b1e11b4f
GM
161(defun getenv (variable)
162 "Get the value of environment variable VARIABLE.
163VARIABLE should be a string. Value is nil if VARIABLE is undefined in
164the environment. Otherwise, value is a string.
165
166This function consults the variable `process-environment'
167for its value."
168 (interactive (list (read-envvar-name "Get environment variable: " t)))
1a90eae6
DL
169 (let ((value (getenv-internal (if (multibyte-string-p variable)
170 (encode-coding-string
171 variable locale-coding-system)
172 variable))))
173 (if (and enable-multibyte-characters value)
174 (setq value (decode-coding-string value locale-coding-system)))
b1e11b4f
GM
175 (when (interactive-p)
176 (message "%s" (if value value "Not set")))
177 value))
178
1bbda2d6
NF
179(provide 'env)
180
181;;; env.el ends here