Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; env.el --- functions to manipulate environment variables |
c88ab9ce | 2 | |
2a5becfb | 3 | ;; Copyright (C) 1991, 1994, 2000, 2001 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 | |
e5167999 ER |
32 | ;;; Code: |
33 | ||
99ac138a RS |
34 | ;; History list for environment variable names. |
35 | (defvar read-envvar-name-history nil) | |
36 | ||
37 | (defun read-envvar-name (prompt &optional mustmatch) | |
38 | "Read environment variable name, prompting with PROMPT. | |
8b740009 RS |
39 | Optional second arg MUSTMATCH, if non-nil, means require existing envvar name. |
40 | If it is also not t, RET does not exit if it does non-null completion." | |
99ac138a RS |
41 | (completing-read prompt |
42 | (mapcar (function | |
43 | (lambda (enventry) | |
44 | (list (substring enventry 0 | |
45 | (string-match "=" enventry))))) | |
46 | process-environment) | |
47 | nil mustmatch nil 'read-envvar-name-history)) | |
48 | ||
49 | ;; History list for VALUE argument to setenv. | |
50 | (defvar setenv-history nil) | |
51 | ||
a4a216c5 GM |
52 | |
53 | (defun substitute-env-vars (string) | |
54 | "Substitute environment variables referred to in STRING. | |
55 | `$FOO' where FOO is an environment variable name means to substitute | |
56 | the value of that variable. The variable name should be terminated | |
57 | with a character not a letter, digit or underscore; otherwise, enclose | |
58 | the entire variable name in braces. Use `$$' to insert a single | |
59 | dollar sign." | |
60 | (let ((start 0)) | |
61 | (while (string-match | |
1fd520b6 GM |
62 | (rx (or (and "$" (submatch (1+ (in "a-zA-Z0-9_")))) |
63 | (and "${" (submatch (minimal-match (0+ anything))) "}") | |
64 | "$$")) | |
a4a216c5 GM |
65 | string start) |
66 | (cond ((match-beginning 1) | |
67 | (let ((value (getenv (match-string 1 string)))) | |
68 | (setq string (replace-match (or value "") t t string) | |
69 | start (+ (match-beginning 0) (length value))))) | |
70 | ((match-beginning 2) | |
71 | (let ((value (getenv (match-string 2 string)))) | |
72 | (setq string (replace-match (or value "") t t string) | |
73 | start (+ (match-beginning 0) (length value))))) | |
74 | (t | |
75 | (setq string (replace-match "$" t t string) | |
76 | start (+ (match-beginning 0) 1))))) | |
77 | string)) | |
78 | ||
79 | ||
80 | (defun setenv (variable &optional value unset substitute-env-vars) | |
6449c898 | 81 | "Set the value of the environment variable named VARIABLE to VALUE. |
1bbda2d6 | 82 | VARIABLE should be a string. VALUE is optional; if not provided or is |
a4a216c5 GM |
83 | `nil', the environment variable VARIABLE will be removed. UNSET |
84 | if non-nil means to remove VARIABLE from the environment. | |
85 | SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment | |
86 | variables in VALUE with `substitute-env-vars', where see. | |
87 | Value is the new value if VARIABLE, or nil if removed from the | |
88 | environment. | |
cbfe666b RS |
89 | |
90 | Interactively, a prefix argument means to unset the variable. | |
99ac138a RS |
91 | Interactively, the current value (if any) of the variable |
92 | appears at the front of the history list when you type in the new value. | |
a4a216c5 | 93 | Interactively, always replace environment variables in the new value. |
99ac138a | 94 | |
b6a5978e | 95 | This function works by modifying `process-environment'." |
cbfe666b RS |
96 | (interactive |
97 | (if current-prefix-arg | |
8b740009 | 98 | (list (read-envvar-name "Clear environment variable: " 'exact) nil t) |
2a5becfb GM |
99 | (let* ((var (read-envvar-name "Set environment variable: " nil)) |
100 | (value (getenv var))) | |
101 | (when value | |
102 | (push value setenv-history)) | |
99ac138a | 103 | ;; Here finally we specify the args to give call setenv with. |
a4a216c5 GM |
104 | (list var |
105 | (read-from-minibuffer (format "Set %s to value: " var) | |
106 | nil nil nil 'setenv-history | |
107 | value) | |
108 | nil | |
109 | t)))) | |
110 | (if unset | |
111 | (setq value nil) | |
112 | (if substitute-env-vars | |
113 | (setq value (substitute-env-vars value)))) | |
6449c898 | 114 | (if (string-match "=" variable) |
1bbda2d6 | 115 | (error "Environment variable name `%s' contains `='" variable) |
971571b9 | 116 | (let ((pattern (concat "\\`" (regexp-quote (concat variable "=")))) |
7e68de56 | 117 | (case-fold-search nil) |
cbfe666b RS |
118 | (scan process-environment) |
119 | found) | |
7fd81709 RS |
120 | (if (string-equal "TZ" variable) |
121 | (set-time-zone-rule value)) | |
cbfe666b RS |
122 | (while scan |
123 | (cond ((string-match pattern (car scan)) | |
124 | (setq found t) | |
125 | (if (eq nil value) | |
126 | (setq process-environment (delq (car scan) process-environment)) | |
127 | (setcar scan (concat variable "=" value))) | |
128 | (setq scan nil))) | |
129 | (setq scan (cdr scan))) | |
130 | (or found | |
131 | (if value | |
a3cda273 | 132 | (setq process-environment |
cbfe666b | 133 | (cons (concat variable "=" value) |
a4a216c5 GM |
134 | process-environment)))))) |
135 | value) | |
49116ac0 | 136 | |
b1e11b4f GM |
137 | (defun getenv (variable) |
138 | "Get the value of environment variable VARIABLE. | |
139 | VARIABLE should be a string. Value is nil if VARIABLE is undefined in | |
140 | the environment. Otherwise, value is a string. | |
141 | ||
142 | This function consults the variable `process-environment' | |
143 | for its value." | |
144 | (interactive (list (read-envvar-name "Get environment variable: " t))) | |
145 | (let ((value (getenv-internal variable))) | |
146 | (when (interactive-p) | |
147 | (message "%s" (if value value "Not set"))) | |
148 | value)) | |
149 | ||
1bbda2d6 NF |
150 | (provide 'env) |
151 | ||
152 | ;;; env.el ends here |