changeset 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 7d72b7d2d93a
children 924f4552996c
files mcabber/mcabber/compl.c mcabber/mcabber/compl.h mcabber/mcabber/main.c
diffstat 3 files changed, 112 insertions(+), 51 deletions(-) [+]
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)
--- a/mcabber/mcabber/compl.h	Wed Nov 10 13:59:48 2010 +0100
+++ b/mcabber/mcabber/compl.h	Wed Nov 10 14:08:24 2010 +0100
@@ -5,37 +5,37 @@
 
 #include <mcabber/config.h>
 
-#define COMPL_CMD         (1U<<0)
-#define COMPL_JID         (1U<<1)
-#define COMPL_URLJID      (1U<<2)   // Not implemented yet
-#define COMPL_NAME        (1U<<3)   // Not implemented yet
-#define COMPL_STATUS      (1U<<4)
-#define COMPL_FILENAME    (1U<<5)   // Not implemented yet
-#define COMPL_ROSTER      (1U<<6)
-#define COMPL_BUFFER      (1U<<7)
-#define COMPL_GROUP       (1U<<8)
-#define COMPL_GROUPNAME   (1U<<9)
-#define COMPL_MULTILINE   (1U<<10)
-#define COMPL_ROOM        (1U<<11)
-#define COMPL_RESOURCE    (1U<<12)
-#define COMPL_AUTH        (1U<<13)
-#define COMPL_REQUEST     (1U<<14)
-#define COMPL_EVENTS      (1U<<15)
-#define COMPL_EVENTSID    (1U<<16)
-#define COMPL_PGP         (1U<<17)
-#define COMPL_COLOR       (1U<<18)
-#define COMPL_OTR         (1U<<19)
-#define COMPL_OTRPOLICY   (1U<<20)
+#define COMPL_CMD         1
+#define COMPL_JID         2
+#define COMPL_URLJID      3   // Not implemented yet
+#define COMPL_NAME        4   // Not implemented yet
+#define COMPL_STATUS      5
+#define COMPL_FILENAME    6   // Not implemented yet
+#define COMPL_ROSTER      7
+#define COMPL_BUFFER      8
+#define COMPL_GROUP       9
+#define COMPL_GROUPNAME   10
+#define COMPL_MULTILINE   11
+#define COMPL_ROOM        12
+#define COMPL_RESOURCE    13
+#define COMPL_AUTH        14
+#define COMPL_REQUEST     15
+#define COMPL_EVENTS      16
+#define COMPL_EVENTSID    17
+#define COMPL_PGP         18
+#define COMPL_COLOR       19
+#define COMPL_OTR         20
+#define COMPL_OTRPOLICY   21
+
 #ifdef MODULES_ENABLE
-#define COMPL_MAX_BUILTIN (1U<<20)
-
-guint compl_new_category (void);
-void  compl_del_category (guint id);
+void  compl_init_system(void);
+guint compl_new_category(void);
+void  compl_del_category(guint id);
 #endif
 
-void    compl_add_category_word(guint, const gchar *command);
+void    compl_add_category_word(guint categ, const gchar *command);
 void    compl_del_category_word(guint categ, const gchar *word);
-GSList *compl_get_category_list(guint cat_flags, guint *dynlist);
+GSList *compl_get_category_list(guint categ, guint *dynlist);
 
 guint   new_completion(const gchar *prefix, GSList *compl_cat,
                        const gchar *suffix);
--- a/mcabber/mcabber/main.c	Wed Nov 10 13:59:48 2010 +0100
+++ b/mcabber/mcabber/main.c	Wed Nov 10 14:08:24 2010 +0100
@@ -47,6 +47,7 @@
 #include "events.h"
 
 #ifdef MODULES_ENABLE
+# include "compl.h"
 # include "modules.h"
 #endif
 
@@ -363,6 +364,7 @@
   scr_init_bindings();
   caps_init();
 #ifdef MODULES_ENABLE
+  compl_init_system();
   modules_init();
 #endif
   /* Initialize charset */