[gtksourceview/wip/regex-search: 4/16] Get regex search error



commit f936486ba4d8cf90b6f28055d7815a3867dd23be
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jul 20 16:34:45 2013 +0200

    Get regex search error
    
    A property is the more convenient way, in my opinion. With a getter
    function.
    
    Another way: add GError parameters to all the search-related setters
    (set search text, set case sensitive, at word boundaries, etc.).
    But when using the properties directly (with a binding for example), we
    don't get the regex error. In this case, a signal would be needed.
    So the first solution, with the property, avoid GError parameters
    everywhere, and to have the signal we can connect to
    notify::regex-search-error.

 gtksourceview/gtksourcebuffer.c |   45 ++++++++++++++++++++++++++++++++++++++-
 gtksourceview/gtksourcebuffer.h |    2 +
 gtksourceview/gtksourcesearch.c |   41 +++++++++++++++++++++++++++++++----
 gtksourceview/gtksourcesearch.h |    3 ++
 4 files changed, 85 insertions(+), 6 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index f79c338..9fbefb5 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -173,7 +173,8 @@ enum {
        PROP_CASE_SENSITIVE_SEARCH,
        PROP_SEARCH_AT_WORD_BOUNDARIES,
        PROP_SEARCH_WRAP_AROUND,
-       PROP_REGEX_SEARCH
+       PROP_REGEX_SEARCH,
+       PROP_REGEX_SEARCH_ERROR
 };
 
 struct _GtkSourceBufferPrivate
@@ -500,6 +501,24 @@ gtk_source_buffer_class_init (GtkSourceBufferClass *klass)
                                                               FALSE,
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 
+       /**
+        * GtkSourceBuffer:regex-search-error:
+        *
+        * If the regex search pattern doesn't follow all the rules,
+        * #GtkSourceBuffer:regex-search-error will be set. If the pattern
+        * doesn't have errors, #GtkSourceBuffer:regex-search-error is %NULL.
+        *
+        * Free with g_error_free().
+        *
+        * Since: 3.10
+        */
+       g_object_class_install_property (object_class,
+                                        PROP_REGEX_SEARCH_ERROR,
+                                        g_param_spec_pointer ("regex-search-error",
+                                                              _("Regex search error"),
+                                                              _("Regular expression search error"),
+                                                              G_PARAM_READABLE));
+
        param_types[0] = GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE;
        param_types[1] = GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE;
 
