1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-01 17:47:52 +02:00

Fix tab-complete bug causing buffer to sometimes clear

This commit is contained in:
Jfreegman 2016-06-11 23:49:56 -04:00
parent cf16849b37
commit 9fcbc3bde0
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63

View File

@ -53,12 +53,14 @@ static void print_matches(ToxWindow *self, Tox *m, const void *list, int n_items
} }
/* puts match in match buffer. if more than one match, add first n chars that are identical. /* puts match in match buffer. if more than one match, add first n chars that are identical.
e.g. if matches contains: [foo, foobar, foe] we put fo in matches. */ * e.g. if matches contains: [foo, foobar, foe] we put fo in match.
static void get_str_match(ToxWindow *self, char *match, char (*matches)[MAX_STR_SIZE], int n) *
* Returns the length of the match.
*/
static size_t get_str_match(ToxWindow *self, char *match, size_t match_sz, char (*matches)[MAX_STR_SIZE], int n)
{ {
if (n == 1) { if (n == 1) {
strcpy(match, matches[0]); return snprintf(match, match_sz, "%s", matches[0]);
return;
} }
int i; int i;
@ -71,14 +73,14 @@ static void get_str_match(ToxWindow *self, char *match, char (*matches)[MAX_STR_
char ch2 = matches[j][i]; char ch2 = matches[j][i];
if (ch1 != ch2 || !ch1) { if (ch1 != ch2 || !ch1) {
strcpy(match, matches[0]); snprintf(match, match_sz, "%s", matches[0]);
match[i] = '\0'; match[i] = '\0';
return; return i;
} }
} }
} }
strcpy(match, matches[0]); return snprintf(match, match_sz, "%s", matches[0]);
} }
/* looks for all instances in list that begin with the last entered word in line according to pos, /* looks for all instances in list that begin with the last entered word in line according to pos,
@ -164,8 +166,11 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size)
print_matches(self, NULL, matches, n_matches, MAX_STR_SIZE); print_matches(self, NULL, matches, n_matches, MAX_STR_SIZE);
char match[MAX_STR_SIZE]; char match[MAX_STR_SIZE];
get_str_match(self, match, matches, n_matches); size_t match_len = get_str_match(self, match, sizeof(match), matches, n_matches);
size_t match_len = strlen(match);
if (match_len == 0) {
return 0;
}
if (dir_search) { if (dir_search) {
if (n_matches == 1) if (n_matches == 1)
@ -180,15 +185,15 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size)
int n_endchrs = strlen(endchrs); int n_endchrs = strlen(endchrs);
int strt = ctx->pos - s_len; int strt = ctx->pos - s_len;
int diff = match_len - s_len + n_endchrs; int diff = match_len - s_len + n_endchrs;
if (ctx->len + diff >= MAX_STR_SIZE) if (ctx->len + diff >= MAX_STR_SIZE)
return -1; return -1;
char tmpend[MAX_STR_SIZE]; char tmpend[MAX_STR_SIZE];
snprintf(tmpend, sizeof(tmpend), "%s", &ubuf[ctx->pos]); snprintf(tmpend, sizeof(tmpend), "%s", &ubuf[ctx->pos]);
if (match_len + n_endchrs + strlen(tmpend) >= sizeof(ubuf)) if (match_len + n_endchrs + strlen(tmpend) >= sizeof(ubuf)) {
return -1; return -1;
}
strcpy(&ubuf[strt], match); strcpy(&ubuf[strt], match);
strcpy(&ubuf[strt + match_len], endchrs); strcpy(&ubuf[strt + match_len], endchrs);
@ -197,8 +202,9 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size)
/* convert to widechar and copy back to original buf */ /* convert to widechar and copy back to original buf */
wchar_t newbuf[MAX_STR_SIZE]; wchar_t newbuf[MAX_STR_SIZE];
if (mbs_to_wcs_buf(newbuf, ubuf, sizeof(newbuf) / sizeof(wchar_t)) == -1) if (mbs_to_wcs_buf(newbuf, ubuf, sizeof(newbuf) / sizeof(wchar_t)) == -1) {
return -1; return -1;
}
wcscpy(ctx->line, newbuf); wcscpy(ctx->line, newbuf);