[gtksourceview/wip/regex-search] SearchContext: set the SearchSettings



commit 6427126ac112c01e39c4972bcbd8c76ba78dbf64
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Jul 30 15:13:18 2013 +0200

    SearchContext: set the SearchSettings
    
    The code doesn't compile.

 gtksourceview/gtksourcesearchcontext.c |  455 ++++++++------------------------
 gtksourceview/gtksourcesearchcontext.h |   39 ++--
 2 files changed, 135 insertions(+), 359 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index 1b6e053..3a43018 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -246,6 +246,17 @@ dispose_has_run (GtkSourceSearchContext *search)
        return search->priv->buffer == NULL;
 }
 
+static GtkSourceSearchSettings *
+get_settings (GtkSourceSearchContext *search)
+{
+       if (search->priv->settings == NULL)
+       {
+               search->priv->settings = gtk_source_search_settings_new ();
+       }
+
+       return search->priv->settings;
+}
+
 static void
 sync_found_tag (GtkSourceSearchContext *search)
 {
@@ -257,11 +268,13 @@ sync_found_tag (GtkSourceSearchContext *search)
                return;
        }
 
+       /*
        if (!search->priv->highlight)
        {
                _gtk_source_style_apply (NULL, search->priv->found_tag);
                return;
        }
+       */
 
        style_scheme = gtk_source_buffer_get_style_scheme (GTK_SOURCE_BUFFER (search->priv->buffer));
 
@@ -2376,6 +2389,80 @@ set_buffer (GtkSourceSearchContext *search,
                                 G_CONNECT_AFTER | G_CONNECT_SWAPPED);
 }
 
