(using-unix-filesystems):
[emacs.git] / lisp / env.el
blobe78bbb314d984cef1abc73aa4cada78d3174d097
1 ;;; env.el --- functions to manipulate environment variables.
3 ;;; Copyright 1991, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: processes, unix
8 ;;; This file is part of GNU Emacs.
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.
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.
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.
24 ;;; Commentary:
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.
31 ;;; Code:
33 ;; History list for environment variable names.
34 (defvar read-envvar-name-history nil)
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))
48 ;; History list for VALUE argument to setenv.
49 (defvar setenv-history nil)
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.
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.
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 (if (string-equal "TZ" variable)
96 (set-time-zone-rule value))
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
107 (setq process-environment
108 (cons (concat variable "=" value)
109 process-environment)))))))
111 (provide 'env)
113 ;;; env.el ends here