From b8d700607ea7a6d27cc9c7ab9d4f9901bd40997c Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Wed, 27 Jan 2021 15:29:36 +0800 Subject: Update Lua Plugin API --- lua/lua-plugin.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'lua/lua-plugin.c') diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c index 2f14156..0fa4c96 100644 --- a/lua/lua-plugin.c +++ b/lua/lua-plugin.c @@ -411,29 +411,58 @@ static const lua_command_candidate_t * ibus_engine_plugin_get_candidate(lua_Stat } /** - * retrieve the first string value. (value has been copied.) + * retrieve the number of string values. */ -gchar * ibus_engine_plugin_get_first_result(IBusEnginePlugin * plugin){ +gint ibus_engine_plugin_get_n_result(IBusEnginePlugin * plugin){ + IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); + int type; + lua_State * L = priv->L; + + type = lua_type(L ,-1); + if ( LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type) + return 1; + else if( LUA_TTABLE == type ) + return lua_objlen (L, -1); + + return 0; +} + +/** + * retrieve the nth string value. (value has been copied.) + */ +gchar * ibus_engine_plugin_get_nth_result(IBusEnginePlugin * plugin, gint index){ IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); const char * result = NULL; int type; lua_State * L = priv->L; type = lua_type(L ,-1); if ( LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type) { + /* check index value */ + g_assert(0 == index); result = g_strdup(lua_tostring(L, -1)); - lua_pop(L, 1); } else if( LUA_TTABLE == type ){ - lua_pushinteger(L, 1); + /* check index value */ + g_assert(index < lua_objlen (L, -1)); + lua_pushinteger(L, (guint) index + 1); lua_gettable(L, -2); int type = lua_type(L, -1); if ( LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type ) result = g_strdup(lua_tostring(L, -1)); - lua_pop(L, 2); + lua_pop(L, 1); } return (char *)result; } +/** + * clear the string values from the stack. + */ +void ibus_engine_plugin_clear_results(IBusEnginePlugin * plugin){ + IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); + lua_State * L = priv->L; + lua_pop(L, 1); +} + /** * retrieve the retval string value. (value has been copied.) */ -- cgit