summaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2018-06-08 16:16:05 +0800
committerPeng Wu <alexepico@gmail.com>2018-06-08 16:16:05 +0800
commit7b485605d696bb96a6024d22a259336dcd16fb72 (patch)
treebb7c7d3684c534024de7041ba8f4cdc3a9840865 /lua
parent41b283aeed897d5ea23ce2a60b9470472b2d492c (diff)
update lua-plugin-init.c
Diffstat (limited to 'lua')
-rw-r--r--lua/lua-plugin-init.c62
-rw-r--r--lua/lua-plugin.c24
2 files changed, 70 insertions, 16 deletions
diff --git a/lua/lua-plugin-init.c b/lua/lua-plugin-init.c
index e42cc17..b5821fc 100644
--- a/lua/lua-plugin-init.c
+++ b/lua/lua-plugin-init.c
@@ -241,44 +241,74 @@ static int ime_register_command(lua_State * L){
}
static int ime_register_trigger(lua_State * L){
- const char * lua_function_name = luaL_checklstring(L, 1, NULL);
- const char * description = luaL_checklstring(L, 2, NULL);
- size_t num; size_t i;
- fprintf(stderr, "TODO: ime_register_trigger unimplemented when called with %s (%s).\n", lua_function_name, description);
+ lua_trigger_t new_trigger;
- luaL_checktype(L, 3, LUA_TTABLE);
+ memset(&new_trigger, 0, sizeof(new_trigger));
+ new_trigger.lua_function_name = luaL_checklstring(L, 1, NULL);
+ lua_getglobal(L, new_trigger.lua_function_name);
+ luaL_checktype(L, -1, LUA_TFUNCTION);
+ lua_pop(L, 1);
- /* TODO: register_trigger with input_trigger_strings. */
+ new_trigger.description = luaL_checklstring(L, 2, NULL);
+ size_t num; gint i;
+ GPtrArray *array;
+
+ /* register_trigger with input_trigger_strings. */
+ array = g_ptr_array_new();
+ luaL_checktype(L, 3, LUA_TTABLE);
num = lua_objlen(L, 3);
for ( i = 0; i < num; ++i) {
lua_pushinteger(L, i + 1);
lua_gettable(L, 3);
- fprintf(stderr, "%d:%s\t", (int)i + 1, lua_tostring(L, -1));
+ g_ptr_array_add(array, (gpointer)lua_tostring(L, -1));
lua_pop(L, 1);
}
- fprintf(stderr, "\n");
+ new_trigger.input_trigger_strings =
+ (gchar **)g_ptr_array_free(array, FALSE);
+ /* register_trigger with candidate_trigger_strings. */
+ array = g_ptr_array_new();
luaL_checktype(L, 4, LUA_TTABLE);
-
- /* TODO: register_trigger with candidate_trigger_strings. */
num = lua_objlen(L, 4);
for ( i = 0; i < num; ++i) {
lua_pushinteger(L, i + 1);
lua_gettable(L, 4);
- fprintf(stderr, "%d:%s\t", (int) i + 1, lua_tostring(L, -1));
+ g_ptr_array_add(array, (gpointer)lua_tostring(L, -1));
+ lua_pop(L, 1);
}
- fprintf(stderr, "\n");
+ new_trigger.candidate_trigger_strings =
+ (gchar **)g_ptr_array_free(array, FALSE);
+
+ gboolean result = ibus_engine_plugin_add_trigger
+ (lua_plugin_retrieve_plugin(L), &new_trigger);
+
+ g_free(new_trigger.input_trigger_strings);
+ g_free(new_trigger.candidate_trigger_strings);
+
+ if (!result)
+ return luaL_error(L, "register trigger with function %s failed.\n", new_trigger.lua_function_name);
return 0;
}
static int ime_register_converter(lua_State * L){
- const char * lua_function_name = luaL_checklstring(L, 1, NULL);
- const char * description = luaL_checklstring(L, 2, NULL);
+ lua_converter_t new_converter;
+
+ memset(&new_converter, 0, sizeof(new_converter));
+ new_converter.lua_function_name = luaL_checklstring(L, 1, NULL);
+ lua_getglobal(L, new_converter.lua_function_name);
+ luaL_checktype(L, -1, LUA_TFUNCTION);
+ lua_pop(L, 1);
- fprintf(stderr, "TODO: ime_register_converter unimplemented when called with %s(%s).\n", lua_function_name, description);
+ new_converter.description = luaL_checklstring(L, 2, NULL);
- return 0;
+ gboolean result = ibus_engine_plugin_add_converter
+ (lua_plugin_retrieve_plugin(L), &new_converter);
+
+ if (!result)
+ return luaL_error(L, "register converter with function %s failed.\n", new_converter.lua_function_name);
+
+ return 0;
}
static int ime_split_string(lua_State * L){
diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c
index 6f48be0..2300930 100644
--- a/lua/lua-plugin.c
+++ b/lua/lua-plugin.c
@@ -114,6 +114,8 @@ static int
lua_plugin_fini(IBusEnginePluginPrivate * plugin){
size_t i;
lua_command_t * command;
+ lua_trigger_t * trigger;
+ lua_converter_t * converter;
if ( plugin->lua_commands ){
for ( i = 0; i < plugin->lua_commands->len; ++i){
@@ -124,8 +126,30 @@ lua_plugin_fini(IBusEnginePluginPrivate * plugin){
plugin->lua_commands = NULL;
}
+ if ( plugin->lua_triggers ){
+ for ( i = 0; i < plugin->lua_triggers->len; ++i){
+ trigger = &g_array_index(plugin->lua_triggers, lua_trigger_t, i);
+ lua_trigger_reclaim(trigger);
+ }
+ g_array_free(plugin->lua_triggers, TRUE);
+ plugin->lua_triggers = NULL;
+ }
+
+ if ( plugin->lua_converters ){
+ for ( i = 0; i < plugin->lua_converters->len; ++i){
+ converter = &g_array_index(plugin->lua_converters, lua_converter_t, i);
+ lua_converter_reclaim(converter);
+ }
+ g_array_free(plugin->lua_converters, TRUE);
+ plugin->lua_converters = NULL;
+ }
+
lua_close(plugin->L);
plugin->L = NULL;
+
+ g_free(plugin->use_converter);
+ plugin->use_converter = NULL;
+
return 0;
}