diff options
author | Peng Wu <alexepico@gmail.com> | 2010-04-16 16:36:08 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2010-05-19 10:09:32 +0800 |
commit | bdc0c91efa8dd6532ad550b46cf3a3756c7662dc (patch) | |
tree | 3e9152d8627d5b78fb76dd52c0cabf6cc5cc9c49 /lua | |
parent | f2411d43bf3a5433930c57809a386703ea31f50f (diff) | |
download | ibus-libpinyin-bdc0c91efa8dd6532ad550b46cf3a3756c7662dc.tar.gz ibus-libpinyin-bdc0c91efa8dd6532ad550b46cf3a3756c7662dc.tar.xz ibus-libpinyin-bdc0c91efa8dd6532ad550b46cf3a3756c7662dc.zip |
add ibus_engine_plugin_get_candidate and enhance ibus_engine_plugin_get_retval.
Diffstat (limited to 'lua')
-rw-r--r-- | lua/lua-plugin.c | 61 |
1 files changed, 42 insertions, 19 deletions
diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c index 11b3b1e..d256fd1 100644 --- a/lua/lua-plugin.c +++ b/lua/lua-plugin.c @@ -192,7 +192,7 @@ int ibus_engine_plugin_call(IBusEnginePlugin * plugin, const char * lua_function type = lua_type(L, -1); if ( LUA_TTABLE == type ){ return lua_objlen(L, -1); - } else if (LUA_TNUMBER == type || LUA_TSTRING == type){ + } else if (LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type){ return 1; } @@ -200,15 +200,47 @@ int ibus_engine_plugin_call(IBusEnginePlugin * plugin, const char * lua_function } /** + * get a candidate from lua return value. + */ +static const char * ibus_engine_plugin_get_candidate(lua_State * L){ + const char * suggest, * help, * candidate = NULL; + + int type = lua_type(L, -1); + + if ( LUA_TTABLE == type ){ + lua_pushliteral(L, "suggest"); + lua_gettable(L, -2); + lua_pushliteral(L, "help"); + lua_gettable(L, -3); + suggest = lua_tostring(L, -2); + help = lua_tostring(L, -1); + candidate = g_strdup_printf("%s [%s]", suggest, help); + lua_pop(L, 2); + } else if (LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type) { + candidate = g_strdup(lua_tostring(L, -1)); + } + + return candidate; +} + +/** * retrieve the retval string value. (value has been copied.) */ const char * ibus_engine_plugin_get_retval(IBusEnginePlugin * plugin){ IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); - const char * result = NULL; + const char * result = NULL; int type; lua_State * L = priv->L; - result = g_strdup(lua_tostring(L, -1)); - lua_pop(L, 1); + 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, 1); + } else if( LUA_TTABLE == type ){ + lua_pushinteger(L, 1); + lua_gettable(L, -2); + result = ibus_engine_plugin_get_candidate(L); + lua_pop(L, 2); + } return result; } @@ -220,7 +252,7 @@ GArray * ibus_engine_plugin_get_retvals(IBusEnginePlugin * plugin){ IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); lua_State * L = priv->L; int elem_num; int type; GArray * result = NULL; int i; - const char * suggest, * help, * candidate; + const char * candidate; type = lua_type(L, -1); if ( LUA_TTABLE != type ) @@ -232,23 +264,14 @@ GArray * ibus_engine_plugin_get_retvals(IBusEnginePlugin * plugin){ for ( i = 0; i < elem_num; ++i ){ lua_pushinteger(L, i + 1); lua_gettable(L, -2); - type = lua_type(L, -1); - if ( LUA_TTABLE == type ){ - lua_pushliteral(L, "suggest"); - lua_gettable(L, -2); - lua_pushliteral(L, "help"); - lua_gettable(L, -3); - suggest = lua_tostring(L, -2); - help = lua_tostring(L, -1); - candidate = g_strdup_printf("%s [%s]", suggest, help); - lua_pop(L, 3); - } else if (LUA_TNUMBER == type || LUA_TSTRING == type) { - candidate = g_strdup(lua_tostring(L, -1)); - lua_pop(L, 1); - } + + candidate = ibus_engine_plugin_get_candidate(L); + lua_pop(L, 1); + g_array_append_val(result, candidate); } + lua_pop(L, 1); return result; } |