home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / env.el < prev    next >
Encoding:
Text File  |  1992-08-06  |  3.1 KB  |  82 lines

  1. ;    I hadn't been expecting this post to be archived, so I didn't package it
  2. ; very well (for example, I didn't put a copyright notice on the message so
  3. ; that it could be distributed under the terms of the GPL).  I would
  4. ; appreciate it if you would replace the archive you made with the following
  5. ; instead.  Thanks.
  6. ;;; env.el - getenv and putenv functions that work with process-environment
  7. ;;; Copyright (C) 1992 Noah S. Friedman
  8. ;;;
  9. ;;; This program is free software; you can redistribute it and/or modify
  10. ;;; it under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 2, or (at your option)
  12. ;;; any later version.
  13. ;;;
  14. ;;; This program is distributed in the hope that it will be useful,
  15. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with this program; if not, you can either send email to this
  21. ;;; program's author (see below) or write to:
  22. ;;;
  23. ;;;              The Free Software Foundation, Inc.
  24. ;;;              675 Massachusetts Avenue.
  25. ;;;              Cambridge, MA 02139, USA. 
  26. ;;;
  27. ;;; Please send bug reports, etc. to friedman@prep.ai.mit.edu
  28.  
  29. ;;; LCD Archive Entry:
  30. ;;; env|Noah S. Friedman|friedman@prep.ai.mit.edu|
  31. ;;; getenv and putenv functions that work with process-environment.|
  32. ;;; 92-07-15|1.0|~/functions/env.el.Z|
  33.  
  34. ;;; Don't compile emacs with -DMAINTAIN_ENVIRONMENT.  It isn't very
  35. ;;; flexible---you can't create buffer-local environments with it.  
  36. ;;; Versions of GNU Emacs prior to 18.58 had bugs and process-environment
  37. ;;; didn't work very well, so I would suggest updating your emacs if it's
  38. ;;; older than 18.58. 
  39.  
  40.  
  41. ;; save old definition
  42. (and (subrp (symbol-function 'getenv))
  43.      (fset 'getenv-subr (symbol-function 'getenv)))
  44.  
  45. (defun getenv (var)
  46.   "Return the value of the environment variable VAR, or nil if none
  47. exists.  First look in process-environment, then in the actual environment."
  48.   (let ((env process-environment)
  49.     (pattern (concat "^" (regexp-quote var) "="))
  50.     found)
  51.     (while env
  52.       (and (string-match pattern (car env))
  53.        (setq found (car env)))
  54.       (setq env (cdr env)))
  55.     (if found
  56.     (substring found (match-end 0))
  57.       (getenv-subr var))))
  58.  
  59.  
  60. (defun putenv (var &optional value)
  61.   "Set the environment variable VAR to VALUE in process-environment.
  62. VALUE is optional.  If unspecified or nil, the variable VAR is removed
  63. >from process-environment (it cannot be removed from the real
  64. environment, however)."
  65.   ;; Remove previous definition of var from process-environment, if it exists.
  66.   (let ((env process-environment)
  67.     (pattern (concat "^" (regexp-quote var) "=")))
  68.     (while (and env
  69.         (not (if (string-match pattern (car env))
  70.              (setq process-environment
  71.                    (delq (car env) process-environment)))))
  72.       (setq env (cdr env))))
  73.   ;; Add new value, if any. 
  74.   (if value
  75.       (setq process-environment 
  76.         (cons (concat var "=" value) process-environment)))
  77.   value)
  78.  
  79. ;; eof
  80.  
  81.