annotate 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
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;;; Copyright 1991, 1994 Free Software Foundation, Inc.
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;; Synched up with: FSF 19.30.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; UNIX processes inherit a list of name-to-string associations from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; their parents called their `environment'; these are commonly used
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; to control program options. This package permits you to set
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; environment variables to be passed to any sub-process run under XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;; History list for environment variable names.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 (defvar read-envvar-name-history nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 (defun read-envvar-name (prompt &optional mustmatch)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 "Read environment variable name, prompting with PROMPT.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 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
42 (completing-read prompt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 (mapcar (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 (lambda (enventry)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (list (substring enventry 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (string-match "=" enventry)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 process-environment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 nil mustmatch nil 'read-envvar-name-history))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; History list for VALUE argument to setenv.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (defvar setenv-history nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 (defun setenv (variable &optional value unset)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 "Set the value of the environment variable named VARIABLE to VALUE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 VARIABLE should be a string. VALUE is optional; if not provided or is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 `nil', the environment variable VARIABLE will be removed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 Interactively, a prefix argument means to unset the variable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 Interactively, the current value (if any) of the variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 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
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 This function works by modifying `process-environment'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (if current-prefix-arg
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (let* ((var (read-envvar-name "Set environment variable: " nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (oldval (getenv var))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 newval
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 oldhist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 ;; Don't put the current value on the history
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 ;; if it is already there.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (if (equal oldval (car setenv-history))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (setq oldval nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ;; Now if OLDVAL is non-nil, we should add it to the history.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (if oldval
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (setq setenv-history (cons oldval setenv-history)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (setq oldhist setenv-history)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (setq newval (read-from-minibuffer (format "Set %s to value: " var)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 nil nil nil 'setenv-history))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 ;; If we added the current value to the history, remove it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ;; Note that read-from-minibuffer may have added the new value.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;; Don't remove that!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (if oldval
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (if (eq oldhist setenv-history)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (setq setenv-history (cdr setenv-history))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (setcdr setenv-history (cdr (cdr setenv-history)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 ;; Here finally we specify the args to give call setenv with.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (list var newval))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (if unset (setq value nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (if (string-match "=" variable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (error "Environment variable name `%s' contains `='" variable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (case-fold-search nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (scan process-environment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 found)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (if (string-equal "TZ" variable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (set-time-zone-rule value))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (while scan
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (cond ((string-match pattern (car scan))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (setq found t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (if (eq nil value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (setq process-environment (delq (car scan) process-environment))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (setcar scan (concat variable "=" value)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (setq scan nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (setq scan (cdr scan)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (or found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (if value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (setq process-environment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (cons (concat variable "=" value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 process-environment)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (provide 'env)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 ;;; env.el ends here