(read-envvar-name): Special meaning for MUSTMATCH
[bpt/emacs.git] / lisp / env.el
1 ;;; env.el --- functions to manipulate environment variables.
2
3 ;;; Copyright 1991, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: processes, unix
7
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
12 ;;; the Free Software Foundation; either version 2, or (at your option)
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
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
31 ;;; Code:
32
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.
38 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
39 If it is also not t, RET does not exit if it does non-null completion."
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
51 ;;;###autoload
52 (defun setenv (variable &optional value unset)
53 "Set the value of the environment variable named VARIABLE to VALUE.
54 VARIABLE should be a string. VALUE is optional; if not provided or is
55 `nil', the environment variable VARIABLE will be removed.
56
57 Interactively, a prefix argument means to unset the variable.
58 Interactively, the current value (if any) of the variable
59 appears at the front of the history list when you type in the new value.
60
61 This function works by modifying `process-environment'."
62 (interactive
63 (if current-prefix-arg
64 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
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))))
88 (if unset (setq value nil))
89 (if (string-match "=" variable)
90 (error "Environment variable name `%s' contains `='" variable)
91 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
92 (case-fold-search nil)
93 (scan process-environment)
94 found)
95 (while scan
96 (cond ((string-match pattern (car scan))
97 (setq found t)
98 (if (eq nil value)
99 (setq process-environment (delq (car scan) process-environment))
100 (setcar scan (concat variable "=" value)))
101 (setq scan nil)))
102 (setq scan (cdr scan)))
103 (or found
104 (if value
105 (setq process-environment
106 (cons (concat variable "=" value)
107 process-environment)))))))
108
109 (provide 'env)
110
111 ;;; env.el ends here