nix-community.nixvim/modules/autocmd.nix

120 lines
3.3 KiB
Nix
Raw Normal View History

2023-01-17 00:40:29 +01:00
{ config, lib, ... }:
with lib;
let
helpers = import ../lib/helpers.nix { inherit lib; };
2023-01-17 00:40:29 +01:00
autoGroupOption = types.submodule {
2023-01-17 00:40:29 +01:00
options = {
clear = mkOption {
type = types.bool;
description = "Clear existing commands if the group already exists.";
default = true;
2023-01-17 00:40:29 +01:00
};
};
};
2023-01-17 00:40:29 +01:00
autoCmdOption = types.submodule {
options = {
event = helpers.mkNullOrOption (types.either types.str (types.listOf types.str)) ''
The event or events to register this autocommand.
'';
2023-01-17 00:40:29 +01:00
group = helpers.mkNullOrOption (types.either types.str types.int) ''
The autocommand group name or id to match against.
'';
2023-01-17 00:40:29 +01:00
pattern = helpers.mkNullOrOption (types.either types.str (types.listOf types.str)) ''
Pattern or patterns to match literally against.
'';
2023-01-17 00:40:29 +01:00
2023-02-02 01:34:48 +00:00
buffer = helpers.defaultNullOpts.mkInt "" ''
Buffer number for buffer local autocommands |autocmd-buflocal|.
Cannot be used with <pattern>.
'';
2023-01-17 00:40:29 +01:00
description = helpers.defaultNullOpts.mkStr "" "A textual description of this autocommand.";
2023-01-17 00:40:29 +01:00
2023-02-02 01:34:48 +00:00
callback = helpers.defaultNullOpts.mkStr "" ''
The name of a Vimscript function to call when this autocommand is triggered. Cannot be used with <command>.
'';
2023-01-17 00:40:29 +01:00
2023-02-02 01:34:48 +00:00
command = helpers.defaultNullOpts.mkStr "" ''
Vim command to execute on event. Cannot be used with <callback>
'';
2023-01-17 00:40:29 +01:00
once = helpers.defaultNullOpts.mkBool false "Run the autocommand only once";
nested = helpers.defaultNullOpts.mkBool false "Run nested autocommands.";
2023-01-17 00:40:29 +01:00
};
};
2023-01-17 00:40:29 +01:00
in
{
options = {
autoGroups = mkOption {
type = types.attrsOf autoGroupOption;
default = { };
description = "augroup definitions";
example = ''
autoGroups = {
my_augroup = {
clear = true;
}
};
'';
};
autoCmd = mkOption {
type = types.listOf autoCmdOption;
default = [ ];
description = "autocmd definitions";
example = ''
autoCmd = [
{
event = [ "BufEnter" "BufWinEnter" ];
pattern = [ "*.c" "*.h" ];
command = "echo 'Entering a C or C++ file'";
}
];
'';
};
2023-01-17 00:40:29 +01:00
};
config = mkIf (config.autoGroups != { } || config.autoCmd != { }) {
extraConfigLuaPost = (optionalString (config.autoGroups != { }) ''
-- Set up autogroups {{
do
local __nixvim_autogroups = ${helpers.toLuaObject config.autoGroups}
for group_name, options in pairs(__nixvim_autogroups) do
vim.api.nvim_create_augroup(group_name, options)
end
end
-- }}
'') +
(optionalString (config.autoCmd != [ ]) ''
-- Set up autocommands {{
2023-01-17 00:40:29 +01:00
do
local __nixvim_autocommands = ${helpers.toLuaObject config.autoCmd}
for _, autocmd in ipairs(__nixvim_autocommands) do
vim.api.nvim_create_autocmd(
autocmd.event,
{
group = autocmd.group,
pattern = autocmd.pattern,
buffer = autocmd.buffer,
desc = autocmd.desc,
callback = autocmd.callback,
command = autocmd.command,
once = autocmd.once,
nested = autocmd.nested
}
)
end
end
-- }}
'');
2023-01-17 00:40:29 +01:00
};
}