[gedit/wip/search-use-new-api] Search: use the new API for the regex replace errors



commit da33bb841c59d6f976a0cd71b1948ba29be21431
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Aug 30 16:06:50 2013 +0200

    Search: use the new API for the regex replace errors

 gedit/gedit-commands-search.c      |   24 ++++++++++-
 gedit/gedit-replace-dialog.c       |   72 ++++++++++++++++++++++-------------
 gedit/gedit-replace-dialog.h       |    3 +
 plugins/spell/gedit-spell-plugin.c |    2 +-
 4 files changed, 70 insertions(+), 31 deletions(-)
---
diff --git a/gedit/gedit-commands-search.c b/gedit/gedit-commands-search.c
index d92009c..64fb417 100644
--- a/gedit/gedit-commands-search.c
+++ b/gedit/gedit-commands-search.c
@@ -404,6 +404,7 @@ do_replace (GeditReplaceDialog *dialog,
        gchar *unescaped_replace_text;
        GtkTextIter start;
        GtkTextIter end;
+       GError *error = NULL;
 
        doc = gedit_window_get_active_document (window);
 
@@ -431,10 +432,17 @@ do_replace (GeditReplaceDialog *dialog,
                                           &start,
                                           &end,
                                           unescaped_replace_text,
-                                          -1);
+                                          -1,
+                                          &error);
 
        g_free (unescaped_replace_text);
 
+       if (error != NULL)
+       {
+               gedit_replace_dialog_set_replace_error (dialog, error->message);
+               g_error_free (error);
+       }
+
        do_find (dialog, window);
 }
 
@@ -447,6 +455,7 @@ do_replace_all (GeditReplaceDialog *dialog,
        const gchar *replace_entry_text;
        gchar *unescaped_replace_text;
        gint count;
+       GError *error = NULL;
 
        doc = gedit_window_get_active_document (window);
 
@@ -468,7 +477,10 @@ do_replace_all (GeditReplaceDialog *dialog,
 
        unescaped_replace_text = gtk_source_utils_unescape_search_text (replace_entry_text);
 
-       count = gtk_source_search_context_replace_all (search_context, unescaped_replace_text, -1);
+       count = gtk_source_search_context_replace_all (search_context,
+                                                      unescaped_replace_text,
+                                                      -1,
+                                                      &error);
 
        g_free (unescaped_replace_text);
 
@@ -476,10 +488,16 @@ do_replace_all (GeditReplaceDialog *dialog,
        {
                text_found (window, count);
        }
-       else
+       else if (error == NULL)
        {
                text_not_found (window, dialog);
        }
+
+       if (error != NULL)
+       {
+               gedit_replace_dialog_set_replace_error (dialog, error->message);
+               g_error_free (error);
+       }
 }
 
 static void
diff --git a/gedit/gedit-replace-dialog.c b/gedit/gedit-replace-dialog.c
index 7b8cd68..909b3e8 100644
--- a/gedit/gedit-replace-dialog.c
+++ b/gedit/gedit-replace-dialog.c
@@ -141,22 +141,29 @@ set_search_error (GeditReplaceDialog *dialog,
        set_error (GTK_ENTRY (dialog->priv->search_text_entry), error_msg);
 }
 
-static void
-set_replace_error (GeditReplaceDialog *dialog,
-                  const gchar        *error_msg)
+void
+gedit_replace_dialog_set_replace_error (GeditReplaceDialog *dialog,
+                                       const gchar        *error_msg)
 {
        set_error (GTK_ENTRY (dialog->priv->replace_text_entry), error_msg);
 }
 
+static gboolean
+has_replace_error (GeditReplaceDialog *dialog)
+{
+       GIcon *icon = gtk_entry_get_icon_gicon (GTK_ENTRY (dialog->priv->replace_text_entry),
+                                               GTK_ENTRY_ICON_SECONDARY);
+
+       return icon != NULL;
+}
+
 static void
 update_regex_error (GeditReplaceDialog *dialog)
 {
        GtkSourceSearchContext *search_context;
        GError *regex_error;
-       GtkSourceRegexSearchState regex_state;
 
        set_search_error (dialog, NULL);
-       set_replace_error (dialog, NULL);
 
        search_context = get_search_context (dialog, dialog->priv->active_document);
 
@@ -166,29 +173,12 @@ update_regex_error (GeditReplaceDialog *dialog)
        }
 
        regex_error = gtk_source_search_context_get_regex_error (search_context);
-       regex_state = gtk_source_search_context_get_regex_state (search_context);
 
-       if (regex_error == NULL)
+       if (regex_error != NULL)
        {
-               return;
+               set_search_error (dialog, regex_error->message);
+               g_error_free (regex_error);
        }
-
-       switch (regex_state)
-       {
-               case GTK_SOURCE_REGEX_SEARCH_COMPILATION_ERROR:
-               case GTK_SOURCE_REGEX_SEARCH_MATCHING_ERROR:
-                       set_search_error (dialog, regex_error->message);
-                       break;
-
-               case GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR:
-                       set_replace_error (dialog, regex_error->message);
-                       break;
-
-               default:
-                       g_return_if_reached ();
-       }
-
-       g_error_free (regex_error);
 }
 
 static gboolean
@@ -199,6 +189,16 @@ update_replace_response_sensitivity_cb (GeditReplaceDialog *dialog)
        GtkTextIter end;
        gint pos;
 
+       if (has_replace_error (dialog))
+       {
+               gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
+                                                  GEDIT_REPLACE_DIALOG_REPLACE_RESPONSE,
+                                                  FALSE);
+
+               dialog->priv->idle_update_sensitivity_id = 0;
+               return G_SOURCE_REMOVE;
+       }
+
        search_context = get_search_context (dialog, dialog->priv->active_document);
 
        if (search_context == NULL)
