summary refs log tree commit diff stats
path: root/src/fe-gtk/xtext.c
diff options
context:
space:
mode:
authorRichardHitt <rbh00@netcom.com>2014-02-18 02:20:30 -0800
committerTingPing <tingping@tingping.se>2014-03-19 10:31:26 -0400
commit0487daf8655c8a353d2804fad7ffc7c5cf22f0a2 (patch)
tree082bb0d69985c62d20e1a7854ef1e8b9c8320583 /src/fe-gtk/xtext.c
parent99ee7b6ef815f591d95f7729a8537904d3bf0c25 (diff)
Fix three miscellaneous bugs in gtk_xtext_get_word()
	* Use utf8 functions for moving within string
	* Fix memory leak
	* Fix non-indent select problem
Diffstat (limited to 'src/fe-gtk/xtext.c')
-rw-r--r--src/fe-gtk/xtext.c46
1 files changed, 24 insertions, 22 deletions
diff --git a/src/fe-gtk/xtext.c b/src/fe-gtk/xtext.c
index 6a6b1215..ff28d25d 100644
--- a/src/fe-gtk/xtext.c
+++ b/src/fe-gtk/xtext.c
@@ -1499,41 +1499,43 @@ gtk_xtext_get_word (GtkXText * xtext, int x, int y, textentry ** ret_ent,
 	textentry *ent;
 	int offset;
 	unsigned char *word;
+	unsigned char *last, *end;
 	int len;
 	int out_of_bounds = 0;
 	int len_to_offset = 0;
 
 	ent = gtk_xtext_find_char (xtext, x, y, &offset, &out_of_bounds);
-	if (!ent)
-		return 0;
-
-	if (out_of_bounds)
-		return 0;
-
-	if (offset == ent->str_len)
-		return 0;
-
-	if (offset < 1)
-		return 0;
-
-	/*offset--;*/	/* FIXME: not all chars are 1 byte */
+	if (ent == NULL || out_of_bounds || offset < 0 || offset >= ent->str_len)
+		return NULL;
 
 	word = ent->str + offset;
-
-	while (!is_del (*word) && word != ent->str)
+	while ((word = g_utf8_find_prev_char (ent->str, word)))
 	{
-		word--;
-		len_to_offset++;
+		if (is_del (*word))
+		{
+			word++;
+			len_to_offset--;
+			break;
+		}
+		len_to_offset += charlen (word);
 	}
-	word++;
-	len_to_offset--;
+	if (!word)
+		word = ent->str;
 
 	/* remove color characters from the length */
-	gtk_xtext_strip_color (word, len_to_offset, xtext->scratch_buffer, &len_to_offset, slp, FALSE);
+	gtk_xtext_strip_color (word, len_to_offset, xtext->scratch_buffer, &len_to_offset, NULL, FALSE);
 
+	last = word;
+	end = ent->str + ent->str_len;
 	len = 0;
-	while (!is_del (word[len]) && len != ent->str_len)
-		len++;
+	do
+	{
+		if (is_del (*last))
+			break;
+		len += charlen (last);
+		last = g_utf8_find_next_char (last, end);
+	}
+	while (last);
 
 	if (len > 0 && word[len-1]=='.')
 		len--;