aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Love2003-02-24 22:51:41 +0000
committerDave Love2003-02-24 22:51:41 +0000
commit1a90eae68dbf762b921f921f4d73c1cae69e840f (patch)
treebd512e2f302651433a1f1ae1cce95bd6b240f714
parent9c5b896b2ba0f2a6cc22493fc860b45ee854edf2 (diff)
downloademacs-1a90eae68dbf762b921f921f4d73c1cae69e840f.tar.gz
emacs-1a90eae68dbf762b921f921f4d73c1cae69e840f.zip
(read-envvar-name): Decode names.
(substitute-env-vars): Use eval-when-compile and char class. (setenv): Doc fix. Encode the data (after checking that's possible). (getenv): Encode the name and decode the result.
-rw-r--r--lisp/env.el55
1 files changed, 43 insertions, 12 deletions
diff --git a/lisp/env.el b/lisp/env.el
index f41677fcf18..912634482bf 100644
--- a/lisp/env.el
+++ b/lisp/env.el
@@ -1,6 +1,6 @@
1;;; env.el --- functions to manipulate environment variables 1;;; env.el --- functions to manipulate environment variables
2 2
3;; Copyright (C) 1991, 1994, 2000, 2001 Free Software Foundation, Inc. 3;; Copyright (C) 1991, 1994, 2000, 2001, 2003 Free Software Foundation, Inc.
4 4
5;; Maintainer: FSF 5;; Maintainer: FSF
6;; Keywords: processes, unix 6;; Keywords: processes, unix
@@ -29,6 +29,10 @@
29;; program options. This package permits you to set environment variables 29;; program options. This package permits you to set environment variables
30;; to be passed to any sub-process run under Emacs. 30;; to be passed to any sub-process run under Emacs.
31 31
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
32;;; Code: 36;;; Code:
33 37
34;; History list for environment variable names. 38;; History list for environment variable names.
@@ -39,10 +43,14 @@
39Optional second arg MUSTMATCH, if non-nil, means require existing envvar name. 43Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
40If it is also not t, RET does not exit if it does non-null completion." 44If it is also not t, RET does not exit if it does non-null completion."
41 (completing-read prompt 45 (completing-read prompt
42 (mapcar (function 46 (mapcar (lambda (enventry)
43 (lambda (enventry) 47 (list (if enable-multibyte-characters
44 (list (substring enventry 0 48 (decode-coding-string
45 (string-match "=" enventry))))) 49 (substring enventry 0
50 (string-match "=" enventry))
51 locale-coding-system t)
52 (substring enventry 0
53 (string-match "=" enventry)))))
46 process-environment) 54 process-environment)
47 nil mustmatch nil 'read-envvar-name-history)) 55 nil mustmatch nil 'read-envvar-name-history))
48 56
@@ -59,9 +67,10 @@ the entire variable name in braces. Use `$$' to insert a single
59dollar sign." 67dollar sign."
60 (let ((start 0)) 68 (let ((start 0))
61 (while (string-match 69 (while (string-match
62 (rx (or (and "$" (submatch (1+ (in "a-zA-Z0-9_")))) 70 (eval-when-compile
63 (and "${" (submatch (minimal-match (0+ anything))) "}") 71 (rx (or (and "$" (submatch (1+ (regexp "[:alnum:]_"))))
64 "$$")) 72 (and "${" (submatch (minimal-match (0+ anything))) "}")
73 "$$")))
65 string start) 74 string start)
66 (cond ((match-beginning 1) 75 (cond ((match-beginning 1)
67 (let ((value (getenv (match-string 1 string)))) 76 (let ((value (getenv (match-string 1 string))))
@@ -76,6 +85,7 @@ dollar sign."
76 start (+ (match-beginning 0) 1))))) 85 start (+ (match-beginning 0) 1)))))
77 string)) 86 string))
78 87
88;; Fixme: Should `process-environment' be recoded if LC_CTYPE &c is set?
79 89
80(defun setenv (variable &optional value unset substitute-env-vars) 90(defun setenv (variable &optional value unset substitute-env-vars)
81 "Set the value of the environment variable named VARIABLE to VALUE. 91 "Set the value of the environment variable named VARIABLE to VALUE.
@@ -92,7 +102,10 @@ Interactively, the current value (if any) of the variable
92appears at the front of the history list when you type in the new value. 102appears at the front of the history list when you type in the new value.
93Interactively, always replace environment variables in the new value. 103Interactively, always replace environment variables in the new value.
94 104
95This function works by modifying `process-environment'." 105This function works by modifying `process-environment'.
106
107As a special case, setting variable `TZ' calls `set-time-zone-rule' as
108a side-effect."
96 (interactive 109 (interactive
97 (if current-prefix-arg 110 (if current-prefix-arg
98 (list (read-envvar-name "Clear environment variable: " 'exact) nil t) 111 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
@@ -107,10 +120,22 @@ This function works by modifying `process-environment'."
107 value) 120 value)
108 nil 121 nil
109 t)))) 122 t))))
110 (if unset 123 (if (and (multibyte-string-p variable) locale-coding-system)
124 (unless (memq (coding-system-base locale-coding-system)
125 (find-coding-systems-string (concat variable value)))
126 (error "Can't encode `%s=%s' with `locale-coding-system'"
127 variable (or value "")))
128 (unless (memq 'undecided (find-coding-systems-string variable))
129 (error "Can't encode `%s=%s' with unspecified `locale-coding-system'"
130 variable (or value ""))))
131 (if unset
111 (setq value nil) 132 (setq value nil)
112 (if substitute-env-vars 133 (if substitute-env-vars
113 (setq value (substitute-env-vars value)))) 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)))
114 (if (string-match "=" variable) 139 (if (string-match "=" variable)
115 (error "Environment variable name `%s' contains `='" variable) 140 (error "Environment variable name `%s' contains `='" variable)
116 (let ((pattern (concat "\\`" (regexp-quote (concat variable "=")))) 141 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
@@ -123,7 +148,8 @@ This function works by modifying `process-environment'."
123 (cond ((string-match pattern (car scan)) 148 (cond ((string-match pattern (car scan))
124 (setq found t) 149 (setq found t)
125 (if (eq nil value) 150 (if (eq nil value)
126 (setq process-environment (delq (car scan) process-environment)) 151 (setq process-environment (delq (car scan)
152 process-environment))
127 (setcar scan (concat variable "=" value))) 153 (setcar scan (concat variable "=" value)))
128 (setq scan nil))) 154 (setq scan nil)))
129 (setq scan (cdr scan))) 155 (setq scan (cdr scan)))
@@ -142,7 +168,12 @@ the environment. Otherwise, value is a string.
142This function consults the variable `process-environment' 168This function consults the variable `process-environment'
143for its value." 169for its value."
144 (interactive (list (read-envvar-name "Get environment variable: " t))) 170 (interactive (list (read-envvar-name "Get environment variable: " t)))
145 (let ((value (getenv-internal variable))) 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)))
146 (when (interactive-p) 177 (when (interactive-p)
147 (message "%s" (if value value "Not set"))) 178 (message "%s" (if value value "Not set")))
148 value)) 179 value))