changeset 116:741e45592522

Add simple "prediction" into tab-completion based on previously gotten last match. We assume that the last match is what the user wants, if new pattern matches previous pattern.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 24 Oct 2010 23:09:42 +0300
parents 300a6ea4aff3
children da05b766b8c0
files libnnchat.c libnnchat.h nnchat.c
diffstat 3 files changed, 27 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/libnnchat.c	Sun Oct 24 21:52:19 2010 +0300
+++ b/libnnchat.c	Sun Oct 24 23:09:42 2010 +0300
@@ -653,7 +653,7 @@
 }
 
 
-nn_user_t *nn_user_match(const nn_userhash_t *list, const char *pattern, const char *current)
+nn_user_t *nn_user_match(const nn_userhash_t *list, const char *pattern, const char *current, BOOL again)
 {
     uint8_t hash;
     
@@ -668,6 +668,8 @@
             nn_user_t *found = NULL;
             while (curr != NULL) {
                 if (strcasecmp(curr->name, current) == 0) {
+                    if (again)
+                        return curr;
                     found = curr->next;
                     break;
                 }
@@ -679,6 +681,7 @@
                 curr = list->buckets[hash];
         }
         
+
         while (curr != NULL) {
             if (len <= strlen(curr->name) && strncasecmp(curr->name, pattern, len) == 0)
                 return curr;
--- a/libnnchat.h	Sun Oct 24 21:52:19 2010 +0300
+++ b/libnnchat.h	Sun Oct 24 23:09:42 2010 +0300
@@ -66,7 +66,7 @@
 
 nn_userhash_t *nn_userhash_new(void);
 nn_user_t * nn_userhash_foreach(const nn_userhash_t *, int (*func)(const nn_user_t *));
-nn_user_t * nn_user_match(const nn_userhash_t *list, const char *str, const char *current);
+nn_user_t * nn_user_match(const nn_userhash_t *list, const char *str, const char *current, BOOL again);
 int         nn_userhash_insert(nn_userhash_t *, const char *name);
 int         nn_userhash_delete(nn_userhash_t *, const char *name);
 void        nn_userhash_free(nn_userhash_t *);
--- a/nnchat.c	Sun Oct 24 21:52:19 2010 +0300
+++ b/nnchat.c	Sun Oct 24 23:09:42 2010 +0300
@@ -45,6 +45,7 @@
         *optLogFilename = NULL,
         *setTarget = NULL,
         *optSite = "NN";
+char    optNickSep = ':';
 BOOL    optDaemon = FALSE;
 FILE    *optLogFile = NULL;
 WINDOW  *mainWin = NULL,
@@ -667,12 +668,13 @@
     static char *previous = NULL;
     static char *pattern = NULL;
     int mode = 0;
+    BOOL again = FALSE;
     char *str = buf->data;
     ssize_t endPos = 0, startPos = buf->pos;
 
     if (startPos > 1 && str[startPos - 1] == ' ') {
         startPos -= 2;
-        if (str[startPos] == ':')
+        if (str[startPos] == optNickSep)
             startPos--;
         if (startPos <= 0 || str[startPos] == ' ')
             return FALSE;
@@ -681,26 +683,36 @@
         mode = 1;
     } else
     if (str[startPos - 1] != ' ' && (startPos == buf->len || str[startPos] == ' ')) {
+        char *npattern;
+
         endPos = startPos;
         while (startPos > 0 && str[startPos - 1] != ' ') startPos--;
         while (endPos < buf->len && str[endPos] != ' ') endPos++;
         while (endPos > 0 && str[endPos] == ' ') endPos--;
-        if (str[endPos] == ':')
+        if (str[endPos] == optNickSep)
             return FALSE;
-        th_free(pattern);
-        pattern = nn_editbuf_get_string(buf, startPos, endPos);
-        th_free(previous); previous = NULL;
+        
+        /* Get pattern, check if it matches previous pattern and set 'again' flag */
+        npattern = nn_editbuf_get_string(buf, startPos, endPos);
+        if (pattern && npattern && strncasecmp(npattern, pattern, strlen(pattern)) == 0)
+            again = TRUE;
+        th_free(pattern); pattern = npattern;
+        if (!again) {
+            th_free(previous);
+            previous = NULL;
+        }
         mode = 2;
     } 
-//    printMsg("%d, %d <-> %d : '%s' (%d)\n", startPos, endPos, buf->len, pattern, mode);
+
+//    printMsg("%d, %d <-> %d : '%s' (%d, %d)\n", startPos, endPos, buf->len, pattern, mode, again);
 
     if (pattern) {
-        nn_user_t *user = nn_user_match(nnUsers, pattern, previous);
+        nn_user_t *user = nn_user_match(nnUsers, pattern, previous, again);
 
         if (user) {
             int i;
             char *c = user->name;
-//            printMsg("match '%s'\n", user->name);
+//            printMsg("match '%s' / prev '%s'\n", user->name, previous);
 
             for (i = startPos; i <= endPos; i++)
                 nn_editbuf_delete(buf, startPos);
@@ -708,9 +720,9 @@
             for (i = startPos; *c; i++, c++)
                 nn_editbuf_insert(buf, i, *c);
 
-            if (previous == NULL) {
+            if (previous == NULL || again) {
                 if (startPos == 0) {
-                    nn_editbuf_insert(buf, i++, ':');
+                    nn_editbuf_insert(buf, i++, optNickSep);
                     startPos++;
                 }
                 nn_editbuf_insert(buf, i++, ' ');