|
Packit |
aa0600 |
/*
|
|
Packit |
aa0600 |
* This file is part of gspell, a spell-checking library.
|
|
Packit |
aa0600 |
*
|
|
Packit |
aa0600 |
* Copyright 2016 - Sébastien Wilmet
|
|
Packit |
aa0600 |
*
|
|
Packit |
aa0600 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
aa0600 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
aa0600 |
* License as published by the Free Software Foundation; either
|
|
Packit |
aa0600 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
aa0600 |
*
|
|
Packit |
aa0600 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
aa0600 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
aa0600 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
aa0600 |
* Lesser General Public License for more details.
|
|
Packit |
aa0600 |
*
|
|
Packit |
aa0600 |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit |
aa0600 |
* along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
aa0600 |
*/
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
aa0600 |
#include <config.h>
|
|
Packit |
aa0600 |
#endif
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
#include "gspell-current-word-policy.h"
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* An object that decides whether to check the current word. When a word is
|
|
Packit |
aa0600 |
* being typed, it should not be spell-checked, because it would be annoying to
|
|
Packit |
aa0600 |
* see the red wavy underline appearing and disappearing constantly.
|
|
Packit |
aa0600 |
*
|
|
Packit |
aa0600 |
* You need to feed the object with events, and get the result with
|
|
Packit |
aa0600 |
* _gspell_current_word_policy_get_check_current_word().
|
|
Packit |
aa0600 |
*/
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
typedef struct _GspellCurrentWordPolicyPrivate GspellCurrentWordPolicyPrivate;
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
struct _GspellCurrentWordPolicyPrivate
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
guint check_current_word : 1;
|
|
Packit |
aa0600 |
};
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
G_DEFINE_TYPE_WITH_PRIVATE (GspellCurrentWordPolicy, _gspell_current_word_policy, G_TYPE_OBJECT)
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
static void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_dispose (GObject *object)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
G_OBJECT_CLASS (_gspell_current_word_policy_parent_class)->dispose (object);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
static void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_finalize (GObject *object)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
G_OBJECT_CLASS (_gspell_current_word_policy_parent_class)->finalize (object);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
static void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_class_init (GspellCurrentWordPolicyClass *klass)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
object_class->dispose = _gspell_current_word_policy_dispose;
|
|
Packit |
aa0600 |
object_class->finalize = _gspell_current_word_policy_finalize;
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
static void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_init (GspellCurrentWordPolicy *policy)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
GspellCurrentWordPolicyPrivate *priv;
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
priv = _gspell_current_word_policy_get_instance_private (policy);
|
|
Packit |
aa0600 |
priv->check_current_word = TRUE;
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
GspellCurrentWordPolicy *
|
|
Packit |
aa0600 |
_gspell_current_word_policy_new (void)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
return g_object_new (GSPELL_TYPE_CURRENT_WORD_POLICY, NULL);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
gboolean
|
|
Packit |
aa0600 |
_gspell_current_word_policy_get_check_current_word (GspellCurrentWordPolicy *policy)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
GspellCurrentWordPolicyPrivate *priv;
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
g_return_val_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy), TRUE);
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
priv = _gspell_current_word_policy_get_instance_private (policy);
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
return priv->check_current_word;
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* For other events, it's better to use the more specific feed functions if
|
|
Packit |
aa0600 |
* possible.
|
|
Packit |
aa0600 |
*/
|
|
Packit |
aa0600 |
void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (GspellCurrentWordPolicy *policy,
|
|
Packit |
aa0600 |
gboolean check_current_word)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
GspellCurrentWordPolicyPrivate *priv;
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
priv = _gspell_current_word_policy_get_instance_private (policy);
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
priv->check_current_word = check_current_word != FALSE;
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* On GspellChecker::session-cleared signal. */
|
|
Packit |
aa0600 |
void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_session_cleared (GspellCurrentWordPolicy *policy)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* On GspellChecker::notify::language signal. */
|
|
Packit |
aa0600 |
void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_language_changed (GspellCurrentWordPolicy *policy)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* When another GspellChecker object is used. */
|
|
Packit |
aa0600 |
void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_checker_changed (GspellCurrentWordPolicy *policy)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_cursor_moved (GspellCurrentWordPolicy *policy)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* After a text insertion. */
|
|
Packit |
aa0600 |
void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_several_chars_inserted (GspellCurrentWordPolicy *policy)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* If more than one character is inserted, it's probably not a normal
|
|
Packit |
aa0600 |
* keypress, e.g. a clipboard paste or DND. So it's better to check the
|
|
Packit |
aa0600 |
* current word in that case, to know ASAP if the word is correctly
|
|
Packit |
aa0600 |
* spelled.
|
|
Packit |
aa0600 |
*/
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* After a text insertion. */
|
|
Packit |
aa0600 |
void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_single_char_inserted (GspellCurrentWordPolicy *policy,
|
|
Packit |
aa0600 |
gunichar ch,
|
|
Packit |
aa0600 |
gboolean empty_selection,
|
|
Packit |
aa0600 |
gboolean at_cursor_pos)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* If e.g. a space or punctuation is inserted, we want to check the
|
|
Packit |
aa0600 |
* current word, since in that case we are not editing the current word.
|
|
Packit |
aa0600 |
* Maybe a word has been split in two, in which case the word on the
|
|
Packit |
aa0600 |
* left will anyway be checked, so it's better to know directly whether
|
|
Packit |
aa0600 |
* the word on the right is correctly spelled as well, so we know if we
|
|
Packit |
aa0600 |
* need to edit it or not.
|
|
Packit |
aa0600 |
* If there is a selection, it means that the text was inserted
|
|
Packit |
aa0600 |
* programmatically, so the user is not editing the current word
|
|
Packit |
aa0600 |
* manually.
|
|
Packit |
aa0600 |
*/
|
|
Packit |
aa0600 |
if (g_unichar_isalnum (ch) &&
|
|
Packit |
aa0600 |
empty_selection &&
|
|
Packit |
aa0600 |
at_cursor_pos)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, FALSE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
else
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
/* Before a text deletion.
|
|
Packit |
aa0600 |
*
|
|
Packit |
aa0600 |
* "start" refers to the start of the deletion.
|
|
Packit |
aa0600 |
* "end" refers to the end of the deletion.
|
|
Packit |
aa0600 |
* It is assumed that start < end.
|
|
Packit |
aa0600 |
*
|
|
Packit |
aa0600 |
* "inside word" and "ends word" have the same semantics as
|
|
Packit |
aa0600 |
* gtk_text_iter_inside_word() and gtk_text_iter_ends_word(), but custom word
|
|
Packit |
aa0600 |
* boundaries can be used.
|
|
Packit |
aa0600 |
*/
|
|
Packit |
aa0600 |
void
|
|
Packit |
aa0600 |
_gspell_current_word_policy_text_deleted (GspellCurrentWordPolicy *policy,
|
|
Packit |
aa0600 |
gboolean empty_selection,
|
|
Packit |
aa0600 |
gboolean spans_several_lines,
|
|
Packit |
aa0600 |
gboolean several_chars,
|
|
Packit |
aa0600 |
gboolean cursor_pos_at_start,
|
|
Packit |
aa0600 |
gboolean cursor_pos_at_end,
|
|
Packit |
aa0600 |
gboolean start_is_inside_word,
|
|
Packit |
aa0600 |
gboolean start_ends_word,
|
|
Packit |
aa0600 |
gboolean end_is_inside_word,
|
|
Packit |
aa0600 |
gboolean end_ends_word)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
g_return_if_fail (GSPELL_IS_CURRENT_WORD_POLICY (policy));
|
|
Packit |
aa0600 |
|
|
Packit |
aa0600 |
if (!empty_selection ||
|
|
Packit |
aa0600 |
spans_several_lines ||
|
|
Packit |
aa0600 |
several_chars)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
/* Probably backspace key */
|
|
Packit |
aa0600 |
else if (cursor_pos_at_end)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
if (start_is_inside_word || start_ends_word)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, FALSE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
else
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
/* Probably delete key */
|
|
Packit |
aa0600 |
else if (cursor_pos_at_start)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
if (end_is_inside_word || end_ends_word)
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, FALSE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
else
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
/* Text deleted programmatically */
|
|
Packit |
aa0600 |
else
|
|
Packit |
aa0600 |
{
|
|
Packit |
aa0600 |
_gspell_current_word_policy_set_check_current_word (policy, TRUE);
|
|
Packit |
aa0600 |
}
|
|
Packit |
aa0600 |
}
|