[gedit/wip/use-gsv-search] search and replace: use async functions



commit 1a965fc4ed29731e35ce9fecaec9036728f00f52
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Jul 10 13:37:28 2013 +0200

    search and replace: use async functions
    
    Async search has a price: more lines of code...
    
    The forward and backward search can be done via the replace dialog, or
    via the menu. In the first case, when the operation is finished, a
    message must be displayed in the statusbar and the sensitivity of the
    replace button must be updated. That's why there are two different
    callbacks: from or not from the dialog window.

 gedit/gedit-commands-search.c |  245 ++++++++++++++++++++++++++++++++---------
 1 files changed, 191 insertions(+), 54 deletions(-)
---
diff --git a/gedit/gedit-commands-search.c b/gedit/gedit-commands-search.c
index 93546b4..da0b9d3 100644
--- a/gedit/gedit-commands-search.c
+++ b/gedit/gedit-commands-search.c
@@ -1,5 +1,5 @@
 /*
- * gedit-search-commands.c
+ * gedit-commands-search.c
  * This file is part of gedit
  *
  * Copyright (C) 1998, 1999 Alex Roberts, Evan Lawrence
@@ -24,7 +24,7 @@
  */
 
 /*
- * Modified by the gedit Team, 1998-2006. See the AUTHORS file for a
+ * Modified by the gedit Team, 1998-2013. See the AUTHORS file for a
  * list of people on the gedit Team.
  * See the ChangeLog files for a list of changes.
  *
@@ -146,59 +146,139 @@ text_not_found (GeditWindow *window,
        g_free (searched);
 }
 
-/*
 static void
+finish_search_from_dialog (GeditWindow *window,
+                          gboolean     found)
+{
+       GeditReplaceDialog *replace_dialog;
+
+       replace_dialog = g_object_get_data (G_OBJECT (window), GEDIT_REPLACE_DIALOG_KEY);
+
+       g_return_if_fail (replace_dialog != NULL);
+
+       if (found)
+       {
+               text_found (window, 0);
+       }
+       else
+       {
+               const gchar *search_text = gedit_replace_dialog_get_search_text (replace_dialog);
+               text_not_found (window, search_text);
+       }
+
+       gtk_dialog_set_response_sensitive (GTK_DIALOG (replace_dialog),
+                                          GEDIT_REPLACE_DIALOG_REPLACE_RESPONSE,
+                                          found);
+}
+
+static gboolean
 forward_search_finished (GtkSourceBuffer *buffer,
                         GAsyncResult    *result,
                         GeditView       *view)
 {
+       gboolean found;
+       GtkTextIter match_start;
+       GtkTextIter match_end;
+
+       found = gtk_source_buffer_forward_search_finish (buffer,
+                                                        result,
+                                                        &match_start,
+                                                        &match_end,
+                                                        NULL);
+
+       if (found)
+       {
+               gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer),
+                                             &match_start,
+                                             &match_end);
+
+               gedit_view_scroll_to_cursor (view);
+       }
+       else
+       {
+               GtkTextIter end_selection;
+
+               gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
+                                                     NULL,
+                                                     &end_selection);
+
+               gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer),
+                                             &end_selection,
+                                             &end_selection);
+       }
+
+       return found;
 }
 
 static void
-backward_search_finished (GtkSourceBuffer *buffer,
-                         GAsyncResult    *result,
-                         GeditView       *view)
+forward_search_from_dialog_finished (GtkSourceBuffer *buffer,
+                                    GAsyncResult    *result,
+                                    GeditWindow     *window)
 {
+       GeditView *view = gedit_window_get_active_view (window);
+       gboolean found;
+
+       if (view == NULL)
+       {
+               return;
+       }
+
+       found = forward_search_finished (buffer, result, view);
+
+       finish_search_from_dialog (window, found);
 }
-*/
 
-static gboolean
-run_search (GeditView   *view,
-           gboolean     wrap_around,
-           gboolean     search_backwards)
+static void
+run_forward_search (GeditWindow *window,
+                   gboolean     from_dialog)
 {
-       GtkSourceBuffer *buffer;
+       GeditView *view;
+       GtkTextBuffer *buffer;
        GtkTextIter start_at;
-       GtkTextIter match_start;
-       GtkTextIter match_end;
-       gboolean found;
 
-       buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
+       view = gedit_window_get_active_view (window);
 
-       gtk_source_buffer_set_search_wrap_around (buffer, wrap_around);
-
-       if (search_backwards)
+       if (view == NULL)
        {
-               gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
-                                                     &start_at,
-                                                     NULL);
+               return;
+       }
+
+       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+
+       gtk_text_buffer_get_selection_bounds (buffer, NULL, &start_at);
 
-               found = gtk_source_buffer_backward_search (buffer,
-                                                          &start_at,
-                                                          &match_start,
-                                                          &match_end);
+       if (from_dialog)
+       {
+               gtk_source_buffer_forward_search_async (GTK_SOURCE_BUFFER (buffer),
+                                                       &start_at,
+                                                       NULL,
+                                                       
(GAsyncReadyCallback)forward_search_from_dialog_finished,
+                                                       window);
        }
        else
        {
-               gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
-                                                     NULL,
-                                                     &start_at);
+               gtk_source_buffer_forward_search_async (GTK_SOURCE_BUFFER (buffer),
+                                                       &start_at,
+                                                       NULL,
+                                                       (GAsyncReadyCallback)forward_search_finished,
+                                                       view);
+       }
+}
 
