*** empty log message ***
[emacs.git] / lisp / env.el
blob409765f5ff4e941fd0d7f58e3c8adbfeac5b408d
1 ;;; env.el --- functions to manipulate environment variables
3 ;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
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 2, or (at your option)
14 ;; 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; 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.
26 ;;; Commentary:
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.
37 ;;; Code:
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
49 (decode-coding-string
50 (substring enventry 0
51 (string-match "=" enventry))
52 locale-coding-system t)
53 (substring enventry 0
54 (string-match "=" enventry)))))
55 process-environment)
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."
71 (let ((start 0))
72 (while (string-match
73 (eval-when-compile
74 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]"))))
75 (and "${" (submatch (minimal-match (0+ anything))) "}")
76 "$$")))
77 string start)
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)))))
82 ((match-beginning 2)
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)))))
89 string))
91 ;; Fixme: Should `process-environment' be recoded if LC_CTYPE &c is set?
93 (defun setenv (variable &optional value unset 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. UNSET
97 if non-nil means to remove VARIABLE from the environment.
98 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
99 variables in VALUE with `substitute-env-vars', where see.
100 Value is the new value if VARIABLE, or nil if removed from the
101 environment.
103 Interactively, a prefix argument means to unset the variable.
104 Interactively, the current value (if any) of the variable
105 appears at the front of the history list when you type in the new value.
106 Interactively, always replace environment variables in the new value.
108 This function works by modifying `process-environment'.
110 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
111 a side-effect."
112 (interactive
113 (if current-prefix-arg
114 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
115 (let* ((var (read-envvar-name "Set environment variable: " nil))
116 (value (getenv var)))
117 (when value
118 (push value setenv-history))
119 ;; Here finally we specify the args to give call setenv with.
120 (list var
121 (read-from-minibuffer (format "Set %s to value: " var)
122 nil nil nil 'setenv-history
123 value)
125 t))))
126 (if (and (multibyte-string-p variable) locale-coding-system)
127 (let ((codings (find-coding-systems-string (concat variable value))))
128 (unless (or (eq 'undecided (car codings))
129 (memq (coding-system-base locale-coding-system) codings))
130 (error "Can't encode `%s=%s' with `locale-coding-system'"
131 variable (or value "")))))
132 (if unset
133 (setq value nil)
134 (if substitute-env-vars
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)
145 found)
146 (if (string-equal "TZ" variable)
147 (set-time-zone-rule value))
148 (while scan
149 (cond ((string-match pattern (car scan))
150 (setq found t)
151 (if (eq nil value)
152 (setq process-environment (delq (car scan)
153 process-environment))
154 (setcar scan (concat variable "=" value)))
155 (setq scan nil)))
156 (setq scan (cdr scan)))
157 (or found
158 (if value
159 (setq process-environment
160 (cons (concat variable "=" value)
161 process-environment))))))
162 value)
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'
170 for its value."
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)
175 variable))))
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")))
180 value))
182 (provide 'env)
184 ;;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8
185 ;;; env.el ends here