write ibus_engine_plugin_load_lua_script.
authorPeng Wu <alexepico@gmail.com>
Fri, 16 Apr 2010 06:10:04 +0000 (14:10 +0800)
committerPeng Wu <alexepico@gmail.com>
Wed, 19 May 2010 02:09:32 +0000 (10:09 +0800)
lua/lua-plugin.c
lua/lua-plugin.h
lua/test-lua-plugin.c

index 65ab9e4..467070c 100644 (file)
@@ -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;
index d7118d6..8e6e056 100644 (file)
@@ -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);
index 72ec552..df8546f 100644 (file)
@@ -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;
 }