(cvs-retrieve-revision): Make sure HEAD gets you the head of the branch.
[emacs.git] / lisp / env.el
blobeae724d148d3ea68d558fcf513c097a1a12579ab
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. Use `$$' to insert a single
67 dollar sign."
68 (let ((start 0))
69 (while (string-match
70 (eval-when-compile
71 (rx (or (and "$" (submatch (1+ (regexp "[:alnum:]_"))))
72 (and "${" (submatch (minimal-match (0+ anything))) "}")
73 "$$")))
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)))))
84 (setq string (replace-match "$" t t string)
85 start (+ (match-beginning 0) 1)))))
86 string))
88 ;; Fixme: Should `process-environment' be recoded if LC_CTYPE &c is set?
90 (defun setenv (variable &optional value unset substitute-env-vars)
91 "Set the value of the environment variable named VARIABLE to VALUE.
92 VARIABLE should be a string. VALUE is optional; if not provided or
93 nil, the environment variable VARIABLE will be removed. UNSET
94 if non-nil means to remove VARIABLE from the environment.
95 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
96 variables in VALUE with `substitute-env-vars', where see.
97 Value is the new value if VARIABLE, or nil if removed from the
98 environment.
100 Interactively, a prefix argument means to unset the variable.
101 Interactively, the current value (if any) of the variable
102 appears at the front of the history list when you type in the new value.
103 Interactively, always replace environment variables in the new value.
105 This function works by modifying `process-environment'.
107 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
108 a side-effect."
109 (interactive
110 (if current-prefix-arg
111 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
112 (let* ((var (read-envvar-name "Set environment variable: " nil))
113 (value (getenv var)))
114 (when value
115 (push value setenv-history))
116 ;; Here finally we specify the args to give call setenv with.
117 (list var
118 (read-from-minibuffer (format "Set %s to value: " var)
119 nil nil nil 'setenv-history
120 value)
122 t))))
123 (if (and (multibyte-string-p variable) locale-coding-system)
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 "")))))
129 (if unset
130 (setq value nil)
131 (if substitute-env-vars
132 (setq value (substitute-env-vars value))))
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)))
137 (if (string-match "=" variable)
138 (error "Environment variable name `%s' contains `='" variable)
139 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
140 (case-fold-search nil)
141 (scan process-environment)
142 found)
143 (if (string-equal "TZ" variable)
144 (set-time-zone-rule value))
145 (while scan
146 (cond ((string-match pattern (car scan))
147 (setq found t)
148 (if (eq nil value)
149 (setq process-environment (delq (car scan)
150 process-environment))
151 (setcar scan (concat variable "=" value)))
152 (setq scan nil)))
153 (setq scan (cdr scan)))
154 (or found
155 (if value
156 (setq process-environment
157 (cons (concat variable "=" value)
158 process-environment))))))
159 value)
161 (defun getenv (variable)
162 "Get the value of environment variable VARIABLE.
163 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
164 the environment. Otherwise, value is a string.
166 This function consults the variable `process-environment'
167 for its value."
168 (interactive (list (read-envvar-name "Get environment variable: " t)))
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)))
175 (when (interactive-p)
176 (message "%s" (if value value "Not set")))
177 value))
179 (provide 'env)
181 ;;; env.el ends here