[gedit/wip/use-gsv-search: 6/6] Use the GtkSourceView search API



commit f45c6b011f20dbef81c48fe698c8cba8211f84d3
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Jun 19 12:20:58 2013 +0200

    Use the GtkSourceView search API

 gedit/gedit-document.c |  392 ++++--------------------------------------------
 gedit/gedit-document.h |    4 -
 gedit/gedit-view.c     |    6 -
 3 files changed, 26 insertions(+), 376 deletions(-)
---
diff --git a/gedit/gedit-document.c b/gedit/gedit-document.c
index 71c36e2..89f5c16 100644
--- a/gedit/gedit-document.c
+++ b/gedit/gedit-document.c
@@ -88,14 +88,6 @@ static void  gedit_document_save_real        (GeditDocument                *doc,
                                                 GeditDocumentNewlineType      newline_type,
                                                 GeditDocumentCompressionType  compression_type,
                                                 GeditDocumentSaveFlags        flags);
-static void    insert_text_cb                  (GeditDocument                *doc,
-                                                GtkTextIter                  *pos,
-                                                const gchar                  *text,
-                                                gint                          length);
-
-static void    delete_range_cb                 (GeditDocument                *doc,
-                                                GtkTextIter                  *start,
-                                                GtkTextIter                  *end);
 
 struct _GeditDocumentPrivate
 {
@@ -117,7 +109,6 @@ struct _GeditDocumentPrivate
 
        guint        search_flags;
        gchar       *search_text;
-       gint         num_of_lines_search_text;
 
        GeditDocumentNewlineType newline_type;
        GeditDocumentCompressionType compression_type;
@@ -133,11 +124,7 @@ struct _GeditDocumentPrivate
        /* Saving stuff */
        GeditDocumentSaver *saver;
 
-       /* Search highlighting support variables */
-       GeditTextRegion *to_search_region;
-       GtkTextTag      *found_tag;
-
-       GtkTextTag      *error_tag;
+       GtkTextTag *error_tag;
 
        /* Mount operation factory */
        GeditMountOperationFactory  mount_operation_factory;
@@ -329,12 +316,6 @@ gedit_document_finalize (GObject *object)
        g_free (doc->priv->content_type);
        g_free (doc->priv->search_text);
 
-       if (doc->priv->to_search_region != NULL)
-       {
-               /* we can't delete marks if we're finalizing the buffer */
-               gedit_text_region_destroy (doc->priv->to_search_region, FALSE);
-       }
-
        G_OBJECT_CLASS (gedit_document_parent_class)->finalize (object);
 }
 
@@ -988,16 +969,6 @@ gedit_document_init (GeditDocument *doc)
                gtk_source_buffer_set_style_scheme (GTK_SOURCE_BUFFER (doc),
                                                    style_scheme);
 
