*** empty log message ***
[emacs.git] / lisp / env.el
blob3d92501279e63cfc954a47f242d21bf24d2ff8b5
1 ;;; env.el --- functions to manipulate environment variables
3 ;; Copyright (C) 1991, 1994, 2000, 2001, 2003 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: processes, unix
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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.
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.
36 ;;; Code:
38 ;; History list for environment variable names.
39 (defvar read-envvar-name-history nil)
41 (defun read-envvar-name (prompt &optional mustmatch)
42 "Read environment variable name, prompting with PROMPT.
43 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
44 If it is also not t, RET does not exit if it does non-null completion."
45 (completing-read prompt
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)))))
54 process-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))
90 ;; Fixme: Should `process-environment' be recoded if LC_CTYPE &c is set?
92 (defun setenv (variable &optional value unset substitute-env-vars)
93 "Set the value of the environment variable named VARIABLE to VALUE.
94 VARIABLE should be a string. VALUE is optional; if not provided or
95 nil, the environment variable VARIABLE will be removed. UNSET
96 if non-nil means to remove VARIABLE from the environment.
97 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
98 variables in VALUE with `substitute-env-vars', where see.
99 Value is the new value if VARIABLE, or nil if removed from the
100 environment.
102 Interactively, a prefix argument means to unset the variable.
103 Interactively, the current value (if any) of the variable
104 appears at the front of the history list when you type in the new value.
105 Interactively, always replace environment variables in the new value.
107 This function works by modifying `process-environment'.
109 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
110 a side-effect."
111 (interactive
112 (if current-prefix-arg
113 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
114 (let* ((var (read-envvar-name "Set environment variable: " nil))
115 (value (getenv var)))
116 (when value
117 (push value setenv-history))
118 ;; Here finally we specify the args to give call setenv with.
119 (list var
120 (read-from-minibuffer (format "Set %s to value: " var)
121 nil nil nil 'setenv-history
122 value)
124 t))))
125 (if (and (multibyte-string-p variable) locale-coding-system)
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 "")))))
131 (if unset
132 (setq value nil)
133 (if substitute-env-vars
134 (setq value (substitute-env-vars value))))
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)))
139 (if (string-match "=" variable)
140 (error "Environment variable name `%s' contains `='" variable)
141 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
142 (case-fold-search nil)
143 (scan process-environment)
144 found)
145 (if (string-equal "TZ" variable)
146 (set-time-zone-rule value))
147 (while scan
148 (cond ((string-match pattern (car scan))
149 (setq found t)
150 (if (eq nil value)
151 (setq process-environment (delq (car scan)
152 process-environment))
153 (setcar scan (concat variable "=" value)))
154 (setq scan nil)))
155 (setq scan (cdr scan)))
156 (or found
157 (if value
158 (setq process-environment
159 (cons (concat variable "=" value)
160 process-environment))))))
161 value)
163 (defun getenv (variable)
164 "Get the value of environment variable VARIABLE.
165 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
166 the environment. Otherwise, value is a string.
168 This function consults the variable `process-environment'
169 for its value."
170 (interactive (list (read-envvar-name "Get environment variable: " t)))
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)))
177 (when (interactive-p)
178 (message "%s" (if value value "Not set")))
179 value))
181 (provide 'env)
183 ;;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
184 ;;; env.el ends here