+static gint
+compute_number_of_lines (const gchar *text)
+{
+       const gchar *p;
+       gint len;
+       gint nb_of_lines = 1;
+
+       if (text == NULL)
+       {
+               return 0;
+       }
+
+       len = strlen (text);
+       p = text;
+
+       while (len > 0)
+       {
+               gint delimiter;
+               gint next_paragraph;
+
+               pango_find_paragraph_boundary (p, len, &delimiter, &next_paragraph);
+
+               if (delimiter == next_paragraph)
+               {
+                       /* not found */
+                       break;
+               }
+
+               p += next_paragraph;
+               len -= next_paragraph;
+               nb_of_lines++;
+       }
+
+       return nb_of_lines;
+}
+
+static void
+search_text_updated (GtkSourceSearchContext *search)
+{
+       GtkSourceSearchSettings *settings = get_settings (search);
+
+       if (gtk_source_search_settings_get_regex_enabled (settings))
+       {
+               search->priv->text_nb_lines = 0;
+       }
+       else
+       {
+               gchar *text = gtk_source_search_settings_get_search_text (settings);
+               search->priv->text_nb_lines = compute_number_of_lines (text);
+       }
+}
+
+static void
+settings_notify_cb (GtkSourceSearchContext  *search,
+                   GParamSpec              *pspec,
+                   GtkSourceSearchSettings *settings)
+{
+       const gchar *property = g_param_spec_get_name (pspec);
+
+       if (g_str_equal (property, "wrap-around"))
+       {
+               update (search);
+               return;
+       }
+
+       if (g_str_equal (property, "search-text"))
+       {
+               search_text_updated (search);
+       }
+
+       update_regex (search);
+       update (search);
+}
+
 static void
 gtk_source_search_context_dispose (GObject *object)
 {
@@ -2422,16 +2509,12 @@ gtk_source_search_context_get_property (GObject    *object,
 
        switch (prop_id)
        {
-               case PROP_HIGHLIGHT:
-                       g_value_set_boolean (value, gtk_source_search_context_get_highlight 
(source_buffer->priv->search));
-                       break;
-
                case PROP_BUFFER:
                        g_value_set_object (value, search->priv->buffer);
                        break;
 
                case PROP_SETTINGS:
-                       g_value_set_object (value, search->priv->settings);
+                       g_value_set_object (value, get_settings (search));
                        break;
 
                case PROP_OCCURRENCES_COUNT:
@@ -2462,19 +2545,12 @@ gtk_source_search_context_set_property (GObject      *object,
 
        switch (prop_id)
        {
-               case PROP_HIGHLIGHT:
-                       gtk_source_search_context_set_highlight (source_buffer->priv->search,
-                                                                g_value_get_boolean (value));
-                       break;
-
                case PROP_BUFFER:
                        set_buffer (search, g_value_get_object (value));
                        break;
 
                case PROP_SETTINGS:
-                       g_clear_object (&search->priv->settings);
-                       search->priv->settings = g_value_get_object (value);
-                       g_object_ref (search->priv->settings);
+                       gtk_source_search_context_set_settings (search, g_value_get_object (value));
                        break;
 
                default:
@@ -2594,310 +2670,51 @@ gtk_source_search_context_new (GtkSourceBuffer         *buffer,
                             NULL);
 }
 
-static gint
-compute_number_of_lines (const gchar *text)
-{
-       const gchar *p;
-       gint len;
-       gint nb_of_lines = 1;
-
-       if (text == NULL)
-       {
-               return 0;
-       }
-
-       len = strlen (text);
-       p = text;
-
-       while (len > 0)
-       {
-               gint delimiter;
-               gint next_paragraph;
-
-               pango_find_paragraph_boundary (p, len, &delimiter, &next_paragraph);
-
-               if (delimiter == next_paragraph)
-               {
-                       /* not found */
-                       break;
-               }
-
-               p += next_paragraph;
-               len -= next_paragraph;
-               nb_of_lines++;
-       }
-
-       return nb_of_lines;
-}
-
-/**
- * gtk_source_search_context_set_search_text:
- * @search: a #GtkSourceSearchContext.
- * @text: (allow-none): the nul-terminated text to search, or %NULL to disable the search.
- *
- * Sets the text to search. If @text is %NULL or is empty, the search will be
- * disabled. A copy of @text will be made, so you can safely free @text after
- * a call to this function.
- *
- * You may be interested to call gtk_source_utils_unescape_search_text() before
- * this function.
- *
- * Since: 3.10
- */
-void
-gtk_source_search_context_set_search_text (GtkSourceSearchContext *search,
-                                          const gchar            *text)
-{
-       g_return_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search));
-       g_return_if_fail (text == NULL || g_utf8_validate (text, -1, NULL));
-
-       if ((search->priv->text == NULL && (text == NULL || text[0] == '\0')) ||
-           g_strcmp0 (search->priv->text, text) == 0)
-       {
-               return;
-       }
-
-       g_free (search->priv->text);
-
-       if (text == NULL || *text == '\0')
-       {
-               search->priv->text = NULL;
-       }
-       else
-       {
-               search->priv->text = g_strdup (text);
-       }
-
-       if (search->priv->regex_enabled)
-       {
-               search->priv->text_nb_lines = 0;
-       }
-       else
-       {
-               search->priv->text_nb_lines = compute_number_of_lines (search->priv->text);
-       }
-
-       update_regex (search);
-       update (search);
-
-       g_object_notify (G_OBJECT (search), "search-text");
-}
-
-/**
- * gtk_source_search_context_get_search_text:
- * @search: a #GtkSourceSearchContext.
- *
- * Gets the text to search. The return value must not be freed.
- *
- * You may be interested to call gtk_source_utils_escape_search_text() after
- * this function.
- *
- * Returns: the text to search, or %NULL if the search is disabled.
- * Since: 3.10
- */
-const gchar *
-gtk_source_search_context_get_search_text (GtkSourceSearchContext *search)
+GtkSourceBuffer *
+gtk_source_search_context_get_buffer (GtkSourceSearchContext *search)
 {
        g_return_val_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search), NULL);
 
-       return search->priv->text;
-}
-
-/**
- * gtk_source_search_context_set_case_sensitive:
- * @search: a #GtkSourceSearchContext.
- * @case_sensitive: the setting.
- *
- * Enables or disables the case sensitivity for the search.
- *
- * Since: 3.10
- */
-void
-gtk_source_search_context_set_case_sensitive (GtkSourceSearchContext *search,
-                                             gboolean                case_sensitive)
-{
-       gboolean cur_val;
-
-       g_return_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search));
-
-       case_sensitive = case_sensitive != FALSE;
-
-       cur_val = gtk_source_search_context_get_case_sensitive (search);
-
-       if (cur_val == case_sensitive)
-       {
-               return;
-       }
-
-       if (case_sensitive)
-       {
-               search->priv->flags &= ~GTK_TEXT_SEARCH_CASE_INSENSITIVE;
-       }
-       else
-       {
-               search->priv->flags |= GTK_TEXT_SEARCH_CASE_INSENSITIVE;
-       }
-
-       update_regex (search);
-       update (search);
-
-       g_object_notify (G_OBJECT (buffer), "case-sensitive");
+       return GTK_SOURCE_BUFFER (search->priv->buffer);
 }
 
