[gtksourceview/wip/search] Add gtk_source_buffer_get_search_occurrence_position()



commit f5c83db87fc8021c3164351260b6a4b87a6eb161
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Jul 2 22:38:39 2013 +0200

    Add gtk_source_buffer_get_search_occurrence_position()

 gtksourceview/gtksourcebuffer.c |   28 ++++++++++++++++
 gtksourceview/gtksourcebuffer.h |    5 +++
 gtksourceview/gtksourcesearch.c |   66 +++++++++++++++++++++++++++++++++++++++
 gtksourceview/gtksourcesearch.h |    5 +++
 4 files changed, 104 insertions(+), 0 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index 8c81284..d8a476c 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -2944,3 +2944,31 @@ gtk_source_buffer_search_replace_all (GtkSourceBuffer *buffer,
                                               replace,
                                               replace_length);
 }
+
+/**
+ * gtk_source_buffer_get_search_occurrence_position:
+ * @buffer: a #GtkSourceBuffer.
+ * @match_start: the start of the occurrence.
+ * @match_end: the end of the occurrence.
+ *
+ * Gets the position of a search occurrence. If the buffer is not already fully
+ * scanned, the position may be unknown, and -1 is returned. Therefore you
+ * should call this function when you know that the buffer is fully scanned.
+ *
+ * Returns: the position of the search occurrence. The first occurrence has the
+ * position 1 (not 0). Returns 0 if @match_start and @match_end doesn't delimit
+ * an occurrence. Returns -1 if the position is not yet known.
+ */
+gint
+gtk_source_buffer_get_search_occurrence_position (GtkSourceBuffer   *buffer,
+                                                 const GtkTextIter *match_start,
+                                                 const GtkTextIter *match_end)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), -1);
+       g_return_val_if_fail (match_start != NULL, -1);
+       g_return_val_if_fail (match_end != NULL, -1);
+
+       return _gtk_source_search_get_occurrence_position (buffer->priv->search,
+                                                          match_start,
+                                                          match_end);
+}
diff --git a/gtksourceview/gtksourcebuffer.h b/gtksourceview/gtksourcebuffer.h
index d118ac7..2029df7 100644
--- a/gtksourceview/gtksourcebuffer.h
+++ b/gtksourceview/gtksourcebuffer.h
@@ -242,6 +242,11 @@ guint                       gtk_source_buffer_search_replace_all   (GtkSourceBuffer      
  *buffer,
                                                                 const gchar            *replace,
                                                                 gint                    replace_length);
 
+gint                    gtk_source_buffer_get_search_occurrence_position
+                                                               (GtkSourceBuffer        *buffer,
+                                                                const GtkTextIter      *match_start,
+                                                                const GtkTextIter      *match_end);
+
 /* private */
 void                    _gtk_source_buffer_update_highlight    (GtkSourceBuffer        *buffer,
                                                                 const GtkTextIter      *start,
diff --git a/gtksourceview/gtksourcesearch.c b/gtksourceview/gtksourcesearch.c
index f4f9d98..71a6bfc 100644
--- a/gtksourceview/gtksourcesearch.c
+++ b/gtksourceview/gtksourcesearch.c
@@ -2009,3 +2009,69 @@ _gtk_source_search_replace_all (GtkSourceSearch *search,
 
        return nb_matches_replaced;
 }
+
+gint
+_gtk_source_search_get_occurrence_position (GtkSourceSearch   *search,
+                                           const GtkTextIter *match_start,
+                                           const GtkTextIter *match_end)
+{
+       GtkTextIter m_start;
+       GtkTextIter m_end;
+       GtkTextIter iter;
+       gboolean found;
+       gint position = 0;
+
+       g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), -1);
+       g_return_val_if_fail (match_start != NULL, -1);
+       g_return_val_if_fail (match_end != NULL, -1);
+
+       /* Verify that the occurrence is correct. */
+
+       found = basic_forward_search (search,
+                                     match_start,
+                                     &m_start,
+                                     &m_end,
+                                     match_end);
+
+       if (!found ||
+           !gtk_text_iter_equal (match_start, &m_start) ||
+           !gtk_text_iter_equal (match_end, &m_end))
+       {
+               return 0;
+       }
+
+       /* Verify that the scan region is empty between the start of the buffer
+        * and the end of the occurrence.
+        */
+
+       gtk_text_buffer_get_start_iter (search->priv->buffer, &iter);
+
+       if (search->priv->scan_region != NULL)
+       {
+               GtkTextRegion *region = gtk_text_region_intersect (search->priv->scan_region,
+                                                                  &iter,
+                                                                  match_end);
+
+               gboolean empty = is_text_region_empty (region);
+
+               if (region != NULL)
+               {
+                       gtk_text_region_destroy (region, TRUE);
+               }
+
+               if (!empty)
+               {
+                       return -1;
+               }
+       }
+
+       /* Everything is fine, count the number of previous occurrences. */
+
+       while (smart_forward_search_without_scanning (search, &iter, &m_start, &m_end, match_start))
+       {
+               position++;
+               iter = m_end;
+       }
+
+       return position + 1;
+}
diff --git a/gtksourceview/gtksourcesearch.h b/gtksourceview/gtksourcesearch.h
index e240afd..a13bf31 100644
--- a/gtksourceview/gtksourcesearch.h
+++ b/gtksourceview/gtksourcesearch.h
@@ -146,6 +146,11 @@ guint                      _gtk_source_search_replace_all                  
(GtkSourceSearch        *search,
                                                                         const gchar            *replace,
                                                                         gint                    
replace_length);
 
+G_GNUC_INTERNAL
+gint                   _gtk_source_search_get_occurrence_position      (GtkSourceSearch        *search,
+                                                                        const GtkTextIter      *match_start,
+                                                                        const GtkTextIter      *match_end);
+
 G_END_DECLS
 
 #endif /* __GTK_SOURCE_SEARCH_H__ */


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