Thursday, 21 June 2012

Re: Patch: desktop/textarea.c, add text fragment select on double click

On Thu, 2012-06-21 at 22:34 +0200, Ole wrote:
> Am Donnerstag, den 21.06.2012, 21:17 +0200 schrieb Ole
> <ole@monochrom.net>:
>
>
> > Attached is a patch that hopefully has no more style glitches.
> > If there is more, please let me know which style rule is broken.

I don't quite understand the rationale behind the separator characters
you have chosen. Could you explain them?

The textarea_select_fragment function looks wrong to me. There seems to
be some confusion between character indices and byte offsets.

The following should work better:


static bool textarea_select_fragment(struct text_area *ta)
{
static const char *sep = "/.\r\n";
int c_caret_pos, index;
size_t b_start, b_end;
int c_sel_start = 0, c_sel_end = ta->text_len;

/* Get character index of caret */
c_caret_pos = textarea_get_caret(ta);
if (c_caret_pos < 0) {
return false;
}

/* Compute byte offset of caret position */
for (b_start = 0, index = 0; c_caret_pos-- > 0;
b_start = utf8_next(ta->text, ta->text_len,
b_start),
index++) {
/* Cache the character offset of the last separator */
if (strchr(sep, ta->text[b_start]) != NULL) {
c_sel_start = index;
}
}

/* Search for next separator, if any */
for (b_end = b_start; b_end < ta->text_len;
b_end = utf8_next(ta->text, ta->text_len,
b_end),
index++) {
if (strchr(sep, ta->text[b_end]) != NULL) {
c_sel_end = index;
break;
}
}

/* Add one to start to skip over separator */
if (c_sel_start + 1 < c_sel_end) {
textarea_select(ta, c_sel_start + 1, c_sel_end);
return true;
}

return false;
}



J.

No comments:

Post a Comment