nix-buffer support improvements.

Use inherit-local, add per-package elisp hooks.
This commit is contained in:
Shea Levy
2016-10-07 10:31:37 -04:00
parent daf4e57577
commit eca0f17ad2
4 changed files with 69 additions and 16 deletions
+36 -12
View File
@@ -1,23 +1,47 @@
# Functions to build elisp files to locally configure emcas buffers.
# See https://github.com/shlevy/nix-buffer
{ lib, writeText }:
{ lib, writeText, inherit-local }:
{
withPackages = pkgs: let
coqs = builtins.filter (x: (builtins.parseDrvName x.name).name == "coq") pkgs;
coq = builtins.head coqs;
pg-setup = if builtins.length coqs == 0 then "" else ''
(setq-local coq-prog-name "${coq}/bin/coqtop")
(setq-local coq-dependency-analyzer "${coq}/bin/coqdep")
(setq-local coq-compiler "${coq}/bin/coqc")
(setq-local coq-library-directory (get-coq-library-directory))
(coq-prog-args)
'';
extras = map (x: x.emacsBufferSetup pkgs) (builtins.filter (builtins.hasAttr "emacsBufferSetup") pkgs);
in writeText "dir-locals.el" ''
(require 'inherit-local "${inherit-local}/share/emacs/site-lisp/elpa/inherit-local-${inherit-local.version}/inherit-local.elc")
; Only set up nixpkgs buffer handling when we have some buffers active
(defvar nixpkgs--buffer-count 0)
(when (eq nixpkgs--buffer-count 0)
; When generating a new temporary buffer (one whose name starts with a space), do inherit-local inheritance and make it a nixpkgs buffer
(defun nixpkgs--around-generate (orig name)
(if (eq (aref name 0) ?\s)
(let ((buf (funcall orig name)))
(when (inherit-local-inherit-child buf)
(with-current-buffer buf
(make-local-variable 'kill-buffer-hook)
(setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
(add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count)))
buf)
(funcall orig name)))
(advice-add 'generate-new-buffer :around #'nixpkgs--around-generate)
; When we have no more nixpkgs buffers, tear down the buffer handling
(defun nixpkgs--decrement-buffer-count ()
(setq nixpkgs--buffer-count (1- nixpkgs--buffer-count))
(when (eq nixpkgs--buffer-count 0)
(advice-remove 'generate-new-buffer #'nixpkgs--around-generate)
(fmakunbound 'nixpkgs--around-generate)
(fmakunbound 'nixpkgs--decrement-buffer-count))))
(setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
(make-local-variable 'kill-buffer-hook)
(add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count)
; Add packages to PATH and exec-path
(make-local-variable 'process-environment)
(put 'process-environment 'permanent-local t)
(inherit-local 'process-environment)
(setenv "PATH" (concat "${lib.makeSearchPath "bin" pkgs}:" (getenv "PATH")))
(setq-local exec-path (append '(${builtins.concatStringsSep " " (map (p: "\"${p}/bin\"") pkgs)}) exec-path))
${pg-setup}
(inherit-local-permanent exec-path (append '(${builtins.concatStringsSep " " (map (p: "\"${p}/bin\"") pkgs)}) exec-path))
${lib.concatStringsSep "\n" extras}
'';
}