diff mcabber/mcabber/compl.c @ 1922:4ba68ad737bc

Increase the number of available categories for completions
author Mikael Berthe <mikael@lilotux.net>
date Wed, 10 Nov 2010 14:08:24 +0100
parents 057b514b1f12
children 924f4552996c
line wrap: on
line diff
--- a/mcabber/mcabber/compl.c	Wed Nov 10 13:59:48 2010 +0100
+++ b/mcabber/mcabber/compl.c	Wed Nov 10 14:08:24 2010 +0100
@@ -36,6 +36,7 @@
 #include "utf8.h"
 #include "roster.h"
 #include "events.h"
+#include "logprint.h"
 
 // Completion structure
 typedef struct {
@@ -47,7 +48,7 @@
 
 // Category structure
 typedef struct {
-  guint flag;
+  guint64 flag;
   GSList *words;
 } category;
 
@@ -55,13 +56,37 @@
 static compl *InputCompl;
 
 #ifdef MODULES_ENABLE
-guint registered_cats = COMPL_CMD|COMPL_JID|COMPL_URLJID|COMPL_NAME| \
-                        COMPL_STATUS|COMPL_FILENAME|COMPL_ROSTER|COMPL_BUFFER| \
-                        COMPL_GROUP|COMPL_GROUPNAME|COMPL_MULTILINE|COMPL_ROOM| \
-                        COMPL_RESOURCE|COMPL_AUTH|COMPL_REQUEST|COMPL_EVENTS| \
-                        COMPL_EVENTSID|COMPL_PGP|COMPL_COLOR| \
-                        COMPL_OTR|COMPL_OTRPOLICY| \
-                        0;
+static guint64 registered_cats;
+
+static inline void register_builtin_cat(guint c) {
+  registered_cats |= 1UL << (c-1);
+}
+
+void compl_init_system(void)
+{
+  // Builtin completion categories:
+  register_builtin_cat(COMPL_CMD);
+  register_builtin_cat(COMPL_JID);
+  register_builtin_cat(COMPL_URLJID);
+  register_builtin_cat(COMPL_NAME);
+  register_builtin_cat(COMPL_STATUS);
+  register_builtin_cat(COMPL_FILENAME);
+  register_builtin_cat(COMPL_ROSTER);
+  register_builtin_cat(COMPL_BUFFER);
+  register_builtin_cat(COMPL_GROUP);
+  register_builtin_cat(COMPL_GROUPNAME);
+  register_builtin_cat(COMPL_MULTILINE);
+  register_builtin_cat(COMPL_ROOM);
+  register_builtin_cat(COMPL_RESOURCE);
+  register_builtin_cat(COMPL_AUTH);
+  register_builtin_cat(COMPL_REQUEST);
+  register_builtin_cat(COMPL_EVENTS);
+  register_builtin_cat(COMPL_EVENTSID);
+  register_builtin_cat(COMPL_PGP);
+  register_builtin_cat(COMPL_COLOR);
+  register_builtin_cat(COMPL_OTR);
+  register_builtin_cat(COMPL_OTRPOLICY);
+}
 
 //  compl_new_category()
 // Reserves id for new completion category.
@@ -73,12 +98,12 @@
   guint i = 0;
   while ((registered_cats >> i) & 1)
     i++;
-  if (i >= 8 * sizeof (guint))
+  if (i >= 8 * sizeof (registered_cats))
     return 0;
   else {
-    guint id = 1 << i;
+    guint64 id = 1 << i;
     registered_cats |= id;
-    return id;
+    return i+1;
   }
 }
 
@@ -88,7 +113,13 @@
 // and specify exactly what you get from compl_new_category.
 void compl_del_category(guint id)
 {
-  registered_cats &= ~id;
+  if (!id) {
+    scr_log_print(LPRINT_LOGNORM, "Error: compl_del_category() - "
+                  "Invalid category.");
+    return;
+  }
+  id--;
+  registered_cats &= ~(1<<id);
 }
 #endif
 
@@ -186,17 +217,28 @@
 // Adds a keyword as a possible completion in category categ.
 void compl_add_category_word(guint categ, const gchar *word)
 {
+  guint64 catv;
   GSList *sl_cat;
   category *cat;
   char *nword;
+
+  if (!categ) {
+    scr_log_print(LPRINT_LOGNORM, "Error: compl_add_category_word() - "
+                  "Invalid category.");
+    return;
+  }
+
+  categ--;
+  catv = 1UL << categ;
+
   // Look for category
   for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
-    if (categ == ((category*)sl_cat->data)->flag)
+    if (catv == ((category*)sl_cat->data)->flag)
       break;
   }
   if (!sl_cat) {   // Category not found, let's create it
     cat = g_new0(category, 1);
-    cat->flag = categ;
+    cat->flag = catv;
     Categories = g_slist_append(Categories, cat);
   } else
     cat = (category*)sl_cat->data;
@@ -222,12 +264,23 @@
 // Removes a keyword from category categ in completion list.
 void compl_del_category_word(guint categ, const gchar *word)
 {
+  guint64 catv;
   GSList *sl_cat, *sl_elt;
   category *cat;
   char *nword;
+
+  if (!categ) {
+    scr_log_print(LPRINT_LOGNORM, "Error: compl_del_category_word() - "
+                  "Invalid category.");
+    return;
+  }
+
+  categ--;
+  catv = 1UL << categ;
+
   // Look for category
   for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
-    if (categ == ((category*)sl_cat->data)->flag)
+    if (catv == ((category*)sl_cat->data)->flag)
       break;
   }
   if (!sl_cat) return;   // Category not found, finished!
@@ -256,18 +309,24 @@
 }
 
 //  compl_get_category_list()
-// Returns a slist of all words in the categories specified by the given flags
+// Returns a slist of all words in the specified categorie.
 // Iff this function sets *dynlist to TRUE, then the caller must free the
 // whole list after use.
-GSList *compl_get_category_list(guint cat_flags, guint *dynlist)
+GSList *compl_get_category_list(guint categ, guint *dynlist)
 {
+  guint64 cat_flags;
   GSList *sl_cat;
 
+  if (!categ) {
+    scr_log_print(LPRINT_LOGNORM, "Error: compl_get_category_list() - "
+                  "Invalid category.");
+    return NULL;
+  }
+
   *dynlist = FALSE;
+  cat_flags = 1UL << (categ - 1);
 
-  // Look for category
-  // XXX Actually that's not that simple... cat_flags can be a combination
-  // of several flags!
+  // Look for the category
   for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
     if (cat_flags == ((category*)sl_cat->data)->flag)
       break;
@@ -277,16 +336,16 @@
 
   // Handle dynamic SLists
   *dynlist = TRUE;
-  if (cat_flags == COMPL_GROUPNAME) {
+  if (categ == COMPL_GROUPNAME) {
     return compl_list(ROSTER_TYPE_GROUP);
   }
-  if (cat_flags == COMPL_JID) {
+  if (categ == COMPL_JID) {
     return compl_list(ROSTER_TYPE_USER);
   }
-  if (cat_flags == COMPL_RESOURCE) {
+  if (categ == COMPL_RESOURCE) {
     return buddy_getresources_locale(NULL);
   }
-  if (cat_flags == COMPL_EVENTSID) {
+  if (categ == COMPL_EVENTSID) {
     GSList *compl = evs_geteventslist();
     GSList *cel;
     for (cel = compl; cel; cel = cel->next)