Friday, 22 June 2012

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

Index: textarea.c
===================================================================
--- textarea.c (Revision 13970)
+++ textarea.c (Arbeitskopie)
@@ -121,6 +121,7 @@
static bool textarea_set_caret_xy(struct text_area *ta, int x, int y);
static bool textarea_scroll_visible(struct text_area *ta);
static bool textarea_select(struct text_area *ta, int c_start, int c_end);
+static bool textarea_select_fragment (struct text_area *ta);
static void textarea_normalise_text(struct text_area *ta,
unsigned int b_start, unsigned int b_len);

@@ -1335,6 +1336,12 @@
ta->vis_height);
}
}
+ else if (mouse & BROWSER_MOUSE_DOUBLE_CLICK) {
+ if (!(ta->flags & TEXTAREA_READONLY)) {
+ textarea_set_caret_xy(ta, x, y);
+ return textarea_select_fragment(ta);
+ }
+ }
else if (mouse & BROWSER_MOUSE_DRAG_1) {
ta->drag_start_char = textarea_get_xy_offset(ta, x, y);
if (!(ta->flags & TEXTAREA_READONLY))
@@ -1406,6 +1413,57 @@


/**
+ * Selects a text fragment, relative to current caret position.
+ *
+ * \param ta Text area
+ * \return true on success false otherwise
+ */
+static bool textarea_select_fragment(struct text_area * ta)
+{
+ int caret_pos, sel_start=0, sel_end=0, index;
+ size_t b_start, b_end;
+
+ /* Fragment seperators must be suitable for URLs and ordinary text */
+ static const char * sep = " /:.\r\n";
+
+ caret_pos = textarea_get_caret(ta);
+ if (caret_pos < 0) {
+ return(false);
+ }
+
+ /* Compute byte offset of caret position */
+ for (b_start = 0, index = 0; index<caret_pos;
+ 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) {
+ /* Add one to start to skip over separator */
+ sel_start = index+1;
+ }
+ }
+
+ /* 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) {
+ sel_end = index;
+ break;
+ }
+ }
+
+ if (sel_start < sel_end) {
+ textarea_select(ta, sel_start, sel_end);
+ return true;
+ }
+
+ return(false);
+}
+
+
+/**
* Normalises any line endings within the text, replacing CRLF or CR with
* LF as necessary. If the textarea is single line, then all linebreaks are
* converted into spaces.
>Am Freitag, den 22.06.2012, 00:33 +0200 schrieb John-Mark Bell
> <jmb@netsurf-browser.org>:

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

It's just what came to my mind when thinking about text element
separators.
Also I don't wanted to have so much characters because then strchr()
takes a
bit longer.

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

Attached is a slightly adjusted version.

Greets,
Ole

No comments:

Post a Comment