annotate lisp/prim/env.el @ 30:ec9a17fef872 r19-15b98

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