lua.c
changeset 142 7e8f523b66af
parent 137 4fda19d05965
child 143 4232a5da1af2
--- a/lua.c	Tue Oct 23 13:15:37 2012 +0300
+++ b/lua.c	Tue Nov 27 12:12:57 2012 +0200
@@ -806,10 +806,25 @@
 	{ "color",     COMPL_COLOR     },
 	{ "otr",       COMPL_OTR       },
 	{ "ortpolicy", COMPL_OTRPOLICY },
+	{ "module",    COMPL_MODULE    },
 	{ "yesno",     0               },
 	{ NULL,        0               },
 };
-#define MLUA_YESNO_POS ( 21 )
+#define MLUA_YESNO_POS ( 22 )
+
+#ifdef MCABBER_API_HAVE_COMPL_FLAGS
+/// completion sorting order
+/// Sorting order for completion category.
+/// G:
+static const string2enum_t lua_completion_sortorder[] = {
+	{ "sort",    COMPL_FLAGS_SORT    },
+	{ "direct",  COMPL_FLAGS_SORT    },
+	{ "reverse", COMPL_FLAGS_REVERSE },
+	{ "append",  COMPL_FLAGS_APPEND  },
+	{ "prepend", COMPL_FLAGS_PREPEND },
+	{ NULL,        0                 },
+};
+#endif
 
 typedef struct {
 	int        cbref;
@@ -966,34 +981,37 @@
 
 /// main.add_category
 /// Adds completion category.
-/// A: table (values are used as words for completion, optional)
+/// A: table (values are used as words for completion, optional), argument enum value (completion sorting order, optional)
 /// R: integer (category id, in fact completion type) or nil
 static int lua_main_add_category (lua_State *L)
 {
 	guint cid;
-
-	if (lua_gettop (L) > 0) {
-		luaL_argcheck (L, lua_type (L, 1) == LUA_TTABLE, 1, "table expected");
-		cid = compl_new_category ();
-		if (cid) {
-			lua_pushnil (L);
-			while (lua_next (L, 1)) {
-				char *word = to_utf8 (luaL_checkstring (L, -1));
-				if (word) {
-					compl_add_category_word (cid, word);
-					g_free (word);
+	int   top = lua_gettop ( L );
+#ifdef MCABBER_API_HAVE_COMPL_FLAGS
+	if ( top > 1 )
+		cid = compl_new_category ( luaL_checkenum ( L, 2, lua_completion_sortorder ) );
+	else
+		cid = compl_new_category ( COMPL_FLAGS_SORT );
+#else
+	cid = compl_new_category ();
+#endif
+	if ( cid ) {
+		if ( ( top > 0 ) && ( lua_type ( L, 1 ) == LUA_TTABLE ) ) {
+			lua_pushnil ( L );
+			while ( lua_next ( L, 1 ) ) {
+				char * word = to_utf8 ( lua_tostring ( L, 4 ) );
+				if ( word ) {
+					compl_add_category_word ( cid, word );
+					g_free ( word );
 				}
-				lua_pop (L, 1);
+				lua_pop ( L, 1 );
 			}
 		}
+
+		lua_added_categories = g_slist_prepend ( lua_added_categories, (gpointer) ((gsize) cid) );
+		lua_pushinteger ( L, cid );
 	} else
-		cid = compl_new_category ();
-
-	if (cid) {
-		lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) ((gsize) cid));
-		lua_pushinteger (L, cid);
-	} else
-		lua_pushnil (L);
+		lua_pushnil ( L );
 
 	return 1;
 }
@@ -1054,7 +1072,11 @@
 
 		if (top > 3) { // Completions provided
 			if (lua_type (L, 4) == LUA_TTABLE) {
+#ifdef MCABBER_API_HAVE_COMPL_FLAGS
+				cid = compl_new_category (COMPL_FLAGS_SORT);
+#else
 				cid = compl_new_category ();
+#endif
 				if (cid) {
 					lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) ((gsize) cid));
 					lua_pushnil (L);
@@ -1764,7 +1786,11 @@
 	lua_register (lua, "print",  lua_global_print );
 
 	{
+#ifdef MCABBER_API_HAVE_COMPL_FLAGS
+		int cid = compl_new_category (COMPL_FLAGS_PREPEND);
+#else
 		int cid = compl_new_category ();
+#endif
 
 		if (cid) {
 			const string2enum_t *word = lua_yesno;