[gtksourceview/wip/search-revalidate-iters] SearchContext: add replace2() function that revalidates iters



commit de1d28913347b178a68b03cad2d78aa5d6736efd
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jun 4 15:14:02 2016 +0200

    SearchContext: add replace2() function that revalidates iters
    
    https://bugzilla.gnome.org/show_bug.cgi?id=754883

 gtksourceview/gtksourcesearchcontext.c |   84 ++++++++++++++++++++++++++++++++
 gtksourceview/gtksourcesearchcontext.h |    8 +++
 2 files changed, 92 insertions(+), 0 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index dd4dbee..2f9006a 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -3591,6 +3591,90 @@ gtk_source_search_context_replace (GtkSourceSearchContext  *search,
 }
 
 /**
+ * gtk_source_search_context_replace2:
+ * @search: a #GtkSourceSearchContext.
+ * @match_start: the start of the match to replace.
+ * @match_end: the end of the match to replace.
+ * @replace: the replacement text.
+ * @replace_length: the length of @replace in bytes, or -1.
+ * @error: location to a #GError, or %NULL to ignore errors.
+ *
+ * Replaces a search match by another text. If @match_start and @match_end
+ * doesn't correspond to a search match, %FALSE is returned.
+ *
+ * Unlike with gtk_source_search_context_replace(), the @match_start and
+ * @match_end iters are revalidated to point to the replacement text boundaries.
+ *
+ * For a regular expression replacement, you can check if @replace is valid by
+ * calling g_regex_check_replacement(). The @replace text can contain
+ * backreferences; read the g_regex_replace() documentation for more details.
+ *
+ * Returns: whether the match has been replaced.
+ * Since: 3.22
+ */
+gboolean
+gtk_source_search_context_replace2 (GtkSourceSearchContext  *search,
+                                   GtkTextIter             *match_start,
+                                   GtkTextIter             *match_end,
+                                   const gchar             *replace,
+                                   gint                     replace_length,
+                                   GError                 **error)
+{
+       GtkTextIter start;
+       GtkTextIter end;
+       GtkTextMark *start_mark;
+       gboolean replaced = FALSE;
+
+       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search), FALSE);
+       g_return_val_if_fail (match_start != NULL, FALSE);
+       g_return_val_if_fail (match_end != NULL, FALSE);
+       g_return_val_if_fail (replace != NULL, FALSE);
+       g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+       if (search->priv->buffer == NULL)
+       {
+               return FALSE;
+       }
+
+       if (!smart_forward_search (search, match_start, &start, &end))
+       {
+               return FALSE;
+       }
+
+       if (!gtk_text_iter_equal (match_start, &start) ||
+           !gtk_text_iter_equal (match_end, &end))
+       {
+               return FALSE;
+       }
+
+       start_mark = gtk_text_buffer_create_mark (search->priv->buffer, NULL, &start, TRUE);
+
+       if (gtk_source_search_settings_get_regex_enabled (search->priv->settings))
+       {
+               replaced = regex_replace (search, &start, &end, replace, error);
+       }
+       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, &end, replace, replace_length);
+               gtk_text_buffer_end_user_action (search->priv->buffer);
+
+               replaced = TRUE;
+       }
+
+       if (replaced)
+       {
+               gtk_text_buffer_get_iter_at_mark (search->priv->buffer, match_start, start_mark);
+               *match_end = end;
+       }
+
+       gtk_text_buffer_delete_mark (search->priv->buffer, start_mark);
+
+       return replaced;
+}
+
+/**
  * gtk_source_search_context_replace_all:
  * @search: a #GtkSourceSearchContext.
  * @replace: the replacement text.
diff --git a/gtksourceview/gtksourcesearchcontext.h b/gtksourceview/gtksourcesearchcontext.h
index 67e14b1..0269e2c 100644
--- a/gtksourceview/gtksourcesearchcontext.h
+++ b/gtksourceview/gtksourcesearchcontext.h
@@ -141,6 +141,14 @@ gboolean            gtk_source_search_context_replace                      
(GtkSourceSearchContext  *search,
                                                                                 gint                     
replace_length,
                                                                                 GError                 
**error);
 
+GTK_SOURCE_AVAILABLE_IN_3_22
+gboolean                gtk_source_search_context_replace2                     (GtkSourceSearchContext  
*search,
+                                                                                GtkTextIter             
*match_start,
+                                                                                GtkTextIter             
*match_end,
+                                                                                const gchar             
*replace,
+                                                                                gint                     
replace_length,
+                                                                                GError                 
**error);
+
 GTK_SOURCE_AVAILABLE_IN_3_10
 guint                   gtk_source_search_context_replace_all                  (GtkSourceSearchContext  
*search,
                                                                                 const gchar             
*replace,


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