[anjuta/gnome-2-32] sourceview: bgo#636312 - Double click fails to select entire word if it contains _ or digits



commit 1df2c97444c90629da7281e08c45ea1ca04a75aa
Author: Johannes Schmid <jhs gnome org>
Date:   Sat Dec 4 12:27:29 2010 +0100

    sourceview: bgo#636312 - Double click fails to select entire word if it contains _ or digits

 plugins/sourceview/anjuta-view.c |   66 +++++++++++++++++++++++++++++++++++++-
 plugins/sourceview/anjuta-view.h |    3 ++
 plugins/sourceview/sourceview.c  |   59 ++++------------------------------
 3 files changed, 75 insertions(+), 53 deletions(-)
---
diff --git a/plugins/sourceview/anjuta-view.c b/plugins/sourceview/anjuta-view.c
index 5b44da6..3327d63 100644
--- a/plugins/sourceview/anjuta-view.c
+++ b/plugins/sourceview/anjuta-view.c
@@ -716,6 +716,16 @@ anjuta_view_button_press_event	(GtkWidget *widget, GdkEventButton *event)
   
 	switch(event->button)
 	{
+		case 1: /* Handle double clicks to select complete word */
+			if (event->type == GDK_2BUTTON_PRESS)
+			{
+				GtkTextIter start, end;
+				anjuta_view_get_current_word (view, &start, &end);
+				gtk_text_buffer_select_range (gtk_text_iter_get_buffer (&start),
+				                              &start, &end);
+				return TRUE;
+			}
+			break;
 		case 3: /* Right Button */
 		{
 			GtkTextBuffer* buffer = GTK_TEXT_BUFFER (view->priv->sv->priv->document);
@@ -741,6 +751,60 @@ anjuta_view_button_press_event	(GtkWidget *widget, GdkEventButton *event)
 			return TRUE;
 		}
 		default:
-			return (* GTK_WIDGET_CLASS (anjuta_view_parent_class)->button_press_event)(widget, event);
+			break;
+	}
+	return (* GTK_WIDGET_CLASS (anjuta_view_parent_class)->button_press_event)(widget, event);
+}
+
+static gboolean
+wordcharacters_contains (gchar c)
+{
+	if (g_ascii_isalnum(c) ||
+	    c == '_')
+		return TRUE;
+	else
+		return FALSE;
+}
+
+void
+anjuta_view_get_current_word (AnjutaView  *view,
+                              GtkTextIter *start,
+                              GtkTextIter *end)
+{
+	GtkTextBuffer *buffer =
+		gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+	gchar c;
+	
+	gtk_text_buffer_get_iter_at_mark (buffer, start,
+	                                  gtk_text_buffer_get_insert (buffer));
+	gtk_text_buffer_get_iter_at_mark (buffer, end,
+	                                  gtk_text_buffer_get_insert (buffer));
+
+	do
+	{
+		gunichar uni = gtk_text_iter_get_char (start);
+		gchar* outbuf = g_new0(gchar, 6);
+		gint len = g_unichar_to_utf8 (uni, outbuf);
+		/* Check for non-ascii */
+		if (len > 1)
+			break;
+		c = outbuf[0];
+		g_free (outbuf);
 	}
+	while (wordcharacters_contains (c) && gtk_text_iter_backward_char (start));
+	do
+	{
+		gunichar uni = gtk_text_iter_get_char (end);
+		gchar* outbuf = g_new0(gchar, 6);
+		gint len = g_unichar_to_utf8 (uni, outbuf);
+		/* Check for non-ascii */
+		if (len > 1)
+			break;
+		c = outbuf[0];
+		g_free (outbuf);
+	}
+	while (wordcharacters_contains (c) && gtk_text_iter_forward_char (end));
+
+	/* Point to the correct start character (not the character before) */
+	gtk_text_iter_forward_char (start);
 }
diff --git a/plugins/sourceview/anjuta-view.h b/plugins/sourceview/anjuta-view.h
index 4bbafb0..f529bf5 100644
--- a/plugins/sourceview/anjuta-view.h
+++ b/plugins/sourceview/anjuta-view.h
@@ -96,6 +96,9 @@ void		 anjuta_view_scroll_to_cursor 	(AnjutaView       *view);
 void 		 anjuta_view_set_font		(AnjutaView       *view,
 						 gboolean         def,
 						 const gchar     *font_name);
+void		 anjuta_view_get_current_word (AnjutaView  *view,
+		                                   GtkTextIter *start,
+		                                   GtkTextIter *end);
 
 G_END_DECLS
 
diff --git a/plugins/sourceview/sourceview.c b/plugins/sourceview/sourceview.c
index 49753aa..90e1140 100644
--- a/plugins/sourceview/sourceview.c
+++ b/plugins/sourceview/sourceview.c
@@ -1122,63 +1122,18 @@ static gint ieditor_get_length(IAnjutaEditor *editor, GError **e)
 	return length;
 }
 
-static gboolean
-wordcharacters_contains (gchar c)
-{
-	const gchar* wordcharacters =
-		"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-	gint pos;
-	
-	for (pos = 0; pos < strlen(wordcharacters); pos++)
-	{
-		if (wordcharacters[pos] == c)
-		{
-			return TRUE;
-		}
-	}
-	return FALSE;
-}
-
 /* Return word on cursor position */
 static gchar* ieditor_get_current_word(IAnjutaEditor *editor, GError **e)
 {
 	Sourceview* sv = ANJUTA_SOURCEVIEW(editor);
-	GtkTextIter begin;
+	GtkTextIter start;
 	GtkTextIter end;
-	GtkTextBuffer* buffer = GTK_TEXT_BUFFER(sv->priv->document);
-	gchar* region;
-	gchar* word;
-	gint startword;
-	gint endword;
-	int cplen;
-	const int maxlength = 100;
-	
-	gtk_text_buffer_get_iter_at_mark (buffer, &begin, 
-									  gtk_text_buffer_get_insert(buffer));
-	gtk_text_buffer_get_iter_at_mark (buffer, &end, 
-									  gtk_text_buffer_get_insert(buffer));
-	startword = gtk_text_iter_get_line_offset (&begin);	
-	endword = gtk_text_iter_get_line_offset (&end);
-	
-	gtk_text_iter_set_line_offset (&begin, 0);
-	gtk_text_iter_forward_to_line_end (&end);
-	
-	region = gtk_text_buffer_get_text (buffer, &begin, &end, FALSE);
-	
-	while (startword> 0 && wordcharacters_contains(region[startword - 1]))
-		startword--;
-	while (region[endword] && wordcharacters_contains(region[endword]))
-		endword++;
-	if(startword == endword)
-		return NULL;
-	
-	region[endword] = '\0';
-	cplen = (maxlength < (endword-startword+1))?maxlength:(endword-startword+1);
-	word = g_strndup (region + startword, cplen);
-	
-	g_free(region);
-	
-	return word;
+
+	anjuta_view_get_current_word (sv->priv->view,
+	                              &start, &end);
+
+	return gtk_text_buffer_get_text (gtk_text_iter_get_buffer (&start),
+	                                 &start, &end, FALSE);
 }
 
 /* Insert text at position */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]