merging Emacs.app (NeXTstep port)
[bpt/emacs.git] / lisp / ns-grabenv.el
1 ;;; ns-grabenv.el --- functions to set environment variables by running a subshell
2 ;;; Copyright (C) 1993, 1994, 2005, 2006, 2008 Free Software Foundation, Inc.
3
4 ;;; Author: Carl Edman, Christian Limpach, Scott Bender, Christophe de Dinechin,
5 ;;; Adrian Robert
6 ;;; Keywords: terminals
7
8 ;;; This file is part of GNU Emacs.
9 ;;;
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 3, or (at your option)
13 ;;; any later version.
14 ;;;
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.
19 ;;;
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
22 ;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;;; Boston, MA 02110-1301, USA.
24
25 ;;; Idea based on NS 4.2 distribution, this version of code based on
26 ;;; mac-read-environment-vars-from-shell () by David Reitter in Aquamacs dist..
27
28
29 ;; utility function
30 (defun ns-make-command-string (cmdlist)
31 (let ((str "")
32 (cmds cmdlist))
33 (while cmds
34 (if (not (eq str "")) (setq str (format "%s ; " str)))
35 (setq str (format "%s%s" str (car cmds)))
36 (setq cmds (cdr cmds)))
37 str))
38
39
40 ;;;###autoload
41 (defun ns-grabenv (&optional shell-path &optional startup)
42 "Run a shell subprocess, and interpret its output as a series of environment\n\
43 variables to insert into the emacs environment. The first optional argument\n\
44 gives the path to the shell (defaults to the current setting of\n\
45 shell-file-name). The remaining arguments are interpreted as a list of\n\
46 commands for it to execute (defaults to \"printenv\")."
47 (interactive)
48 (with-temp-buffer
49 (let ((shell-file-name (if shell-path shell-path shell-file-name))
50 (cmd (ns-make-command-string (if startup startup '("printenv")))))
51 (shell-command cmd t)
52 (while (search-forward-regexp "^\\([A-Za-z_0-9]+\\)=\\(.*\\)$" nil t)
53 (setenv (match-string 1)
54 (if (equal (match-string 1) "PATH")
55 (concat (getenv "PATH") ":" (match-string 2))
56 (match-string 2)))))))
57
58 (provide 'ns-grabenv)
59
60 ;;; ns-grabenv.el ends here
61
62 ; (autoload (quote ns-grabenv) "ns-grabenv" "\
63 ; Run a shell subprocess, and interpret its output as a series of environment
64 ; variables to insert into the emacs environment. The first optional argument
65 ; gives the path to the shell (defaults to the current setting of
66 ; shell-file-name). The remaining arguments are interpreted as a list of
67 ; commands for it to execute (defaults to \"printenv\")." nil nil)