@@ -842,6 +861,10 @@ gtk_source_buffer_get_property (GObject    *object,
                        g_value_set_boolean (value, _gtk_source_search_get_regex_enabled 
(source_buffer->priv->search));
                        break;
 
+               case PROP_REGEX_SEARCH_ERROR:
+                       g_value_set_pointer (value, _gtk_source_search_get_regex_error 
(source_buffer->priv->search));
+                       break;
+
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -2947,6 +2970,26 @@ gtk_source_buffer_get_regex_search (GtkSourceBuffer *buffer)
 }
 
 /**
+ * gtk_source_buffer_get_regex_search_error:
+ * @buffer: a #GtkSourceBuffer.
+ *
+ * Regular expression patterns must follow certain rules. If
+ * #GtkSourceBuffer:search-text breaks a rule, the error can be retrieved with
+ * this function. The error domain is #G_REGEX_ERROR.
+ *
+ * Free the return value with g_error_free().
+ *
+ * Returns: the #GError, or %NULL.
+ */
+GError *
+gtk_source_buffer_get_regex_search_error (GtkSourceBuffer *buffer)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
+
+       return _gtk_source_search_get_regex_error (buffer->priv->search);
+}
+
+/**
  * gtk_source_buffer_set_highlight_search:
  * @buffer: a #GtkSourceBuffer.
  * @highlight: the setting.
diff --git a/gtksourceview/gtksourcebuffer.h b/gtksourceview/gtksourcebuffer.h
index 144f414..72e6dc9 100644
--- a/gtksourceview/gtksourcebuffer.h
+++ b/gtksourceview/gtksourcebuffer.h
@@ -206,6 +206,8 @@ void                         gtk_source_buffer_set_regex_search                     
(GtkSourceBuffer        *buffer,
 
 gboolean                gtk_source_buffer_get_regex_search                     (GtkSourceBuffer        
*buffer);
 
+GError                 *gtk_source_buffer_get_regex_search_error               (GtkSourceBuffer        
*buffer);
+
 void                    gtk_source_buffer_set_highlight_search                 (GtkSourceBuffer        
*buffer,
                                                                                 gboolean                
highlight);
 
diff --git a/gtksourceview/gtksourcesearch.c b/gtksourceview/gtksourcesearch.c
index 54a78ef..0067b4c 100644
--- a/gtksourceview/gtksourcesearch.c
+++ b/gtksourceview/gtksourcesearch.c
@@ -196,6 +196,7 @@ struct _GtkSourceSearchPrivate
        gchar *text;
        gint text_nb_lines;
        GRegex *regex;
+       GError *regex_error;
        GtkTextSearchFlags flags;
        guint at_word_boundaries : 1;
        guint wrap_around : 1;
@@ -2069,17 +2070,25 @@ add_subregion_to_scan (GtkSourceSearch   *search,
 static void
 update_regex (GtkSourceSearch *search)
 {
+       gboolean regex_error_changed = FALSE;
+
        if (search->priv->regex != NULL)
        {
                g_regex_unref (search->priv->regex);
                search->priv->regex = NULL;
        }
 
+       if (search->priv->regex_error != NULL)
+       {
+               g_error_free (search->priv->regex_error);
+               search->priv->regex_error = NULL;
+               regex_error_changed = TRUE;
+       }
+
        if (search->priv->regex_enabled && search->priv->text != NULL)
        {
                GRegexCompileFlags compile_flags = G_REGEX_OPTIMIZE | G_REGEX_MULTILINE;
                gchar *pattern = search->priv->text;
-               GError *error = NULL;
 
                search->priv->text_nb_lines = 0;
 
@@ -2096,12 +2105,11 @@ update_regex (GtkSourceSearch *search)
                search->priv->regex = g_regex_new (pattern,
                                                   compile_flags,
                                                   G_REGEX_MATCH_NOTEMPTY,
-                                                  &error);
+                                                  &search->priv->regex_error);
 
-               if (error != NULL)
+               if (search->priv->regex_error != NULL)
                {
-                       g_warning ("Error with the regex: %s", error->message);
-                       g_error_free (error);
+                       regex_error_changed = TRUE;
                }
 
                if (search->priv->at_word_boundaries)
@@ -2109,6 +2117,11 @@ update_regex (GtkSourceSearch *search)
                        g_free (pattern);
                }
        }
+
+       if (regex_error_changed)
+       {
+               g_object_notify (G_OBJECT (search->priv->buffer), "regex-search-error");
+       }
 }
 
 static void
@@ -2283,6 +2296,11 @@ _gtk_source_search_finalize (GObject *object)
                g_regex_unref (search->priv->regex);
        }
 
+       if (search->priv->regex_error != NULL)
+       {
+               g_error_free (search->priv->regex_error);
+       }
+
        G_OBJECT_CLASS (_gtk_source_search_parent_class)->finalize (object);
 }
 
@@ -2474,6 +2492,19 @@ _gtk_source_search_get_regex_enabled (GtkSourceSearch *search)
        return search->priv->regex_enabled;
 }
 
+GError *
+_gtk_source_search_get_regex_error (GtkSourceSearch *search)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), NULL);
+
+       if (search->priv->regex_error == NULL)
+       {
+               return NULL;
+       }
+
+       return g_error_copy (search->priv->regex_error);
+}
+
 void
 _gtk_source_search_set_highlight (GtkSourceSearch *search,
                                  gboolean         highlight)
diff --git a/gtksourceview/gtksourcesearch.h b/gtksourceview/gtksourcesearch.h
index fc7ed5d..6413043 100644
--- a/gtksourceview/gtksourcesearch.h
+++ b/gtksourceview/gtksourcesearch.h
@@ -93,6 +93,9 @@ G_GNUC_INTERNAL
 gboolean               _gtk_source_search_get_regex_enabled            (GtkSourceSearch        *search);
 
 G_GNUC_INTERNAL
+GError *               _gtk_source_search_get_regex_error              (GtkSourceSearch        *search);
+
+G_GNUC_INTERNAL
 void                   _gtk_source_search_set_highlight                (GtkSourceSearch        *search,
                                                                         gboolean                highlight);
 


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