--- gedit-2.24.2-all_files/gedit/gedit-commands-search.c	2009-02-16 17:12:45.000000000 -0300
+++ /tmp/gedit-2.24.2/gedit/gedit-commands-search.c	2009-02-17 22:08:00.000000000 -0300
@@ -50,6 +50,12 @@
 #define GEDIT_SEARCH_DIALOG_KEY		"gedit-search-dialog-key"
 #define GEDIT_LAST_SEARCH_DATA_KEY	"gedit-last-search-data-key"
 
+typedef enum
+{
+	FIND,
+	REPLACE
+} SearchAction;
+
 /* data stored in the document
  * to persist the last searched
  * words and options
@@ -60,11 +66,11 @@ struct _LastSearchData
 	gchar *find_text;
 	gchar *replace_text;
 
-	gint match_case :1;
-	gint entire_word :1;
-	gint backwards :1;
-	gint open_documents :1;
-	gint wrap_around :1;
+	guint match_case :1;
+	guint entire_word :1;
+	guint backwards :1;
+	guint open_documents :1;
+	guint wrap_around :1;
 };
 
 static void
@@ -75,9 +81,10 @@ last_search_data_free (LastSearchData *d
 	g_free (data);
 }
 
-static void
+static gboolean
 last_search_data_set (LastSearchData    *data,
-		      GeditSearchDialog *dialog)
+		      GeditSearchDialog *dialog,
+		      SearchAction       action)
 {
 	const gchar *str;
 
@@ -87,19 +94,28 @@ last_search_data_set (LastSearchData    
 		g_free (data->find_text);
 		data->find_text = gedit_utils_unescape_search_text (str);
 	}
+	else
+		return FALSE;
 
 	str = gedit_search_dialog_get_replace_text (dialog);
-	if (str != NULL && *str != '\0')
+	if (str != NULL && (action == REPLACE || *str != '\0'))
 	{
 		g_free (data->replace_text);
 		data->replace_text = gedit_utils_unescape_search_text (str);
 	}
+	else
+	{
+		if (action == REPLACE)
+			return FALSE;
+	}
 
 	data->match_case = gedit_search_dialog_get_match_case (dialog);
 	data->entire_word = gedit_search_dialog_get_entire_word (dialog);
 	data->backwards = gedit_search_dialog_get_backwards (dialog);
 	data->open_documents = gedit_search_dialog_get_open_documents (dialog);
 	data->wrap_around = gedit_search_dialog_get_wrap_around (dialog);
+
+	return TRUE;
 }
 
 static void
@@ -270,8 +286,8 @@ run_search (GeditView   *view,
 	return found;
 }
 
-static gboolean
-manage_search (GeditWindow *window, LastSearchData *data)
+static guint
+manage_search (SearchAction action, GeditWindow *window, LastSearchData *data)
 {
 	GeditView *view;
 	GList *views;
@@ -282,10 +298,11 @@ manage_search (GeditWindow *window, Last
 	gboolean entire_word;
 	gboolean wrap_around;
 	gboolean search_backwards;	
+	gboolean search_open_docs;	
 	guint flags = 0;
 	guint old_flags = 0;
-	gboolean found;
-	gboolean search_open_docs;	
+	gboolean found = FALSE;	/* for action == FIND */
+	guint count = 0;	/* for action == REPLACE */
 	gboolean wrapped = FALSE;
 	gint active_index, index, nviews;
 
