2023-02-20 11:42:13 +01:00
|
|
|
{
|
|
|
|
lib,
|
2023-11-06 15:04:08 +01:00
|
|
|
helpers,
|
|
|
|
config,
|
2023-02-20 11:42:13 +01:00
|
|
|
...
|
|
|
|
}:
|
|
|
|
with lib; let
|
2023-02-01 22:25:56 +01:00
|
|
|
autoGroupOption = types.submodule {
|
2023-01-17 00:40:29 +01:00
|
|
|
options = {
|
2023-02-01 22:25:56 +01:00
|
|
|
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-02-01 22:25:56 +01:00
|
|
|
};
|
|
|
|
};
|
2023-01-17 00:40:29 +01:00
|
|
|
|
2023-02-01 22:25:56 +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
|
|
|
|
2023-02-01 22:25:56 +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
|
|
|
|
2023-02-01 22:25:56 +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-20 11:53:18 +01:00
|
|
|
buffer = helpers.mkNullOrOption types.int ''
|
2023-02-01 22:25:56 +01:00
|
|
|
Buffer number for buffer local autocommands |autocmd-buflocal|.
|
2023-02-20 11:53:18 +01:00
|
|
|
Cannot be used with `pattern`.
|
2023-02-01 22:25:56 +01:00
|
|
|
'';
|
2023-01-17 00:40:29 +01:00
|
|
|
|
2023-09-30 22:21:22 +02:00
|
|
|
# Introduced early October 2023.
|
|
|
|
# TODO remove in early December 2023.
|
|
|
|
description = helpers.mkNullOrOption types.str ''
|
|
|
|
DEPRECATED, please use `desc`.
|
|
|
|
'';
|
|
|
|
|
2023-09-29 09:07:24 +02:00
|
|
|
desc = helpers.mkNullOrOption types.str ''
|
|
|
|
A textual description of this autocommand.
|
|
|
|
'';
|
2023-01-17 00:40:29 +01:00
|
|
|
|
2023-02-20 11:53:18 +01:00
|
|
|
callback = helpers.mkNullOrOption (types.either types.str helpers.rawType) ''
|
|
|
|
A function or a string.
|
|
|
|
- if a string, the name of a Vimscript function to call when this autocommand is triggered.
|
|
|
|
- Otherwise, a Lua function which is called when this autocommand is triggered.
|
|
|
|
Cannot be used with `command`.
|
|
|
|
Lua callbacks can return true to delete the autocommand; in addition, they accept a single
|
|
|
|
table argument with the following keys:
|
|
|
|
- id: (number) the autocommand id
|
|
|
|
- event: (string) the name of the event that triggered the autocommand |autocmd-events|
|
|
|
|
- group: (number|nil) the autocommand group id, if it exists
|
|
|
|
- match: (string) the expanded value of |<amatch>|
|
|
|
|
- buf: (number) the expanded value of |<abuf>|
|
|
|
|
- file: (string) the expanded value of |<afile>|
|
|
|
|
- data: (any) arbitrary data passed to |nvim_exec_autocmds()|
|
|
|
|
|
|
|
|
Example using callback:
|
|
|
|
autoCmd = [
|
|
|
|
{
|
|
|
|
event = [ "BufEnter" "BufWinEnter" ];
|
|
|
|
pattern = [ "*.c" "*.h" ];
|
2023-03-22 07:42:02 +01:00
|
|
|
callback = { __raw = "function() print('This buffer enters') end"; };
|
2023-02-20 11:53:18 +01:00
|
|
|
}
|
2023-02-01 22:25:56 +01:00
|
|
|
'';
|
2023-01-17 00:40:29 +01:00
|
|
|
|
2023-02-02 01:34:48 +00:00
|
|
|
command = helpers.defaultNullOpts.mkStr "" ''
|
2023-02-20 11:53:18 +01:00
|
|
|
Vim command to execute on event. Cannot be used with `callback`.
|
2023-02-01 22:25:56 +01:00
|
|
|
'';
|
2023-01-17 00:40:29 +01:00
|
|
|
|
2023-02-20 11:53:18 +01:00
|
|
|
once = helpers.defaultNullOpts.mkBool false "Run the autocommand only once.";
|
2023-02-01 22:25:56 +01:00
|
|
|
|
|
|
|
nested = helpers.defaultNullOpts.mkBool false "Run nested autocommands.";
|
2023-01-17 00:40:29 +01:00
|
|
|
};
|
|
|
|
};
|
2023-02-20 11:42:13 +01:00
|
|
|
in {
|
2023-02-01 22:25:56 +01:00
|
|
|
options = {
|
|
|
|
autoGroups = mkOption {
|
|
|
|
type = types.attrsOf autoGroupOption;
|
2023-02-20 11:42:13 +01:00
|
|
|
default = {};
|
2023-02-01 22:25:56 +01:00
|
|
|
description = "augroup definitions";
|
|
|
|
example = ''
|
|
|
|
autoGroups = {
|
|
|
|
my_augroup = {
|
|
|
|
clear = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
autoCmd = mkOption {
|
|
|
|
type = types.listOf autoCmdOption;
|
2023-02-20 11:42:13 +01:00
|
|
|
default = [];
|
2023-02-01 22:25:56 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2023-02-20 11:53:18 +01:00
|
|
|
config = let
|
|
|
|
inherit (config) autoGroups autoCmd;
|
|
|
|
in
|
|
|
|
mkIf (autoGroups != {} || autoCmd != {}) {
|
2023-09-30 22:21:22 +02:00
|
|
|
# Introduced early October 2023.
|
|
|
|
# TODO remove in early December 2023.
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion =
|
|
|
|
all
|
|
|
|
(x: x.description == null)
|
|
|
|
autoCmd;
|
|
|
|
message = ''
|
|
|
|
RENAMED OPTION: `autoCmd[].description` has been renamed `autoCmd[].desc`.
|
|
|
|
Please update your configuration.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2023-02-20 11:53:18 +01:00
|
|
|
extraConfigLuaPost =
|
|
|
|
(optionalString (autoGroups != {}) ''
|
|
|
|
-- Set up autogroups {{
|
|
|
|
do
|
|
|
|
local __nixvim_autogroups = ${helpers.toLuaObject autoGroups}
|
2023-02-01 22:25:56 +01:00
|
|
|
|
2023-02-20 11:53:18 +01:00
|
|
|
for group_name, options in pairs(__nixvim_autogroups) do
|
|
|
|
vim.api.nvim_create_augroup(group_name, options)
|
|
|
|
end
|
2023-02-20 11:42:13 +01:00
|
|
|
end
|
2023-02-20 11:53:18 +01:00
|
|
|
-- }}
|
|
|
|
'')
|
|
|
|
+ (optionalString (autoCmd != []) ''
|
|
|
|
-- Set up autocommands {{
|
|
|
|
do
|
|
|
|
local __nixvim_autocommands = ${helpers.toLuaObject autoCmd}
|
2023-01-17 00:40:29 +01:00
|
|
|
|
2023-02-20 11:53:18 +01:00
|
|
|
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
|
2023-02-20 11:42:13 +01:00
|
|
|
end
|
2023-02-20 11:53:18 +01:00
|
|
|
-- }}
|
|
|
|
'');
|
|
|
|
};
|
2023-01-17 00:40:29 +01:00
|
|
|
}
|