(undigestify-rmail-message): Better error messages.
[bpt/emacs.git] / lisp / env.el
CommitLineData
1bbda2d6 1;;; env.el --- functions to manipulate environment variables.
c88ab9ce 2
dffc4996 3;;; Copyright 1991, 1994 Free Software Foundation, Inc.
971571b9 4
e5167999 5;; Maintainer: FSF
d9ecc911 6;; Keywords: processes, unix
e5167999 7
6449c898
JB
8;;; This file is part of GNU Emacs.
9
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
e5167999 12;;; the Free Software Foundation; either version 2, or (at your option)
6449c898
JB
13;;; any later version.
14
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.
19
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
22;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
d9ecc911
ER
24;;; Commentary:
25
26;; UNIX processes inherit a list of name-to-string associations from
27;; their parents called their `environment'; these are commonly used
28;; to control program options. This package permits you to set
29;; environment variables to be passed to any sub-process run under Emacs.
30
e5167999
ER
31;;; Code:
32
99ac138a
RS
33;; History list for environment variable names.
34(defvar read-envvar-name-history nil)
35
36(defun read-envvar-name (prompt &optional mustmatch)
37 "Read environment variable name, prompting with PROMPT.
8b740009
RS
38Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
39If it is also not t, RET does not exit if it does non-null completion."
99ac138a
RS
40 (completing-read prompt
41 (mapcar (function
42 (lambda (enventry)
43 (list (substring enventry 0
44 (string-match "=" enventry)))))
45 process-environment)
46 nil mustmatch nil 'read-envvar-name-history))
47
48;; History list for VALUE argument to setenv.
49(defvar setenv-history nil)
50
aa673ecc 51;;;###autoload
cbfe666b 52(defun setenv (variable &optional value unset)
6449c898 53 "Set the value of the environment variable named VARIABLE to VALUE.
1bbda2d6
NF
54VARIABLE should be a string. VALUE is optional; if not provided or is
55`nil', the environment variable VARIABLE will be removed.
cbfe666b
RS
56
57Interactively, a prefix argument means to unset the variable.
99ac138a
RS
58Interactively, the current value (if any) of the variable
59appears at the front of the history list when you type in the new value.
60
b6a5978e 61This function works by modifying `process-environment'."
cbfe666b
RS
62 (interactive
63 (if current-prefix-arg
8b740009 64 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
99ac138a
RS
65 (let* ((var (read-envvar-name "Set environment variable: " nil))
66 (oldval (getenv var))
67 newval
68 oldhist)
69 ;; Don't put the current value on the history
70 ;; if it is already there.
71 (if (equal oldval (car setenv-history))
72 (setq oldval nil))
73 ;; Now if OLDVAL is non-nil, we should add it to the history.
74 (if oldval
75 (setq setenv-history (cons oldval setenv-history)))
76 (setq oldhist setenv-history)
77 (setq newval (read-from-minibuffer (format "Set %s to value: " var)
78 nil nil nil 'setenv-history))
79 ;; If we added the current value to the history, remove it.
80 ;; Note that read-from-minibuffer may have added the new value.
81 ;; Don't remove that!
82 (if oldval
83 (if (eq oldhist setenv-history)
84 (setq setenv-history (cdr setenv-history))
85 (setcdr setenv-history (cdr (cdr setenv-history)))))
86 ;; Here finally we specify the args to give call setenv with.
87 (list var newval))))
cbfe666b 88 (if unset (setq value nil))
6449c898 89 (if (string-match "=" variable)
1bbda2d6 90 (error "Environment variable name `%s' contains `='" variable)
971571b9 91 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
7e68de56 92 (case-fold-search nil)
cbfe666b
RS
93 (scan process-environment)
94 found)
7fd81709
RS
95 (if (string-equal "TZ" variable)
96 (set-time-zone-rule value))
cbfe666b
RS
97 (while scan
98 (cond ((string-match pattern (car scan))
99 (setq found t)
100 (if (eq nil value)
101 (setq process-environment (delq (car scan) process-environment))
102 (setcar scan (concat variable "=" value)))
103 (setq scan nil)))
104 (setq scan (cdr scan)))
105 (or found
106 (if value
a3cda273 107 (setq process-environment
cbfe666b
RS
108 (cons (concat variable "=" value)
109 process-environment)))))))
49116ac0 110
1bbda2d6
NF
111(provide 'env)
112
113;;; env.el ends here