# HG changeset patch # User Matti Hamalainen # Date 1337992952 -10800 # Node ID 2d650408f5de432e9c26e2827cfb7844766ad7d6 # Parent 3396acd40147f11d9aa6e48f0a5fdd1060be24b9 Modularize tab completion code a bit. diff -r 3396acd40147 -r 2d650408f5de main.c --- a/main.c Sat May 26 03:40:57 2012 +0300 +++ b/main.c Sat May 26 03:42:32 2012 +0300 @@ -1340,13 +1340,34 @@ } +void tabCompletionProd(nn_editbuf_t *buf, size_t *pi, const size_t startPos, const size_t endPos, char *c) +{ + size_t i; + + for (i = startPos; i <= endPos; i++) + nn_editbuf_delete(buf, startPos); + + for (i = startPos; *c; i++, c++) + nn_editbuf_insert(buf, i, *c); + + *pi = i; +} + + +void tabCompletionFinish(nn_editbuf_t *buf, char **previous, const size_t startPos, const char *name) +{ + nn_editbuf_setpos(buf, startPos + 1 + strlen(name)); + th_free(*previous); + *previous = th_strdup(name); +} + + BOOL performTabCompletion(nn_editbuf_t *buf) { static char *previous = NULL, *pattern = NULL; BOOL again = FALSE, hasSeparator = FALSE, - newPattern = FALSE, hasSpace = FALSE, isCommand; + hasSpace, newPattern = FALSE, isCommand; char *str = buf->data; - int mode = 0; size_t endPos, startPos = buf->pos; // previous word @@ -1355,7 +1376,6 @@ startPos -= 2; endPos = startPos; while (startPos > 0 && str[startPos - 1] != ' ') startPos--; - mode = 1; } else // middle of a word, new pattern @@ -1365,7 +1385,6 @@ while (startPos > 0 && str[startPos - 1] != ' ') startPos--; while (endPos < buf->len - 1 && str[endPos + 1] != ' ') endPos++; newPattern = TRUE; - mode = 2; } else // previous word, new pattern @@ -1375,7 +1394,6 @@ endPos = startPos; while (startPos > 0 && str[startPos - 1] != ' ') startPos--; newPattern = TRUE; - mode = 3; } else { @@ -1399,10 +1417,8 @@ hasSeparator = TRUE; } - if (buf->pos > 0 && str[buf->pos - 1] == ' ') - hasSpace = TRUE; - if (buf->pos <= buf->len && str[buf->pos] == ' ') - hasSpace = TRUE; + hasSpace = (buf->pos > 0 && str[buf->pos - 1] == ' ') || + (buf->pos <= buf->len && str[buf->pos] == ' '); if (newPattern) { @@ -1451,31 +1467,21 @@ if (user) { size_t i; - char *c = user->name; - if (optDebug) - printMsg(currWin, "match='%s' / prev='%s'\n", user->name, previous); - - for (i = startPos; i <= endPos; i++) - nn_editbuf_delete(buf, startPos); - - for (i = startPos; *c; i++, c++) - nn_editbuf_insert(buf, i, *c); + tabCompletionProd(buf, &i, startPos, endPos, user->name); if (!hasSeparator && startPos == 0) { nn_editbuf_insert(buf, i++, optNickSep); startPos++; } + else if (hasSeparator) startPos++; + if (!hasSpace) nn_editbuf_insert(buf, i++, ' '); - nn_editbuf_setpos(buf, startPos + 1 + strlen(user->name)); - - th_free(previous); - previous = th_strdup(user->name); - + tabCompletionFinish(buf, &previous, startPos, user->name); return TRUE; } }