-               found = gtk_source_buffer_forward_search (buffer,
-                                                         &start_at,
+static gboolean
+backward_search_finished (GtkSourceBuffer *buffer,
+                         GAsyncResult    *result,
+                         GeditView       *view)
+{
+       gboolean found;
+       GtkTextIter match_start;
+       GtkTextIter match_end;
+
+       found = gtk_source_buffer_backward_search_finish (buffer,
+                                                         result,
                                                          &match_start,
-                                                         &match_end);
-       }
+                                                         &match_end,
+                                                         NULL);
 
        if (found)
        {
@@ -210,15 +290,76 @@ run_search (GeditView   *view,
        }
        else
        {
+               GtkTextIter start_selection;
+
+               gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (buffer),
+                                                     &start_selection,
+                                                     NULL);
+
                gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer),
-                                             &start_at,
-                                             &start_at);
+                                             &start_selection,
+                                             &start_selection);
        }
 
        return found;
 }
 
 static void
+backward_search_from_dialog_finished (GtkSourceBuffer *buffer,
+                                     GAsyncResult    *result,
+                                     GeditWindow     *window)
+{
+       GeditView *view = gedit_window_get_active_view (window);
+       gboolean found;
+
+       if (view == NULL)
+       {
+               return;
+       }
+
+       found = backward_search_finished (buffer, result, view);
+
+       finish_search_from_dialog (window, found);
+}
+
+static void
+run_backward_search (GeditWindow *window,
+                    gboolean     from_dialog)
+{
+       GeditView *view;
+       GtkTextBuffer *buffer;
+       GtkTextIter start_at;
+
+       view = gedit_window_get_active_view (window);
+
+       if (view == NULL)
+       {
+               return;
+       }
+
+       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+
+       gtk_text_buffer_get_selection_bounds (buffer, &start_at, NULL);
+
+       if (from_dialog)
+       {
+               gtk_source_buffer_backward_search_async (GTK_SOURCE_BUFFER (buffer),
+                                                        &start_at,
+                                                        NULL,
+                                                        
(GAsyncReadyCallback)backward_search_from_dialog_finished,
+                                                        window);
+       }
+       else
+       {
+               gtk_source_buffer_backward_search_async (GTK_SOURCE_BUFFER (buffer),
+                                                        &start_at,
+                                                        NULL,
+                                                        (GAsyncReadyCallback)backward_search_finished,
+                                                        view);
+       }
+}
+
+static void
 set_search_state (GeditReplaceDialog *dialog,
                  GtkSourceBuffer    *buffer)
 {
@@ -249,9 +390,6 @@ do_find (GeditReplaceDialog *dialog,
 {
        GeditView *active_view;
        GtkSourceBuffer *buffer;
-       gboolean wrap_around;
-       gboolean search_backwards;
-       gboolean found;
 
        /* TODO: make the dialog insensitive when all the tabs are closed
         * and assert here that the view is not NULL */
@@ -266,24 +404,14 @@ do_find (GeditReplaceDialog *dialog,
 
        set_search_state (dialog, buffer);
 
-       wrap_around = gedit_replace_dialog_get_wrap_around (dialog);
-       search_backwards = gedit_replace_dialog_get_backwards (dialog);
-
-       found = run_search (active_view, wrap_around, search_backwards);
-
-       if (found)
+       if (gedit_replace_dialog_get_backwards (dialog))
        {
-               text_found (window, 0);
+               run_backward_search (window, TRUE);
        }
        else
        {
-               const gchar *search_text = gedit_replace_dialog_get_search_text (dialog);
-               text_not_found (window, search_text);
+               run_forward_search (window, TRUE);
        }
-
-       gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
-                                          GEDIT_REPLACE_DIALOG_REPLACE_RESPONSE,
-                                          found);
 }
 
 /* FIXME: move in gedit-document.c and share it with gedit-view */
@@ -529,7 +657,6 @@ do_find_again (GeditWindow *window,
               gboolean     backward)
 {
        GeditView *active_view;
-       gboolean wrap_around = TRUE;
        gpointer data;
 
        active_view = gedit_window_get_active_view (window);
@@ -539,10 +666,20 @@ do_find_again (GeditWindow *window,
 
        if (data != NULL)
        {
-               wrap_around = gedit_replace_dialog_get_wrap_around (GEDIT_REPLACE_DIALOG (data));
+               GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (active_view));
+
+               set_search_state (GEDIT_REPLACE_DIALOG (data),
+                                 GTK_SOURCE_BUFFER (buffer));
        }
 
-       run_search (active_view, wrap_around, backward);
+       if (backward)
+       {
+               run_backward_search (window, FALSE);
+       }
+       else
+       {
+               run_forward_search (window, FALSE);
+       }
 }
 
 void


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