[gnome-text-editor] spellcheck: allow ignoring a word for the session



commit 165c0d14c72bd7a76befb9cbe2be6c190f64c022
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jul 16 19:53:07 2021 -0700

    spellcheck: allow ignoring a word for the session

 src/editor-document-private.h               |  4 +++-
 src/editor-document.c                       | 14 ++++++++++++--
 src/editor-source-view.c                    | 28 ++++++++++++++++++++++++----
 src/editor-spell-checker.c                  | 11 +++++++++++
 src/editor-spell-checker.h                  |  2 ++
 src/editor-spell-language.c                 | 11 +++++++++++
 src/editor-spell-language.h                 |  4 ++++
 src/enchant/editor-enchant-spell-language.c | 13 +++++++++++++
 8 files changed, 80 insertions(+), 7 deletions(-)
---
diff --git a/src/editor-document-private.h b/src/editor-document-private.h
index b12951b..f65de02 100644
--- a/src/editor-document-private.h
+++ b/src/editor-document-private.h
@@ -80,7 +80,9 @@ gboolean                  _editor_document_check_spelling          (EditorDocume
                                                                     const char               *word);
 char                    **_editor_document_list_corrections        (EditorDocument           *self,
                                                                     const char               *word);
-void                      _editor_document_add_spelling_word       (EditorDocument           *self,
+void                      _editor_document_add_spelling            (EditorDocument           *self,
+                                                                    const char               *word);
+void                      _editor_document_ignore_spelling         (EditorDocument           *self,
                                                                     const char               *word);
 
 G_END_DECLS
diff --git a/src/editor-document.c b/src/editor-document.c
index a70ae2b..f7bd274 100644
--- a/src/editor-document.c
+++ b/src/editor-document.c
@@ -1842,11 +1842,21 @@ _editor_document_list_corrections (EditorDocument *self,
 }
 
 void
-_editor_document_add_spelling_word (EditorDocument *self,
-                                    const char     *word)
+_editor_document_add_spelling (EditorDocument *self,
+                               const char     *word)
 {
   g_return_if_fail (EDITOR_IS_DOCUMENT (self));
 
   if (self->spell_checker != NULL)
     editor_spell_checker_add_word (self->spell_checker, word);
 }
+
+void
+_editor_document_ignore_spelling (EditorDocument *self,
+                                  const char     *word)
+{
+  g_return_if_fail (EDITOR_IS_DOCUMENT (self));
+
+  if (self->spell_checker != NULL)
+    editor_spell_checker_ignore_word (self->spell_checker, word);
+}
diff --git a/src/editor-source-view.c b/src/editor-source-view.c
index d395b8a..235554f 100644
--- a/src/editor-source-view.c
+++ b/src/editor-source-view.c
@@ -160,9 +160,8 @@ on_click_pressed_cb (GtkGestureClick  *click,
 cleanup:
   g_free (self->spelling_word);
   self->spelling_word = g_steal_pointer (&word);
-  gtk_widget_action_set_enabled (GTK_WIDGET (self),
-                                 "spelling.add",
-                                 self->spelling_word != NULL);
+  gtk_widget_action_set_enabled (GTK_WIDGET (self), "spelling.add", self->spelling_word != NULL);
+  gtk_widget_action_set_enabled (GTK_WIDGET (self), "spelling.ignore", self->spelling_word != NULL);
   editor_spell_menu_set_corrections (self->spelling_menu,
                                      (const char * const *)corrections);
 }
@@ -197,7 +196,26 @@ editor_source_view_action_spelling_add (GtkWidget  *widget,
   if (EDITOR_IS_DOCUMENT (buffer))
     {
       g_debug ("Adding ā€œ%sā€ to dictionary\n", self->spelling_word);
-      _editor_document_add_spelling_word (EDITOR_DOCUMENT (buffer), self->spelling_word);
+      _editor_document_add_spelling (EDITOR_DOCUMENT (buffer), self->spelling_word);
+    }
+}
+
+static void
+editor_source_view_action_spelling_ignore (GtkWidget  *widget,
+                                           const char *action_name,
+                                           GVariant   *param)
+{
+  EditorSourceView *self = (EditorSourceView *)widget;
+  GtkTextBuffer *buffer;
+
+  g_assert (EDITOR_IS_SOURCE_VIEW (self));
+
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self));
+
+  if (EDITOR_IS_DOCUMENT (buffer))
+    {
+      g_debug ("Ignoring ā€œ%sā€\n", self->spelling_word);
+      _editor_document_ignore_spelling (EDITOR_DOCUMENT (buffer), self->spelling_word);
     }
 }
 
@@ -221,6 +239,7 @@ editor_source_view_class_init (EditorSourceViewClass *klass)
   object_class->finalize = editor_source_view_finalize;
 
   gtk_widget_class_install_action (widget_class, "spelling.add", NULL, 
editor_source_view_action_spelling_add);
+  gtk_widget_class_install_action (widget_class, "spelling.ignore", NULL, 
editor_source_view_action_spelling_ignore);
 }
 
 static void
