|
| 1 | +;;; exec-path-from-shell.el --- Make Emacs use the $PATH set up by the user's shell |
| 2 | + |
| 3 | +;; Copyright (C) 2012 Steve Purcell |
| 4 | + |
| 5 | +;; Author: Steve Purcell <[email protected]> |
| 6 | +;; Keywords: environment |
| 7 | +;; URL: https://github.com/purcell/exec-path-from-shell |
| 8 | +;; Version: DEV |
| 9 | + |
| 10 | +;; This file is not part of GNU Emacs. |
| 11 | + |
| 12 | +;; This file is free software: you can redistribute it and/or modify |
| 13 | +;; it under the terms of the GNU General Public License as published by |
| 14 | +;; the Free Software Foundation, either version 3 of the License, or |
| 15 | +;; (at your option) any later version. |
| 16 | + |
| 17 | +;; This file is distributed in the hope that it will be useful, |
| 18 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | +;; GNU General Public License for more details. |
| 21 | + |
| 22 | +;; You should have received a copy of the GNU General Public License |
| 23 | +;; along with this file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + |
| 25 | +;;; Commentary: |
| 26 | + |
| 27 | +;; On OS X (and perhaps elsewhere) the $PATH environment variable and |
| 28 | +;; `exec-path' used by a windowed Emacs instance will usually be the |
| 29 | +;; system-wide default path, rather than that seen in a terminal |
| 30 | +;; window. |
| 31 | + |
| 32 | +;; This library allows the user to set Emacs' `exec-path' and $PATH |
| 33 | +;; from the shell path, so that `shell-command', `compile' and the |
| 34 | +;; like work as expected. |
| 35 | + |
| 36 | +;; Installation: |
| 37 | + |
| 38 | +;; ELPA packages are available on Marmalade and Melpa. Alternatively, place |
| 39 | +;; this file on a directory in your `load-path', and explicitly require it. |
| 40 | + |
| 41 | +;; Usage: |
| 42 | +;; |
| 43 | +;; (require 'exec-path-from-shell) ;; if not using the ELPA package |
| 44 | +;; (exec-path-from-shell-initialize) |
| 45 | +;; |
| 46 | +;; If you use your Emacs config on other platforms, you can instead |
| 47 | +;; make initialization conditional as follows: |
| 48 | +;; |
| 49 | +;; (when (and window-system (eq system-type 'darwin)) |
| 50 | +;; (exec-path-from-shell-initialize)) |
| 51 | +;; |
| 52 | +;; To copy the values of other environment variables, you can use |
| 53 | +;; `exec-path-from-shell-copy-env', e.g. |
| 54 | +;; |
| 55 | +;; (exec-path-from-shell-copy-env "PYTHONPATH") |
| 56 | + |
| 57 | +;;; Code: |
| 58 | + |
| 59 | +(defun exec-path-from-shell-getenv (name) |
| 60 | + (replace-regexp-in-string |
| 61 | + "[ \t\n]*$" "" |
| 62 | + (shell-command-to-string (format "$SHELL --login -i -c 'echo $%s'" name)))) |
| 63 | + |
| 64 | +(defun exec-path-from-shell-copy-env (name) |
| 65 | + "Set the environment variable with `NAME' to match the value seen in the user's shell." |
| 66 | + (interactive "sCopy value of which environment variable from shell? ") |
| 67 | + (setenv name (exec-path-from-shell-getenv name))) |
| 68 | + |
| 69 | +(defun exec-path-from-shell-initialize () |
| 70 | + "Set the PATH environment variable and `exec-path' to match that seen in the user's shell." |
| 71 | + (interactive) |
| 72 | + (let ((path-from-shell (exec-path-from-shell-getenv "PATH"))) |
| 73 | + (setenv "PATH" path-from-shell) |
| 74 | + (setq exec-path (split-string path-from-shell path-separator)))) |
| 75 | + |
| 76 | + |
| 77 | +(provide 'exec-path-from-shell) |
| 78 | + |
| 79 | +;; Local Variables: |
| 80 | +;; coding: utf-8 |
| 81 | +;; indent-tabs-mode: nil |
| 82 | +;; mangle-whitespace: t |
| 83 | +;; require-final-newline: t;; eval: (checkdoc-minor-mode 1) |
| 84 | +;; End: |
| 85 | + |
| 86 | +;;; exec-path-from-shell.el ends here |
0 commit comments