-       g_signal_connect_after (doc,
-                               "insert-text",
-                               G_CALLBACK (insert_text_cb),
-                               NULL);
-
-       g_signal_connect_after (doc,
-                               "delete-range",
-                               G_CALLBACK (delete_range_cb),
-                               NULL);
-
        g_signal_connect (doc,
                          "notify::content-type",
                          G_CALLBACK (on_content_type_changed),
@@ -2039,61 +2010,6 @@ gedit_document_goto_line_offset (GeditDocument *doc,
        return ret;
 }
 
-static gint
-compute_num_of_lines (const gchar *text)
-{
-       const gchar *p;
-       gint len;
-       gint n = 1;
-
-       g_return_val_if_fail (text != NULL, 0);
-
-       len = strlen (text);
-       p = text;
-
-       while (len > 0)
-       {
-               gint del, par;
-
-               pango_find_paragraph_boundary (p, len, &del, &par);
-
-               if (del == par) /* not found */
-                       break;
-
-               p += par;
-               len -= par;
-               ++n;
-       }
-
-       return n;
-}
-
-static void
-to_search_region_range (GeditDocument *doc,
-                       GtkTextIter   *start,
-                       GtkTextIter   *end)
-{
-       gedit_debug (DEBUG_DOCUMENT);
-
-       if (doc->priv->to_search_region == NULL)
-               return;
-
-       gtk_text_iter_set_line_offset (start, 0);
-       gtk_text_iter_forward_to_line_end (end);
-
-       /*
-       g_print ("+ [%u (%u), %u (%u)]\n", gtk_text_iter_get_line (start), gtk_text_iter_get_offset (start),
-                                          gtk_text_iter_get_line (end), gtk_text_iter_get_offset (end));
-       */
-
-       /* Add the region to the refresh region */
-       gedit_text_region_add (doc->priv->to_search_region, start, end);
-
-       /* Notify views of the updated highlight region */
-       gtk_text_iter_backward_lines (start, doc->priv->num_of_lines_search_text);
-       gtk_text_iter_forward_lines (end, doc->priv->num_of_lines_search_text);
-}
-
 /**
  * gedit_document_set_search_text:
  * @doc:
@@ -2107,7 +2023,6 @@ gedit_document_set_search_text (GeditDocument *doc,
 {
        gchar *converted_text;
        gboolean notify = FALSE;
-       gboolean update_to_search_region = FALSE;
 
        g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
        g_return_if_fail ((text == NULL) || (doc->priv->search_text != text));
@@ -2124,42 +2039,41 @@ gedit_document_set_search_text (GeditDocument *doc,
                }
                else
                {
-                       converted_text = g_strdup("");
+                       converted_text = g_strdup ("");
                        notify = gedit_document_get_can_search_again (doc);
                }
 
                g_free (doc->priv->search_text);
-
                doc->priv->search_text = converted_text;
-               doc->priv->num_of_lines_search_text = compute_num_of_lines (doc->priv->search_text);
-               update_to_search_region = TRUE;
        }
 
        if (!GEDIT_SEARCH_IS_DONT_SET_FLAGS (flags))
        {
-               if (doc->priv->search_flags != flags)
-                       update_to_search_region = TRUE;
+               GtkSourceSearchFlags search_flags = 0;
 
                doc->priv->search_flags = flags;
 
-       }
-
-       if (update_to_search_region)
-       {
-               GtkTextIter begin;
-               GtkTextIter end;
+               if (GEDIT_SEARCH_IS_CASE_SENSITIVE (flags))
+               {
+                       search_flags |= GTK_SOURCE_SEARCH_CASE_SENSITIVE;
+               }
 
-               gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc),
-                                           &begin,
-                                           &end);
+               if (GEDIT_SEARCH_IS_ENTIRE_WORD (flags))
+               {
+                       search_flags |= GTK_SOURCE_SEARCH_AT_WORD_BOUNDARIES;
+               }
 
-               to_search_region_range (doc,
-                                       &begin,
-                                       &end);
+               gtk_source_buffer_set_search_flags (GTK_SOURCE_BUFFER (doc),
+                                                   search_flags);
        }
 
+       gtk_source_buffer_set_search_text (GTK_SOURCE_BUFFER (doc),
+                                          doc->priv->search_text);
+
        if (notify)
+       {
                g_object_notify (G_OBJECT (doc), "can-search-again");
+       }
 }
 
 /**
@@ -2612,14 +2526,6 @@ sync_tag_style (GeditDocument *doc,
 }
 
 static void
-sync_found_tag (GeditDocument *doc,
-               GParamSpec    *pspec,
-               gpointer       data)
-{
-       sync_tag_style (doc, doc->priv->found_tag, "search-match");
-}
-
-static void
 text_tag_set_highest_priority (GtkTextTag    *tag,
                               GtkTextBuffer *buffer)
 {
@@ -2631,217 +2537,6 @@ text_tag_set_highest_priority (GtkTextTag    *tag,
        gtk_text_tag_set_priority (tag, n - 1);
 }
 
-static void
-search_region (GeditDocument *doc,
-              GtkTextIter   *start,
-              GtkTextIter   *end)
-{
-       GtkTextIter iter;
-       GtkTextIter m_start;
-       GtkTextIter m_end;
-       GtkTextSearchFlags search_flags = 0;
-       gboolean found = TRUE;
-
-       GtkTextBuffer *buffer;
-
-       gedit_debug (DEBUG_DOCUMENT);
-
-       buffer = GTK_TEXT_BUFFER (doc);
-
-       if (doc->priv->found_tag == NULL)
-       {
-               doc->priv->found_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (doc),
-                                                                  "found",
-                                                                  NULL);
-
-               sync_found_tag (doc, NULL, NULL);
-
-               g_signal_connect (doc,
-                                 "notify::style-scheme",
-                                 G_CALLBACK (sync_found_tag),
-                                 NULL);
-       }
-
-       /* make sure the 'found' tag has the priority over
-        * syntax highlighting tags */
-       text_tag_set_highest_priority (doc->priv->found_tag,
-                                      GTK_TEXT_BUFFER (doc));
-
-
-       if (doc->priv->search_text == NULL)
-               return;
-
-       g_return_if_fail (doc->priv->num_of_lines_search_text > 0);
-
-       gtk_text_iter_backward_lines (start, doc->priv->num_of_lines_search_text);
-       gtk_text_iter_forward_lines (end, doc->priv->num_of_lines_search_text);
-
-       if (gtk_text_iter_has_tag (start, doc->priv->found_tag) &&
-           !gtk_text_iter_begins_tag (start, doc->priv->found_tag))
-       {
-               gtk_text_iter_backward_to_tag_toggle (start, doc->priv->found_tag);
-       }
-
-       if (gtk_text_iter_has_tag (end, doc->priv->found_tag) &&
-           !gtk_text_iter_ends_tag (end, doc->priv->found_tag))
-       {
-               gtk_text_iter_forward_to_tag_toggle (end, doc->priv->found_tag);
-       }
-
-       /*
-       g_print ("[%u (%u), %u (%u)]\n", gtk_text_iter_get_line (start), gtk_text_iter_get_offset (start),
-                                          gtk_text_iter_get_line (end), gtk_text_iter_get_offset (end));
-       */
-
-       gtk_text_buffer_remove_tag (buffer,
-                                   doc->priv->found_tag,
-                                   start,
-                                   end);
-
-       if (*doc->priv->search_text == '\0')
-               return;
-
-       iter = *start;
-
-       search_flags = GTK_TEXT_SEARCH_VISIBLE_ONLY | GTK_TEXT_SEARCH_TEXT_ONLY;
-
-       if (!GEDIT_SEARCH_IS_CASE_SENSITIVE (doc->priv->search_flags))
-       {
-               search_flags = search_flags | GTK_TEXT_SEARCH_CASE_INSENSITIVE;
-       }
-
-       do
-       {
-               if ((end != NULL) && gtk_text_iter_is_end (end))
-                       end = NULL;
-
-               found = gtk_text_iter_forward_search (&iter,
-                                                     doc->priv->search_text,
-                                                     search_flags,
-                                                     &m_start,
-                                                     &m_end,
-                                                     end);
-
-               iter = m_end;
-
-               if (found && GEDIT_SEARCH_IS_ENTIRE_WORD (doc->priv->search_flags))
-               {
-                       gboolean word;
-
-                       word = gtk_text_iter_starts_word (&m_start) &&
-                              gtk_text_iter_ends_word (&m_end);
-
-                       if (!word)
-                               continue;
-               }
-
-               if (found)
-               {
-                       gtk_text_buffer_apply_tag (buffer,
-                                                  doc->priv->found_tag,
-                                                  &m_start,
-                                                  &m_end);
-               }
-
-       } while (found);
-}
-
-void
-_gedit_document_search_region (GeditDocument     *doc,
-                              const GtkTextIter *start,
-                              const GtkTextIter *end)
-{
-       GeditTextRegion *region;
-
-       gedit_debug (DEBUG_DOCUMENT);
-
-       g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
-       g_return_if_fail (start != NULL);
-       g_return_if_fail (end != NULL);
-
-       if (doc->priv->to_search_region == NULL)
-               return;
-
-       /*
-       g_print ("U [%u (%u), %u (%u)]\n", gtk_text_iter_get_line (start), gtk_text_iter_get_offset (start),
-                                          gtk_text_iter_get_line (end), gtk_text_iter_get_offset (end));
-       */
-
-       /* get the subregions not yet highlighted */
-       region = gedit_text_region_intersect (doc->priv->to_search_region,
-                                             start,
-                                             end);
-       if (region)
-       {
-               gint i;
-               GtkTextIter start_search;
-               GtkTextIter end_search;
-
-               i = gedit_text_region_subregions (region);
-               gedit_text_region_nth_subregion (region,
-                                                0,
-                                                &start_search,
-                                                NULL);
-
-               gedit_text_region_nth_subregion (region,
-                                                i - 1,
-                                                NULL,
-                                                &end_search);
-
-               gedit_text_region_destroy (region, TRUE);
-
-               gtk_text_iter_order (&start_search, &end_search);
-
-               search_region (doc, &start_search, &end_search);
-
-               /* remove the just highlighted region */
-               gedit_text_region_subtract (doc->priv->to_search_region,
-                                           start,
-                                           end);
-       }
-}
-
-static void
-insert_text_cb (GeditDocument *doc,
-               GtkTextIter   *pos,
-               const gchar   *text,
-               gint           length)
-{
-       GtkTextIter start;
-       GtkTextIter end;
-
-       gedit_debug (DEBUG_DOCUMENT);
-
-       start = end = *pos;
-
-       /*
-        * pos is invalidated when
-        * insertion occurs (because the buffer contents change), but the
-        * default signal handler revalidates it to point to the end of the
-        * inserted text
-        */
-       gtk_text_iter_backward_chars (&start,
-                                     g_utf8_strlen (text, length));
-
-       to_search_region_range (doc, &start, &end);
-}
-
-static void
-delete_range_cb (GeditDocument *doc,
-                GtkTextIter   *start,
-                GtkTextIter   *end)
-{
-       GtkTextIter d_start;
-       GtkTextIter d_end;
-
-       gedit_debug (DEBUG_DOCUMENT);
-
-       d_start = *start;
-       d_end = *end;
-
-       to_search_region_range (doc, &d_start, &d_end);
-}
-
 /**
  * gedit_document_set_language:
  * @doc:
@@ -2898,51 +2593,16 @@ gedit_document_set_enable_search_highlighting (GeditDocument *doc,
 {
        g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
 
-       enable = enable != FALSE;
-
-       if ((doc->priv->to_search_region != NULL) == enable)
-               return;
-
-       if (doc->priv->to_search_region != NULL)
+       if (!enable)
        {
-               /* Disable search highlighting */
-               if (doc->priv->found_tag != NULL)
-               {
-                       /* If needed remove the found_tag */
-                       GtkTextIter begin;
-                       GtkTextIter end;
-
-                       gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc),
-                                                   &begin,
-                                                   &end);
-
-                       gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (doc),
-                                                   doc->priv->found_tag,
-                                                   &begin,
-                                                   &end);
-               }
-
-               gedit_text_region_destroy (doc->priv->to_search_region,
-                                          TRUE);
-               doc->priv->to_search_region = NULL;
+               gtk_source_buffer_set_search_text (GTK_SOURCE_BUFFER (doc), NULL);
+               return;
        }