@@ -233,6 +252,7 @@ editor_source_view_init (EditorSourceView *self)
   GMenuModel *extra_menu;
 
   gtk_widget_action_set_enabled (GTK_WIDGET (self), "spelling.add", FALSE);
+  gtk_widget_action_set_enabled (GTK_WIDGET (self), "spelling.ignore", FALSE);
 
   g_signal_connect (self,
                     "notify::buffer",
diff --git a/src/editor-spell-checker.c b/src/editor-spell-checker.c
index 3a49b34..f4aae26 100644
--- a/src/editor-spell-checker.c
+++ b/src/editor-spell-checker.c
@@ -300,3 +300,14 @@ editor_spell_checker_add_word (EditorSpellChecker *self,
   if (self->language != NULL)
     editor_spell_language_add_word (self->language, word);
 }
+
+void
+editor_spell_checker_ignore_word (EditorSpellChecker *self,
+                                  const char         *word)
+{
+  g_return_if_fail (EDITOR_IS_SPELL_CHECKER (self));
+  g_return_if_fail (word != NULL);
+
+  if (self->language != NULL)
+    editor_spell_language_ignore_word (self->language, word);
+}
diff --git a/src/editor-spell-checker.h b/src/editor-spell-checker.h
index 1a03cb2..2553f06 100644
--- a/src/editor-spell-checker.h
+++ b/src/editor-spell-checker.h
@@ -41,5 +41,7 @@ char                **editor_spell_checker_list_corrections (EditorSpellChecker
                                                              const char          *word);
 void                  editor_spell_checker_add_word         (EditorSpellChecker  *self,
                                                              const char          *word);
+void                  editor_spell_checker_ignore_word      (EditorSpellChecker  *self,
+                                                             const char          *word);
 
 G_END_DECLS
diff --git a/src/editor-spell-language.c b/src/editor-spell-language.c
index b39377c..a60b145 100644
--- a/src/editor-spell-language.c
+++ b/src/editor-spell-language.c
@@ -153,3 +153,14 @@ editor_spell_language_add_word (EditorSpellLanguage *self,
   if (EDITOR_SPELL_LANGUAGE_GET_CLASS (self)->add_word)
     EDITOR_SPELL_LANGUAGE_GET_CLASS (self)->add_word (self, word);
 }
+
+void
+editor_spell_language_ignore_word (EditorSpellLanguage *self,
+                                   const char          *word)
+{
+  g_return_if_fail (EDITOR_IS_SPELL_LANGUAGE (self));
+  g_return_if_fail (word != NULL);
+
+  if (EDITOR_SPELL_LANGUAGE_GET_CLASS (self)->ignore_word)
+    EDITOR_SPELL_LANGUAGE_GET_CLASS (self)->ignore_word (self, word);
+}
diff --git a/src/editor-spell-language.h b/src/editor-spell-language.h
index 9d8d8b5..8c2055b 100644
--- a/src/editor-spell-language.h
+++ b/src/editor-spell-language.h
@@ -40,6 +40,8 @@ struct _EditorSpellLanguageClass
                                    gssize               word_len);
   void        (*add_word)         (EditorSpellLanguage *self,
                                    const char          *word);
+  void        (*ignore_word)      (EditorSpellLanguage *self,
+                                   const char          *word);
 
   /*< private >*/
   gpointer _reserved[8];
@@ -54,5 +56,7 @@ char       **editor_spell_language_list_corrections (EditorSpellLanguage *self,
                                                      gssize               word_len);
 void         editor_spell_language_add_word         (EditorSpellLanguage *self,
                                                      const char          *word);
+void         editor_spell_language_ignore_word      (EditorSpellLanguage *self,
+                                                     const char          *word);
 
 G_END_DECLS
diff --git a/src/enchant/editor-enchant-spell-language.c b/src/enchant/editor-enchant-spell-language.c
index 00b6986..04da11e 100644
--- a/src/enchant/editor-enchant-spell-language.c
+++ b/src/enchant/editor-enchant-spell-language.c
@@ -162,6 +162,18 @@ editor_enchant_spell_language_add_word (EditorSpellLanguage *language,
   enchant_dict_add (self->native, word, -1);
 }
 
+static void
+editor_enchant_spell_language_ignore_word (EditorSpellLanguage *language,
+                                           const char          *word)
+{
+  EditorEnchantSpellLanguage *self = (EditorEnchantSpellLanguage *)language;
+
+  g_assert (EDITOR_IS_SPELL_LANGUAGE (language));
+  g_assert (word != NULL);
+
+  enchant_dict_add_to_session (self->native, word, -1);
+}
+
 static void
 editor_enchant_spell_language_constructed (GObject *object)
 {
@@ -243,6 +255,7 @@ editor_enchant_spell_language_class_init (EditorEnchantSpellLanguageClass *klass
   spell_language_class->contains_word = editor_enchant_spell_language_contains_word;
   spell_language_class->list_corrections = editor_enchant_spell_language_list_corrections;
   spell_language_class->add_word = editor_enchant_spell_language_add_word;
+  spell_language_class->ignore_word = editor_enchant_spell_language_ignore_word;
 
   properties [PROP_NATIVE] =
     g_param_spec_pointer ("native",


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