$ show purcell/envrc
envrc.el is an Emacs Lisp package that provides buffer-local environment variable management using the direnv tool. It implements a global minor mode (`envrc-global-mode`) that enables a local minor mode (`envrc-mode`) in every buffer, which sets buffer-local `process-environment`, `exec-path`, and related variables based on the nearest `.envrc` file. The package caches direnv results per directory, supports asynchronous direnv execution with configurable blocking behavior, and advises several Emacs shell commands (e.g., `shell-command`, `async-shell-command`, `org-babel-eval`) to propagate the local environment into temporary buffers. It also includes TRAMP support for remote files, a major mode for editing `.envrc` files, and a comprehensive test suite. The architecture is a single-file Emacs package with clean separation of concerns: environment detection, direnv process management, result caching, and application to buffer-local state. The code demonstrates advanced Emacs Lisp patterns including minor modes, process filters and sentinels, advice, and buffer-local variable management.
The LLM identified 2 files worth highlighting for reuse:
The `envrc-propagate-environment` function and the associated `advice-add` calls demonstrate a reusable pattern for wrapping Emacs commands to ensure they use the buffer-local environment when starting subprocesses. This pattern is generic and could be extracted to propagate any buffer-local state (not just environment) to commands that create temporary buffers. The function uses the `inheritenv` package to copy the local environment into a temp buffer before executing the original command.
The `envrc--direnv-export` function implements a robust pattern for running an external process asynchronously, collecting its JSON output via a filter, and applying the results to multiple buffers upon completion. It handles process cancellation, error states, and status propagation. The caching mechanism (`envrc--direnv-result`, `envrc--direnv-global-process-environment`) ensures that subsequent buffers in the same directory reuse results without re-running direnv, with automatic invalidation when the global environment changes.