forked from nix-community/nixvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocmd.nix
167 lines (150 loc) · 5.07 KB
/
autocmd.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{
config,
lib,
...
}:
with lib; let
helpers = import ../lib/helpers.nix {inherit lib;};
autoGroupOption = types.submodule {
options = {
clear = mkOption {
type = types.bool;
description = "Clear existing commands if the group already exists.";
default = true;
};
};
};
autoCmdOption = types.submodule {
options = {
event = helpers.mkNullOrOption (types.either types.str (types.listOf types.str)) ''
The event or events to register this autocommand.
'';
group = helpers.mkNullOrOption (types.either types.str types.int) ''
The autocommand group name or id to match against.
'';
pattern = helpers.mkNullOrOption (types.either types.str (types.listOf types.str)) ''
Pattern or patterns to match literally against.
'';
buffer = helpers.mkNullOrOption types.int ''
Buffer number for buffer local autocommands |autocmd-buflocal|.
Cannot be used with `pattern`.
'';
# Introduced early October 2023.
# TODO remove in early December 2023.
description = helpers.mkNullOrOption types.str ''
DEPRECATED, please use `desc`.
'';
desc = helpers.mkNullOrOption types.str ''
A textual description of this autocommand.
'';
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" ];
callback = { __raw = "function() print('This buffer enters') end"; };
}
'';
command = helpers.defaultNullOpts.mkStr "" ''
Vim command to execute on event. Cannot be used with `callback`.
'';
once = helpers.defaultNullOpts.mkBool false "Run the autocommand only once.";
nested = helpers.defaultNullOpts.mkBool false "Run nested autocommands.";
};
};
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'";
}
];
'';
};
};
config = let
inherit (config) autoGroups autoCmd;
in
mkIf (autoGroups != {} || autoCmd != {}) {
# 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.
'';
}
];
extraConfigLuaPost =
(optionalString (autoGroups != {}) ''
-- Set up autogroups {{
do
local __nixvim_autogroups = ${helpers.toLuaObject autoGroups}
for group_name, options in pairs(__nixvim_autogroups) do
vim.api.nvim_create_augroup(group_name, options)
end
end
-- }}
'')
+ (optionalString (autoCmd != []) ''
-- Set up autocommands {{
do
local __nixvim_autocommands = ${helpers.toLuaObject 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
-- }}
'');
};
}