>From bf05a2bedd51bc27cfddc4165f3f269aac9e7e25 Mon Sep 17 00:00:00 2001 From: "sumedh.pendurkar" Date: Sun, 30 Oct 2016 22:48:27 +0530 Subject: [PATCH 4/4] complete:now works with utf8 --- src/text.c | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/text.c b/src/text.c index e494545..828f989 100644 --- a/src/text.c +++ b/src/text.c @@ -50,8 +50,8 @@ static filestruct *jusbottom = NULL; static int pletion_x = 0; /* The x position in pletion_line of the last found completion. */ -static int pleted_length = 0; - /* The number of bytes added in the last completion. */ +static int pleted_mbchars = 0; + /* The number of characters added in the last completion. */ static completion_word *list_of_completions; /* A linked list of the words that are possible completions. */ @@ -3696,19 +3696,23 @@ void do_verbatim_input(void) char *copy_completion(char *check_line, int start) { char *word; - int i = start, j = 0; + int i = start, j = 0, len_mbchar = 0; int len_of_word = 0; + pleted_mbchars = 0; /* Calculate the length of word by travelling till end of word*/ - while (is_word_mbchar(&check_line[i++], FALSE)) - len_of_word++; - + while (is_word_mbchar(&check_line[i], FALSE)) { + len_mbchar = move_mbright(check_line, i); + len_of_word += len_mbchar - i; + i = len_mbchar; + pleted_mbchars++; + } word = (char *)nmalloc((len_of_word + 1) * sizeof(char)); i = start; /* Simply copy the word */ - while (is_word_mbchar(&check_line[i], FALSE)) + while (j < len_of_word) word[j++] = check_line[i++]; word[j] = '\0'; @@ -3722,7 +3726,7 @@ char *copy_completion(char *check_line, int start) void complete_a_word(void) { char *shard, *completion = NULL; - int to_find_start_pos, shard_length = 0; + int to_find_start_pos, shard_length = 0, shard_mbchars = 0; int i = 0, j = 0; completion_word *some_word; @@ -3748,7 +3752,7 @@ void complete_a_word(void) wnoutrefresh(bottomwin); } else { /* Remove the earlier completion from the buffer. */ - for (i = 0; i < pleted_length; i++) + for (i = 0; i < pleted_mbchars; i++) do_backspace(); /* Pop the last two undo items off the stack, because the completion @@ -3762,10 +3766,19 @@ void complete_a_word(void) some_word = list_of_completions; /* Find the start of the fragment that the user typed. */ - while (--to_find_start_pos >= 0) - if (!is_word_mbchar(&openfile->current->data[to_find_start_pos], FALSE)) + to_find_start_pos = move_mbleft(openfile->current->data, to_find_start_pos); + while (to_find_start_pos >= 0) { + if (!is_word_mbchar(&openfile->current->data[to_find_start_pos], FALSE) + || !to_find_start_pos) break; - to_find_start_pos++; + to_find_start_pos = move_mbleft(openfile->current->data, to_find_start_pos); + shard_mbchars++; + } + /* Taking care if we reach the start of line */ + if (to_find_start_pos) + to_find_start_pos++; + else + shard_mbchars++; /* If there is no word fragment before the cursor, do nothing. */ if (to_find_start_pos == openfile->current_x) { @@ -3794,8 +3807,9 @@ void complete_a_word(void) /* This loop traverses in shard and match it against pletion_line * It would get out of loop if j < shard_length which suggests that match is found * or if end of word is encountered */ - for (j = 0; (i == 0 || !is_word_mbchar(&pletion_line->data[i - 1], FALSE)) && - j < shard_length; j++) + for (j = 0; (i == 0 || + !is_word_mbchar(&pletion_line->data[move_mbleft(pletion_line->data, i)], FALSE)) && + j < shard_length; j++) /* Also break even if a character mismatches */ if (shard[j] != pletion_line->data[i + j]) @@ -3825,8 +3839,8 @@ void complete_a_word(void) list_of_completions = some_word; /* Inject the completion into the buffer. */ - pleted_length = strlen(completion) - shard_length; - do_output(&some_word->word[shard_length], pleted_length, FALSE); + pleted_mbchars -= shard_mbchars; + do_output(&some_word->word[shard_length], strlen(completion) - shard_length, FALSE); pletion_x = i; free(shard); -- 2.7.4