@@ -334,14 +351,15 @@ manage_search (GeditWindow *window, Last
 
 		g_free (search_text);
 
-		if (!search_open_docs)
-			found = run_search (view,
-				            wrap_around,
-			   	            search_backwards,
-				            FALSE); /* entire_document */
-		else
+		if (action == FIND)
 		{
-			if (index != active_index) {
+			if (!search_open_docs)
+				found = run_search (view,
+					            wrap_around,
+				   	            search_backwards,
+					            FALSE); /* entire_document */
+			else if (index != active_index)
+			{
 				/* When switch to other doc, it is necessary
 				   set the same data, so next time the "Find"
 				   dialog shows up or "Find Next" is run,
@@ -355,28 +373,34 @@ manage_search (GeditWindow *window, Last
 				                    TRUE);
 			}
 			else if (wrapped)
-			{
 				found = run_search (view,
 				                    TRUE,
 				                    search_backwards,
 				                    FALSE);
-				break;
-			}
 			else
 				found = run_search (view,
 				                    FALSE,
 				                    search_backwards,
 				                    FALSE);
-			if (found)
-			{
-				if (gedit_window_get_active_tab (window) !=
-				    gedit_tab_get_from_document (doc))
-				{
-					gedit_window_set_active_tab (window,
-					    gedit_tab_get_from_document (doc));
-				}
-				break;
-			}
+		}
+
+		if (found
+		    || (wrapped && index == active_index))
+		{
+			break;
+		}
+
+		if (action == REPLACE)
+		{
+			count += gedit_document_replace_all (doc, 
+							    entry_text,
+							    data->replace_text,
+							    flags);
+		}
+
+		if (!search_open_docs)
+			break;
+
 			if (search_backwards)
 			{
 				if (index == 0)
@@ -406,14 +430,27 @@ manage_search (GeditWindow *window, Last
 				}
 			}
 			view = g_list_nth_data (views, index);
-		}
 	}
 	while (search_open_docs);
 
 	if (search_open_docs)
 		g_list_free (views);
 
-	return found;
+	if (action == REPLACE)
+		return count;
+
+	if (found)
+	{
+		if (gedit_window_get_active_tab (window) !=
+		    gedit_tab_get_from_document (doc))
+		{
+			gedit_window_set_active_tab (window,
+			    gedit_tab_get_from_document (doc));
+		}
+		return 1;
+	}
+	else
+		return 0;
 }
 
 static void
@@ -435,11 +472,14 @@ do_find (GeditSearchDialog *dialog,
 	data = get_doc_search_data (doc);
 
 	if (dialog != NULL)
-		last_search_data_set (data, dialog);
+	{
+		if (!last_search_data_set (data, dialog, FIND))
+			return;
+	}
 	else
 		data->backwards = backward;
 
-	found = manage_search (window, data);
+	found = manage_search (FIND, window, data);
 
 	if (found)
 		phrase_found (window, 0);
@@ -558,71 +598,20 @@ do_replace_all (GeditSearchDialog *dialo
 {
 	GeditView *view;
 	GeditDocument *doc;
-	GList *views;
-	const gchar *search_entry_text;
-	const gchar *replace_entry_text;	
-	gboolean match_case;
-	gboolean entire_word;
-	gboolean search_open_docs;
-	guint flags = 0;
 	gint count = 0;
-	gint active_index, index, nviews;
-	LastSearchData *last_search_data;
-
-	search_entry_text = gedit_search_dialog_get_search_text (dialog);
-	g_return_if_fail ((search_entry_text) != NULL);
-	g_return_if_fail ((*search_entry_text) != '\0');
-
-	/* replace text may be "", we just delete all occurrencies */
-	replace_entry_text = gedit_search_dialog_get_replace_text (dialog);
-	g_return_if_fail ((replace_entry_text) != NULL);
-
-	match_case = gedit_search_dialog_get_match_case (dialog);
-	entire_word = gedit_search_dialog_get_entire_word (dialog);
-	search_open_docs = gedit_search_dialog_get_open_documents (dialog);
-
-	GEDIT_SEARCH_SET_CASE_SENSITIVE (flags, match_case);
-	GEDIT_SEARCH_SET_ENTIRE_WORD (flags, entire_word);
+	LastSearchData *data;
 
 	view = gedit_window_get_active_view (window);
 	if (view == NULL)
 		return;
 
-	if (search_open_docs)
-	{
-		views = gedit_window_get_views (window);
-		index = active_index = g_list_index (views, view);
-		nviews = g_list_length (views);
-	}
-	else
-	{
-		views = NULL;
-		index = active_index = nviews = 0;
-	}
-
-	do
-	{
-		doc = GEDIT_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
-
-		count += gedit_document_replace_all (doc, 
-						    search_entry_text,
-						    replace_entry_text,
-						    flags);
-		if (!search_open_docs)
-			break;
+	doc = GEDIT_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
 
-		/* we are not considering wrap and backward settings like done
-		   by manage_search function, because the user might expect
-		   an effect in all files without restrictions. */
-		index++;
-		if (index == nviews)
-			index = 0;
-		view = g_list_nth_data (views, index);
-	}
-	while (index != active_index);
+	data = get_doc_search_data (doc);
+	if (!last_search_data_set (data, dialog, REPLACE))
+		return;
 
-	if (search_open_docs)
-		g_list_free (views);
+	count = manage_search (REPLACE, window, data);
 
 	if (count > 0)
 	{
@@ -636,9 +625,6 @@ do_replace_all (GeditSearchDialog *dialo
 	gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
 					   GEDIT_SEARCH_DIALOG_REPLACE_RESPONSE,
 					   FALSE);
-
-	last_search_data = get_doc_search_data (doc);
-	last_search_data_set (last_search_data, dialog);
 }
 
 static void
