env.el 8.7 KB

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