comparison nnchat.c @ 107:8037a3a7e491

Implement username tab-completion. It's still buggy, tho.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 22 Oct 2010 12:07:38 +0300
parents 06e6ac51e3f2
children f323b137ca08
comparison
equal deleted inserted replaced
106:c587a99e2096 107:8037a3a7e491
458 *s = 0; 458 *s = 0;
459 459
460 p = nn_dbldecode_str(str); 460 p = nn_dbldecode_str(str);
461 if (!p) return -1; 461 if (!p) return -1;
462 462
463 nn_userhash_insert(nnUsers, str); 463 nn_userhash_insert(nnUsers, p);
464 464
465 printMsg("! ˝3˝%s˝0˝ ˝2˝ADDED.˝0˝\n", p); 465 printMsg("! ˝3˝%s˝0˝ ˝2˝ADDED.˝0˝\n", p);
466 th_free(p); 466 th_free(p);
467 return 0; 467 return 0;
468 } 468 }
478 *s = 0; 478 *s = 0;
479 479
480 p = nn_dbldecode_str(str); 480 p = nn_dbldecode_str(str);
481 if (!p) return -1; 481 if (!p) return -1;
482 482
483 nn_userhash_delete(nnUsers, str); 483 nn_userhash_delete(nnUsers, p);
484
484 printMsg("! ˝3˝%s˝0˝ ˝1˝DELETED.˝0˝\n", p); 485 printMsg("! ˝3˝%s˝0˝ ˝1˝DELETED.˝0˝\n", p);
485 th_free(p); 486 th_free(p);
486 return 0; 487 return 0;
487 } 488 }
488 489
623 scrollok(mainWin, 1); 624 scrollok(mainWin, 1);
624 625
625 return TRUE; 626 return TRUE;
626 } 627 }
627 628
628 629 void performTabCompletion(nn_editbuf_t *buf)
629 int printUserFunc(const nn_user_t *user) 630 {
630 { 631 static char *previous = NULL;
631 if (user) 632 static char *matsi = NULL;
632 printMsgC("%s, ", user->name); 633 char *s = NULL, *b = buf->data;
633 return 0; 634 ssize_t pos2, pos1 = buf->pos;
635
636 if (pos1 > 0) pos1--;
637
638 if (pos1 > 0 && b[pos1 - 1] == ':') {
639 pos1 -= 2;
640 pos2 = pos1;
641 while (pos1 > 0 && b[pos1 - 1] != ' ') pos1--;
642 s = nn_editbuf_get_string(buf, pos1, pos2);
643 } else
644 if (b[pos1] != ' ' && b[pos1] != ':') {
645 pos2 = pos1;
646 while (pos1 > 0 && b[pos1 - 1] != ' ') pos1--;
647 while (pos2 < buf->len && b[pos2] != ' ') pos2++;
648 while (pos2 > 0 && b[pos2] == ' ') pos2--;
649 s = nn_editbuf_get_string(buf, pos1, pos2);
650 th_free(previous);
651 th_free(matsi);
652 matsi = NULL;
653 previous = NULL;
654 }
655
656 if (s) {
657 nn_user_t *m;
658 if (matsi)
659 m = nn_user_match(nnUsers, matsi, previous);
660 else
661 m = nn_user_match(nnUsers, s, previous);
662
663 if (m) {
664 int i;
665 char *c = m->name;
666 printMsg("match '%s'\n", m->name);
667
668 for (i = pos1; i <= pos2; i++)
669 nn_editbuf_delete(buf, pos1);
670
671 for (i = pos1; *c; i++, c++)
672 nn_editbuf_insert(buf, i, *c);
673
674 if (previous == NULL) {
675 nn_editbuf_insert(buf, i+1, ':');
676 nn_editbuf_insert(buf, i+2, ' ');
677 }
678 nn_editbuf_setpos(buf, pos1 + 2 + strlen(m->name));
679
680 if (previous == NULL) {
681 previous = th_strdup(m->name);
682 matsi = th_strdup(s);
683 }
684 }
685 th_free(s);
686 }
634 } 687 }
635 688
636 int main(int argc, char *argv[]) 689 int main(int argc, char *argv[])
637 { 690 {
638 int tmpSocket = -1, curVis = ERR, updateCount = 0; 691 int tmpSocket = -1, curVis = ERR, updateCount = 0;
992 } 1045 }
993 update = TRUE; 1046 update = TRUE;
994 break; 1047 break;
995 1048
996 case 0x09: /* Tab = complete username */ 1049 case 0x09: /* Tab = complete username */
997 { 1050 performTabCompletion(editBuf);
998 ssize_t pos = editBuf->pos; 1051 update = TRUE;
999 while (pos > 0 && !isspace((int) editBuf->data[pos - 1]))
1000 pos--;
1001
1002 if (!isspace(editBuf->data[pos])) {
1003 char *str = nn_editbuf_get_string(editBuf, pos, editBuf->pos);
1004 if (str) {
1005 static char *c_prev = NULL;
1006 nn_user_t *c_curr = nn_user_match(nnUsers, str, c_prev);
1007 th_free(c_prev);
1008 c_prev = NULL;
1009
1010 if (c_curr) {
1011 c_prev = th_strdup(c_curr->name);
1012 printMsg("Lol: %s\n", c_curr->name);
1013 }
1014
1015 }
1016 }
1017 }
1018 break; 1052 break;
1019 1053
1020 case 0x0c: /* Ctrl + L */ 1054 case 0x0c: /* Ctrl + L */
1021 redrawwin(mainWin); 1055 redrawwin(mainWin);
1022 redrawwin(statusWin); 1056 redrawwin(statusWin);