-       else
-       {
-               doc->priv->to_search_region = gedit_text_region_new (GTK_TEXT_BUFFER (doc));
-               if (gedit_document_get_can_search_again (doc))
-               {
-                       /* If search_text is not empty, highligth all its occurrences */
-                       GtkTextIter begin;
-                       GtkTextIter end;
-
-                       gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (doc),
-                                                   &begin,
-                                                   &end);
 
-                       to_search_region_range (doc,
-                                               &begin,
-                                               &end);
-               }
+       if (gedit_document_get_can_search_again (doc))
+       {
+               gtk_source_buffer_set_search_text (GTK_SOURCE_BUFFER (doc),
+                                                  doc->priv->search_text);
        }
 }
 
@@ -2951,7 +2611,7 @@ gedit_document_get_enable_search_highlighting (GeditDocument *doc)
 {
        g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), FALSE);
 
-       return (doc->priv->to_search_region != NULL);
+       return gtk_source_buffer_get_search_text (GTK_SOURCE_BUFFER (doc)) != NULL;
 }
 
 GeditDocumentNewlineType
diff --git a/gedit/gedit-document.h b/gedit/gedit-document.h
index 920a372..4c76109 100644
--- a/gedit/gedit-document.h
+++ b/gedit/gedit-document.h
@@ -328,10 +328,6 @@ void                _gedit_document_apply_error_style
 gboolean       _gedit_document_check_externally_modified
                                                (GeditDocument       *doc);
 
-void           _gedit_document_search_region   (GeditDocument       *doc,
-                                                const GtkTextIter   *start,
-                                                const GtkTextIter   *end);
-
 /* Search macros */
 #define GEDIT_SEARCH_IS_DONT_SET_FLAGS(sflags) ((sflags & GEDIT_SEARCH_DONT_SET_FLAGS) != 0)
 #define GEDIT_SEARCH_SET_DONT_SET_FLAGS(sflags,state) ((state == TRUE) ? \
diff --git a/gedit/gedit-view.c b/gedit/gedit-view.c
index f69c092..62c7e71 100644
--- a/gedit/gedit-view.c
+++ b/gedit/gedit-view.c
@@ -56,12 +56,6 @@
 
 #define GEDIT_VIEW_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GEDIT_TYPE_VIEW, 
GeditViewPrivate))
 
-typedef enum
-{
-       GOTO_LINE,
-       SEARCH
-} SearchMode;
-
 enum
 {
        TARGET_URI_LIST = 100,


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