@@ -1,23 +1,47 @@
11# Functions to build elisp files to locally configure emcas buffers.
22# See https://github.com/shlevy/nix-buffer
33
4−{ lib, writeText }:
4+{ lib, writeText, inherit-local }:
55
66{
77 withPackages = pkgs: let
8− coqs = builtins.filter (x: (builtins.parseDrvName x.name).name == "coq") pkgs;
9− coq = builtins.head coqs;
10− pg-setup = if builtins.length coqs == 0 then "" else ''
11− (setq-local coq-prog-name "${coq}/bin/coqtop")
12− (setq-local coq-dependency-analyzer "${coq}/bin/coqdep")
13− (setq-local coq-compiler "${coq}/bin/coqc")
14− (setq-local coq-library-directory (get-coq-library-directory))
15− (coq-prog-args)
16− '';
8+ extras = map (x: x.emacsBufferSetup pkgs) (builtins.filter (builtins.hasAttr "emacsBufferSetup") pkgs);
179 in writeText "dir-locals.el" ''
10+ (require 'inherit-local "${inherit-local}/share/emacs/site-lisp/elpa/inherit-local-${inherit-local.version}/inherit-local.elc")
11+
12+ ; Only set up nixpkgs buffer handling when we have some buffers active
13+ (defvar nixpkgs--buffer-count 0)
14+ (when (eq nixpkgs--buffer-count 0)
15+ ; When generating a new temporary buffer (one whose name starts with a space), do inherit-local inheritance and make it a nixpkgs buffer
16+ (defun nixpkgs--around-generate (orig name)
17+ (if (eq (aref name 0) ?\s)
18+ (let ((buf (funcall orig name)))
19+ (when (inherit-local-inherit-child buf)
20+ (with-current-buffer buf
21+ (make-local-variable 'kill-buffer-hook)
22+ (setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
23+ (add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count)))
24+ buf)
25+ (funcall orig name)))
26+ (advice-add 'generate-new-buffer :around #'nixpkgs--around-generate)
27+ ; When we have no more nixpkgs buffers, tear down the buffer handling
28+ (defun nixpkgs--decrement-buffer-count ()
29+ (setq nixpkgs--buffer-count (1- nixpkgs--buffer-count))
30+ (when (eq nixpkgs--buffer-count 0)
31+ (advice-remove 'generate-new-buffer #'nixpkgs--around-generate)
32+ (fmakunbound 'nixpkgs--around-generate)
33+ (fmakunbound 'nixpkgs--decrement-buffer-count))))
34+ (setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
35+ (make-local-variable 'kill-buffer-hook)
36+ (add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count)
37+
38+ ; Add packages to PATH and exec-path
1839 (make-local-variable 'process-environment)
40+ (put 'process-environment 'permanent-local t)
41+ (inherit-local 'process-environment)
1942 (setenv "PATH" (concat "${lib.makeSearchPath "bin" pkgs}:" (getenv "PATH")))
20− (setq-local exec-path (append '(${builtins.concatStringsSep " " (map (p: "\"${p}/bin\"") pkgs)}) exec-path))
21− ${pg-setup}
43+ (inherit-local-permanent exec-path (append '(${builtins.concatStringsSep " " (map (p: "\"${p}/bin\"") pkgs)}) exec-path))
44+
45+ ${lib.concatStringsSep "\n" extras}
2246 '';
2347}