>From 44f8b73a757ae42527d5766ae77d4b3515c52c55 Mon Sep 17 00:00:00 2001
From: Eugene Arshinov <earshinov@gmail.com>
Date: Sat, 21 May 2011 11:32:19 +0400
Subject: [PATCH 2/3] Follow Colomban's advice.  Two more publicly visible
 signatures changed: - document_replace_all() -
 document_replace_sel()

Additionally, apply the same technique to show proper (with escape
sequences not yet processed) replace text in messages.
---
 src/document.c |   47 ++++++++++++-----------------------------------
 src/document.h |    6 +++---
 src/search.c   |   25 +++++++++++--------------
 3 files changed, 26 insertions(+), 52 deletions(-)

diff --git a/src/document.c b/src/document.c
index 3e47d1c..2346202 100644
--- a/src/document.c
+++ b/src/document.c
@@ -2068,45 +2068,22 @@ gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gch
 }
 
 
-static void show_replace_summary(GeanyDocument *doc, gint count, const gchar *find_text,
-		const gchar *replace_text, gboolean escaped_chars)
+static void show_replace_summary(GeanyDocument *doc, gint count, const gchar *original_find_text,
+	const gchar *original_replace_text)
 {
-	gchar *escaped_find_text = NULL, *escaped_replace_text, *filename;
+	gchar *filename;
 
 	if (count == 0)
 	{
-		if (escaped_chars)
-		{
-			/* escape special characters for showing */
-			escaped_find_text = g_strescape(find_text, NULL);
-			ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), escaped_find_text);
-			g_free(escaped_find_text);
-		}
-		else
-			ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), find_text);
+		ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), original_find_text);
 		return;
 	}
 
 	filename = g_path_get_basename(DOC_FILENAME(doc));
-
-	if (escaped_chars)
-	{	/* escape special characters for showing */
-		escaped_find_text = g_strescape(find_text, NULL);
-		escaped_replace_text = g_strescape(replace_text, NULL);
-		ui_set_statusbar(TRUE, ngettext(
-			"%s: replaced %d occurrence of \"%s\" with \"%s\".",
-			"%s: replaced %d occurrences of \"%s\" with \"%s\".",
-			count),	filename, count, escaped_find_text, escaped_replace_text);
-		g_free(escaped_find_text);
-		g_free(escaped_replace_text);
-	}
-	else
-	{
-		ui_set_statusbar(TRUE, ngettext(
-			"%s: replaced %d occurrence of \"%s\" with \"%s\".",
-			"%s: replaced %d occurrences of \"%s\" with \"%s\".",
-			count), filename, count, find_text, replace_text);
-	}
+	ui_set_statusbar(TRUE, ngettext(
+		"%s: replaced %d occurrence of \"%s\" with \"%s\".",
+		"%s: replaced %d occurrences of \"%s\" with \"%s\".",
+		count), filename, count, original_find_text, original_replace_text);
 	g_free(filename);
 }
 
@@ -2156,7 +2133,7 @@ document_replace_range(GeanyDocument *doc, const gchar *find_text, const gchar *
 
 
 void document_replace_sel(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
-						  gint flags, gboolean escaped_chars)
+						  const gchar *original_find_text, const gchar *original_replace_text, gint flags)
 {
 	gint selection_end, selection_start, selection_mode, selected_lines, last_line = 0;
 	gint max_column = 0, count = 0;
@@ -2253,13 +2230,13 @@ void document_replace_sel(GeanyDocument *doc, const gchar *find_text, const gcha
 	else /* no replacements */
 		utils_beep();
 
-	show_replace_summary(doc, count, find_text, replace_text, escaped_chars);
+	show_replace_summary(doc, count, original_find_text, original_replace_text);
 }
 
 
 /* returns number of replacements made. */
 gint document_replace_all(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
-		gint flags, gboolean escaped_chars)
+		const gchar *original_find_text, const gchar *original_replace_text, gint flags)
 {
 	gint len, count;
 	g_return_val_if_fail(doc != NULL && find_text != NULL && replace_text != NULL, FALSE);
@@ -2271,7 +2248,7 @@ gint document_replace_all(GeanyDocument *doc, const gchar *find_text, const gcha
 	count = document_replace_range(
 			doc, find_text, replace_text, flags, 0, len, TRUE, NULL);
 
-	show_replace_summary(doc, count, find_text, replace_text, escaped_chars);
+	show_replace_summary(doc, count, original_find_text, original_replace_text);
 	return count;
 }
 
diff --git a/src/document.h b/src/document.h
index 6de5d31..0b0b0f0 100644
--- a/src/document.h
+++ b/src/document.h
@@ -224,10 +224,10 @@ gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gch
 		const gchar *replace_text, gint flags, gboolean search_backwards);
 
 gint document_replace_all(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
-		gint flags, gboolean escaped_chars);
+		const gchar *original_find_text, const gchar *original_replace_text, gint flags);
 
