diff options
Diffstat (limited to 'lua/lua-plugin.c')
-rw-r--r-- | lua/lua-plugin.c | 60 |
1 files changed, 55 insertions, 5 deletions
diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c index ac03e15..65ab9e4 100644 --- a/lua/lua-plugin.c +++ b/lua/lua-plugin.c @@ -1,4 +1,5 @@ #include <string.h> +#include <stdlib.h> #include <lua.h> #include <lualib.h> #include <lauxlib.h> @@ -14,6 +15,22 @@ struct _IBusEnginePluginPrivate{ G_DEFINE_TYPE (IBusEnginePlugin, ibus_engine_plugin, G_TYPE_OBJECT); +static void lua_command_clone(lua_command_t * command, lua_command_t * new_command){ + new_command->command_name = g_strdup(command->command_name); + new_command->lua_function_name = g_strdup(command->command_name); + new_command->description = g_strdup(command->description); + new_command->leading = g_strdup(command->leading); + new_command->help = g_strdup(command->help); +} + +static void lua_command_reclaim(lua_command_t * command){ + g_free((gpointer)command->command_name); + g_free((gpointer)command->lua_function_name); + g_free((gpointer)command->description); + g_free((gpointer)command->leading); + g_free((gpointer)command->help); +} + static int lua_plugin_init(IBusEnginePluginPrivate * plugin){ g_assert(NULL == plugin->L); @@ -36,11 +53,7 @@ lua_plugin_fini(IBusEnginePluginPrivate * plugin){ if ( plugin->lua_commands ){ for ( i = 0; i < plugin->lua_commands->len; ++i){ command = &g_array_index(plugin->lua_commands, lua_command_t, i); - g_free((gpointer)command->command_name); - g_free((gpointer)command->lua_function_name); - g_free((gpointer)command->description); - g_free((gpointer)command->leading); - g_free((gpointer)command->help); + lua_command_reclaim(command); } g_array_free(plugin->lua_commands, TRUE); plugin->lua_commands = NULL; @@ -96,6 +109,43 @@ IBusEnginePlugin * ibus_engine_plugin_new(){ return plugin; } +static gint compare_command(gconstpointer a, gconstpointer b){ + lua_command_t * ca = (lua_command_t *) a; + lua_command_t * cb = (lua_command_t *) b; + return strcmp(ca->command_name, cb->command_name); +} + +gboolean ibus_engine_plugin_add_command(IBusEnginePlugin * plugin, lua_command_t * command){ + IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); + GArray * lua_commands = priv->lua_commands; + + if ( ibus_engine_plugin_lookup_command( plugin, command->command_name) ) + return FALSE; + + lua_command_t new_command; + lua_command_clone(command, &new_command); + + g_array_append_val(priv->lua_commands, new_command); + g_array_sort(priv->lua_commands, compare_command); + + return TRUE; +} + +lua_command_t * ibus_engine_plugin_lookup_command(IBusEnginePlugin * plugin, const char * command){ + IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); + GArray * lua_commands = priv->lua_commands; + lua_command_t lookup_command = {.command_name = command, }; + + lua_command_t * result = bsearch(&lookup_command, lua_commands->data, lua_commands->len, sizeof(lua_command_t), compare_command); + return result; +} + +const GArray * ibus_engine_plugin_get_available_commands(IBusEnginePlugin * plugin){ + IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); + return priv->lua_commands; +} + + /* will drop this function soon. */ lua_State * ibus_engine_plugin_get_lua_State(IBusEnginePlugin * plugin){ return plugin->priv->L; |