+129
-15
lines changedFilter options
+129
-15
lines changed Original file line number Diff line number Diff line change
@@ -3240,7 +3240,7 @@ nvim_create_autocmd({event}, {*opts}) *nvim_create_autocmd()*
3240
3240
<
3241
3241
3242
3242
Parameters: ~
3243
-
{event} (String|Array) The event or events to register
3243
+
{event} (string|array) The event or events to register
3244
3244
this autocommand
3245
3245
{opts} Dictionary of autocommand options:
3246
3246
• group (string|integer) optional: the
@@ -3252,9 +3252,26 @@ nvim_create_autocmd({event}, {*opts}) *nvim_create_autocmd()*
3252
3252
Cannot be used with {pattern}.
3253
3253
• desc (string) optional: description of the
3254
3254
autocommand.
3255
-
• callback (function|string) optional: Lua
3256
-
function or Vim function (as string) to execute
3257
-
on event. Cannot be used with {command}
3255
+
• callback (function|string) optional: if a
3256
+
string, the name of a Vimscript function to
3257
+
call when this autocommand is triggered.
3258
+
Otherwise, a Lua function which is called when
3259
+
this autocommand is triggered. Cannot be used
3260
+
with {command}. Lua callbacks can return true
3261
+
to delete the autocommand; in addition, they
3262
+
accept a single table argument with the
3263
+
following keys:
3264
+
• id: (number) the autocommand id
3265
+
• event: (string) the name of the event that
3266
+
triggered the autocommand |autocmd-events|
3267
+
• group: (number|nil) the autocommand group id,
3268
+
if it exists
3269
+
• match: (string) the expanded value of
3270
+
|<amatch>|
3271
+
• buf: (number) the expanded value of |<abuf>|
3272
+
• file: (string) the expanded value of
3273
+
|<afile>|
3274
+
3258
3275
• command (string) optional: Vim command to
3259
3276
execute on event. Cannot be used with
3260
3277
{callback}
Original file line number Diff line number Diff line change
@@ -366,7 +366,7 @@ Array nvim_get_autocmds(Dict(get_autocmds) *opts, Error *err)
366
366
/// {"CursorHold", "BufPreWrite", "BufPostWrite"}
367
367
/// </pre>
368
368
///
369
-
/// @param event (String|Array) The event or events to register this autocommand
369
+
/// @param event (string|array) The event or events to register this autocommand
370
370
/// @param opts Dictionary of autocommand options:
371
371
/// - group (string|integer) optional: the autocommand group name or
372
372
/// id to match against.
@@ -375,8 +375,18 @@ Array nvim_get_autocmds(Dict(get_autocmds) *opts, Error *err)
375
375
/// - buffer (integer) optional: buffer number for buffer local autocommands
376
376
/// |autocmd-buflocal|. Cannot be used with {pattern}.
377
377
/// - desc (string) optional: description of the autocommand.
378
-
/// - callback (function|string) optional: Lua function or Vim function (as string) to
379
-
/// execute on event. Cannot be used with {command}
378
+
/// - callback (function|string) optional: if a string, the name of a Vimscript function
379
+
/// to call when this autocommand is triggered. Otherwise, a Lua function which is
380
+
/// called when this autocommand is triggered. Cannot be used with {command}. Lua
381
+
/// callbacks can return true to delete the autocommand; in addition, they accept a
382
+
/// single table argument with the following keys:
383
+
/// - id: (number) the autocommand id
384
+
/// - event: (string) the name of the event that triggered the autocommand
385
+
/// |autocmd-events|
386
+
/// - group: (number|nil) the autocommand group id, if it exists
387
+
/// - match: (string) the expanded value of |<amatch>|
388
+
/// - buf: (number) the expanded value of |<abuf>|
389
+
/// - file: (string) the expanded value of |<afile>|
380
390
/// - command (string) optional: Vim command to execute on event. Cannot be used with
381
391
/// {callback}
382
392
/// - once (boolean) optional: defaults to false. Run the autocommand
Original file line number Diff line number Diff line change
@@ -2005,6 +2005,50 @@ void auto_next_pat(AutoPatCmd *apc, int stop_at_last)
2005
2005
}
2006
2006
}
2007
2007
2008
+
static bool call_autocmd_callback(const AutoCmd *ac, const AutoPatCmd *apc)
2009
+
{
2010
+
bool ret = false;
2011
+
Callback callback = ac->exec.callable.cb;
2012
+
if (callback.type == kCallbackLua) {
2013
+
Dictionary data = ARRAY_DICT_INIT;
2014
+
PUT(data, "id", INTEGER_OBJ(ac->id));
2015
+
PUT(data, "event", CSTR_TO_OBJ(event_nr2name(apc->event)));
2016
+
PUT(data, "match", CSTR_TO_OBJ((char *)autocmd_match));
2017
+
PUT(data, "file", CSTR_TO_OBJ((char *)autocmd_fname));
2018
+
PUT(data, "buf", INTEGER_OBJ(autocmd_bufnr));
2019
+
2020
+
int group = apc->curpat->group;
2021
+
switch (group) {
2022
+
case AUGROUP_ERROR:
2023
+
abort(); // unreachable
2024
+
case AUGROUP_DEFAULT:
2025
+
case AUGROUP_ALL:
2026
+
case AUGROUP_DELETED:
2027
+
// omit group in these cases
2028
+
break;
2029
+
default:
2030
+
PUT(data, "group", INTEGER_OBJ(group));
2031
+
break;
2032
+
}
2033
+
2034
+
FIXED_TEMP_ARRAY(args, 1);
2035
+
args.items[0] = DICTIONARY_OBJ(data);
2036
+
2037
+
Object result = nlua_call_ref(callback.data.luaref, NULL, args, true, NULL);
2038
+
if (result.type == kObjectTypeBoolean) {
2039
+
ret = result.data.boolean;
2040
+
}
2041
+
api_free_dictionary(data);
2042
+
api_free_object(result);
2043
+
} else {
2044
+
typval_T argsin = TV_INITIAL_VALUE;
2045
+
typval_T rettv = TV_INITIAL_VALUE;
2046
+
callback_call(&callback, 0, &argsin, &rettv);
2047
+
}
2048
+
2049
+
return ret;
2050
+
}
2051
+
2008
2052
/// Get next autocommand command.
2009
2053
/// Called by do_cmdline() to get the next line for ":if".
2010
2054
/// @return allocated string, or NULL for end of autocommands.
@@ -2069,16 +2113,11 @@ char_u *getnextac(int c, void *cookie, int indent, bool do_concat)
2069
2113
current_sctx = ac->script_ctx;
2070
2114
2071
2115
if (ac->exec.type == CALLABLE_CB) {
2072
-
typval_T argsin = TV_INITIAL_VALUE;
2073
-
typval_T rettv = TV_INITIAL_VALUE;
2074
-
if (callback_call(&ac->exec.callable.cb, 0, &argsin, &rettv)) {
2075
-
if (ac->exec.callable.cb.type == kCallbackLua) {
2076
-
// If a Lua callback returns 'true' then the autocommand is removed
2077
-
oneshot = true;
2078
-
}
2116
+
if (call_autocmd_callback(ac, acp)) {
2117
+
// If an autocommand callback returns true, delete the autocommand
2118
+
oneshot = true;
2079
2119
}
2080
2120
2081
-
2082
2121
// TODO(tjdevries):
2083
2122
//
2084
2123
// Major Hack Alert:
Original file line number Diff line number Diff line change
@@ -182,6 +182,54 @@ describe('autocmd api', function()
182
182
meths.exec_autocmds("User", {pattern = "Test"})
183
183
eq({}, meths.get_autocmds({event = "User", pattern = "Test"}))
184
184
end)
185
+
186
+
it('receives an args table', function()
187
+
local res = exec_lua [[
188
+
local group_id = vim.api.nvim_create_augroup("TestGroup", {})
189
+
local autocmd_id = vim.api.nvim_create_autocmd("User", {
190
+
group = "TestGroup",
191
+
pattern = "Te*",
192
+
callback = function(args)
193
+
vim.g.autocmd_args = args
194
+
end,
195
+
})
196
+
197
+
return {group_id, autocmd_id}
198
+
]]
199
+
200
+
meths.exec_autocmds("User", {pattern = "Test pattern"})
201
+
eq({
202
+
id = res[2],
203
+
group = res[1],
204
+
event = "User",
205
+
match = "Test pattern",
206
+
file = "Test pattern",
207
+
buf = 1,
208
+
}, meths.get_var("autocmd_args"))
209
+
210
+
-- Test without a group
211
+
res = exec_lua [[
212
+
local autocmd_id = vim.api.nvim_create_autocmd("User", {
213
+
pattern = "*",
214
+
callback = function(args)
215
+
vim.g.autocmd_args = args
216
+
end,
217
+
})
218
+
219
+
return {autocmd_id}
220
+
]]
221
+
222
+
meths.exec_autocmds("User", {pattern = "some_pat"})
223
+
eq({
224
+
id = res[1],
225
+
group = nil,
226
+
event = "User",
227
+
match = "some_pat",
228
+
file = "some_pat",
229
+
buf = 1,
230
+
}, meths.get_var("autocmd_args"))
231
+
232
+
end)
185
233
end)
186
234
187
235
describe('nvim_get_autocmds', function()
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4