>From 9897da03d7155c407834df61115379d2d5daf3de Mon Sep 17 00:00:00 2001
From: Eugene Arshinov <earshinov@gmail.com>
Date: Sun, 15 May 2011 16:06:13 +0400
Subject: [PATCH] In messages show the actual text user entered in Find and
 Replace dialogs. Previously we could put into a message a
 string where escape sequences were already translated.

In the code, now we pass the original text together with the one
that is actually searched for.  New `original_text' field was added to
GeanySearchData.  A bug was fixed in document.c:show_replace_summary(): it
did not escape the "No matches found for ..." string.
---
 src/callbacks.c |    4 +-
 src/document.c  |   50 ++++++++++++++++++++++++++++++++-------------
 src/document.h  |    8 +++---
 src/editor.c    |    4 +-
 src/search.c    |   60 +++++++++++++++++++++++++++++++++---------------------
 src/search.h    |    4 ++-
 6 files changed, 83 insertions(+), 47 deletions(-)

diff --git a/src/callbacks.c b/src/callbacks.c
index 9fbe2cd..da64a7f 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -538,6 +538,7 @@ on_toolbutton_save_clicked             (GtkAction       *action,
 static void setup_find(const gchar *text, gboolean backwards)
 {
 	setptr(search_data.text, g_strdup(text));
+	setptr(search_data.original_text, g_strdup(text));
 	search_data.flags = 0;
 	search_data.backwards = backwards;
 	search_data.search_bar = TRUE;
@@ -1037,7 +1038,7 @@ static void find_usage(gboolean in_session)
 		flags = SCFIND_MATCHCASE | SCFIND_WHOLEWORD;
 	}
 
-	search_find_usage(search_text, flags, in_session);
+	search_find_usage(search_text, search_text, flags, in_session);
 	g_free(search_text);
 }
 
@@ -2414,4 +2415,3 @@ on_mark_all1_activate                  (GtkMenuItem     *menuitem,
 {
 	keybindings_send_command(GEANY_KEY_GROUP_SEARCH, GEANY_KEYS_SEARCH_MARKALL);
 }
-
diff --git a/src/document.c b/src/document.c
index 971936b..3e47d1c 100644
--- a/src/document.c
+++ b/src/document.c
@@ -1928,9 +1928,13 @@ gboolean document_search_bar_find(GeanyDocument *doc, const gchar *text, gint fl
 
 /* General search function, used from the find dialog.
  * Returns -1 on failure or the start position of the matching text.
- * Will skip past any selection, ignoring it. */
-gint document_find_text(GeanyDocument *doc, const gchar *text, gint flags, gboolean search_backwards,
-		gboolean scroll, GtkWidget *parent)
+ * Will skip past any selection, ignoring it.
+ *
+ * @param text Text to find.
+ * @param original_text Text as it was entered by user, or @c NULL to use @c text
+ */
+gint document_find_text(GeanyDocument *doc, const gchar *text, const gchar *original_text,
+		gint flags, gboolean search_backwards, gboolean scroll, GtkWidget *parent)
 {
 	gint selection_end, selection_start, search_pos;
 
@@ -1942,6 +1946,9 @@ gint document_find_text(GeanyDocument *doc, const gchar *text, gint flags, gbool
 	if (flags & SCFIND_REGEXP)
 		search_backwards = FALSE;
 
+	if (!original_text)
+		original_text = text;
+
 	selection_start = sci_get_selection_start(doc->editor->sci);
 	selection_end = sci_get_selection_end(doc->editor->sci);
 	if ((selection_end - selection_start) > 0)
@@ -1974,7 +1981,7 @@ gint document_find_text(GeanyDocument *doc, const gchar *text, gint flags, gbool
 		if ((selection_end == 0 && ! search_backwards) ||
 			(selection_end == sci_len && search_backwards))
 		{
-			ui_set_statusbar(FALSE, _("\"%s\" was not found."), text);
+			ui_set_statusbar(FALSE, _("\"%s\" was not found."), original_text);
 			utils_beep();
 			return -1;
 		}
@@ -1982,12 +1989,12 @@ gint document_find_text(GeanyDocument *doc, const gchar *text, gint flags, gbool
 		/* we searched only part of the document, so ask whether to wraparound. */
 		if (search_prefs.suppress_dialogs ||
 			dialogs_show_question_full(parent, GTK_STOCK_FIND, GTK_STOCK_CANCEL,
-				_("Wrap search and find again?"), _("\"%s\" was not found."), text))
+				_("Wrap search and find again?"), _("\"%s\" was not found."), original_text))
 		{
 			gint ret;
 
 			sci_set_current_position(doc->editor->sci, (search_backwards) ? sci_len : 0, FALSE);
-			ret = document_find_text(doc, text, flags, search_backwards, scroll, parent);
+			ret = document_find_text(doc, text, original_text, flags, search_backwards, scroll, parent);
 			if (ret == -1)
 			{	/* return to original cursor position if not found */
 				sci_set_current_position(doc->editor->sci, selection_start, FALSE);
@@ -2000,9 +2007,13 @@ gint document_find_text(GeanyDocument *doc, const gchar *text, gint flags, gbool
 
 
 /* Replaces the selection if it matches, otherwise just finds the next match.
- * Returns: start of replaced text, or -1 if no replacement was made */
-gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
-		gint flags, gboolean search_backwards)
+ * Returns: start of replaced text, or -1 if no replacement was made
+ *
+ * @param find_text Text to find.
+ * @param original_find_text Text to find as it was entered by user, or @c NULL to use @c find_text
+ */
+gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gchar *original_find_text,
+		const gchar *replace_text, gint flags, gboolean search_backwards)
 {
 	gint selection_end, selection_start, search_pos;
 
@@ -2015,12 +2026,15 @@ gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gch
 	if (flags & SCFIND_REGEXP)
 		search_backwards = FALSE;
 
+	if (!original_find_text)
+		original_find_text = find_text;
+
 	selection_start = sci_get_selection_start(doc->editor->sci);
 	selection_end = sci_get_selection_end(doc->editor->sci);
 	if (selection_end == selection_start)
 	{
 		/* no selection so just find the next match */
-		document_find_text(doc, find_text, flags, search_backwards, TRUE, NULL);
+		document_find_text(doc, find_text, original_find_text, flags, search_backwards, TRUE, NULL);
 		return -1;
 	}
 	/* there's a selection so go to the start before finding to search through it
@@ -2030,7 +2044,7 @@ gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gch
 	else
 		sci_goto_pos(doc->editor->sci, selection_start, TRUE);
 
-	search_pos = document_find_text(doc, find_text, flags, search_backwards, TRUE, NULL);
+	search_pos = document_find_text(doc, find_text, original_find_text, flags, search_backwards, TRUE, NULL);
 	/* return if the original selected text did not match (at the start of the selection) */
 	if (search_pos != selection_start)
 		return -1;
@@ -2057,11 +2071,19 @@ 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)
 {
-	gchar *escaped_find_text, *escaped_replace_text, *filename;
+	gchar *escaped_find_text = NULL, *escaped_replace_text, *filename;
 
 	if (count == 0)
 	{
-		ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), find_text);
+		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);
 		return;
 	}
 
@@ -3127,5 +3149,3 @@ gint document_compare_by_tab_order_reverse(gconstpointer a, gconstpointer b)
 	/* equality */
 	return 0;
 }
-
-
diff --git a/src/document.h b/src/document.h
index 39d90f2..6de5d31 100644
--- a/src/document.h
+++ b/src/document.h
@@ -217,11 +217,11 @@ void document_open_files(const GSList *filenames, gboolean readonly, GeanyFilety
 gboolean document_search_bar_find(GeanyDocument *doc, const gchar *text, gint flags, gboolean inc,
 		gboolean backwards);
 
-gint document_find_text(GeanyDocument *doc, const gchar *text, gint flags, gboolean search_backwards,
-		gboolean scroll, GtkWidget *parent);
+gint document_find_text(GeanyDocument *doc, const gchar *text, const gchar *original_text,
+		gint flags, gboolean search_backwards, gboolean scroll, GtkWidget *parent);
 
-gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
-		gint flags, gboolean search_backwards);
+gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gchar *original_find_text,
+		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);
diff --git a/src/editor.c b/src/editor.c
index 3dfd29b..74f9bb0 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -2791,7 +2791,7 @@ static void real_uncomment_multiline(GeanyEditor *editor)
 	doc = editor->document;
 
 	/* remove comment open chars */
-	pos = document_find_text(doc, doc->file_type->comment_open, 0, TRUE, FALSE, NULL);
+	pos = document_find_text(doc, doc->file_type->comment_open, NULL, 0, TRUE, FALSE, NULL);
 	SSM(editor->sci, SCI_DELETEBACK, 0, 0);
 
 	/* check whether the line is empty and can be deleted */
@@ -2804,7 +2804,7 @@ static void real_uncomment_multiline(GeanyEditor *editor)
 	g_free(linebuf);
 
 	/* remove comment close chars */
-	pos = document_find_text(doc, doc->file_type->comment_close, 0, FALSE, FALSE, NULL);
+	pos = document_find_text(doc, doc->file_type->comment_close, NULL, 0, FALSE, FALSE, NULL);
 	SSM(editor->sci, SCI_DELETEBACK, 0, 0);
 
 	/* check whether the line is empty and can be deleted */
diff --git a/src/search.c b/src/search.c
index 767bc50..fc9a149 100644
--- a/src/search.c
+++ b/src/search.c
@@ -266,6 +266,7 @@ static void init_prefs(void)
 void search_init(void)
 {
 	search_data.text = NULL;
+	search_data.original_text = NULL;
 	init_prefs();
 }
 
@@ -279,6 +280,7 @@ void search_finalize(void)
 	FREE_WIDGET(replace_dlg.dialog);
 	FREE_WIDGET(fif_dlg.dialog);
 	g_free(search_data.text);
+	g_free(search_data.original_text);
 }
 
 
@@ -358,7 +360,9 @@ static void send_find_dialog_response(GtkButton *button, gpointer user_data)
 static void setup_find_next(const gchar *text)
 {
 	g_free(search_data.text);
+	g_free(search_data.original_text);
 	search_data.text = g_strdup(text);
+	search_data.original_text = g_strdup(text);
 	search_data.flags = 0;
 	search_data.backwards = FALSE;
 	search_data.search_bar = FALSE;
@@ -405,7 +409,7 @@ void search_find_selection(GeanyDocument *doc, gboolean search_backwards)
 	{
 		setup_find_next(s);	/* allow find next/prev */
 
-		if (document_find_text(doc, s, 0, search_backwards, FALSE, NULL) > -1)
+		if (document_find_text(doc, s, NULL, 0, search_backwards, FALSE, NULL) > -1)
 			editor_display_current_line(doc->editor, 0.3F);
 		g_free(s);
 	}
@@ -1228,7 +1232,9 @@ on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 		search_data.search_bar = FALSE;
 
 		g_free(search_data.text);
+		g_free(search_data.original_text);
 		search_data.text = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(user_data)))));
+		search_data.original_text = g_strdup(search_data.text);
 		search_data.flags = int_search_flags(settings.find_case_sensitive,
 			settings.find_match_whole_word, settings.find_regexp, settings.find_match_word_start);
 
@@ -1244,14 +1250,14 @@ on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 			if (! utils_str_replace_escape(search_data.text, search_data.flags & SCFIND_REGEXP))
 				goto fail;
 		}
-		ui_combo_box_add_to_history(GTK_COMBO_BOX_ENTRY(user_data), search_data.text, 0);
+		ui_combo_box_add_to_history(GTK_COMBO_BOX_ENTRY(user_data), search_data.original_text, 0);
 
 		switch (response)
 		{
 			case GEANY_RESPONSE_FIND:
 			case GEANY_RESPONSE_FIND_PREVIOUS:
 			{
-				gint result = document_find_text(doc, search_data.text, search_data.flags,
+				gint result = document_find_text(doc, search_data.text, search_data.original_text, search_data.flags,
 					(response == GEANY_RESPONSE_FIND_PREVIOUS), TRUE, GTK_WIDGET(find_dlg.dialog));
 				ui_set_search_entry_background(find_dlg.entry, (result > -1));
 				check_close = FALSE;
@@ -1260,11 +1266,11 @@ on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 				break;
 			}
 			case GEANY_RESPONSE_FIND_IN_FILE:
-				search_find_usage(search_data.text, search_data.flags, FALSE);
+				search_find_usage(search_data.text, search_data.original_text, search_data.flags, FALSE);
 				break;
 
 			case GEANY_RESPONSE_FIND_IN_SESSION:
-				search_find_usage(search_data.text, search_data.flags, TRUE);
+				search_find_usage(search_data.text, search_data.original_text, search_data.flags, TRUE);
 				break;
 
 			case GEANY_RESPONSE_MARK:
@@ -1272,12 +1278,12 @@ on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 				gint count = search_mark_all(doc, search_data.text, search_data.flags);
 
 				if (count == 0)
-					ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), search_data.text);
+					ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), search_data.original_text);
 				else
 					ui_set_statusbar(FALSE,
 						ngettext("Found %d match for \"%s\".",
 								 "Found %d matches for \"%s\".", count),
-						count, search_data.text);
+						count, search_data.original_text);
 			}
 			break;
 		}
@@ -1296,7 +1302,7 @@ 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 *replace)
+		const gchar *find, const gchar *original_find, const gchar *replace)
 {
 	guint n, page_count, rep_count = 0, file_count = 0;
 
@@ -1316,7 +1322,7 @@ static void replace_in_session(GeanyDocument *doc,
 	if (file_count == 0)
 	{
 		utils_beep();
-		ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), find);
+		ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), original_find);
 		return;
 	}
 	/* if only one file was changed, don't override that document's status message
@@ -1338,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;
+	gchar *find, *replace, *original_find;
 
 	gtk_window_get_position(GTK_WINDOW(replace_dlg.dialog),
 		&replace_dlg.position[0], &replace_dlg.position[1]);
@@ -1370,17 +1376,25 @@ on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 		g_free(replace);
 		return;
 	}
+
+	original_find = g_strdup(find);
 	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(
@@ -1392,21 +1406,21 @@ on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 	{
 		case GEANY_RESPONSE_REPLACE_AND_FIND:
 		{
-			gint rep = document_replace_text(doc, find, replace, search_flags_re,
+			gint rep = document_replace_text(doc, find, original_find, replace, search_flags_re,
 				search_backwards_re);
 			if (rep != -1)
-				document_find_text(doc, find, search_flags_re, search_backwards_re,
+				document_find_text(doc, find, original_find, search_flags_re, search_backwards_re,
 					TRUE, NULL);
 			break;
 		}
 		case GEANY_RESPONSE_REPLACE:
-			document_replace_text(doc, find, replace, search_flags_re,
+			document_replace_text(doc, find, original_find, replace, search_flags_re,
 				search_backwards_re);
 			break;
 
 		case GEANY_RESPONSE_FIND:
 		{
-			gint result = document_find_text(doc, find, search_flags_re,
+			gint result = document_find_text(doc, find, original_find, search_flags_re,
 								search_backwards_re, TRUE, GTK_WIDGET(dialog));
 			ui_set_search_entry_background(replace_dlg.find_entry, (result > -1));
 			break;
@@ -1418,7 +1432,7 @@ on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 			break;
 
 		case GEANY_RESPONSE_REPLACE_IN_SESSION:
-			replace_in_session(doc, search_flags_re, search_replace_escape_re, find, replace);
+			replace_in_session(doc, search_flags_re, search_replace_escape_re, find, original_find, replace);
 			break;
 
 		case GEANY_RESPONSE_REPLACE_IN_SEL:
@@ -1435,6 +1449,7 @@ on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 	}
 	g_free(find);
 	g_free(replace);
+	g_free(original_find);
 }
 
 
@@ -2012,7 +2027,8 @@ static gint find_document_usage(GeanyDocument *doc, const gchar *search_text, gi
 }
 
 
-void search_find_usage(const gchar *search_text, gint flags, gboolean in_session)
+void search_find_usage(const gchar *search_text, const gchar *original_search_text,
+		gint flags, gboolean in_session)
 {
 	GeanyDocument *doc;
 	gint count = 0;
@@ -2047,17 +2063,17 @@ void search_find_usage(const gchar *search_text, gint flags, gboolean in_session
 
 	if (count == 0) /* no matches were found */
 	{
-		ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), search_text);
-		msgwin_msg_add(COLOR_BLUE, -1, NULL, _("No matches found for \"%s\"."), search_text);
+		ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), original_search_text);
+		msgwin_msg_add(COLOR_BLUE, -1, NULL, _("No matches found for \"%s\"."), original_search_text);
 	}
 	else
 	{
 		ui_set_statusbar(FALSE, ngettext(
 			"Found %d match for \"%s\".", "Found %d matches for \"%s\".", count),
-			count, search_text);
+			count, original_search_text);
 		msgwin_msg_add(COLOR_BLUE, -1, NULL, ngettext(
 			"Found %d match for \"%s\".", "Found %d matches for \"%s\".", count),
-			count, search_text);
+			count, original_search_text);
 	}
 }
 
@@ -2135,7 +2151,7 @@ void search_find_again(gboolean change_direction)
 	if (search_data.text)
 	{
 		gboolean forward = ! search_data.backwards;
-		gint result = document_find_text(doc, search_data.text, search_data.flags,
+		gint result = document_find_text(doc, search_data.text, search_data.original_text, search_data.flags,
 			change_direction ? forward : !forward, FALSE, NULL);
 
 		if (result > -1)
@@ -2146,5 +2162,3 @@ void search_find_again(gboolean change_direction)
 				toolbar_get_widget_child_by_name("SearchEntry"), (result > -1));
 	}
 }
-
-
diff --git a/src/search.h b/src/search.h
index 0bfd596..a100b09 100644
--- a/src/search.h
+++ b/src/search.h
@@ -39,6 +39,8 @@ typedef struct GeanySearchData
 	/* set to TRUE when text was set by a search bar callback to keep track of
 	 * search bar background colour */
 	gboolean	search_bar;
+	/* text as it was entered by user */
+	gchar		*original_text;
 }
 GeanySearchData;
 
@@ -85,7 +87,7 @@ gint search_find_text(struct _ScintillaObject *sci, gint flags, struct Sci_TextT
 
 void search_find_again(gboolean change_direction);
 
-void search_find_usage(const gchar *search_text, gint flags, gboolean in_session);
+void search_find_usage(const gchar *search_text, const gchar *original_search_text, gint flags, gboolean in_session);
 
 void search_find_selection(GeanyDocument *doc, gboolean search_backwards);
 
-- 
1.7.5.1

