comparison lisp/prim/env.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
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 XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Synched up with: FSF 19.30.
25
26 ;;; Commentary:
27
28 ;; UNIX processes inherit a list of name-to-string associations from
29 ;; their parents called their `environment'; these are commonly used
30 ;; to control program options. This package permits you to set
31 ;; environment variables to be passed to any sub-process run under XEmacs.
32
33 ;;; Code:
34
35 ;; History list for environment variable names.
36 (defvar read-envvar-name-history nil)
37
38 (defun read-envvar-name (prompt &optional mustmatch)
39 "Read environment variable name, prompting with PROMPT.
40 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
41 If it is also not t, RET does not exit if it does non-null completion."
42 (completing-read prompt
43 (mapcar (function
44 (lambda (enventry)
45 (list (substring enventry 0
46 (string-match "=" enventry)))))
47 process-environment)
48 nil mustmatch nil 'read-envvar-name-history))
49
50 ;; History list for VALUE argument to setenv.
51 (defvar setenv-history nil)
52
53 ;;;###autoload
54 (defun setenv (variable &optional value unset)
55 "Set the value of the environment variable named VARIABLE to VALUE.
56 VARIABLE should be a string. VALUE is optional; if not provided or is
57 `nil', the environment variable VARIABLE will be removed.
58
59 Interactively, a prefix argument means to unset the variable.
60 Interactively, the current value (if any) of the variable
61 appears at the front of the history list when you type in the new value.
62
63 This function works by modifying `process-environment'."
64 (interactive
65 (if current-prefix-arg
66 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
67 (let* ((var (read-envvar-name "Set environment variable: " nil))
68 (oldval (getenv var))
69 newval
70 oldhist)
71 ;; Don't put the current value on the history
72 ;; if it is already there.
73 (if (equal oldval (car setenv-history))
74 (setq oldval nil))
75 ;; Now if OLDVAL is non-nil, we should add it to the history.
76 (if oldval
77 (setq setenv-history (cons oldval setenv-history)))
78 (setq oldhist setenv-history)
79 (setq newval (read-from-minibuffer (format "Set %s to value: " var)
80 nil nil nil 'setenv-history))
81 ;; If we added the current value to the history, remove it.
82 ;; Note that read-from-minibuffer may have added the new value.
83 ;; Don't remove that!
84 (if oldval
85 (if (eq oldhist setenv-history)
86 (setq setenv-history (cdr setenv-history))
87 (setcdr setenv-history (cdr (cdr setenv-history)))))
88 ;; Here finally we specify the args to give call setenv with.
89 (list var newval))))
90 (if unset (setq value nil))
91 (if (string-match "=" variable)
92 (error "Environment variable name `%s' contains `='" variable)
93 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
94 (case-fold-search nil)
95 (scan process-environment)
96 found)
97 (if (string-equal "TZ" variable)
98 (set-time-zone-rule value))
99 (while scan
100 (cond ((string-match pattern (car scan))
101 (setq found t)
102 (if (eq nil value)
103 (setq process-environment (delq (car scan) process-environment))
104 (setcar scan (concat variable "=" value)))
105 (setq scan nil)))
106 (setq scan (cdr scan)))
107 (or found
108 (if value
109 (setq process-environment
110 (cons (concat variable "=" value)
111 process-environment)))))))
112
113 (provide 'env)
114
115 ;;; env.el ends here