[gedit/wip/spell-checking: 3/3] auto-spell: misc code improvements



commit 950d0c9fed62b6b96d29fe24f4f035c9202fe77d
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jul 11 17:53:46 2015 +0200

    auto-spell: misc code improvements

 plugins/spell/gedit-automatic-spell-checker.c |  127 +++++++++++++------------
 1 files changed, 66 insertions(+), 61 deletions(-)
---
diff --git a/plugins/spell/gedit-automatic-spell-checker.c b/plugins/spell/gedit-automatic-spell-checker.c
index 799e33c..a67030f 100644
--- a/plugins/spell/gedit-automatic-spell-checker.c
+++ b/plugins/spell/gedit-automatic-spell-checker.c
@@ -60,8 +60,8 @@ view_destroy (GtkTextView                *view,
 
 static void
 check_word (GeditAutomaticSpellChecker *spell,
-           GtkTextIter                *start,
-           GtkTextIter                *end)
+           const GtkTextIter          *start,
+           const GtkTextIter          *end)
 {
        gchar *word;
 
@@ -201,6 +201,7 @@ check_deferred_range (GeditAutomaticSpellChecker *spell,
        gtk_text_buffer_get_iter_at_mark (spell->buffer,
                                          &start,
                                          spell->mark_insert_start);
+
        gtk_text_buffer_get_iter_at_mark (spell->buffer,
                                          &end,
                                          spell->mark_insert_end);
@@ -266,7 +267,9 @@ mark_set (GtkTextBuffer              *buffer,
 {
        /* if the cursor has moved and there is a deferred check so handle it now */
        if ((mark == gtk_text_buffer_get_insert (buffer)) && spell->deferred_check)
+       {
                check_deferred_range (spell, FALSE);
+       }
 }
 
 static void
@@ -275,15 +278,19 @@ get_word_extents_from_mark (GtkTextBuffer *buffer,
                            GtkTextIter   *end,
                            GtkTextMark   *mark)
 {
-       gtk_text_buffer_get_iter_at_mark(buffer, start, mark);
+       gtk_text_buffer_get_iter_at_mark (buffer, start, mark);
 
        if (!gtk_text_iter_starts_word (start))
+       {
                gtk_text_iter_backward_word_start (start);
+       }
 
        *end = *start;
 
        if (gtk_text_iter_inside_word (end))
+       {
                gtk_text_iter_forward_word_end (end);
+       }
 }
 
 static void
@@ -291,53 +298,52 @@ remove_tag_to_word (GeditAutomaticSpellChecker *spell,
                    const gchar                *word)
 {
        GtkTextIter iter;
-       GtkTextIter match_start, match_end;
-
-       gboolean found;
 
-       gtk_text_buffer_get_iter_at_offset (spell->buffer, &iter, 0);
+       gtk_text_buffer_get_start_iter (spell->buffer, &iter);
 
-       found = TRUE;
-
-       while (found)
+       while (TRUE)
        {
-               found = gtk_text_iter_forward_search (&iter,
-                               word,
-                               GTK_TEXT_SEARCH_VISIBLE_ONLY | GTK_TEXT_SEARCH_TEXT_ONLY,
-                               &match_start,
-                               &match_end,
-                               NULL);
+               gboolean found;
+               GtkTextIter match_start;
+               GtkTextIter match_end;
 
-               if (found)
+               found = gtk_text_iter_forward_search (&iter,
+                                                     word,
+                                                     GTK_TEXT_SEARCH_VISIBLE_ONLY |
+                                                     GTK_TEXT_SEARCH_TEXT_ONLY,
+                                                     &match_start,
+                                                     &match_end,
+                                                     NULL);
+
+               if (!found)
                {
-                       if (gtk_text_iter_starts_word (&match_start) &&
-                           gtk_text_iter_ends_word (&match_end))
-                       {
-                               gtk_text_buffer_remove_tag (spell->buffer,
-                                               spell->tag_highlight,
-                                               &match_start,
-                                               &match_end);
-                       }
+                       break;
+               }
 
-                       iter = match_end;
+               if (gtk_text_iter_starts_word (&match_start) &&
+                   gtk_text_iter_ends_word (&match_end))
+               {
+                       gtk_text_buffer_remove_tag (spell->buffer,
+                                                   spell->tag_highlight,
+                                                   &match_start,
+                                                   &match_end);
                }
+
+               iter = match_end;
        }
 }
 
 static void
-add_to_dictionary (GtkWidget                  *menuitem,
+add_to_dictionary (GtkWidget                  *menu_item,
                   GeditAutomaticSpellChecker *spell)
 {
+       GtkTextIter start;
+       GtkTextIter end;
        gchar *word;
 
-       GtkTextIter start, end;
-
        get_word_extents_from_mark (spell->buffer, &start, &end, spell->mark_click);
 
-       word = gtk_text_buffer_get_text (spell->buffer,
-                                        &start,
-                                        &end,
-                                        FALSE);
+       word = gtk_text_buffer_get_text (spell->buffer, &start, &end, FALSE);
 
        gedit_spell_checker_add_word_to_personal (spell->spell_checker, word, -1);
 
@@ -345,19 +351,16 @@ add_to_dictionary (GtkWidget                  *menuitem,
 }
 
 static void
-ignore_all (GtkWidget                  *menuitem,
+ignore_all (GtkWidget                  *menu_item,
            GeditAutomaticSpellChecker *spell)
 {
+       GtkTextIter start;
+       GtkTextIter end;
        gchar *word;
 
-       GtkTextIter start, end;
-
        get_word_extents_from_mark (spell->buffer, &start, &end, spell->mark_click);
 
-       word = gtk_text_buffer_get_text (spell->buffer,
-                                        &start,
-                                        &end,
-                                        FALSE);
+       word = gtk_text_buffer_get_text (spell->buffer, &start, &end, FALSE);
 
        gedit_spell_checker_add_word_to_session (spell->spell_checker, word, -1);
 
@@ -365,46 +368,47 @@ ignore_all (GtkWidget                  *menuitem,
 }
 
 static void
-replace_word (GtkWidget                  *menuitem,
+replace_word (GtkWidget                  *menu_item,
              GeditAutomaticSpellChecker *spell)
 {
-       gchar *oldword;
-       const gchar *newword;
-
-       GtkTextIter start, end;
+       GtkTextIter start;
+       GtkTextIter end;
+       gchar *old_word;
+       const gchar *new_word;
 
        get_word_extents_from_mark (spell->buffer, &start, &end, spell->mark_click);
 
-       oldword = gtk_text_buffer_get_text (spell->buffer, &start, &end, FALSE);
+       old_word = gtk_text_buffer_get_text (spell->buffer, &start, &end, FALSE);
 
-       newword =  g_object_get_data (G_OBJECT (menuitem), SUGGESTION_KEY);
-       g_return_if_fail (newword != NULL);
+       new_word = g_object_get_data (G_OBJECT (menu_item), SUGGESTION_KEY);
+       g_return_if_fail (new_word != NULL);
 
        gtk_text_buffer_begin_user_action (spell->buffer);
 
        gtk_text_buffer_delete (spell->buffer, &start, &end);
-       gtk_text_buffer_insert (spell->buffer, &start, newword, -1);
+       gtk_text_buffer_insert (spell->buffer, &start, new_word, -1);
 
        gtk_text_buffer_end_user_action (spell->buffer);
 
        gedit_spell_checker_set_correction (spell->spell_checker,
-                               oldword, strlen (oldword),
-                               newword, strlen (newword));
+                                           old_word, strlen (old_word),
+                                           new_word, strlen (new_word));
 
-       g_free (oldword);
+       g_free (old_word);
 }
 
 static GtkWidget *
 build_suggestion_menu (GeditAutomaticSpellChecker *spell,
                       const gchar                *word)
 {
-       GtkWidget *topmenu, *menu;
-       GtkWidget *mi;
+       GtkWidget *top_menu;
+       GtkWidget *menu;
+       GtkWidget *menu_item;
        GSList *suggestions;
        GSList *list;
        gchar *label_text;
 
-       topmenu = menu = gtk_menu_new();
+       top_menu = menu = gtk_menu_new ();
 
        suggestions = gedit_spell_checker_get_suggestions (spell->spell_checker, word, -1);
 
@@ -412,16 +416,17 @@ build_suggestion_menu (GeditAutomaticSpellChecker *spell,
 
        if (suggestions == NULL)
        {
-               /* no suggestions.  put something in the menu anyway... */
                GtkWidget *label;
+
+               /* No suggestions. Put something in the menu anyway... */
                /* Translators: Displayed in the "Check Spelling" dialog if there are no suggestions for the 
current misspelled word */
                label = gtk_label_new (_("(no suggested words)"));
 
-               mi = gtk_menu_item_new ();
-               gtk_widget_set_sensitive (mi, FALSE);
-               gtk_container_add (GTK_CONTAINER(mi), label);
-               gtk_widget_show_all (mi);
-               gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), mi);
+               menu_item = gtk_menu_item_new ();
+               gtk_widget_set_sensitive (menu_item, FALSE);
+               gtk_container_add (GTK_CONTAINER (menu_item), label);
+               gtk_widget_show_all (menu_item);
+               gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), menu_item);
        }
        else
        {


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