Merge from emacs--rel--22
[emacs.git] / lisp / ns-grabenv.el
blob643daba2ca269c828d768d3d9f45f8bbcec1109c
1 ;;; ns-grabenv.el --- functions to set environment variables by running a subshell
3 ;; Copyright (C) 1993, 1994, 2005, 2006, 2008 Free Software Foundation, Inc.
5 ;; Author: Carl Edman, Christian Limpach, Scott Bender, Christophe de Dinechin, Adrian Robert
6 ;; Keywords: terminals
8 ;; This file is part of GNU Emacs.
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 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Idea based on NS 4.2 distribution, this version of code based on
25 ;;; mac-read-environment-vars-from-shell () by David Reitter in Aquamacs dist..
28 ;; utility function
29 (defun ns-make-command-string (cmdlist)
30 (let ((str "")
31 (cmds cmdlist))
32 (while cmds
33 (if (not (eq str "")) (setq str (format "%s ; " str)))
34 (setq str (format "%s%s" str (car cmds)))
35 (setq cmds (cdr cmds)))
36 str))
39 ;;;###autoload
40 (defun ns-grabenv (&optional shell-path &optional startup)
41 "Run a shell subprocess, and interpret its output as a series of environment\n\
42 variables to insert into the emacs environment. The first optional argument\n\
43 gives the path to the shell (defaults to the current setting of\n\
44 shell-file-name). The remaining arguments are interpreted as a list of\n\
45 commands for it to execute (defaults to \"printenv\")."
46 (interactive)
47 (with-temp-buffer
48 (let ((shell-file-name (if shell-path shell-path shell-file-name))
49 (cmd (ns-make-command-string (if startup startup '("printenv")))))
50 (shell-command cmd t)
51 (while (search-forward-regexp "^\\([A-Za-z_0-9]+\\)=\\(.*\\)$" nil t)
52 (setenv (match-string 1)
53 (if (equal (match-string 1) "PATH")
54 (concat (getenv "PATH") ":" (match-string 2))
55 (match-string 2)))))))
57 (provide 'ns-grabenv)
59 ;; arch-tag: e65e1dd8-1566-460c-ad66-07948588be56
60 ;;; ns-grabenv.el ends here