aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkylo252 <59826753+kylo252@users.noreply.github.com>2022-06-09 15:18:56 +0200
committerGitHub <noreply@github.com>2022-06-09 07:18:56 -0600
commit3da3cfc864e89a2dca6917183915683373c85af8 (patch)
tree8233b38ee94a3aa3c8cede35dabb9ee8c774993a /src
parentc5720c72213810adb75d3277ac645eb6fc8dafa8 (diff)
downloadrneovim-3da3cfc864e89a2dca6917183915683373c85af8.tar.gz
rneovim-3da3cfc864e89a2dca6917183915683373c85af8.tar.bz2
rneovim-3da3cfc864e89a2dca6917183915683373c85af8.zip
feat(autocmds): retrieve lua callback (#18642)
add a new `callback` field to `nvim_get_autocmds`
Diffstat (limited to 'src')
-rw-r--r--src/nvim/api/autocmd.c38
-rw-r--r--src/nvim/autocmd.c28
-rw-r--r--src/nvim/eval/typval.c24
3 files changed, 56 insertions, 34 deletions
diff --git a/src/nvim/api/autocmd.c b/src/nvim/api/autocmd.c
index 19b4119344..3a35e49dc8 100644
--- a/src/nvim/api/autocmd.c
+++ b/src/nvim/api/autocmd.c
@@ -66,7 +66,9 @@ static int64_t next_autocmd_id = 1;
/// - group_name (string): the autocommand group name.
/// - desc (string): the autocommand description.
/// - event (string): the autocommand event.
-/// - command (string): the autocommand command.
+/// - command (string): the autocommand command. Note: this will be empty if a callback is set.
+/// - callback (function|string|nil): Lua function or name of a Vim script function
+/// which is executed when this autocommand is triggered.
/// - once (boolean): whether the autocommand is only run once.
/// - pattern (string): the autocommand pattern.
/// If the autocommand is buffer local |autocmd-buffer-local|:
@@ -280,9 +282,28 @@ Array nvim_get_autocmds(Dict(get_autocmds) *opts, Error *err)
PUT(autocmd_info, "desc", CSTR_TO_OBJ(ac->desc));
}
- PUT(autocmd_info,
- "command",
- STRING_OBJ(cstr_as_string(aucmd_exec_to_string(ac, ac->exec))));
+ if (ac->exec.type == CALLABLE_CB) {
+ PUT(autocmd_info, "command", STRING_OBJ(STRING_INIT));
+
+ Callback *cb = &ac->exec.callable.cb;
+ switch (cb->type) {
+ case kCallbackLua:
+ if (nlua_ref_is_function(cb->data.luaref)) {
+ PUT(autocmd_info, "callback", LUAREF_OBJ(api_new_luaref(cb->data.luaref)));
+ }
+ break;
+ case kCallbackFuncref:
+ case kCallbackPartial:
+ PUT(autocmd_info, "callback", STRING_OBJ(cstr_as_string(callback_to_string(cb))));
+ break;
+ default:
+ abort();
+ }
+ } else {
+ PUT(autocmd_info,
+ "command",
+ STRING_OBJ(cstr_as_string(xstrdup(ac->exec.callable.cmd))));
+ }
PUT(autocmd_info,
"pattern",
@@ -442,7 +463,8 @@ Integer nvim_create_autocmd(uint64_t channel_id, Object event, Dict(create_autoc
// not do that.
Object *callback = &opts->callback;
- if (callback->type == kObjectTypeLuaRef) {
+ switch (callback->type) {
+ case kObjectTypeLuaRef:
if (callback->data.luaref == LUA_NOREF) {
api_set_error(err,
kErrorTypeValidation,
@@ -459,10 +481,12 @@ Integer nvim_create_autocmd(uint64_t channel_id, Object event, Dict(create_autoc
cb.type = kCallbackLua;
cb.data.luaref = api_new_luaref(callback->data.luaref);
- } else if (callback->type == kObjectTypeString) {
+ break;
+ case kObjectTypeString:
cb.type = kCallbackFuncref;
cb.data.funcref = string_to_cstr(callback->data.string);
- } else {
+ break;
+ default:
api_set_error(err,
kErrorTypeException,
"'callback' must be a lua function or name of vim function");
diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c
index 31efce13f6..69f01afcb6 100644
--- a/src/nvim/autocmd.c
+++ b/src/nvim/autocmd.c
@@ -2477,32 +2477,6 @@ bool autocmd_delete_id(int64_t id)
// AucmdExecutable Functions
// ===========================================================================
-/// Generate a string description of a callback
-static char *aucmd_callback_to_string(Callback cb)
-{
- // NOTE: this function probably belongs in a helper
-
- size_t msglen = 100;
- char *msg = (char *)xmallocz(msglen);
-
- switch (cb.type) {
- case kCallbackLua:
- snprintf(msg, msglen, "<lua: %d>", cb.data.luaref);
- break;
- case kCallbackFuncref:
- // TODO(tjdevries): Is this enough space for this?
- snprintf(msg, msglen, "<vim function: %s>", cb.data.funcref);
- break;
- case kCallbackPartial:
- snprintf(msg, msglen, "<vim partial: %s>", cb.data.partial->pt_name);
- break;
- default:
- snprintf(msg, msglen, "%s", "");
- break;
- }
- return msg;
-}
-
/// Generate a string description for the command/callback of an autocmd
char *aucmd_exec_to_string(AutoCmd *ac, AucmdExecutable acc)
FUNC_ATTR_PURE
@@ -2511,7 +2485,7 @@ char *aucmd_exec_to_string(AutoCmd *ac, AucmdExecutable acc)
case CALLABLE_EX:
return xstrdup(acc.callable.cmd);
case CALLABLE_CB:
- return aucmd_callback_to_string(acc.callable.cb);
+ return callback_to_string(&acc.callable.cb);
case CALLABLE_NONE:
return "This is not possible";
}
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index 2c76741891..97726da5f4 100644
--- a/src/nvim/eval/typval.c
+++ b/src/nvim/eval/typval.c
@@ -1205,6 +1205,30 @@ void callback_copy(Callback *dest, Callback *src)
}
}
+/// Generate a string description of a callback
+char *callback_to_string(Callback *cb)
+{
+ size_t msglen = 100;
+ char *msg = (char *)xmallocz(msglen);
+
+ switch (cb->type) {
+ case kCallbackLua:
+ snprintf(msg, msglen, "<lua: %d>", cb->data.luaref);
+ break;
+ case kCallbackFuncref:
+ // TODO(tjdevries): Is this enough space for this?
+ snprintf(msg, msglen, "<vim function: %s>", cb->data.funcref);
+ break;
+ case kCallbackPartial:
+ snprintf(msg, msglen, "<vim partial: %s>", cb->data.partial->pt_name);
+ break;
+ default:
+ snprintf(msg, msglen, "%s", "");
+ break;
+ }
+ return msg;
+}
+
/// Remove watcher from a dictionary
///
/// @param dict Dictionary to remove watcher from.