diff options
author | Peng Wu <alexepico@gmail.com> | 2010-05-07 12:51:24 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2010-05-19 10:09:33 +0800 |
commit | 209d06b04a13247ffd31895e9e87d59c25fd3b2d (patch) | |
tree | a82940b28572a16e9583139a7d8fa16267e1b931 | |
parent | e63b0016502ff2f535894006c67364ed8fa48ef0 (diff) | |
download | ibus-libpinyin-209d06b04a13247ffd31895e9e87d59c25fd3b2d.tar.gz ibus-libpinyin-209d06b04a13247ffd31895e9e87d59c25fd3b2d.tar.xz ibus-libpinyin-209d06b04a13247ffd31895e9e87d59c25fd3b2d.zip |
refactor lua plugin interface.
-rw-r--r-- | lua/lua-ext-console.c | 13 | ||||
-rw-r--r-- | lua/lua-plugin.c | 38 | ||||
-rw-r--r-- | lua/lua-plugin.h | 11 |
3 files changed, 41 insertions, 21 deletions
diff --git a/lua/lua-ext-console.c b/lua/lua-ext-console.c index 6f61f25..9490cc9 100644 --- a/lua/lua-ext-console.c +++ b/lua/lua-ext-console.c @@ -34,15 +34,20 @@ void list_all_commands(IBusEnginePlugin * plugin){ int print_lua_call_result(IBusEnginePlugin * plugin, size_t num){ if ( 1 == num ) { - const char * result = ibus_engine_plugin_get_retval(plugin); - printf("result: %s.\n", result); + const lua_command_candidate_t * result = ibus_engine_plugin_get_retval(plugin); + if (result->content) + printf("result: %s.\n", result->content); } if ( num > 1) { GArray * results = ibus_engine_plugin_get_retvals(plugin); size_t i; for ( i = 0; i < results->len; ++i) { - const char * result = g_array_index(results, const char *, i); - printf("%d.%s >\t", i, result); + const lua_command_candidate_t * result = g_array_index(results, const lua_command_candidate_t *, i); + if (result->content) + printf("%d.%s >\t", i, result->content); + else{ + printf("%d. %s [%s]\t", i, result->suggest, result->help); + } } printf("\n"); } diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c index 56c1c5a..5e52620 100644 --- a/lua/lua-plugin.c +++ b/lua/lua-plugin.c @@ -155,7 +155,7 @@ gboolean ibus_engine_plugin_add_command(IBusEnginePlugin * plugin, lua_command_t return TRUE; } -lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugin, const char * command_name){ +const lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugin, const char * command_name){ IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); GArray * lua_commands = priv->lua_commands; lua_command_t lookup_command = {.command_name = command_name, }; @@ -202,8 +202,12 @@ 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; +static const lua_command_candidate_t * ibus_engine_plugin_get_candidate(lua_State * L){ + + const char * suggest, * help, * content = NULL; + lua_command_candidate_t * candidate = malloc(sizeof(lua_command_candidate_t)); + + memset(candidate, 0, sizeof(lua_command_candidate_t)); int type = lua_type(L, -1); @@ -214,10 +218,12 @@ static const char * ibus_engine_plugin_get_candidate(lua_State * L){ lua_gettable(L, -3); suggest = lua_tostring(L, -2); help = lua_tostring(L, -1); - candidate = g_strdup_printf("%s [%s]", suggest, help); + candidate->suggest = g_strdup(suggest); + candidate->help = g_strdup(help); lua_pop(L, 2); } else if (LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type) { - candidate = g_strdup(lua_tostring(L, -1)); + content = lua_tostring(L, -1); + candidate->content = g_strdup(content); } return candidate; @@ -226,23 +232,25 @@ static const char * ibus_engine_plugin_get_candidate(lua_State * L){ /** * retrieve the retval string value. (value has been copied.) */ -const char * ibus_engine_plugin_get_retval(IBusEnginePlugin * plugin){ +const lua_command_candidate_t * ibus_engine_plugin_get_retval(IBusEnginePlugin * plugin){ IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); - const char * result = NULL; int type; + lua_command_candidate_t * result = NULL; int type; lua_State * L = priv->L; type = lua_type(L ,-1); if ( LUA_TNUMBER == type || LUA_TBOOLEAN == type || LUA_TSTRING == type) { - result = g_strdup(lua_tostring(L, -1)); + result = malloc(sizeof(lua_command_candidate_t)); + memset(result, 0, sizeof(lua_command_candidate_t)); + result->content = 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); + result = (lua_command_candidate_t *)ibus_engine_plugin_get_candidate(L); lua_pop(L, 2); } - return result; + return (const lua_command_candidate_t *)result; } /** @@ -252,13 +260,13 @@ 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 * candidate; + const lua_command_candidate_t * candidate = NULL; type = lua_type(L, -1); if ( LUA_TTABLE != type ) return result; - result = g_array_new(TRUE, TRUE, sizeof(char *)); + result = g_array_new(TRUE, TRUE, sizeof(lua_command_candidate_t *)); elem_num = lua_objlen(L, -1); for ( i = 0; i < elem_num; ++i ){ @@ -274,3 +282,9 @@ GArray * ibus_engine_plugin_get_retvals(IBusEnginePlugin * plugin){ lua_pop(L, 1); return result; } + +void ibus_engine_plugin_free(lua_command_candidate_t * candidate){ + g_free((gpointer)candidate->content); + g_free((gpointer)candidate->suggest); + g_free((gpointer)candidate->help); +} diff --git a/lua/lua-plugin.h b/lua/lua-plugin.h index 9ae71b4..8116ae1 100644 --- a/lua/lua-plugin.h +++ b/lua/lua-plugin.h @@ -22,8 +22,8 @@ typedef struct{ } lua_command_candidate_t; typedef struct{ - const char * lua_function_name; - const char * description; + char * lua_function_name; + char * description; /*< private, skip it, and register it into Special Table directly with * wildcard. >*/ /* * list of input_trigger_strings; @@ -90,7 +90,7 @@ const GArray * ibus_engine_plugin_get_available_commands(IBusEnginePlugin * plug * command must be an 2-char long string. * return the matched command. */ -lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugin, const char * command_name); +const lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugin, const char * command_name); /** * retval int: returns the number of results, @@ -102,11 +102,12 @@ int ibus_engine_plugin_call(IBusEnginePlugin * plugin, const char * lua_function /** * retrieve the retval string value. (value has been copied.) */ -const char * ibus_engine_plugin_get_retval(IBusEnginePlugin * plugin); +const lua_command_candidate_t * ibus_engine_plugin_get_retval(IBusEnginePlugin * plugin); /** - * retrieve the array of string values. (string values have been copied.) + * retrieve the array of lua_command_candidate_t values. (string values have been copied.) */ GArray * ibus_engine_plugin_get_retvals(IBusEnginePlugin * plugin); +void ibus_engine_plugin_free(lua_command_candidate_t * candidate); #endif |