[comp.emacs] setenv inside gemacs?

brians@hpcljms.HP.COM (Brian Sullivan) (10/05/88)

   I need to know if it is possible to set environment variable in GNU emacs.  I
have set up some bindings to compile the file that I am editing but the compiler
needs to have some environment variables set before it can be run.  I notice that
GNU emacs does have an eLisp fuction "getenv" but no "setenv" or "putenv".  Has any
one run into this problem before, and what was the solution?

--- Brian Sullivan --- 

mesard@bbn.com (Wayne Mesard) (10/06/88)

From article <860006@hpcljms.HP.COM>, by brians@hpcljms.HP.COM (Brian Sullivan):
 
>    I need to know if it is possible to set environment variable in GNU
> emacs.  I have set up some bindings to compile the file that I am
> editing but the compiler needs to have some environment variables set

Well, an apropos on "env" points to (among other things):

  process-environment's value is ([[my environment deleted]])

  Documentation:
  List of strings to append to environment of subprocesses that are started.
  Each string should have the format ENVVARNAME=VALUE.

> I notice that GNU emacs does have an eLisp
> fuction "getenv" but no "setenv" or "putenv".

Actually there is a setenv in environ.c, but it's enclosed in an #ifdef
MAINTAIN_ENVIRONMENT, which I imagine is not set by default.

-- 
void *Wayne_Mesard();         MESARD@BBN.COM         BBN, Cambridge, MA

jjd@ALEXANDER.BBN.COM (James J Dempsey) (10/06/88)

>> To: unix-emacs@bbn.com
>> Date: 4 Oct 88 22:43:52 GMT
>> From: Brian Sullivan <hp-sde!hpcuhb!hpcllla!hpclisp!hpcljms!brians@hplabs.hp.com>
>> Subject: setenv inside gemacs?
>> 
>>    I need to know if it is possible to set environment variable in GNU emacs.  I

From what I can tell, if you define MAINTAIN_ENVIRONMENT in your
src/config.h, then you get the behaviour you are looking for -- that
is to say there is a setenv elisp function.

However, I don't find any documentation for MAINTAIN_ENVIRONMENT.
Stephen Gildea (gildea@bbn.com) asked bug-gnu-emacs about it and got
no responses.  However, I did try building an emacs with
MAINTAIN_ENVIRONMENT defined and it seemed to work fine.

		--Jim Dempsey--
		BBN Communications
		jjd@bbn.com (ARPA Internet)
                ..!{decvax, harvard, wjh12, linus}!bbn!jjd

wolfgang@mgm.mit.edu (Wolfgang Rupprecht) (10/06/88)

In article <860006@hpcljms.HP.COM> brians@hpcljms.HP.COM (Brian Sullivan) writes:
>   I need to know if it is possible to set environment variable in
>GNU emacs.  I have set up some bindings to compile the file that I am
>editing but the compiler needs to have some environment variables set
>before it can be run.  I notice that GNU emacs does have an eLisp
>fuction "getenv" but no "setenv" or "putenv".  Has any one run into
>this problem before, and what was the solution?


try this:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;									     ;;
;;	File:     env.el						     ;;
;;	Author:   Wolfgang Rupprecht					     ;;
;;	Contents: modify and print the unix environment variables	     ;;
;; 		  for subprocesses     					     ;;
;;									     ;;
;;	$Log$								     ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Copyright (C) 1987 Wolfgang S. Rupprecht
;; This file is for GNU Emacs.

;; Env.el is distributed in the hope that it will be useful in conjuction with
;; GNU Emacs, but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  This program is distributed for use with
;; and under the same terms as GNU Emacs. Refer to the GNU Emacs General Public
;; License for full details.

;; Everyone is granted permission to copy, modify and redistribute
;; env.el, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.


;; Code for hacking environment variables.
;; The most notable use is to clean up the environment of
;; the subprocesses. (setting TERM=dumb, unsetting TERMCAP, etc.) 
;; Requires GnuEmacs version 18.36 or later

(provide 'env)

(defvar old-env process-environment
  "A safe haven for the initial enviromment variables. Useful if you
ever want to look at or reset back to the initial environment.")

(defun modify-environment (name value)
  "Sets environment VARIABLE to have VALUE. Variable must be a string,
value may be either a string or nil.  Tries to preserve order of
variables and number of occurances of variable.  Appends new variable
to end of enironment variable list if it doesn't already occur.  If
value is nil, then all occurances of that variable are removed.  To
set a blank value, but have the variable name defined use the empty
string \"\"."
  (interactive "sVariable: \nxValue (as lisp string, or nil): ")
  (let ((env process-environment) new-env seen)
    (while env
      (if (null (string-match (concat "^" name "=") (car env)))
	  (setq new-env (cons (car env) new-env))
	  (if value
	      (setq new-env (cons (concat name "=" value) new-env)
		    seen t)))
      (setq env (cdr env)))
    (if (and value (null seen))
	(setq new-env (cons (concat name "=" value) new-env)))
    (setq process-environment (nreverse new-env))))

(defun print-environment-variable (name)
  "Print the value of an environment variable in the minibuffer.
Returns the variable as a string (or nil if variable is not set)"
  (interactive "sEnvironment Variable: ")
  (let ((env process-environment) value)
    (while (and env (null (string-match (concat "^" name "=\\(.*\\)$") (car env))))
      (setq env (cdr env)))
    (if env
	(setq value (substring (car env) (match-beginning 1) (match-end 1))))
    (if (interactive-p)
	(if env
	    (message "Variable '%s's value is '%s'" name value)
	    (message "Variable '%s' is NOT set" name)))
    value))

Wolfgang Rupprecht	ARPA:  wolfgang@mgm.mit.edu (IP 18.82.0.114)
TEL: (617) 267-4365	UUCP:  mit-eddie!mgm.mit.edu!wolfgang