@@ -291,14 +291,18 @@ update_responses_sensitivity (GeditReplaceDialog *dialog)
 
                regex_state = gtk_source_search_context_get_regex_state (search_context);
 
-               sensitive = (regex_state == GTK_SOURCE_REGEX_SEARCH_NO_ERROR ||
-                            regex_state == GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR);
+               sensitive = regex_state == GTK_SOURCE_REGEX_SEARCH_NO_ERROR;
        }
 
        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                                           GEDIT_REPLACE_DIALOG_FIND_RESPONSE,
                                           sensitive);
 
+       if (has_replace_error (dialog))
+       {
+               sensitive = FALSE;
+       }
+
        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                                           GEDIT_REPLACE_DIALOG_REPLACE_ALL_RESPONSE,
                                           sensitive);
@@ -486,6 +490,15 @@ search_text_entry_changed (GtkEditable        *editable,
        update_responses_sensitivity (dialog);
 }
 
+static void
+replace_text_entry_changed (GtkEditable        *editable,
+                           GeditReplaceDialog *dialog)
+{
+       gedit_replace_dialog_set_replace_error (dialog, NULL);
+
+       update_responses_sensitivity (dialog);
+}
+
 /* TODO: move in gedit-document.c and share it with gedit-view-frame */
 static gboolean
 get_selected_text (GtkTextBuffer  *doc,
@@ -636,6 +649,11 @@ gedit_replace_dialog_init (GeditReplaceDialog *dlg)
                          G_CALLBACK (search_text_entry_changed),
                          dlg);
 
+       g_signal_connect (dlg->priv->replace_text_entry,
+                         "changed",
+                         G_CALLBACK (replace_text_entry_changed),
+                         dlg);
+
        dlg->priv->search_settings = gtk_source_search_settings_new ();
 
        g_object_bind_property (dlg->priv->match_case_checkbutton, "active",
diff --git a/gedit/gedit-replace-dialog.h b/gedit/gedit-replace-dialog.h
index f26d919..a989e4c 100644
--- a/gedit/gedit-replace-dialog.h
+++ b/gedit/gedit-replace-dialog.h
@@ -97,6 +97,9 @@ const gchar           *gedit_replace_dialog_get_replace_text          (GeditReplaceDialog 
*dialog
 
 gboolean                gedit_replace_dialog_get_backwards             (GeditReplaceDialog *dialog);
 
+void                    gedit_replace_dialog_set_replace_error         (GeditReplaceDialog *dialog,
+                                                                        const gchar        *error_msg);
+
 G_END_DECLS
 
 #endif  /* __GEDIT_REPLACE_DIALOG_H__  */
diff --git a/plugins/spell/gedit-spell-plugin.c b/plugins/spell/gedit-spell-plugin.c
index 0bc498a..007a582 100644
--- a/plugins/spell/gedit-spell-plugin.c
+++ b/plugins/spell/gedit-spell-plugin.c
@@ -701,7 +701,7 @@ change_all_cb (GeditSpellCheckerDialog *dlg,
 
        gtk_source_search_context_set_highlight (search_context, FALSE);
 
-       gtk_source_search_context_replace_all (search_context, change, -1);
+       gtk_source_search_context_replace_all (search_context, change, -1, NULL);
 
        update_current (doc, range->mw_start + g_utf8_strlen (change, -1));
 


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