Cache PATH and env vars. fetched with exec-path-from-shell

See updated documentation in this commit.
This commit is contained in:
syl20bnr 2018-06-11 03:47:50 -04:00
parent 41b90e403c
commit c12b72090c
4 changed files with 56 additions and 5 deletions

View file

@ -94,6 +94,7 @@
- [[#layout-key-bindings][Layout key bindings]]
- [[#workspaces][Workspaces]]
- [[#workspace-key-bindings][Workspace key bindings]]
- [[#environment-variables-and-path-from-the-shell][Environment variables and PATH from the shell]]
- [[#commands][Commands]]
- [[#vim-key-bindings][Vim key bindings]]
- [[#escaping][Escaping]]
@ -1716,6 +1717,24 @@ There are also some handy globally available key bindings related to workspaces:
| ~gT~ | go to previous workspace |
| ~SPC b W~ | go to workspace and window by buffer |
* Environment variables and PATH from the shell
When using the Emacs GUI environment variables defined by your shell may not
be available to the Emacs process. This is especially true for macOS.
The command to fetch the environment variables from your shell is very expensive
and increases the startup time quite a lot.
For this reason, Spacemacs caches the PATH value and some environment variables
in the file located at =~/.emacs.d/.cache/.env-vars=.
Any environment variables that are not found are still written to the cache file
with the value =<VAR>-NOTFOUND-PLEASE-DEFINE-IN-YOUR-SHELL=.
Spacemacs won't update the cached values by itself, if you want to update your
shell variables then either delete the cache file or execute the interactive
function =spacemacs/import-path= and restart Emacs to fetch any updated
environment variables.
* Commands
** Vim key bindings
Spacemacs is based on =Vim= modal user interface to navigate and edit text. If

View file

@ -81,3 +81,6 @@ if used there.")
("iedit-insert" "firebrick1" (bar . 2)))
"Colors assigned to evil states with cursor definitions.
To add your own, use `spacemacs/add-evil-curosr'.")
(defvar spacemacs-env-vars-file (concat spacemacs-cache-directory ".env-vars")
"Cache file for exec-path variable.")

View file

@ -183,8 +183,38 @@ Example: (evil-map visual \"<\" \"<gv\")"
;; exec-path-from-shell
(defun spacemacs//initialize-exec-path-from-shell (&optional force)
"Initialize exec-path and cache its value.
Load from cache file if cache file exists and FORCE is nil."
(when (display-graphic-p)
(when force (delete-file spacemacs-env-vars-file))
(if (file-exists-p spacemacs-env-vars-file)
(load spacemacs-env-vars-file nil (not init-file-debug))
(require 'exec-path-from-shell)
(exec-path-from-shell-initialize)
(spacemacs/dump-vars-to-file '(exec-path) spacemacs-env-vars-file))))
(defun spacemacs/import-path ()
"Import value of PATH."
(interactive)
(spacemacs//initialize-exec-path-from-shell t))
(defun spacemacs/copy-env-list (vars)
"Copy list of env. VARS using `exec-path-from-shell'."
"Copy list of env. VARS using `exec-path-from-shell'.
Cache the found value in `spacemacs-env-vars-file'."
(dolist (var vars)
(unless (getenv var)
(exec-path-from-shell-copy-env var))))
(unless (or (getenv var)
(null (configuration-layer/package-used-p
'exec-path-from-shell)))
(require 'exec-path-from-shell)
(exec-path-from-shell-copy-env var)
(with-temp-file spacemacs-env-vars-file
(insert-file-contents spacemacs-env-vars-file)
(print (list 'setenv var
(if (getenv var)
(getenv var)
(format "%s-NOTFOUND-PLEASE-DEFINE-IN-YOUR-SHELL"
var)))
(current-buffer))))))

View file

@ -287,8 +287,7 @@
(evil-declare-ignore-repeat 'spacemacs/previous-error))
(defun spacemacs-bootstrap/init-exec-path-from-shell ()
(require 'exec-path-from-shell)
(exec-path-from-shell-initialize))
(spacemacs//initialize-exec-path-from-shell))
(defun spacemacs-bootstrap/init-hydra ()
(require 'hydra)