diff --git a/src/plugins/lua_api.c b/src/plugins/lua_api.c index be64e21c..ff970ae0 100644 --- a/src/plugins/lua_api.c +++ b/src/plugins/lua_api.c @@ -32,6 +32,7 @@ #include "plugins/api.h" #include "plugins/lua_api.h" #include "plugins/callbacks.h" +#include "plugins/autocompleters.h" #include "ui/ui.h" @@ -82,6 +83,37 @@ lua_api_register_timed(lua_State *L) return 0; } +static int +lua_api_register_ac(lua_State *L) +{ + const char *key = lua_tostring(L, -2); + int len = lua_objlen(L, -1); + char *c_items[len]; + + int i; + for (i = 0; i < len; i++) { + lua_pushinteger(L, i+1); + lua_gettable(L, -2); + + // create copy as cannot guarantee the + // item does not get gc'd after pop + const char *val = lua_tostring(L, -1); + c_items[i] = strdup(val); + lua_pop(L, 1); + } + c_items[len] = NULL; + + // makes own copy of items + autocompleters_add(key, c_items); + + // free all items + for (i = 0; i < len; i++) { + free(c_items[i]); + } + + return 0; +} + static int lua_api_notify(lua_State *L) { @@ -309,6 +341,8 @@ lua_api_init(lua_State *L) lua_setglobal(L, "prof_register_command"); lua_pushcfunction(L, lua_api_register_timed); lua_setglobal(L, "prof_register_timed"); + lua_pushcfunction(L, lua_api_register_ac); + lua_setglobal(L, "prof_register_ac"); lua_pushcfunction(L, lua_api_send_line); lua_setglobal(L, "prof_send_line"); lua_pushcfunction(L, lua_api_notify);