(texinfo-format-scan): Accept @^, @", @?, @!, @-.
[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.
38Optional second arg MUSTMATCH, if non-nil, means require existing envvar name."
39 (completing-read prompt
40 (mapcar (function
41 (lambda (enventry)
42 (list (substring enventry 0
43 (string-match "=" enventry)))))
44 process-environment)
45 nil mustmatch nil 'read-envvar-name-history))
46
47;; History list for VALUE argument to setenv.
48(defvar setenv-history nil)
49
aa673ecc 50;;;###autoload
cbfe666b 51(defun setenv (variable &optional value unset)
6449c898 52 "Set the value of the environment variable named VARIABLE to VALUE.
1bbda2d6
NF
53VARIABLE should be a string. VALUE is optional; if not provided or is
54`nil', the environment variable VARIABLE will be removed.
cbfe666b
RS
55
56Interactively, a prefix argument means to unset the variable.
99ac138a
RS
57Interactively, the current value (if any) of the variable
58appears at the front of the history list when you type in the new value.
59
b6a5978e 60This function works by modifying `process-environment'."
cbfe666b
RS
61 (interactive
62 (if current-prefix-arg
99ac138a
RS
63 (list (read-envvar-name "Clear environment variable: " t) nil t)
64 (let* ((var (read-envvar-name "Set environment variable: " nil))
65 (oldval (getenv var))
66 newval
67 oldhist)
68 ;; Don't put the current value on the history
69 ;; if it is already there.
70 (if (equal oldval (car setenv-history))
71 (setq oldval nil))
72 ;; Now if OLDVAL is non-nil, we should add it to the history.
73 (if oldval
74 (setq setenv-history (cons oldval setenv-history)))
75 (setq oldhist setenv-history)
76 (setq newval (read-from-minibuffer (format "Set %s to value: " var)
77 nil nil nil 'setenv-history))
78 ;; If we added the current value to the history, remove it.
79 ;; Note that read-from-minibuffer may have added the new value.
80 ;; Don't remove that!
81 (if oldval
82 (if (eq oldhist setenv-history)
83 (setq setenv-history (cdr setenv-history))
84 (setcdr setenv-history (cdr (cdr setenv-history)))))
85 ;; Here finally we specify the args to give call setenv with.
86 (list var newval))))
cbfe666b 87 (if unset (setq value nil))
6449c898 88 (if (string-match "=" variable)
1bbda2d6 89 (error "Environment variable name `%s' contains `='" variable)
971571b9 90 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
7e68de56 91 (case-fold-search nil)
cbfe666b
RS
92 (scan process-environment)
93 found)
94 (while scan
95 (cond ((string-match pattern (car scan))
96 (setq found t)
97 (if (eq nil value)
98 (setq process-environment (delq (car scan) process-environment))
99 (setcar scan (concat variable "=" value)))
100 (setq scan nil)))
101 (setq scan (cdr scan)))
102 (or found
103 (if value
a3cda273 104 (setq process-environment
cbfe666b
RS
105 (cons (concat variable "=" value)
106 process-environment)))))))
49116ac0 107
1bbda2d6
NF
108(provide 'env)
109
110;;; env.el ends here