--- a/main.c Tue Feb 24 19:42:48 2009 +0200
+++ b/main.c Wed Feb 25 22:58:34 2009 +0200
@@ -20,6 +20,7 @@
#include "utils.h" // from_utf8, jidtodisp
#include "hooks.h" // hk_add_handler, hk_del_handler
#include "settings.h" // settings_set, settings_del, settings_get
+#include "compl.h" // compl_new_category, compl_add_category_word, compl_del_category_word
// global lua state object, necessary for uninitialization function
@@ -341,23 +342,69 @@
}
}
+/// main.add_completion
+/// Adds word to a completion list.
+/// A: integer (completion group id), string (word)
+static int lua_main_add_completion (lua_State *L)
+{
+ guint cid = luaL_checkinteger (L, 1);
+ const char *word = luaL_checkstring (L, 2);
+ compl_add_category_word (cid, word);
+}
+
+/// main.del_completion
+/// Removes word from a completion list.
+/// A: integer (completion group id), string (word)
+static int lua_main_del_completion (lua_State *L)
+{
+ guint cid = luaL_checkinteger (L, 1);
+ const char *word = luaL_checkstring (L, 2);
+ compl_del_category_word (cid, word);
+}
+
/// main.command
/// Associates or breaks association between mcabber command name and lua function.
/// To unregister command omit function argument.
-/// A: string (command name), command function (optional)
+/// If you specify a third argument, table (even empty), function will return completion group id or nothing.
+/// You can also specify an integer instead of table, then no new id will be registered.
+/// Note, that for now there are no way to unregister completion group, so, resources can be exausted easily.
+/// Also note, that it ignores keys in a completion table.
+/// A: string (command name), command function (optional), table (completions, optional)/integer (comletion group id, optional)
+/// R: integer (completion group id, optional) or nil
static int lua_main_command (lua_State *L)
{
const char *name = luaL_checkstring (L, 1);
lua_command_callback_t *cb;
if (lua_gettop (L) > 1) { // Register
+ guint cid = 0;
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
+ if (lua_gettop (L) > 2) { // Completions provided
+ if (lua_type (L, 3) == LUA_TTABLE) {
+ cid = compl_new_category ();
+ if (cid) {
+ lua_pushnil (L);
+ while (lua_next (L, 3)) {
+ compl_add_category_word (cid, luaL_checkstring (L, -1));
+ lua_pop (L, 1);
+ }
+ }
+ } else
+ cid = luaL_checkinteger (L, 3);
+ }
+
cb = luaL_malloc (L, sizeof (lua_command_callback_t));
+ lua_pushvalue (L, 2);
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
cb->L = L;
- cmd_add (name, "", 0, 0, (void (*) (char *p)) lua_main_command_handler, cb);
+ cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb);
lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name));
+
+ if (cid) {
+ lua_pushinteger (L, cid);
+ return 1;
+ }
} else { // Unregister
GSList *el = g_slist_find_custom (lua_added_commands, name, (GCompareFunc) g_strcmp0);
@@ -546,7 +593,7 @@
if (lua_pcall (L, 0, 0, 0)) {
scr_LogPrint (LPRINT_NORMAL, "lua: Runtime error: %s", lua_tostring(lua, -1));
- lua_pop (L, -1);
+ lua_pop (L, 1);
return;
}
}
@@ -590,6 +637,8 @@
{ "option", lua_main_option },
{ "add_feature", lua_main_add_feature },
{ "del_feature", lua_main_del_feature },
+ { "add_completion",lua_main_add_completion},
+ { "del_completion",lua_main_del_completion},
{ "command", lua_main_command },
{ "print_info", lua_main_print_info },
{ "beep", lua_main_beep },