diff --git a/doc/arif_fetch.3 b/doc/arif_fetch.3 index 0d91039..7819b1f 100644 --- a/doc/arif_fetch.3 +++ b/doc/arif_fetch.3 @@ -39,6 +39,7 @@ type is defined as: struct arif_cand { char const *text; int len; + int replace_start; int replace_len; char const *transform; int transform_len; @@ -57,7 +58,8 @@ is its length in bytes. Member .I replace_len is the number of leading bytes of the original text that are meant to -be replaced by the candidate. +be replaced by the candidate, starting from byte offset +.IR replace_start . .PP Should the original text passed to .BR arif_query () diff --git a/examples/arif_rime.c b/examples/arif_rime.c index 4e3d37b..33d09c5 100644 --- a/examples/arif_rime.c +++ b/examples/arif_rime.c @@ -227,13 +227,14 @@ copy_candidate ( char const *text = menu->candidates[candidate_idx].text; int text_len = strlen(text); char const *line = composition->preedit; - int line_len = strlen(line); + int line_len = composition->length; char *buf = malloc(text_len + line_len); assert(buf != NULL); dest->text = memcpy(buf, text, text_len); dest->len = text_len; + dest->replace_start = composition->sel_start; dest->replace_len = composition->sel_end - composition->sel_start; dest->transform = memcpy(buf + text_len, line, line_len); dest->transform_len = line_len; diff --git a/include/arif.h b/include/arif.h index d040585..f55c808 100644 --- a/include/arif.h +++ b/include/arif.h @@ -64,6 +64,7 @@ typedef int (arif_engine_query_func) ( struct arif_cand { char const *text; int len; + int replace_start; int replace_len; char const *transform; int transform_len; diff --git a/src/arif.c b/src/arif.c index 43a6be0..e7465c9 100644 --- a/src/arif.c +++ b/src/arif.c @@ -131,6 +131,7 @@ copy_candidate ( *dest = (struct arif_cand) { .text = src->text, .len = src->len, + .replace_start = src->replace_start, .replace_len = src->replace_len, .transform = src->transform, .transform_len = src->transform_len, diff --git a/src/arif_rl.c b/src/arif_rl.c index ed9bb03..f9d22a1 100644 --- a/src/arif_rl.c +++ b/src/arif_rl.c @@ -87,12 +87,19 @@ arif_rl_complete( char *match = malloc(sizeof(char) * (len + 1)); assert(match != NULL); - memcpy(match, text, skip); - memcpy(match + skip, cand->text, cand->len); - memcpy(match + skip + cand->len, line + cand->replace_len, - len - cand->len - skip); match[len] = '\0'; - comp_list[idx] = match; + comp_list[idx] = memcpy(match, text, skip); + + int offset = skip; + memcpy(match + offset, line, cand->replace_start); + + offset += cand->replace_start; + memcpy(match + offset, cand->text, cand->len); + + offset += cand->len; + memcpy(match + offset, + line + cand->replace_start + cand->replace_len, + len - offset); } comp_list[num + 1] = NULL;