Mercurial > emacs
annotate lisp/env.el @ 83427:2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
* src/callproc.c (child_setup, getenv_internal, Fgetenv_internal):
Store the local environment in a frame (not terminal) parameter.
Update doc strings.
(syms_of_callproc): Update doc strings.
(Qenvironment): Moved to frame.c.
* lisp/env.el (read-envvar-name, setenv, getenv, environment): Use frame
parameters to store the local environment, not terminal parameters.
* server.el (server-process-filter): Store the local environment in a
frame (not terminal) parameter. Do not try to decode environment
strings.
* lisp/frame.el (make-frame): Set up the 'environment frame parameter,
when needed.
* src/frame.c (Qenvironment): Move here from callproc.c.
(Fdelete_frame): Don't allow other frames to refer to a deleted frame
in their 'environment parameter.
(Fframe_with_environment): New function.
(syms_of_frame): Defsubr it. Initialize and staticpro Qenvironment.
* frame.h (Qenvironment): Declare.
* lisp.h (Fframe_with_environment): EXFUN it.
git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-467
author | Karoly Lorentey <lorentey@elte.hu> |
---|---|
date | Thu, 29 Dec 2005 04:31:04 +0000 |
parents | c82829d08b89 |
children | 10f26433fe3f |
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 |
64762
41bb365f41c4
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64091
diff
changeset
|
3 ;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004, |
41bb365f41c4
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64091
diff
changeset
|
4 ;; 2005 Free Software Foundation, Inc. |
1185 | 5 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
6 ;; Maintainer: FSF |
2315
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1218
diff
changeset
|
7 ;; Keywords: processes, unix |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
8 |
14169 | 9 ;; This file is part of GNU Emacs. |
489 | 10 |
14169 | 11 ;; GNU Emacs is free software; you can redistribute it and/or modify |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
489 | 15 |
14169 | 16 ;; GNU Emacs is distributed in the hope that it will be useful, |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
489 | 20 |
14169 | 21 ;; You should have received a copy of the GNU General Public License |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64091 | 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 ;; Boston, MA 02110-1301, USA. | |
489 | 25 |
2315
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1218
diff
changeset
|
26 ;;; Commentary: |
9e7ec92a4fdf
Added or corrected Commentary headers
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1218
diff
changeset
|
27 |
14169 | 28 ;; UNIX processes inherit a list of name-to-string associations from their |
29 ;; parents called their `environment'; these are commonly used to control | |
30 ;; program options. This package permits you to set environment variables | |
31 ;; 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
|
32 |
49968 | 33 ;; Note that the environment string `process-environment' is not |
34 ;; decoded, but the args of `setenv' and `getenv' are normally | |
35 ;; multibyte text and get coding conversion. | |
36 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
37 ;;; Code: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
38 |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
39 (eval-when-compile (require 'cl)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
40 |
9220
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
41 ;; History list for environment variable names. |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
42 (defvar read-envvar-name-history nil) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
43 |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
44 (defun read-envvar-name (prompt &optional mustmatch) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
45 "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
|
46 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
|
47 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
|
48 (completing-read prompt |
49968 | 49 (mapcar (lambda (enventry) |
50 (list (if enable-multibyte-characters | |
51 (decode-coding-string | |
52 (substring enventry 0 | |
53 (string-match "=" enventry)) | |
54 locale-coding-system t) | |
55 (substring enventry 0 | |
56 (string-match "=" enventry))))) | |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
57 (append process-environment |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
58 (frame-parameter (frame-with-environment) 'environment) |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
59 global-environment)) |
9220
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
60 nil mustmatch nil 'read-envvar-name-history)) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
61 |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
62 ;; History list for VALUE argument to setenv. |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
63 (defvar setenv-history nil) |
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
64 |
39554
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
65 |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
66 (defun substitute-env-vars (string) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
67 "Substitute environment variables referred to in STRING. |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
68 `$FOO' where FOO is an environment variable name means to substitute |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
69 the value of that variable. The variable name should be terminated |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
70 with a character not a letter, digit or underscore; otherwise, enclose |
51279
92f5fdc30889
(substitute-env-vars): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
50873
diff
changeset
|
71 the entire variable name in braces. For instance, in `ab$cd-x', |
92f5fdc30889
(substitute-env-vars): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
50873
diff
changeset
|
72 `$cd' is treated as an environment variable. |
92f5fdc30889
(substitute-env-vars): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
50873
diff
changeset
|
73 |
92f5fdc30889
(substitute-env-vars): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
50873
diff
changeset
|
74 Use `$$' to insert a single dollar sign." |
39554
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
75 (let ((start 0)) |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
40430
diff
changeset
|
76 (while (string-match |
49968 | 77 (eval-when-compile |
51279
92f5fdc30889
(substitute-env-vars): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
50873
diff
changeset
|
78 (rx (or (and "$" (submatch (1+ (regexp "[[:alnum:]_]")))) |
49968 | 79 (and "${" (submatch (minimal-match (0+ anything))) "}") |
80 "$$"))) | |
39554
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
81 string start) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
82 (cond ((match-beginning 1) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
83 (let ((value (getenv (match-string 1 string)))) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
84 (setq string (replace-match (or value "") t t string) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
85 start (+ (match-beginning 0) (length value))))) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
86 ((match-beginning 2) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
87 (let ((value (getenv (match-string 2 string)))) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
88 (setq string (replace-match (or value "") t t string) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
89 start (+ (match-beginning 0) (length value))))) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
90 (t |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
91 (setq string (replace-match "$" t t string) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
92 start (+ (match-beginning 0) 1))))) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
93 string)) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
94 |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
95 ;; Fixme: Should the environment be recoded if LC_CTYPE &c is set? |
39554
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
96 |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
97 (defun setenv (variable &optional value unset substitute-env-vars frame) |
489 | 98 "Set the value of the environment variable named VARIABLE to VALUE. |
50873
562c854aa190
(setenv): Don't quote nil and t in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents:
49977
diff
changeset
|
99 VARIABLE should be a string. VALUE is optional; if not provided or |
562c854aa190
(setenv): Don't quote nil and t in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents:
49977
diff
changeset
|
100 nil, the environment variable VARIABLE will be removed. UNSET |
39554
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
101 if non-nil means to remove VARIABLE from the environment. |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
102 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
103 variables in VALUE with `substitute-env-vars', where see. |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
104 Value is the new value if VARIABLE, or nil if removed from the |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
105 environment. |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
106 |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
107 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
|
108 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
|
109 appears at the front of the history list when you type in the new value. |
39554
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
110 Interactively, always replace environment variables in the new value. |
9220
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
111 |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
112 If VARIABLE is set in `process-environment', then this function |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
113 modifies its value there. Otherwise, this function works by |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
114 modifying either `global-environment' or the environment |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
115 belonging to the selected frame, depending on the value of |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
116 `local-environment-variables'. |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
117 |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
118 If optional parameter FRAME is non-nil, then it should be a a |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
119 frame. If the specified frame has its own set of environment |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
120 variables, this function will modify VARIABLE in it. Note that |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
121 frames on the same terminal device usually share their |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
122 environment, so calling `setenv' on one of them affects the |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
123 others as well. |
83421
bb2edc915032
Implement automatic terminal-local environment variables via `local-environment-variables'.
Karoly Lorentey <lorentey@elte.hu>
parents:
64762
diff
changeset
|
124 |
49968 | 125 As a special case, setting variable `TZ' calls `set-time-zone-rule' as |
126 a side-effect." | |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
127 (interactive |
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
128 (if current-prefix-arg |
9345
832197fec54d
(read-envvar-name): Special meaning for MUSTMATCH
Richard M. Stallman <rms@gnu.org>
parents:
9220
diff
changeset
|
129 (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
|
130 (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
|
131 (value (getenv var))) |
c0713ad66d33
(setenv): Interactively, if VARIABLE has a current value,
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
132 (when value |
c0713ad66d33
(setenv): Interactively, if VARIABLE has a current value,
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
133 (push value setenv-history)) |
9220
8f05784959cc
(setenv-history): New history list.
Richard M. Stallman <rms@gnu.org>
parents:
8005
diff
changeset
|
134 ;; Here finally we specify the args to give call setenv with. |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
40430
diff
changeset
|
135 (list var |
39554
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
136 (read-from-minibuffer (format "Set %s to value: " var) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
137 nil nil nil 'setenv-history |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
138 value) |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
40430
diff
changeset
|
139 nil |
39554
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
140 t)))) |
49968 | 141 (if (and (multibyte-string-p variable) locale-coding-system) |
49977
f491c5cee974
(setenv): Fix previous change.
Kenichi Handa <handa@m17n.org>
parents:
49968
diff
changeset
|
142 (let ((codings (find-coding-systems-string (concat variable value)))) |
f491c5cee974
(setenv): Fix previous change.
Kenichi Handa <handa@m17n.org>
parents:
49968
diff
changeset
|
143 (unless (or (eq 'undecided (car codings)) |
f491c5cee974
(setenv): Fix previous change.
Kenichi Handa <handa@m17n.org>
parents:
49968
diff
changeset
|
144 (memq (coding-system-base locale-coding-system) codings)) |
f491c5cee974
(setenv): Fix previous change.
Kenichi Handa <handa@m17n.org>
parents:
49968
diff
changeset
|
145 (error "Can't encode `%s=%s' with `locale-coding-system'" |
f491c5cee974
(setenv): Fix previous change.
Kenichi Handa <handa@m17n.org>
parents:
49968
diff
changeset
|
146 variable (or value ""))))) |
50873
562c854aa190
(setenv): Don't quote nil and t in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents:
49977
diff
changeset
|
147 (if unset |
39554
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
148 (setq value nil) |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
149 (if substitute-env-vars |
c1fb5574fc7a
(substitute-env-vars): New function.
Gerd Moellmann <gerd@gnu.org>
parents:
39126
diff
changeset
|
150 (setq value (substitute-env-vars value)))) |
49968 | 151 (if (multibyte-string-p variable) |
152 (setq variable (encode-coding-string variable locale-coding-system))) | |
153 (if (and value (multibyte-string-p value)) | |
154 (setq value (encode-coding-string value locale-coding-system))) | |
489 | 155 (if (string-match "=" variable) |
83421
bb2edc915032
Implement automatic terminal-local environment variables via `local-environment-variables'.
Karoly Lorentey <lorentey@elte.hu>
parents:
64762
diff
changeset
|
156 (error "Environment variable name `%s' contains `='" variable)) |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
157 (let ((pattern (concat "\\`" (regexp-quote variable) "\\(=\\|\\'\\)")) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
158 (case-fold-search nil) |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
159 (frame-env (frame-parameter (frame-with-environment frame) 'environment)) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
160 (frame-forced (not frame)) |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
161 (scan process-environment) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
162 found) |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
163 (setq frame (frame-with-environment frame)) |
83421
bb2edc915032
Implement automatic terminal-local environment variables via `local-environment-variables'.
Karoly Lorentey <lorentey@elte.hu>
parents:
64762
diff
changeset
|
164 (if (string-equal "TZ" variable) |
bb2edc915032
Implement automatic terminal-local environment variables via `local-environment-variables'.
Karoly Lorentey <lorentey@elte.hu>
parents:
64762
diff
changeset
|
165 (set-time-zone-rule value)) |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
166 (block nil |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
167 ;; Look for an existing entry for VARIABLE; try `process-environment' first. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
168 (while (and scan (stringp (car scan))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
169 (when (string-match pattern (car scan)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
170 (if value |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
171 (setcar scan (concat variable "=" value)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
172 ;; Leave unset variables in `process-environment', |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
173 ;; otherwise the overridden value in `global-environment' |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
174 ;; or frame-env would become unmasked. |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
175 (setcar scan variable)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
176 (return value)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
177 (setq scan (cdr scan))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
178 |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
179 ;; Look in the local or global environment, whichever is relevant. |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
180 (let ((local-var-p (and frame-env |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
181 (or frame-forced |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
182 (eq t local-environment-variables) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
183 (member variable local-environment-variables))))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
184 (setq scan (if local-var-p |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
185 frame-env |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
186 global-environment)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
187 (while scan |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
188 (when (string-match pattern (car scan)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
189 (if value |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
190 (setcar scan (concat variable "=" value)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
191 (if local-var-p |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
192 (set-frame-parameter frame 'environment |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
193 (delq (car scan) frame-env)) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
194 (setq global-environment (delq (car scan) global-environment)))) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
195 (return value)) |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
196 (setq scan (cdr scan))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
197 |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
198 ;; VARIABLE is not in any environment list. |
83421
bb2edc915032
Implement automatic terminal-local environment variables via `local-environment-variables'.
Karoly Lorentey <lorentey@elte.hu>
parents:
64762
diff
changeset
|
199 (if value |
bb2edc915032
Implement automatic terminal-local environment variables via `local-environment-variables'.
Karoly Lorentey <lorentey@elte.hu>
parents:
64762
diff
changeset
|
200 (if local-var-p |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
201 (set-frame-parameter frame 'environment |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
202 (cons (concat variable "=" value) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
203 frame-env)) |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
204 (setq global-environment |
8004
a86eceda6537
(setenv): Rewrite. Provide a way to unset interactively.
Richard M. Stallman <rms@gnu.org>
parents:
8002
diff
changeset
|
205 (cons (concat variable "=" value) |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
206 global-environment)))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
207 (return value))))) |
584 | 208 |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
209 (defun getenv (variable &optional frame) |
28917
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
210 "Get the value of environment variable VARIABLE. |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
211 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
|
212 the environment. Otherwise, value is a string. |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
213 |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
214 If optional parameter FRAME is non-nil, then it should be a |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
215 frame. If the specified terminal device has its own set of |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
216 environment variables, this function will look up VARIABLE in it. |
83421
bb2edc915032
Implement automatic terminal-local environment variables via `local-environment-variables'.
Karoly Lorentey <lorentey@elte.hu>
parents:
64762
diff
changeset
|
217 |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
218 Otherwise, this function searches `process-environment' for |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
219 VARIABLE. If it was not found there, then it continues the |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
220 search in either `global-environment' or the environment list of |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
221 the selected frame, depending on the value of |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
222 `local-environment-variables'." |
28917
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
223 (interactive (list (read-envvar-name "Get environment variable: " t))) |
49968 | 224 (let ((value (getenv-internal (if (multibyte-string-p variable) |
225 (encode-coding-string | |
226 variable locale-coding-system) | |
227 variable)))) | |
228 (if (and enable-multibyte-characters value) | |
229 (setq value (decode-coding-string value locale-coding-system))) | |
28917
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
230 (when (interactive-p) |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
231 (message "%s" (if value value "Not set"))) |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
232 value)) |
845292e36d62
(getenv): New function, interactively callable.
Gerd Moellmann <gerd@gnu.org>
parents:
21201
diff
changeset
|
233 |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
234 (defun environment () |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
235 "Return a list of environment variables with their values. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
236 Each entry in the list is a string of the form NAME=VALUE. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
237 |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
238 The returned list can not be used to change environment |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
239 variables, only read them. See `setenv' to do that. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
240 |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
241 The list is constructed from elements of `process-environment', |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
242 `global-environment' and the local environment list of the |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
243 selected frame, as specified by `local-environment-variables'. |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
244 |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
245 Non-ASCII characters are encoded according to the initial value of |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
246 `locale-coding-system', i.e. the elements must normally be decoded for use. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
247 See `setenv' and `getenv'." |
83427
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
248 (let ((env (let ((local-env (frame-parameter (frame-with-environment) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
249 'environment))) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
250 (cond ((or (not local-environment-variables) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
251 (not local-env)) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
252 (append process-environment global-environment nil)) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
253 ((consp local-environment-variables) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
254 (let ((e (reverse process-environment))) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
255 (dolist (entry local-environment-variables) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
256 (setq e (cons (getenv entry) e))) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
257 (append (nreverse e) global-environment nil))) |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
258 (t |
2afc49c9f0c0
Store local environment in frame (not terminal) parameters.
Karoly Lorentey <lorentey@elte.hu>
parents:
83425
diff
changeset
|
259 (append process-environment local-env nil))))) |
83425
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
260 scan seen) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
261 ;; Find the first valid entry in env. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
262 (while (and env (stringp (car env)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
263 (or (not (string-match "=" (car env))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
264 (member (substring (car env) 0 (string-match "=" (car env))) seen))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
265 (setq seen (cons (car env) seen) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
266 env (cdr env))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
267 (setq scan env) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
268 (while (and (cdr scan) (stringp (cadr scan))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
269 (let* ((match (string-match "=" (cadr scan))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
270 (name (substring (cadr scan) 0 match))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
271 (cond ((not match) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
272 ;; Unset variable. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
273 (setq seen (cons name seen)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
274 (setcdr scan (cddr scan))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
275 ((member name seen) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
276 ;; Duplicate variable. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
277 (setcdr scan (cddr scan))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
278 (t |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
279 ;; New variable. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
280 (setq seen (cons name seen) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
281 scan (cdr scan)))))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
282 env)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
283 |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
284 (defmacro let-environment (varlist &rest body) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
285 "Evaluate BODY with environment variables set according to VARLIST. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
286 The environment variables are then restored to their previous |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
287 values. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
288 The value of the last form in BODY is returned. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
289 |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
290 Each element of VARLIST is either a string (which variable is |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
291 then removed from the environment), or a list (NAME |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
292 VALUEFORM) (which sets NAME to the value of VALUEFORM, a string). |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
293 All the VALUEFORMs are evaluated before any variables are set." |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
294 (declare (indent 2)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
295 (let ((old-env (make-symbol "old-env")) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
296 (name (make-symbol "name")) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
297 (value (make-symbol "value")) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
298 (entry (make-symbol "entry")) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
299 (frame (make-symbol "frame"))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
300 `(let ((,frame (selected-frame)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
301 ,old-env) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
302 ;; Evaluate VALUEFORMs and replace them in VARLIST with their values. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
303 (dolist (,entry ,varlist) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
304 (unless (stringp ,entry) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
305 (if (cdr (cdr ,entry)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
306 (error "`let-environment' bindings can have only one value-form")) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
307 (setcdr ,entry (eval (cadr ,entry))))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
308 ;; Set the variables. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
309 (dolist (,entry ,varlist) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
310 (let ((,name (if (stringp ,entry) ,entry (car ,entry))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
311 (,value (if (consp ,entry) (cdr ,entry)))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
312 (setq ,old-env (cons (cons ,name (getenv ,name)) ,old-env)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
313 (setenv ,name ,value))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
314 (unwind-protect |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
315 (progn ,@body) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
316 ;; Restore old values. |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
317 (with-selected-frame (if (frame-live-p ,frame) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
318 ,frame |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
319 (selected-frame)) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
320 (dolist (,entry ,old-env) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
321 (setenv (car ,entry) (cdr ,entry)))))))) |
c82829d08b89
Fix semantics of let-binding `process-environment'.
Karoly Lorentey <lorentey@elte.hu>
parents:
83421
diff
changeset
|
322 |
2403
05d8916e4cde
renamed to env.el; changed setenv to putenv.
Noah Friedman <friedman@splode.com>
parents:
2315
diff
changeset
|
323 (provide 'env) |
05d8916e4cde
renamed to env.el; changed setenv to putenv.
Noah Friedman <friedman@splode.com>
parents:
2315
diff
changeset
|
324 |
52401 | 325 ;;; arch-tag: b7d6a8f7-bc81-46db-8e39-8d721d4ed0b8 |
2403
05d8916e4cde
renamed to env.el; changed setenv to putenv.
Noah Friedman <friedman@splode.com>
parents:
2315
diff
changeset
|
326 ;;; env.el ends here |