2002-08-04 23:11:50 +00:00
|
|
|
/**
|
|
|
|
* \file pspell.C
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-08-04 23:11:50 +00:00
|
|
|
*
|
|
|
|
* \author Kevin Atkinson
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-08-04 23:11:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.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-09-09 18:19:34 +00:00
|
|
|
#include <boost/assert.hpp>
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2003-02-17 18:40:04 +00:00
|
|
|
using std::endl;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2003-02-17 18:40:04 +00:00
|
|
|
|
2003-09-09 18:19:34 +00:00
|
|
|
|
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);
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GUI) << "created pspell" << endl;
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PSpell::~PSpell()
|
|
|
|
{
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GUI) << "killed pspell" << endl;
|
2003-02-17 18:40:04 +00:00
|
|
|
|
|
|
|
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());
|
2006-12-10 17:47:44 +00:00
|
|
|
pspell_config_replace(config, "encoding", "utf-8");
|
2002-08-06 22:38:44 +00:00
|
|
|
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
|
|
|
{
|
2005-01-20 16:17:37 +00:00
|
|
|
Result res = UNKNOWN_WORD;
|
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
|
|
|
|
2006-12-08 19:46:16 +00:00
|
|
|
int word_ok = pspell_manager_check(m, to_utf8(word.word()).c_str());
|
2003-09-09 17:25:35 +00:00
|
|
|
BOOST_ASSERT(word_ok != -1);
|
2002-08-04 23:11:50 +00:00
|
|
|
|
|
|
|
if (word_ok) {
|
|
|
|
res = OK;
|
|
|
|
} else {
|
|
|
|
PspellWordList const * sugs =
|
2006-12-08 19:46:16 +00:00
|
|
|
pspell_manager_suggest(m, to_utf8(word.word()).c_str());
|
2003-09-09 17:25:35 +00:00
|
|
|
BOOST_ASSERT(sugs != 0);
|
2002-08-04 23:11:50 +00:00
|
|
|
els = pspell_word_list_elements(sugs);
|
|
|
|
if (pspell_word_list_empty(sugs))
|
2005-01-20 16:17:37 +00:00
|
|
|
res = UNKNOWN_WORD;
|
2002-08-04 23:11:50 +00:00
|
|
|
else
|
2005-01-20 16:17:37 +00:00
|
|
|
res = SUGGESTED_WORDS;
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
|
|
|
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())
|
2006-12-08 19:46:16 +00:00
|
|
|
pspell_manager_add_to_personal(it->second.manager, to_utf8(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())
|
2006-12-08 19:46:16 +00:00
|
|
|
pspell_manager_add_to_session(it->second.manager, to_utf8(word.word()).c_str());
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-08 19:46:16 +00:00
|
|
|
docstring const PSpell::nextMiss()
|
2002-08-04 23:11:50 +00:00
|
|
|
{
|
|
|
|
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)
|
2006-12-08 19:46:16 +00:00
|
|
|
return from_utf8(str);
|
|
|
|
return docstring();
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring const PSpell::error()
|
2002-08-04 23:11:50 +00:00
|
|
|
{
|
|
|
|
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)
|
2006-12-08 19:41:20 +00:00
|
|
|
return from_utf8(err);
|
|
|
|
return docstring();
|
2002-08-04 23:11:50 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|