-void document_replace_sel(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text, gint flags,
-						  gboolean escaped_chars);
+void document_replace_sel(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
+						  const gchar *original_find_text, const gchar *original_replace_text, gint flags);
 
 void document_update_tag_list(GeanyDocument *doc, gboolean update);
 
diff --git a/src/search.c b/src/search.c
index 9f26d00..3d29c9e 100644
--- a/src/search.c
+++ b/src/search.c
@@ -1302,7 +1302,8 @@ on_replace_entry_activate(GtkEntry *entry, gpointer user_data)
 
 static void replace_in_session(GeanyDocument *doc,
 		gint search_flags_re, gboolean search_replace_escape_re,
-		const gchar *find, const gchar *original_find, const gchar *replace)
+		const gchar *find, const gchar *replace,
+		const gchar *original_find, const gchar *original_replace)
 {
 	guint n, page_count, rep_count = 0, file_count = 0;
 
@@ -1313,8 +1314,7 @@ static void replace_in_session(GeanyDocument *doc,
 		GeanyDocument *tmp_doc = document_get_from_page(n);
 		gint reps = 0;
 
-		reps = document_replace_all(tmp_doc, find, replace, search_flags_re,
-			search_replace_escape_re);
+		reps = document_replace_all(tmp_doc, find, replace, original_find, original_replace, search_flags_re);
 		rep_count += reps;
 		if (reps)
 			file_count++;
@@ -1344,7 +1344,7 @@ on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 	GeanyDocument *doc = document_get_current();
 	gint search_flags_re;
 	gboolean search_backwards_re, search_replace_escape_re;
-	gchar *find, *replace, *original_find;
+	gchar *find, *replace, *original_find = NULL, *original_replace = NULL;
 
 	gtk_window_get_position(GTK_WINDOW(replace_dlg.dialog),
 		&replace_dlg.position[0], &replace_dlg.position[1]);
@@ -1371,23 +1371,18 @@ on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 		goto fail;
 
 	original_find = g_strdup(find);
+	original_replace = g_strdup(replace);
 	if (search_flags_re & SCFIND_REGEXP)
 	{
 		if (! utils_str_replace_escape(find, TRUE) ||
 			! utils_str_replace_escape(replace, TRUE))
-		{
-			g_free(original_find);
 			goto fail;
-		}
 	}
 	else if (search_replace_escape_re)
 	{
 		if (! utils_str_replace_escape(find, FALSE) ||
 			! utils_str_replace_escape(replace, FALSE))
-		{
-			g_free(original_find);
 			goto fail;
-		}
 	}
 
 	ui_combo_box_add_to_history(GTK_COMBO_BOX_ENTRY(
@@ -1419,17 +1414,16 @@ on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 			break;
 		}
 		case GEANY_RESPONSE_REPLACE_IN_FILE:
-			if (! document_replace_all(doc, find, replace, search_flags_re,
-				search_replace_escape_re))
+			if (! document_replace_all(doc, find, replace, original_find, original_replace, search_flags_re))
 				utils_beep();
 			break;
 
 		case GEANY_RESPONSE_REPLACE_IN_SESSION:
-			replace_in_session(doc, search_flags_re, search_replace_escape_re, find, original_find, replace);
+			replace_in_session(doc, search_flags_re, search_replace_escape_re, find, replace, original_find, original_replace);
 			break;
 
 		case GEANY_RESPONSE_REPLACE_IN_SEL:
-			document_replace_sel(doc, find, replace, search_flags_re, search_replace_escape_re);
+			document_replace_sel(doc, find, replace, original_find, original_replace, search_flags_re);
 			break;
 	}
 	switch (response)
@@ -1443,6 +1437,7 @@ on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 	g_free(find);
 	g_free(replace);
 	g_free(original_find);
+	g_free(original_replace);
 	return;
 
 fail:
@@ -1450,6 +1445,8 @@ fail:
 	gtk_widget_grab_focus(replace_dlg.find_entry);
 	g_free(find);
 	g_free(replace);
+	g_free(original_find);
+	g_free(original_replace);
 }
 
 
-- 
1.7.5.1

