nixeovim/modules/buffer.nix

58 lines
1.5 KiB
Nix

{ config, lib, ... }:
let
cfg = config.buffer;
options = import ./vim/options.nix { inherit lib; };
bufferOpts = { name, config, ... }: {
options = with lib; {
pattern = mkOption {
type = types.extCommas;
description = "File types for which the buffer is enabled";
};
opt = options.buffer // options.window;
};
config = {
pattern = lib.mkIf (!lib.hasPrefix "[definition " name) (lib.mkDefault name);
};
};
mkBuffer = { prefix, event }: name: cfg:
lib.nameValuePair
"buffer-${prefix}-${name}"
{
inherit event;
inherit (cfg) pattern;
callback = with lib; with nix2lua; lambda [ "ev" ] (lib.flatten [
(flip mapAttrsToList cfg.opt (k: v: if v == null then null else set "vim.opt.${k}" v))
]);
};
mkFiletypeBuffer = mkBuffer { prefix = "filetype"; event = "FileType"; };
mkReadBuffer = cfg:
mkBuffer
{ prefix = "read"; event = [ "BufNewFile" "BufRead" ]; }
(builtins.hashString "sha1" cfg.patterns)
cfg;
in
{
options.buffer = {
read = lib.mkOption {
type = with lib.types; listOf (submodule bufferOpts);
default = [ ];
};
filetype = lib.mkOption {
type = with lib.types; attrsOf (submodule bufferOpts);
default = { };
};
};
config = {
vim.augroup = lib.mkMerge [
(lib.mkIf (cfg.filetype != { }) (lib.listToAttrs (lib.mapAttrsToList mkFiletypeBuffer cfg.filetype)))
(lib.mkIf (cfg.read != [ ]) (lib.listToAttrs (lib.map mkReadBuffer cfg.read)))
];
};
}