-/**
- * gtk_source_search_context_get_case_sensitive:
- * @search: a #GtkSourceSearchContext.
- *
- * Returns: whether the search is case sensitive.
- * Since: 3.10
- */
-gboolean
-gtk_source_search_context_get_case_sensitive (GtkSourceSearchContext *search)
+GtkSourceSearchSettings *
+gtk_source_search_context_get_settings (GtkSourceSearchContext *search)
 {
-       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search), FALSE);
+       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search), NULL);
 
-       return (search->priv->flags & GTK_TEXT_SEARCH_CASE_INSENSITIVE) == 0;
+       return get_settings (search);
 }
 
-/**
- * gtk_source_search_context_set_at_word_boundaries:
- * @search: a #GtkSourceSearchContext.
- * @at_word_boundaries: the setting.
- *
- * Change whether the search is done at word boundaries. If @at_word_boundaries
- * is %TRUE, a search match must start and end a word. The match can span
- * multiple words. See also gtk_text_iter_starts_word() and
- * gtk_text_iter_ends_word().
- *
- * Since: 3.10
- */
 void
-gtk_source_search_context_set_at_word_boundaries (GtkSourceSearchContext *search,
-                                                 gboolean                at_word_boundaries)
+gtk_source_search_context_set_settings (GtkSourceSearchContext  *search,
+                                       GtkSourceSearchSettings *settings)
 {
        g_return_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search));
+       g_return_if_fail (GTK_SOURCE_IS_SEARCH_SETTINGS (settings));
 
-       at_word_boundaries = at_word_boundaries != FALSE;
-
-       if (search->priv->at_word_boundaries == at_word_boundaries)
+       if (search->priv->settings != NULL)
        {
-               return;
-       }
+               g_signal_handlers_disconnect_by_func (search->priv->settings,
+                                                     settings_notify_cb,
+                                                     search);
 
-       search->priv->at_word_boundaries = at_word_boundaries;
-
-       update_regex (search);
-       update (search);
-
-       g_object_notify (G_OBJECT (buffer), "at-word-boundaries");
-}
-
-/**
- * gtk_source_search_context_get_at_word_boundaries:
- * @search: a #GtkSourceSearchContext.
- *
- * Returns: whether to search at word boundaries.
- * Since: 3.10
- */
-gboolean
-gtk_source_search_context_get_at_word_boundaries (GtkSourceSearchContext *search)
-{
-       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search), FALSE);
-
-       return search->priv->at_word_boundaries;
-}
-
-/**
- * gtk_source_search_context_set_wrap_around:
- * @search: a #GtkSourceSearchContext.
- * @wrap_around: the setting.
- *
- * Enables or disables the wrap around search. If @wrap_around is %TRUE, the
- * forward search continues at the beginning of the buffer if no search
- * occurrences are found. Similarly, the backward search continues to search at
- * the end of the buffer.
- *
- * Since: 3.10
- */
-void
-gtk_source_search_context_set_wrap_around (GtkSourceSearchContext *search,
-                                          gboolean                wrap_around)
-{
-       g_return_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search));
-
-       wrap_around = wrap_around != FALSE;
-
-       if (search->priv->wrap_around == wrap_around)
-       {
-               return;
+               g_object_unref (search->priv->settings);
        }
 
-       search->priv->wrap_around = wrap_around;
-       update (search);
-
-       g_object_notify (G_OBJECT (buffer), "wrap-around");
-}
-
-/**
- * gtk_source_search_context_get_wrap_around:
- * @search: a #GtkSourceSearchContext.
- *
- * Returns: whether to wrap around the search.
- * Since: 3.10
- */
-gboolean
-gtk_source_search_context_get_wrap_around (GtkSourceSearchContext *search)
-{
-       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search), FALSE);
-
-       return search->priv->wrap_around;
-}
-
-/**
- * gtk_source_search_context_set_regex_enabled:
- * @search: a #GtkSourceSearchContext.
- * @regex: the setting.
- *
- * Enables or disables whether to search by regular expressions.
- * If enabled, the #GtkSourceSearchContext:search-text property contains the
- * pattern of the regular expression.
- *
- * Since: 3.10
- */
-void
-gtk_source_search_context_set_regex_enabled (GtkSourceSearchContext *search,
-                                            gboolean                regex_enabled)
-{
-       g_return_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search));
-
-       regex_enabled = regex_enabled != FALSE;
+       search->priv->settings = g_object_ref (settings);
 
