Mercurial > hg > nnchat
comparison 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 |
comparison
equal
deleted
inserted
replaced
115:300a6ea4aff3 | 116:741e45592522 |
---|---|
43 *optUserName2 = NULL, | 43 *optUserName2 = NULL, |
44 *optPassword = NULL, | 44 *optPassword = NULL, |
45 *optLogFilename = NULL, | 45 *optLogFilename = NULL, |
46 *setTarget = NULL, | 46 *setTarget = NULL, |
47 *optSite = "NN"; | 47 *optSite = "NN"; |
48 char optNickSep = ':'; | |
48 BOOL optDaemon = FALSE; | 49 BOOL optDaemon = FALSE; |
49 FILE *optLogFile = NULL; | 50 FILE *optLogFile = NULL; |
50 WINDOW *mainWin = NULL, | 51 WINDOW *mainWin = NULL, |
51 *statusWin = NULL, | 52 *statusWin = NULL, |
52 *editWin = NULL; | 53 *editWin = NULL; |
665 BOOL performTabCompletion(nn_editbuf_t *buf) | 666 BOOL performTabCompletion(nn_editbuf_t *buf) |
666 { | 667 { |
667 static char *previous = NULL; | 668 static char *previous = NULL; |
668 static char *pattern = NULL; | 669 static char *pattern = NULL; |
669 int mode = 0; | 670 int mode = 0; |
671 BOOL again = FALSE; | |
670 char *str = buf->data; | 672 char *str = buf->data; |
671 ssize_t endPos = 0, startPos = buf->pos; | 673 ssize_t endPos = 0, startPos = buf->pos; |
672 | 674 |
673 if (startPos > 1 && str[startPos - 1] == ' ') { | 675 if (startPos > 1 && str[startPos - 1] == ' ') { |
674 startPos -= 2; | 676 startPos -= 2; |
675 if (str[startPos] == ':') | 677 if (str[startPos] == optNickSep) |
676 startPos--; | 678 startPos--; |
677 if (startPos <= 0 || str[startPos] == ' ') | 679 if (startPos <= 0 || str[startPos] == ' ') |
678 return FALSE; | 680 return FALSE; |
679 endPos = startPos; | 681 endPos = startPos; |
680 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; | 682 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; |
681 mode = 1; | 683 mode = 1; |
682 } else | 684 } else |
683 if (str[startPos - 1] != ' ' && (startPos == buf->len || str[startPos] == ' ')) { | 685 if (str[startPos - 1] != ' ' && (startPos == buf->len || str[startPos] == ' ')) { |
686 char *npattern; | |
687 | |
684 endPos = startPos; | 688 endPos = startPos; |
685 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; | 689 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; |
686 while (endPos < buf->len && str[endPos] != ' ') endPos++; | 690 while (endPos < buf->len && str[endPos] != ' ') endPos++; |
687 while (endPos > 0 && str[endPos] == ' ') endPos--; | 691 while (endPos > 0 && str[endPos] == ' ') endPos--; |
688 if (str[endPos] == ':') | 692 if (str[endPos] == optNickSep) |
689 return FALSE; | 693 return FALSE; |
690 th_free(pattern); | 694 |
691 pattern = nn_editbuf_get_string(buf, startPos, endPos); | 695 /* Get pattern, check if it matches previous pattern and set 'again' flag */ |
692 th_free(previous); previous = NULL; | 696 npattern = nn_editbuf_get_string(buf, startPos, endPos); |
697 if (pattern && npattern && strncasecmp(npattern, pattern, strlen(pattern)) == 0) | |
698 again = TRUE; | |
699 th_free(pattern); pattern = npattern; | |
700 if (!again) { | |
701 th_free(previous); | |
702 previous = NULL; | |
703 } | |
693 mode = 2; | 704 mode = 2; |
694 } | 705 } |
695 // printMsg("%d, %d <-> %d : '%s' (%d)\n", startPos, endPos, buf->len, pattern, mode); | 706 |
707 // printMsg("%d, %d <-> %d : '%s' (%d, %d)\n", startPos, endPos, buf->len, pattern, mode, again); | |
696 | 708 |
697 if (pattern) { | 709 if (pattern) { |
698 nn_user_t *user = nn_user_match(nnUsers, pattern, previous); | 710 nn_user_t *user = nn_user_match(nnUsers, pattern, previous, again); |
699 | 711 |
700 if (user) { | 712 if (user) { |
701 int i; | 713 int i; |
702 char *c = user->name; | 714 char *c = user->name; |
703 // printMsg("match '%s'\n", user->name); | 715 // printMsg("match '%s' / prev '%s'\n", user->name, previous); |
704 | 716 |
705 for (i = startPos; i <= endPos; i++) | 717 for (i = startPos; i <= endPos; i++) |
706 nn_editbuf_delete(buf, startPos); | 718 nn_editbuf_delete(buf, startPos); |
707 | 719 |
708 for (i = startPos; *c; i++, c++) | 720 for (i = startPos; *c; i++, c++) |
709 nn_editbuf_insert(buf, i, *c); | 721 nn_editbuf_insert(buf, i, *c); |
710 | 722 |
711 if (previous == NULL) { | 723 if (previous == NULL || again) { |
712 if (startPos == 0) { | 724 if (startPos == 0) { |
713 nn_editbuf_insert(buf, i++, ':'); | 725 nn_editbuf_insert(buf, i++, optNickSep); |
714 startPos++; | 726 startPos++; |
715 } | 727 } |
716 nn_editbuf_insert(buf, i++, ' '); | 728 nn_editbuf_insert(buf, i++, ' '); |
717 } | 729 } |
718 nn_editbuf_setpos(buf, startPos + 2 + strlen(user->name)); | 730 nn_editbuf_setpos(buf, startPos + 2 + strlen(user->name)); |