modules/buffer: add read buffer

This commit is contained in:
Dmitriy Pleshevskiy 2024-05-20 10:46:25 +03:00
parent c38340b046
commit 001a91bc8d
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2

View file

@ -1,39 +1,58 @@
{ config, lib, ... }: { config, lib, ... }:
let let
cfg = config.buffer;
options = import ./vim/options.nix { inherit lib; }; options = import ./vim/options.nix { inherit lib; };
bufferOpts = { name, config, ... }: { bufferOpts = { name, config, ... }: {
options = with lib; { options = with lib; {
filetypes = mkOption { pattern = mkOption {
type = types.extCommas; type = types.extCommas;
description = "File types for which the buffer is enabled"; description = "File types for which the buffer is enabled";
}; };
opt = options.buffer // options.window; opt = options.buffer // options.window;
}; };
config = { config = {
filetypes = lib.mkDefault name; pattern = lib.mkIf (!lib.hasPrefix "[definition " name) (lib.mkDefault name);
}; };
}; };
mkFiletypeBuffer = name: cfg: mkBuffer = { prefix, event }: name: cfg:
lib.nameValuePair lib.nameValuePair
"buffer-${name}" "buffer-${prefix}-${name}"
{ {
event = "FileType"; inherit event;
pattern = cfg.filetypes; inherit (cfg) pattern;
callback = with lib; with nix2lua; lambda ["ev"] (lib.flatten [ 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)) (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 in
{ {
options.buffer = lib.mkOption { options.buffer = {
type = with lib.types; attrsOf (submodule bufferOpts); read = lib.mkOption {
default = { }; type = with lib.types; listOf (submodule bufferOpts);
default = [ ];
};
filetype = lib.mkOption {
type = with lib.types; attrsOf (submodule bufferOpts);
default = { };
};
}; };
config = { config = {
vim.augroup = lib.mkIf (config.buffer != {}) (lib.listToAttrs (lib.mapAttrsToList mkFiletypeBuffer config.buffer)); 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)))
];
}; };
} }