-       if (search->priv->regex_enabled == regex)
-       {
-               return;
-       }
-
-       search->priv->regex_enabled = regex_enabled;
+       g_signal_connect_object (settings,
+                                "notify",
+                                G_CALLBACK (settings_notify_cb),
+                                search,
+                                G_CONNECT_SWAPPED);
 
+       search_text_updated (search);
        update_regex (search);
        update (search);
 
-       g_object_notify (G_OBJECT (buffer), "regex-enabled");
-}
-
-/**
- * gtk_source_search_context_get_regex_enabled:
- * @search: a #GtkSourceSearchContext.
- *
- * Returns: whether to search by regular expressions.
- * Since: 3.10
- */
-gboolean
-gtk_source_search_context_get_regex_enabled (GtkSourceSearchContext *search)
-{
-       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search), FALSE);
-
-       return search->priv->regex_enabled;
+       g_object_notify (G_OBJECT (search), "settings");
 }
 
 /**
@@ -2927,54 +2744,6 @@ gtk_source_search_context_get_regex_error (GtkSourceSearchContext *search)
 }
 
 /**
- * gtk_source_search_context_set_highlight:
- * @search: a #GtkSourceSearchContext.
- * @highlight: the setting.
- *
- * Enables or disables search highlighting. If you disable the search
- * highlighting, you can still use the other search and replace functions.
- *
- * Since: 3.10
- */
-void
-gtk_source_search_context_set_highlight (GtkSourceSearchContext *search,
-                                        gboolean                highlight)
-{
-       g_return_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search));
-
-       highlight = highlight != FALSE;
-
-       if (search->priv->highlight == highlight)
-       {
-               return;
-       }
-
-       search->priv->highlight = highlight;
-
-       if (search->priv->found_tag != NULL)
-       {
-               sync_found_tag (search);
-       }
-
-       g_object_notify (G_OBJECT (buffer), "highlight");
-}
-
-/**
- * gtk_source_search_context_get_highlight:
- * @search: a #GtkSourceSearchContext.
- *
- * Returns: whether to highlight search occurrences.
- * Since: 3.10
- */
-gboolean
-gtk_source_search_context_get_highlight (GtkSourceSearchContext *search)
-{
-       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH_CONTEXT (search), FALSE);
-
-       return search->priv->highlight;
-}
-
-/**
  * gtk_source_search_context_get_occurrences_count:
  * @search: a #GtkSourceSearchContext.
  *
diff --git a/gtksourceview/gtksourcesearchcontext.h b/gtksourceview/gtksourcesearchcontext.h
index 9267094..6c0c60a 100644
--- a/gtksourceview/gtksourcesearchcontext.h
+++ b/gtksourceview/gtksourcesearchcontext.h
@@ -49,70 +49,77 @@ struct _GtkSourceSearchContextClass
        GObjectClass parent_class;
 };
 
-GType                  gtk_source_search_context_get_type                      (void) G_GNUC_CONST;
+GType                   gtk_source_search_context_get_type                     (void) G_GNUC_CONST;
 
-GtkSourceSearchContext *gtk_source_search_context_new                          (GtkSourceBuffer         
*buffer,
+GtkSourceSearchContext *gtk_source_search_context_new                          (GtkSourceBuffer         
*buffer,
                                                                                 GtkSourceSearchSettings 
*settings);
 
-GError *               gtk_source_search_context_get_regex_error               (GtkSourceSearchContext  
*search);
+GtkSourceBuffer                *gtk_source_search_context_get_buffer                   
(GtkSourceSearchContext  *search);
 
-void                   gtk_source_search_context_set_highlight                 (GtkSourceSearchContext  
*search,
+GtkSourceSearchSettings        *gtk_source_search_context_get_settings                 
(GtkSourceSearchContext  *search);
+
+void                    gtk_source_search_context_set_settings                 (GtkSourceSearchContext  
*search,
+                                                                                GtkSourceSearchSettings 
*settings);
+
+GError                 *gtk_source_search_context_get_regex_error              (GtkSourceSearchContext  
*search);
+
+void                    gtk_source_search_context_set_highlight                (GtkSourceSearchContext  
*search,
                                                                                 gboolean                 
highlight);
 
-gboolean               gtk_source_search_context_get_highlight                 (GtkSourceSearchContext  
*search);
+gboolean                gtk_source_search_context_get_highlight                (GtkSourceSearchContext  
*search);
 
-gint                   gtk_source_search_context_get_occurrences_count         (GtkSourceSearchContext  
*search);
+gint                    gtk_source_search_context_get_occurrences_count        (GtkSourceSearchContext  
*search);
 
-gint                   gtk_source_search_context_get_occurrence_position       (GtkSourceSearchContext  
*search,
+gint                    gtk_source_search_context_get_occurrence_position      (GtkSourceSearchContext  
*search,
                                                                                 const GtkTextIter       
*match_start,
                                                                                 const GtkTextIter       
*match_end);
 
-gboolean               gtk_source_search_context_forward                       (GtkSourceSearchContext  
*search,
+gboolean                gtk_source_search_context_forward                      (GtkSourceSearchContext  
*search,
                                                                                 const GtkTextIter       
*iter,
                                                                                 GtkTextIter             
*match_start,
                                                                                 GtkTextIter             
*match_end);
 
-void                   gtk_source_search_context_forward_async                 (GtkSourceSearchContext  
*search,
+void                    gtk_source_search_context_forward_async                (GtkSourceSearchContext  
*search,
                                                                                 const GtkTextIter       
*iter,
                                                                                 GCancellable            
*cancellable,
                                                                                 GAsyncReadyCallback      
callback,
                                                                                 gpointer                 
user_data);
 
-gboolean               gtk_source_search_context_forward_finish                (GtkSourceSearchContext  
*search,
+gboolean                gtk_source_search_context_forward_finish               (GtkSourceSearchContext  
*search,
                                                                                 GAsyncResult            
*result,
                                                                                 GtkTextIter             
*match_start,
                                                                                 GtkTextIter             
*match_end,
                                                                                 GError                 
**error);
 
-gboolean               gtk_source_search_context_backward                      (GtkSourceSearchContext  
*search,
+gboolean                gtk_source_search_context_backward                     (GtkSourceSearchContext  
*search,
                                                                                 const GtkTextIter       
*iter,
                                                                                 GtkTextIter             
*match_start,
                                                                                 GtkTextIter             
*match_end);
 
-void                   gtk_source_search_context_backward_async                (GtkSourceSearchContext  
*search,
+void                    gtk_source_search_context_backward_async               (GtkSourceSearchContext  
*search,
                                                                                 const GtkTextIter       
*iter,
                                                                                 GCancellable            
*cancellable,
                                                                                 GAsyncReadyCallback      
callback,
                                                                                 gpointer                 
user_data);
 
-gboolean               gtk_source_search_context_backward_finish               (GtkSourceSearchContext  
*search,
+gboolean                gtk_source_search_context_backward_finish              (GtkSourceSearchContext  
*search,
                                                                                 GAsyncResult            
*result,
                                                                                 GtkTextIter             
*match_start,
                                                                                 GtkTextIter             
*match_end,
                                                                                 GError                 
**error);
 
-gboolean               gtk_source_search_context_replace                       (GtkSourceSearchContext  
*search,
+gboolean                gtk_source_search_context_replace                      (GtkSourceSearchContext  
*search,
                                                                                 const GtkTextIter       
*match_start,
                                                                                 const GtkTextIter       
*match_end,
                                                                                 const gchar             
*replace,
                                                                                 gint                     
replace_length);
 
-guint                  gtk_source_search_context_replace_all                   (GtkSourceSearchContext  
*search,
+guint                   gtk_source_search_context_replace_all                  (GtkSourceSearchContext  
*search,
                                                                                 const gchar             
*replace,
                                                                                 gint                     
replace_length);
 
 G_GNUC_INTERNAL
-void                   _gtk_source_search_context_update_highlight             (GtkSourceSearchContext  
*search,
+void                    _gtk_source_search_context_update_highlight            (GtkSourceSearchContext  
*search,
                                                                                 const GtkTextIter       
*start,
                                                                                 const GtkTextIter       *end,
                                                                                 gboolean                 
synchronous);


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