doomemacs/modules/tools/direnv/config.el
Henrik Lissner 06c404ecb1
tools/direnv: replace direnv package with envrc
wbolster/emacs-direnv uses an approach that mutates global state,
allowing a direnv to bleed into unrelated buffers and contexts. For this
to work it must rereads the direnv every time you switch buffers, which
can be very slow.

purcell/envrc, on the other hand, makes env state buffer-local, so the
direnv only needs to be read once, when a is first initialized. This is
faster and less error prone. However, it's necessary to manually reload
the direnv if you've changed your .envrc outside of Emacs (with `M-x
envrc-reload` or `M-x envrc-reload-all`).
2020-08-21 04:45:04 -04:00

43 lines
1.6 KiB
EmacsLisp

;;; tools/direnv/config.el -*- lexical-binding: t; -*-
(defvar +direnv-keywords
'("direnv_layout_dir" "PATH_add" "path_add" "log_status" "log_error" "has"
"join_args" "expand_path" "dotenv" "user_rel_path" "find_up" "source_env"
"watch_file" "source_up" "direnv_load" "MANPATH_add" "load_prefix" "layout"
"use" "rvm" "use_nix" "use_guix")
"A list of direnv keywords, which are fontified when in `+direnv-rc-mode'.")
;;
;;; Packages
(use-package! envrc
:when (executable-find "direnv")
:after-call doom-first-file
:mode ("\\.envrc\\'" . +direnv-rc-mode)
:config
(add-to-list 'doom-debug-variables 'envrc-debug)
;; I'm avoiding `global-envrc-mode' intentionally, because it has the
;; potential to run too late in the mode startup process (and after, say,
;; server hooks that may rely on that local direnv environment).
(add-hook! 'change-major-mode-after-body-hook
(defun +direnv-init-h ()
(unless (or envrc-mode
(minibufferp)
(file-remote-p default-directory))
(envrc-mode 1))))
(define-derived-mode +direnv-rc-mode sh-mode "envrc"
"Major mode for .envrc files."
;; Fontify .envrc keywords; it's a good indication of whether or not we've
;; typed them correctly, and that we're in the correct major mode.
(font-lock-add-keywords
nil `((,(regexp-opt +direnv-keywords 'symbols)
(0 font-lock-keyword-face)))))
(defadvice! +direnv--fail-gracefully-a (&rest _)
"Don't try to use direnv if the executable isn't present."
:before-while #'envrc-mode
(or (executable-find "direnv")
(ignore (doom-log "Couldn't find direnv executable")))))