Skip to content

Instantly share code, notes, and snippets.

@intinig
Created April 4, 2012 09:50
Show Gist options
  • Select an option

  • Save intinig/2300044 to your computer and use it in GitHub Desktop.

Select an option

Save intinig/2300044 to your computer and use it in GitHub Desktop.
;; Apply shell environment to emacs
(require 'cl)
(defun env-line-to-cons (env-line)
"Convert a string of the form \"VAR=VAL\" to a cons cell containing (\"VAR\" . \"VAL\")."
(if (string-match "\\([^=]+\\)=\\(.*\\)" env-line)
(cons (match-string 1 env-line) (match-string 2 env-line))))
(defun interactive-env-alist (&optional shell-cmd env-cmd)
"Launch /usr/bin/env or the equivalent from a login shell, parsing and returning the
environment as an alist."
(let ((cmd (concat (or shell-cmd "$SHELL -lc")
" "
(or env-cmd "/usr/bin/env"))))
(mapcar 'env-line-to-cons
(remove-if
(lambda (str)
(string-equal str ""))
(split-string (shell-command-to-string cmd) "[\r\n]")))))
(defun setenv-from-cons (var-val)
"Set an environment variable from a cons cell containing
two strings, where the car is the variable name and cdr is
the value, e.g. (\"VAR\" . \"VAL\")"
(setenv (car var-val) (cdr var-val)))
(defun setenv-from-shell-environment (&optional shell-cmd env-cmd)
"Apply the environment reported by `/usr/bin/env' (or env-cmd)
as launched by `$SHELL -lc' (or shell-cmd) to the current
environment."
(mapc 'setenv-from-cons (interactive-env-alist shell-cmd env-cmd)))
(setenv-from-shell-environment)
(setq exec-path (split-string (getenv "PATH") path-separator))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment