Add completion sorting.
authorMyhailo Danylenko <isbear@ukrpost.net>
Tue, 27 Nov 2012 12:12:57 +0200
changeset 142 7e8f523b66af
parent 141 1e36a08d7734
child 143 4232a5da1af2
Add completion sorting. main.add_category now accepts second parameter - flags
CMakeLists.txt
docs/api.mdwn
lua.avv.in
lua.c
--- a/CMakeLists.txt	Tue Oct 23 13:15:37 2012 +0300
+++ b/CMakeLists.txt	Tue Nov 27 12:12:57 2012 +0200
@@ -32,6 +32,17 @@
 pkg_check_modules(GMODULE REQUIRED gmodule-2.0)
 pkg_check_modules(MCABBER REQUIRED mcabber)
 find_program(DOCGEN_EXECUTABLE NAMES docgen.pl docgen DOC "Docgen documentation generator script (optional)")
+include(CheckCSourceCompiles)
+set(CMAKE_REQUIRED_INCLUDES ${MCABBER_INCLUDE_DIRS})
+set(CMAKE_REQUIRED_LIBRARIES ${MCABBER_LIBRARIES})
+set(CMAKE_REQUIRED_FLAGS ${MCABBER_LDFLAGS} ${MCABBER_CFLAGS})
+check_c_source_compiles("
+	#include <glib.h>
+	#include <mcabber/api.h>
+	#ifdef MCABBER_API_HAVE_COMPL_FLAGS
+	int main (void) { return 0; }
+	#endif
+	" HAVE_MCABBER_COMPLETION_FLAGS)
 link_directories(${GLIB_LIBRARY_DIRS}
 				 ${GMODULE_LIBRARY_DIRS}
 				 ${MCABBER_LIBRARY_DIRS})
@@ -90,6 +101,11 @@
 set(CPACK_SOURCE_IGNORE_FILES "/\\\\..*;\\\\.swp;~$;/build/;\\\\.tar\\\\.;\\\\.deb;\\\\.so")
 include(CPack)
 
+if(HAVE_MCABBER_COMPLETION_FLAGS)
+	set(AVV_COMPL_VERSION "compl:4")
+else()
+	set(AVV_COMPL_VERSION "compl:3")
+endif()
 configure_file(lua.avv.in lua.avv)
 
 ## Set up installer
@@ -102,4 +118,4 @@
 install(DIRECTORY help DESTINATION share/mcabber)
 install(FILES ${PROJECT_BINARY_DIR}/lua.avv DESTINATION share/mcabber/avv/modules RENAME lua)
 
-## The End ## vim: se ts=4: ##
+## The End ## vim: se ts=4 sw=4: ##
--- a/docs/api.mdwn	Tue Oct 23 13:15:37 2012 +0300
+++ b/docs/api.mdwn	Tue Nov 27 12:12:57 2012 +0200
@@ -177,7 +177,12 @@
 <a name="completion.type"></a>
 ### completion type
 Built-it completion types can be specified as string, instead of id.  
-**Values:**  cmd, jid, urljid, name, status, filename, roster, buffer, group, groupname, multiline, room, resource, auth, request, events, eventsid, pgp, color, otr, ortpolicy, yesno  
+**Values:**  cmd, jid, urljid, name, status, filename, roster, buffer, group, groupname, multiline, room, resource, auth, request, events, eventsid, pgp, color, otr, ortpolicy, module, yesno  
+
+<a name="completion.sorting.order"></a>
+### completion sorting order
+Sorting order for completion category.  
+**Values:**  sort, direct, reverse, append, prepend  
 
 <a name="command.arguments.table"></a>
 ### command arguments table
@@ -213,7 +218,7 @@
 <a name="main.add.category"></a>
 ### main.add_category
 Adds completion category.  
-**Arguments:** table (values are used as words for completion, optional)    
+**Arguments:** table (values are used as words for completion, optional), argument enum value ([completion sorting order](#completion.sorting.order), optional)    
 **Return values:** integer (category id, in fact [completion type](#completion.type)) or nil  
 
 <a name="main.del.category"></a>
--- a/lua.avv.in	Tue Oct 23 13:15:37 2012 +0300
+++ b/lua.avv.in	Tue Nov 27 12:12:57 2012 +0200
@@ -6,7 +6,7 @@
 # commands depend on build environment: 4+3, 2+1
 # completion depends on constants: 3, 2+1
 Requires:    utils:2 + ( roster:4 | roster:3 | roster:2 | roster:1 ) + ( commands:4 | commands:3 ) +
-             compl:3 + events:1 + utils:2 + ( hooks:5 | hooks:4 | hooks:3 | hooks:2 ) +
+             ( ${AVV_COMPL_VERSION} ) + events:1 + utils:2 + ( hooks:5 | hooks:4 | hooks:3 | hooks:2 ) +
 	     ( xmpp:6 | xmpp:5 | xmpp:4 | xmpp:3 | xmpp:2 | xmpp:1 ) + api:1 + main:1 +
 	     ( screen:10 | screen:9 | screen:8 | screen:7 | screen:6 | screen:5 | screen:4 ) +
 	     logprint:3 + ( hbuf:3 | hbuf:2 | hbuf:1 ) + settings:1 +
--- 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;