# HG changeset patch # User Matti Hamalainen # Date 1287738458 -10800 # Node ID 8037a3a7e4918f8404e95abee2f7dbde9536e99e # Parent c587a99e2096efb00d7d293a8b77ca55429812dd Implement username tab-completion. It's still buggy, tho. diff -r c587a99e2096 -r 8037a3a7e491 nnchat.c --- a/nnchat.c Fri Oct 22 12:07:15 2010 +0300 +++ b/nnchat.c Fri Oct 22 12:07:38 2010 +0300 @@ -460,7 +460,7 @@ p = nn_dbldecode_str(str); if (!p) return -1; - nn_userhash_insert(nnUsers, str); + nn_userhash_insert(nnUsers, p); printMsg("! ˝3˝%s˝0˝ ˝2˝ADDED.˝0˝\n", p); th_free(p); @@ -480,7 +480,8 @@ p = nn_dbldecode_str(str); if (!p) return -1; - nn_userhash_delete(nnUsers, str); + nn_userhash_delete(nnUsers, p); + printMsg("! ˝3˝%s˝0˝ ˝1˝DELETED.˝0˝\n", p); th_free(p); return 0; @@ -625,12 +626,64 @@ return TRUE; } - -int printUserFunc(const nn_user_t *user) +void performTabCompletion(nn_editbuf_t *buf) { - if (user) - printMsgC("%s, ", user->name); - return 0; + static char *previous = NULL; + static char *matsi = NULL; + char *s = NULL, *b = buf->data; + ssize_t pos2, pos1 = buf->pos; + + if (pos1 > 0) pos1--; + + if (pos1 > 0 && b[pos1 - 1] == ':') { + pos1 -= 2; + pos2 = pos1; + while (pos1 > 0 && b[pos1 - 1] != ' ') pos1--; + s = nn_editbuf_get_string(buf, pos1, pos2); + } else + if (b[pos1] != ' ' && b[pos1] != ':') { + pos2 = pos1; + while (pos1 > 0 && b[pos1 - 1] != ' ') pos1--; + while (pos2 < buf->len && b[pos2] != ' ') pos2++; + while (pos2 > 0 && b[pos2] == ' ') pos2--; + s = nn_editbuf_get_string(buf, pos1, pos2); + th_free(previous); + th_free(matsi); + matsi = NULL; + previous = NULL; + } + + if (s) { + nn_user_t *m; + if (matsi) + m = nn_user_match(nnUsers, matsi, previous); + else + m = nn_user_match(nnUsers, s, previous); + + if (m) { + int i; + char *c = m->name; + printMsg("match '%s'\n", m->name); + + for (i = pos1; i <= pos2; i++) + nn_editbuf_delete(buf, pos1); + + for (i = pos1; *c; i++, c++) + nn_editbuf_insert(buf, i, *c); + + if (previous == NULL) { + nn_editbuf_insert(buf, i+1, ':'); + nn_editbuf_insert(buf, i+2, ' '); + } + nn_editbuf_setpos(buf, pos1 + 2 + strlen(m->name)); + + if (previous == NULL) { + previous = th_strdup(m->name); + matsi = th_strdup(s); + } + } + th_free(s); + } } int main(int argc, char *argv[]) @@ -994,27 +1047,8 @@ break; case 0x09: /* Tab = complete username */ - { - ssize_t pos = editBuf->pos; - while (pos > 0 && !isspace((int) editBuf->data[pos - 1])) - pos--; - - if (!isspace(editBuf->data[pos])) { - char *str = nn_editbuf_get_string(editBuf, pos, editBuf->pos); - if (str) { - static char *c_prev = NULL; - nn_user_t *c_curr = nn_user_match(nnUsers, str, c_prev); - th_free(c_prev); - c_prev = NULL; - - if (c_curr) { - c_prev = th_strdup(c_curr->name); - printMsg("Lol: %s\n", c_curr->name); - } - - } - } - } + performTabCompletion(editBuf); + update = TRUE; break; case 0x0c: /* Ctrl + L */