diff nnchat.c @ 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
line wrap: on
line diff
--- 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++, ' ');