Mercurial > emacs
annotate lisp/env.el @ 39519:865d22a5dca3
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Mon, 01 Oct 2001 09:32:09 +0000 |
parents | c0713ad66d33 |
children | c1fb5574fc7a |
rev | line source |
---|---|
38412
253f761ad37b
Some fixes to follow coding conventions in files maintained by FSF.
Pavel Janík <Pavel@Janik.cz>
parents:
28917
diff
changeset
|
1 ;;; env.el --- functions to manipulate environment variables |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
39126
c0713ad66d33
(setenv): Interactively, if VARIABLE has a current value,
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
3 ;; Copyright (C) 1991, 1994, 2000, 2001 Free Software Foundation, Inc. |
1185 | 4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
5 ;; Maintainer: FSF |
2315
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1218
diff
changeset
|
6 ;; Keywords: processes, unix |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
7 |
14169 | 8 ;; This file is part of GNU Emacs. |
489 | 9 |
14169 | 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. | |
489 | 14 |
14169 | 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. | |
489 | 19 |
14169 | 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 the | |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
489 | 24 |
2315
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1218
diff
changeset
|
25 ;;; Commentary: |
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1218
diff
changeset
|
26 |
14169 | 27 ;; UNIX processes inherit a list of name-to-string associations from their |
28 ;; parents called their `environment'; these are commonly used to control | |
29 ;; program options. This package permits you to set environment variables | |
30 ;; to be passed to any sub-process run under Emacs. | |
2315
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1218
diff
changeset
|
31 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
32 ;;; Code: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
33 |
9220
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
34 ;; History list for environment variable names. |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
35 (defvar read-envvar-name-history nil) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
36 |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
37 (defun read-envvar-name (prompt &optional mustmatch) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
38 "Read environment variable name, prompting with PROMPT. |
9345
832197fec54d
(read-envvar-name): Special meaning for MUSTMATCH
Richard M. Stallman <rms@gnu.org>
parents:
9220
diff
changeset
|
39 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name. |
832197fec54d
(read-envvar-name): Special meaning for MUSTMATCH
Richard M. Stallman <rms@gnu.org>
parents:
9220
diff
changeset
|
40 If it is also not t, RET does not exit if it does non-null completion." |
9220
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
41 (completing-read prompt |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
42 (mapcar (function |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
43 (lambda (enventry) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
44 (list (substring enventry 0 |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
45 (string-match "=" enventry))))) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
46 process-environment) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
47 nil mustmatch nil 'read-envvar-name-history)) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
48 |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
49 ;; History list for VALUE argument to setenv. |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
50 (defvar setenv-history nil) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
51 |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
52 (defun setenv (variable &optional value unset) |
489 | 53 "Set the value of the environment variable named VARIABLE to VALUE. |
2403
05d8916e4cde
renamed to env.el; changed setenv to putenv.
Noah Friedman <friedman@splode.com>
parents:
2315
diff
changeset
|
54 VARIABLE should be a string. VALUE is optional; if not provided or is |
05d8916e4cde
renamed to env.el; changed setenv to putenv.
Noah Friedman <friedman@splode.com>
parents:
2315
diff
changeset
|
55 `nil', the environment variable VARIABLE will be removed. |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
56 |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
57 Interactively, a prefix argument means to unset the variable. |
9220
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
58 Interactively, the current value (if any) of the variable |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
59 appears at the front of the history list when you type in the new value. |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
60 |
1216 | 61 This function works by modifying `process-environment'." |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
62 (interactive |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
63 (if current-prefix-arg |
9345
832197fec54d
(read-envvar-name): Special meaning for MUSTMATCH
Richard M. Stallman <rms@gnu.org>
parents:
9220
diff
changeset
|
64 (list (read-envvar-name "Clear environment variable: " 'exact) nil t) |
39126
c0713ad66d33
(setenv): Interactively, if VARIABLE has a current value,
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
65 (let* ((var (read-envvar-name "Set environment variable: " nil)) |
c0713ad66d33
(setenv): Interactively, if VARIABLE has a current value,
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
66 (value (getenv var))) |
c0713ad66d33
(setenv): Interactively, if VARIABLE has a current value,
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
67 (when value |
c0713ad66d33
(setenv): Interactively, if VARIABLE has a current value,
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
68 (push value setenv-history)) |
9220
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
69 ;; Here finally we specify the args to give call setenv with. |
21201
425bf2edf1d4
(setenv): Simplify reading of args by passing old value as the default.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
70 (list var (read-from-minibuffer (format "Set %s to value: " var) |
425bf2edf1d4
(setenv): Simplify reading of args by passing old value as the default.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
71 nil nil nil 'setenv-history |
39126
c0713ad66d33
(setenv): Interactively, if VARIABLE has a current value,
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
72 value))))) |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
73 (if unset (setq value nil)) |
489 | 74 (if (string-match "=" variable) |
2403
05d8916e4cde
renamed to env.el; changed setenv to putenv.
Noah Friedman <friedman@splode.com>
parents:
2315
diff
changeset
|
75 (error "Environment variable name `%s' contains `='" variable) |
1185 | 76 (let ((pattern (concat "\\`" (regexp-quote (concat variable "=")))) |
3667
f4a7ac2ec651
(setenv): Treat case as significant.
Richard M. Stallman <rms@gnu.org>
parents:
2410
diff
changeset
|
77 (case-fold-search nil) |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
78 (scan process-environment) |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
79 found) |
13018
9c090a7674c8
(setenv): Call set-time-zone-rule when setting TZ.
Richard M. Stallman <rms@gnu.org>
parents:
9345
diff
changeset
|
80 (if (string-equal "TZ" variable) |
9c090a7674c8
(setenv): Call set-time-zone-rule when setting TZ.
Richard M. Stallman <rms@gnu.org>
parents:
9345
diff
changeset
|
81 (set-time-zone-rule value)) |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
82 (while scan |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
83 (cond ((string-match pattern (car scan)) |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
84 (setq found t) |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
85 (if (eq nil value) |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
86 (setq process-environment (delq (car scan) process-environment)) |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
87 (setcar scan (concat variable "=" value))) |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
88 (setq scan nil))) |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
89 (setq scan (cdr scan))) |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
90 (or found |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
91 (if value |
8002
f8c8bbeca971
(setenv): Do something even if process-environment is nil.
Richard M. Stallman <rms@gnu.org>
parents:
3667
diff
changeset
|
92 (setq process-environment |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
93 (cons (concat variable "=" value) |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
94 process-environment))))))) |
584 | 95 |
28917
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
96 (defun getenv (variable) |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
97 "Get the value of environment variable VARIABLE. |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
98 VARIABLE should be a string. Value is nil if VARIABLE is undefined in |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
99 the environment. Otherwise, value is a string. |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
100 |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
101 This function consults the variable `process-environment' |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
102 for its value." |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
103 (interactive (list (read-envvar-name "Get environment variable: " t))) |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
104 (let ((value (getenv-internal variable))) |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
105 (when (interactive-p) |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
106 (message "%s" (if value value "Not set"))) |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
107 value)) |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
108 |
2403
05d8916e4cde
renamed to env.el; changed setenv to putenv.
Noah Friedman <friedman@splode.com>
parents:
2315
diff
changeset
|
109 (provide 'env) |
05d8916e4cde
renamed to env.el; changed setenv to putenv.
Noah Friedman <friedman@splode.com>
parents:
2315
diff
changeset
|
110 |
05d8916e4cde
renamed to env.el; changed setenv to putenv.
Noah Friedman <friedman@splode.com>
parents:
2315
diff
changeset
|
111 ;;; env.el ends here |