| 1 | ;;; env.el --- functions to manipulate environment variables -*- lexical-binding:t -*- |
| 2 | |
| 3 | ;; Copyright (C) 1991, 1994, 2000-2014 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Maintainer: emacs-devel@gnu.org |
| 6 | ;; Keywords: processes, unix |
| 7 | ;; Package: emacs |
| 8 | |
| 9 | ;; This file is part of GNU Emacs. |
| 10 | |
| 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 3 of the License, or |
| 14 | ;; (at your option) any later version. |
| 15 | |
| 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. |
| 20 | |
| 21 | ;; You should have received a copy of the GNU General Public License |
| 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | ;;; Commentary: |
| 25 | |
| 26 | ;; UNIX processes inherit a list of name-to-string associations from their |
| 27 | ;; parents called their `environment'; these are commonly used to control |
| 28 | ;; program options. This package permits you to set environment variables |
| 29 | ;; to be passed to any sub-process run under Emacs. |
| 30 | |
| 31 | ;; Note that the environment string `process-environment' is not |
| 32 | ;; decoded, but the args of `setenv' and `getenv' are normally |
| 33 | ;; multibyte text and get coding conversion. |
| 34 | |
| 35 | ;;; Code: |
| 36 | |
| 37 | ;; History list for environment variable names. |
| 38 | (defvar read-envvar-name-history nil) |
| 39 | |
| 40 | (defun read-envvar-name (prompt &optional mustmatch) |
| 41 | "Read environment variable name, prompting with PROMPT. |
| 42 | Optional second arg MUSTMATCH, if non-nil, means require existing envvar name. |
| 43 | If it is also not t, RET does not exit if it does non-null completion." |
| 44 | (completing-read prompt |
| 45 | (mapcar (lambda (enventry) |
| 46 | (let ((str (substring enventry 0 |
| 47 | (string-match "=" enventry)))) |
| 48 | (if (multibyte-string-p str) |
| 49 | (decode-coding-string |
| 50 | str locale-coding-system t) |
| 51 | str))) |
| 52 | (append process-environment |
| 53 | ;;(frame-environment) |
| 54 | )) |
| 55 | nil mustmatch nil 'read-envvar-name-history)) |
| 56 | |
| 57 | ;; History list for VALUE argument to setenv. |
| 58 | (defvar setenv-history nil) |
| 59 | |
| 60 | (defconst env--substitute-vars-regexp |
| 61 | "\\$\\(?:\\(?1:[[:alnum:]_]+\\)\\|{\\(?1:[^{}]+\\)}\\|\\$\\)") |
| 62 | |
| 63 | (defun substitute-env-vars (string &optional when-undefined) |
| 64 | "Substitute environment variables referred to in STRING. |
| 65 | `$FOO' where FOO is an environment variable name means to substitute |
| 66 | the value of that variable. The variable name should be terminated |
| 67 | with a character not a letter, digit or underscore; otherwise, enclose |
| 68 | the entire variable name in braces. For instance, in `ab$cd-x', |
| 69 | `$cd' is treated as an environment variable. |
| 70 | |
| 71 | If WHEN-DEFINED is nil, references to undefined environment variables |
| 72 | are replaced by the empty string; if it is a function, the function is called |
| 73 | with the variable name as argument and should return the text with which |
| 74 | to replace it or nil to leave it unchanged. |
| 75 | If it is non-nil and not a function, references to undefined variables are |
| 76 | left unchanged. |
| 77 | |
| 78 | Use `$$' to insert a single dollar sign." |
| 79 | (let ((start 0)) |
| 80 | (while (string-match env--substitute-vars-regexp string start) |
| 81 | (cond ((match-beginning 1) |
| 82 | (let* ((var (match-string 1 string)) |
| 83 | (value (getenv var))) |
| 84 | (if (and (null value) |
| 85 | (if (functionp when-undefined) |
| 86 | (null (setq value (funcall when-undefined var))) |
| 87 | when-undefined)) |
| 88 | (setq start (match-end 0)) |
| 89 | (setq string (replace-match (or value "") t t string) |
| 90 | start (+ (match-beginning 0) (length value)))))) |
| 91 | (t |
| 92 | (setq string (replace-match "$" t t string) |
| 93 | start (+ (match-beginning 0) 1))))) |
| 94 | string)) |
| 95 | |
| 96 | (defun substitute-env-in-file-name (filename) |
| 97 | (substitute-env-vars filename |
| 98 | ;; How 'bout we lookup other tables than the env? |
| 99 | ;; E.g. we could accept bookmark names as well! |
| 100 | (if (memq system-type '(windows-nt ms-dos)) |
| 101 | (lambda (var) (getenv (upcase var))) |
| 102 | t))) |
| 103 | |
| 104 | (defun setenv-internal (env variable value keep-empty) |
| 105 | "Set VARIABLE to VALUE in ENV, adding empty entries if KEEP-EMPTY. |
| 106 | Changes ENV by side-effect, and returns its new value." |
| 107 | (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)")) |
| 108 | (case-fold-search nil) |
| 109 | (scan env) |
| 110 | prev found) |
| 111 | ;; Handle deletions from the beginning of the list specially. |
| 112 | (if (and (null value) |
| 113 | (not keep-empty) |
| 114 | env |
| 115 | (stringp (car env)) |
| 116 | (string-match pattern (car env))) |
| 117 | (cdr env) |
| 118 | ;; Try to find existing entry for VARIABLE in ENV. |
| 119 | (while (and scan (stringp (car scan))) |
| 120 | (when (string-match pattern (car scan)) |
| 121 | (if value |
| 122 | (setcar scan (concat variable "=" value)) |
| 123 | (if keep-empty |
| 124 | (setcar scan variable) |
| 125 | (setcdr prev (cdr scan)))) |
| 126 | (setq found t |
| 127 | scan nil)) |
| 128 | (setq prev scan |
| 129 | scan (cdr scan))) |
| 130 | (if (and (not found) (or value keep-empty)) |
| 131 | (cons (if value |
| 132 | (concat variable "=" value) |
| 133 | variable) |
| 134 | env) |
| 135 | env)))) |
| 136 | |
| 137 | ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set? |
| 138 | |
| 139 | (defun setenv (variable &optional value substitute-env-vars) |
| 140 | "Set the value of the environment variable named VARIABLE to VALUE. |
| 141 | VARIABLE should be a string. VALUE is optional; if not provided or |
| 142 | nil, the environment variable VARIABLE will be removed. |
| 143 | |
| 144 | Interactively, a prefix argument means to unset the variable, and |
| 145 | otherwise the current value (if any) of the variable appears at |
| 146 | the front of the history list when you type in the new value. |
| 147 | This function always replaces environment variables in the new |
| 148 | value when called interactively. |
| 149 | |
| 150 | SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment |
| 151 | variables in VALUE with `substitute-env-vars', which see. |
| 152 | This is normally used only for interactive calls. |
| 153 | |
| 154 | The return value is the new value of VARIABLE, or nil if |
| 155 | it was removed from the environment. |
| 156 | |
| 157 | This function works by modifying `process-environment'. |
| 158 | |
| 159 | As a special case, setting variable `TZ' calls `set-time-zone-rule' as |
| 160 | a side-effect." |
| 161 | (interactive |
| 162 | (if current-prefix-arg |
| 163 | (list (read-envvar-name "Clear environment variable: " 'exact) nil) |
| 164 | (let* ((var (read-envvar-name "Set environment variable: " nil)) |
| 165 | (value (getenv var))) |
| 166 | (when value |
| 167 | (add-to-history 'setenv-history value)) |
| 168 | ;; Here finally we specify the args to give call setenv with. |
| 169 | (list var |
| 170 | (read-from-minibuffer (format "Set %s to value: " var) |
| 171 | nil nil nil 'setenv-history |
| 172 | value) |
| 173 | t)))) |
| 174 | (if (and (multibyte-string-p variable) locale-coding-system) |
| 175 | (let ((codings (find-coding-systems-string (concat variable value)))) |
| 176 | (unless (or (eq 'undecided (car codings)) |
| 177 | (memq (coding-system-base locale-coding-system) codings)) |
| 178 | (error "Can't encode `%s=%s' with `locale-coding-system'" |
| 179 | variable (or value ""))))) |
| 180 | (and value |
| 181 | substitute-env-vars |
| 182 | (setq value (substitute-env-vars value))) |
| 183 | (if (multibyte-string-p variable) |
| 184 | (setq variable (encode-coding-string variable locale-coding-system))) |
| 185 | (if (and value (multibyte-string-p value)) |
| 186 | (setq value (encode-coding-string value locale-coding-system))) |
| 187 | (if (string-match "=" variable) |
| 188 | (error "Environment variable name `%s' contains `='" variable)) |
| 189 | (if (string-equal "TZ" variable) |
| 190 | (set-time-zone-rule value)) |
| 191 | (setq process-environment (setenv-internal process-environment |
| 192 | variable value t)) |
| 193 | value) |
| 194 | |
| 195 | (defun getenv (variable &optional frame) |
| 196 | "Get the value of environment variable VARIABLE. |
| 197 | VARIABLE should be a string. Value is nil if VARIABLE is undefined in |
| 198 | the environment. Otherwise, value is a string. |
| 199 | |
| 200 | If optional parameter FRAME is non-nil, then it should be a |
| 201 | frame. This function will look up VARIABLE in its `environment' |
| 202 | parameter. |
| 203 | |
| 204 | Otherwise, this function searches `process-environment' for |
| 205 | VARIABLE. If it is not found there, then it continues the search |
| 206 | in the environment list of the selected frame." |
| 207 | (interactive (list (read-envvar-name "Get environment variable: " t))) |
| 208 | (let ((value (getenv-internal (if (multibyte-string-p variable) |
| 209 | (encode-coding-string |
| 210 | variable locale-coding-system) |
| 211 | variable) |
| 212 | (and frame |
| 213 | (assq 'environment |
| 214 | (frame-parameters frame)))))) |
| 215 | (if (and enable-multibyte-characters value) |
| 216 | (setq value (decode-coding-string value locale-coding-system))) |
| 217 | (when (called-interactively-p 'interactive) |
| 218 | (message "%s" (if value value "Not set"))) |
| 219 | value)) |
| 220 | |
| 221 | (provide 'env) |
| 222 | |
| 223 | ;;; env.el ends here |