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");
}
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, };
/**
* 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);
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;
/**
* 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;
}
/**
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 ){
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);
+}
} 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;
* 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,
/**
* 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