[gtksourceview/wip/regex-search: 9/16] Regex search: replace all with g_regex_replace()



commit 613a26db270e8a8b6097d002c29ff520b22f3866
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Jul 21 17:56:08 2013 +0200

    Regex search: replace all with g_regex_replace()

 gtksourceview/gtksourcesearch.c |   62 +++++++++++++++++++++++++-------------
 1 files changed, 41 insertions(+), 21 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearch.c b/gtksourceview/gtksourcesearch.c
index 325a888..eb53f8d 100644
--- a/gtksourceview/gtksourcesearch.c
+++ b/gtksourceview/gtksourcesearch.c
@@ -2870,15 +2870,14 @@ _gtk_source_search_backward_finish (GtkSourceSearch  *search,
                                                  error);
 }
 
-static void
-regex_replace (GtkSourceSearch   *search,
-              const GtkTextIter *match_start,
-              const GtkTextIter *match_end,
-              const gchar       *replace)
+/* Returns %TRUE if replaced. */
+static gboolean
+regex_replace (GtkSourceSearch *search,
+              GtkTextIter     *match_start,
+              GtkTextIter     *match_end,
+              const gchar     *replace)
 {
        GtkTextIter real_start;
-       GtkTextIter m_start = *match_start;
-       GtkTextIter m_end = *match_end;
        gint start_pos;
        gchar *subject;
        gchar *subject_replaced;
@@ -2908,15 +2907,16 @@ regex_replace (GtkSourceSearch   *search,
                g_warning ("Regex replace error: %s", error->message);
                g_error_free (error);
                g_free (subject_replaced);
-               return;
+               return FALSE;
        }
 
        gtk_text_buffer_begin_user_action (search->priv->buffer);
-       gtk_text_buffer_delete (search->priv->buffer, &m_start, &m_end);
-       gtk_text_buffer_insert (search->priv->buffer, &m_start, subject_replaced, -1);
+       gtk_text_buffer_delete (search->priv->buffer, match_start, match_end);
+       gtk_text_buffer_insert (search->priv->buffer, match_end, subject_replaced, -1);
        gtk_text_buffer_end_user_action (search->priv->buffer);
 
        g_free (subject_replaced);
+       return TRUE;
 }
 
 gboolean
@@ -2952,16 +2952,14 @@ _gtk_source_search_replace (GtkSourceSearch   *search,
 
        if (search->priv->regex_enabled)
        {
-               regex_replace (search, &start, &end, replace);
-       }
-       else
-       {
-               gtk_text_buffer_begin_user_action (search->priv->buffer);
-               gtk_text_buffer_delete (search->priv->buffer, &start, &end);
-               gtk_text_buffer_insert (search->priv->buffer, &start, replace, replace_length);
-               gtk_text_buffer_end_user_action (search->priv->buffer);
+               return regex_replace (search, &start, &end, replace);
        }
 
+       gtk_text_buffer_begin_user_action (search->priv->buffer);
+       gtk_text_buffer_delete (search->priv->buffer, &start, &end);
+       gtk_text_buffer_insert (search->priv->buffer, &start, replace, replace_length);
+       gtk_text_buffer_end_user_action (search->priv->buffer);
+
        return TRUE;
 }
 
@@ -2975,6 +2973,7 @@ _gtk_source_search_replace_all (GtkSourceSearch *search,
        GtkTextIter match_end;
        guint nb_matches_replaced = 0;
        gboolean highlight_matching_brackets;
+       gboolean has_regex_references = FALSE;
 
        g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), 0);
        g_return_val_if_fail (replace != NULL, 0);
@@ -2984,6 +2983,12 @@ _gtk_source_search_replace_all (GtkSourceSearch *search,
                return 0;
        }
 
+       if (search->priv->regex_enabled &&
+           !g_regex_check_replacement (replace, &has_regex_references, NULL))
+       {
+               return 0;
+       }
+
        g_signal_handlers_block_by_func (search->priv->buffer, insert_text_before_cb, search);
        g_signal_handlers_block_by_func (search->priv->buffer, insert_text_after_cb, search);
        g_signal_handlers_block_by_func (search->priv->buffer, delete_range_before_cb, search);
@@ -3001,11 +3006,26 @@ _gtk_source_search_replace_all (GtkSourceSearch *search,
 
        while (smart_forward_search (search, &iter, &match_start, &match_end))
        {
-               gtk_text_buffer_delete (search->priv->buffer, &match_start, &match_end);
-               gtk_text_buffer_insert (search->priv->buffer, &match_end, replace, replace_length);
+               gboolean replaced = FALSE;
+
+               if (has_regex_references)
+               {
+                       replaced = regex_replace (search, &match_start, &match_end, replace);
+               }
+               else
+               {
+                       gtk_text_buffer_delete (search->priv->buffer, &match_start, &match_end);
+                       gtk_text_buffer_insert (search->priv->buffer, &match_end, replace, replace_length);
+
+                       replaced = TRUE;
+               }
+
+               if (replaced)
+               {
+                       nb_matches_replaced++;
+               }
 
                iter = match_end;
-               nb_matches_replaced++;
        }
 
        gtk_text_buffer_end_user_action (search->priv->buffer);


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