2002-08-04 23:11:50 +00:00
|
|
|
/**
|
|
|
|
* \file pspell.C
|
|
|
|
* Copyright 2001 the LyX Team
|
|
|
|
* Read the file COPYING
|
|
|
|
*
|
|
|
|
* \author Kevin Atkinson
|
|
|
|
* \author John Levon <levon@movementarian.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef USE_PSPELL
|
|
|
|
|
|
|
|
#include "support/LAssert.h"
|
2003-02-17 18:40:04 +00:00
|
|
|
#include "debug.h"
|
2002-08-04 23:11:50 +00:00
|
|
|
|
|
|
|
#define USE_ORIGINAL_MANAGER_FUNCS 1
|
2002-12-01 22:59:25 +00:00
|
|
|
// new aspell pspell missing extern "C"
|
|
|
|
extern "C" {
|
2002-08-04 23:11:50 +00:00
|
|
|
#include <pspell/pspell.h>
|
2002-08-06 02:37:41 +00:00
|
|
|
}
|
2002-08-04 23:11:50 +00:00
|
|
|
|
|
|
|
#include "pspell.h"
|
2002-08-13 14:40:38 +00:00
|
|
|
#include "WordLangTuple.h"
|
2002-08-04 23:11:50 +00:00
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
using std::endl;
|
|
|
|
|
2002-08-06 02:37:41 +00:00
|
|
|
PSpell::PSpell(BufferParams const &, string const & lang)
|
2002-08-06 22:38:44 +00:00
|
|
|
: els(0), spell_error_object(0)
|
2002-08-04 23:11:50 +00:00
|
|
|
{
|
2002-12-01 22:59:25 +00:00
|
|
|
addManager(lang);
|
2003-02-17 18:40:04 +00:00
|
|
|
lyxerr[Debug::GUI] << "created pspell" << endl;
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PSpell::~PSpell()
|
|
|
|
{
|
2003-02-17 18:40:04 +00:00
|
|
|
lyxerr[Debug::GUI] << "killed pspell" << endl;
|
|
|
|
|
|
|
|
if (spell_error_object) {
|
|
|
|
delete_pspell_can_have_error(spell_error_object);
|
|
|
|
spell_error_object = 0;
|
|
|
|
}
|
|
|
|
|
2002-08-04 23:11:50 +00:00
|
|
|
if (els)
|
|
|
|
delete_pspell_string_emulation(els);
|
2003-02-17 18:40:04 +00:00
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
Managers::iterator it = managers_.begin();
|
|
|
|
Managers::iterator end = managers_.end();
|
2002-12-01 22:59:25 +00:00
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
2003-02-17 18:40:04 +00:00
|
|
|
pspell_manager_save_all_word_lists(it->second.manager);
|
2002-08-06 22:38:44 +00:00
|
|
|
delete_pspell_manager(it->second.manager);
|
|
|
|
delete_pspell_config(it->second.config);
|
|
|
|
}
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
void PSpell::addManager(string const & lang)
|
|
|
|
{
|
|
|
|
PspellConfig * config = new_pspell_config();
|
|
|
|
pspell_config_replace(config, "language-tag", lang.c_str());
|
|
|
|
PspellCanHaveError * err = new_pspell_manager(config);
|
|
|
|
if (spell_error_object)
|
|
|
|
delete_pspell_can_have_error(spell_error_object);
|
|
|
|
spell_error_object = 0;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
if (pspell_error_number(err) == 0) {
|
|
|
|
Manager m;
|
|
|
|
m.manager = to_pspell_manager(err);
|
|
|
|
m.config = config;
|
|
|
|
managers_[lang] = m;
|
|
|
|
} else {
|
|
|
|
spell_error_object = err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
enum PSpell::Result PSpell::check(WordLangTuple const & word)
|
2002-08-04 23:11:50 +00:00
|
|
|
{
|
|
|
|
Result res = UNKNOWN;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
Managers::iterator it = managers_.find(word.lang_code());
|
|
|
|
if (it == managers_.end()) {
|
|
|
|
addManager(word.lang_code());
|
|
|
|
it = managers_.find(word.lang_code());
|
|
|
|
// FIXME
|
2002-12-01 22:59:25 +00:00
|
|
|
if (it == managers_.end())
|
2002-08-06 22:38:44 +00:00
|
|
|
return res;
|
|
|
|
}
|
2002-08-04 23:11:50 +00:00
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
PspellManager * m = it->second.manager;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
int word_ok = pspell_manager_check(m, word.word().c_str());
|
2002-08-04 23:11:50 +00:00
|
|
|
lyx::Assert(word_ok != -1);
|
|
|
|
|
|
|
|
if (word_ok) {
|
|
|
|
res = OK;
|
|
|
|
} else {
|
|
|
|
PspellWordList const * sugs =
|
2002-08-06 22:38:44 +00:00
|
|
|
pspell_manager_suggest(m, word.word().c_str());
|
2002-08-04 23:11:50 +00:00
|
|
|
lyx::Assert(sugs != 0);
|
|
|
|
els = pspell_word_list_elements(sugs);
|
|
|
|
if (pspell_word_list_empty(sugs))
|
|
|
|
res = UNKNOWN;
|
|
|
|
else
|
|
|
|
res = MISSED;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
void PSpell::insert(WordLangTuple const & word)
|
2002-08-04 23:11:50 +00:00
|
|
|
{
|
2002-08-06 22:38:44 +00:00
|
|
|
Managers::iterator it = managers_.find(word.lang_code());
|
|
|
|
if (it != managers_.end())
|
|
|
|
pspell_manager_add_to_personal(it->second.manager, word.word().c_str());
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-06 22:38:44 +00:00
|
|
|
void PSpell::accept(WordLangTuple const & word)
|
2002-08-04 23:11:50 +00:00
|
|
|
{
|
2002-08-06 22:38:44 +00:00
|
|
|
Managers::iterator it = managers_.find(word.lang_code());
|
2002-12-01 22:59:25 +00:00
|
|
|
if (it != managers_.end())
|
2002-08-06 22:38:44 +00:00
|
|
|
pspell_manager_add_to_session(it->second.manager, word.word().c_str());
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const PSpell::nextMiss()
|
|
|
|
{
|
|
|
|
char const * str = 0;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-08-04 23:11:50 +00:00
|
|
|
if (els)
|
|
|
|
str = pspell_string_emulation_next(els);
|
|
|
|
if (str)
|
|
|
|
return str;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const PSpell::error()
|
|
|
|
{
|
|
|
|
char const * err = 0;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-08-06 02:37:41 +00:00
|
|
|
if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
|
2002-08-04 23:11:50 +00:00
|
|
|
err = pspell_error_message(spell_error_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_PSPELL
|