diff options
author | Peng Wu <alexepico@gmail.com> | 2010-04-16 14:10:04 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2010-05-19 10:09:32 +0800 |
commit | 560cdb15c6c238252d49c362a631de35d05713c2 (patch) | |
tree | d2d567ed439ef27219b8fff932e26f60219a5bb6 /lua | |
parent | 7cf2505a5790195814e05e4a26f40fe8319d5e67 (diff) | |
download | ibus-libpinyin-560cdb15c6c238252d49c362a631de35d05713c2.tar.gz ibus-libpinyin-560cdb15c6c238252d49c362a631de35d05713c2.tar.xz ibus-libpinyin-560cdb15c6c238252d49c362a631de35d05713c2.zip |
write ibus_engine_plugin_load_lua_script.
Diffstat (limited to 'lua')
-rw-r--r-- | lua/lua-plugin.c | 24 | ||||
-rw-r--r-- | lua/lua-plugin.h | 8 | ||||
-rw-r--r-- | lua/test-lua-plugin.c | 30 |
3 files changed, 34 insertions, 28 deletions
diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c index 65ab9e4..467070c 100644 --- a/lua/lua-plugin.c +++ b/lua/lua-plugin.c @@ -109,6 +109,29 @@ IBusEnginePlugin * ibus_engine_plugin_new(){ return plugin; } +static void l_message (const char *pname, const char *msg) { + if (pname) fprintf(stderr, "%s: ", pname); + fprintf(stderr, "%s\n", msg); + fflush(stderr); +} + +static int report (lua_State *L, int status) { + if (status && !lua_isnil(L, -1)) { + const char *msg = lua_tostring(L, -1); + if (msg == NULL) msg = "(error object is not a string)"; + l_message(NULL, msg); + lua_pop(L, 1); + } + return status; +} + +int ibus_engine_plugin_load_lua_script(IBusEnginePlugin * plugin, const char * filename){ + IBusEnginePluginPrivate * priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE(plugin); + int status = luaL_dofile(priv->L, filename); + return report(priv->L, status); +} + + static gint compare_command(gconstpointer a, gconstpointer b){ lua_command_t * ca = (lua_command_t *) a; lua_command_t * cb = (lua_command_t *) b; @@ -126,6 +149,7 @@ gboolean ibus_engine_plugin_add_command(IBusEnginePlugin * plugin, lua_command_t lua_command_clone(command, &new_command); g_array_append_val(priv->lua_commands, new_command); + /* Note: need to improve speed here? */ g_array_sort(priv->lua_commands, compare_command); return TRUE; diff --git a/lua/lua-plugin.h b/lua/lua-plugin.h index d7118d6..8e6e056 100644 --- a/lua/lua-plugin.h +++ b/lua/lua-plugin.h @@ -59,9 +59,17 @@ struct _IBusEnginePluginClass GType ibus_engine_plugin_get_type(void); +/** + * create a new ibus engine plugin. + */ IBusEnginePlugin * ibus_engine_plugin_new(); /** + * load a new lua script for ibus engine plugin. + */ +int ibus_engine_plugin_load_lua_script(IBusEnginePlugin * plugin, const char * filename); + +/** * add a lua_command_t to plugin. */ gboolean ibus_engine_plugin_add_command(IBusEnginePlugin * plugin, lua_command_t * command); diff --git a/lua/test-lua-plugin.c b/lua/test-lua-plugin.c index 72ec552..df8546f 100644 --- a/lua/test-lua-plugin.c +++ b/lua/test-lua-plugin.c @@ -7,32 +7,6 @@ #include "lua-plugin.h" - -static const char * progname = "test-lua-plugin"; -static lua_State * L = NULL; - -static void l_message (const char *pname, const char *msg) { - if (pname) fprintf(stderr, "%s: ", pname); - fprintf(stderr, "%s\n", msg); - fflush(stderr); -} - -static int report (lua_State *L, int status) { - if (status && !lua_isnil(L, -1)) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "(error object is not a string)"; - l_message(progname, msg); - lua_pop(L, 1); - } - return status; -} - -static int run_test(lua_State *L, const char * filename){ - int status = luaL_dofile(L, filename); - fprintf(stderr, "%s done.\n", filename); - return report(L, status); -} - int main(int argc, char * argv[]){ printf("starting test...\n"); @@ -42,11 +16,11 @@ int main(int argc, char * argv[]){ plugin = ibus_engine_plugin_new(); lua_State * L = ibus_engine_plugin_get_lua_State(plugin); - run_test(L, "test.lua"); + ibus_engine_plugin_load_lua_script(plugin, "test.lua"); g_assert(lua_plugin_retrieve_plugin(L) == plugin); - g_object_unref(plugin); + printf("done.\n"); return 0; } |