[gedit-list] i18n fixes for "search and replace" in gedit



"search and replace" in gedit behaves wrongly in multi-byte lingual like
Korean. Note that return value from gtk_text_get_length is not bytes length.
It is character(can be multibyte) length.

So strlen(gtk_editable_get_text(..)) != gtk_text_get_length(...).

The attached patch seems to fix this problems.

Index: src/prefs.c
===================================================================
RCS file: /cvs/gnome/gedit/src/prefs.c,v
retrieving revision 1.39
diff -u -r1.39 prefs.c
--- src/prefs.c	2001/02/12 03:07:58	1.39
+++ src/prefs.c	2001/03/07 05:20:07
@@ -172,10 +172,13 @@
 	}
 #endif	
 
-	settings->use_fontset = FALSE;
 	settings->font = gnome_config_get_string ("font");
 	if (settings->font == NULL)
 		settings->font = g_strdup (DEFAULT_FONT);
+	if (strchr(settings->font, ',') >= 0) 
+		settings->use_fontset = TRUE;
+	else
+		settings->use_fontset = FALSE;
 	
 	if (mdi)
 		tab_pos (settings->tab_pos);
Index: src/search.c
===================================================================
RCS file: /cvs/gnome/gedit/src/search.c,v
retrieving revision 1.78
diff -u -r1.78 search.c
--- src/search.c	2001/02/28 12:59:38	1.78
+++ src/search.c	2001/03/07 05:20:07
@@ -469,10 +469,11 @@
 		       gint  *total_lines,
 		       gboolean return_the_line_number)
 {
-	gint p1 = 0;
 	guint p2 = 0;
 	gint text_length;
 	gint case_sensitive_mask;
+	guchar *src;
+	const guchar *dest;
 
 	gedit_debug (DEBUG_RECENT, "");
 
@@ -491,17 +492,28 @@
 		return FALSE;
 	
 	case_sensitive_mask = case_sensitive?0:32;
-	text_length = strlen (text_to_search_for);
+	text_length = mb_strlen (text_to_search_for);
+	src = mb_index(search_info.buffer, starting_position);
+	dest = text_to_search_for;
 	for ( p2=starting_position; p2 < search_info.length; p2 ++)
 	{
-		if ((search_info.buffer[p2]|case_sensitive_mask)==(text_to_search_for[p1]|case_sensitive_mask))
-		{
-			p1++;
-			if (p1==text_length)
-				break;
+	        int len_s = mblen(src, MB_CUR_MAX);
+		int len_d;
+
+	        if ((case_sensitive && *src != *dest) || (!case_sensitive && (tolower(*src) != tolower(*dest)))) {
+		    dest = text_to_search_for;
+		    src += len_s;
+		    continue;
+		}
+		len_d = mblen(dest, MB_CUR_MAX);
+		if (len_s == len_d && (len_s == 1 || 
+		    ((case_sensitive?strncmp(src, dest, len_s):g_strncasecmp(src, dest, len_s)) == 0))) {
+			if (*(dest+len_d) == '\0') break;
+			dest += len_d;
+		} else {
+		    dest = text_to_search_for;
 		}
-		else
-			p1 = 0;
+		src += len_s;
 	}
 
 	if (p2 == search_info.length)
@@ -586,7 +598,7 @@
 	replace_text_length = strlen (replace_text);
 
 	buffer_in = search_info.buffer;
-	buffer_in_length = search_info.length;
+	buffer_in_length = strlen(search_info.buffer);
 
 	if (buffer_in_length == 0)
 	{
@@ -621,6 +633,7 @@
 	case_sensitive_mask = case_sensitive?0:32;
 
 	/* Copy the start of buffer if start_pos = cursor position */
+	start_pos = ((guchar *)mb_index(buffer_in, start_pos)) - buffer_in;
 	while (p1 < start_pos)
 	{
 		buffer_out [p2++] = buffer_in [p1++];
@@ -649,7 +662,7 @@
 			
 		}
 		
-		if ((buffer_in [p1]|case_sensitive_mask) == (search_text [p3]|case_sensitive_mask))
+		if ((buffer_in[p1] == search_text[p3]) || (buffer_in[p1] < 0x80 && (buffer_in [p1]|case_sensitive_mask) == (search_text [p3]|case_sensitive_mask)))
 		{
 			p3++;
 			if (p3 == search_text_length)
Index: src/utils.c
===================================================================
RCS file: /cvs/gnome/gedit/src/utils.c,v
retrieving revision 1.28
diff -u -r1.28 utils.c
--- src/utils.c	2000/10/16 04:41:08	1.28
+++ src/utils.c	2001/03/07 05:20:08
@@ -314,3 +314,30 @@
 	if (widget != NULL)
 		gdk_window_raise (widget->window);
 }
+
+char *
+mb_index(char *s, int pos)
+{
+    while (pos--) {
+	int i = mblen(s, MB_CUR_MAX);
+	if (i == 0) return s;
+	if (i < 0) s++;
+	else s += i;
+    }
+    return s;
+}
+
+int
+mb_strlen(const char *s)
+{
+    int i;
+    int len = 0;
+    while (*s) {
+	i = mblen(s, MB_CUR_MAX);
+	if (i <= 0) i = 1;
+	s += i;
+	len++;
+    }
+    return len;
+}
+
Index: src/utils.h
===================================================================
RCS file: /cvs/gnome/gedit/src/utils.h,v
retrieving revision 1.28
diff -u -r1.28 utils.h
--- src/utils.h	2001/02/25 02:42:03	1.28
+++ src/utils.h	2001/03/07 05:20:08
@@ -95,4 +95,6 @@
 gchar *	gedit_utils_create_temp_from_doc (GeditDocument *doc, gint number);
 void	gedit_utils_error_dialog (gchar *error_message, GtkWidget *widget);
 
+int mb_strlen(const char *s);
+char *mb_index(char *, int);
 #endif /* __GEDIT_UTILS_H__ */
Index: src/dialogs/dialog-replace.c
===================================================================
RCS file: /cvs/gnome/gedit/src/dialogs/dialog-replace.c,v
retrieving revision 1.43
diff -u -r1.43 dialog-replace.c
--- src/dialogs/dialog-replace.c	2001/02/28 12:59:38	1.43
+++ src/dialogs/dialog-replace.c	2001/03/07 05:20:08
@@ -189,7 +189,7 @@
 	gedit_view_set_position	(dialog->view, pos_found);
 	gedit_view_set_selection (dialog->view,
 				  pos_found,
-				  pos_found + strlen (search_text));
+				  pos_found + mb_strlen (search_text));
 	
 	gtk_radio_button_select (GTK_RADIO_BUTTON(dialog->position)->group, 1);
 }
@@ -205,15 +205,15 @@
 
 	gedit_document_replace_text (dialog->view->doc,
 				     replace_text,
-				     strlen (search_text),
-				     start_pos - strlen (search_text),
+				     mb_strlen (search_text),
+				     start_pos - mb_strlen (search_text),
 				     TRUE);
 
 	/* We need to reload the buffer since we changed it */
 	gedit_search_end();
 	gedit_search_start();
 
-	start_pos += strlen (replace_text) - strlen (search_text);
+	start_pos += mb_strlen (replace_text) - mb_strlen (search_text);
 	
 	action_find (dialog, start_pos, search_text, case_sensitive);
 


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