2003-03-26 01:20:25 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file ASpell.cpp
|
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.
|
2003-03-26 01:20:25 +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.
|
2003-03-26 01:20:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2003-03-26 01:20:25 +00:00
|
|
|
|
|
|
|
#include <aspell.h>
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "ASpell_local.h"
|
2003-03-26 01:20:25 +00:00
|
|
|
#include "WordLangTuple.h"
|
|
|
|
|
2003-09-12 07:41:09 +00:00
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2003-10-07 13:32:17 +00:00
|
|
|
|
2006-10-21 07:26:07 +00:00
|
|
|
namespace lyx {
|
2003-09-05 16:31:30 +00:00
|
|
|
|
2003-03-26 01:20:25 +00:00
|
|
|
ASpell::ASpell(BufferParams const &, string const & lang)
|
|
|
|
: els(0), spell_error_object(0)
|
|
|
|
{
|
|
|
|
addSpeller(lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASpell::~ASpell()
|
|
|
|
{
|
|
|
|
if (spell_error_object) {
|
|
|
|
delete_aspell_can_have_error(spell_error_object);
|
|
|
|
spell_error_object = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (els)
|
|
|
|
delete_aspell_string_enumeration(els);
|
|
|
|
|
|
|
|
Spellers::iterator it = spellers_.begin();
|
|
|
|
Spellers::iterator end = spellers_.end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
aspell_speller_save_all_word_lists(it->second.speller);
|
|
|
|
delete_aspell_speller(it->second.speller);
|
|
|
|
delete_aspell_config(it->second.config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASpell::addSpeller(string const & lang)
|
|
|
|
{
|
|
|
|
AspellConfig * config = new_aspell_config();
|
2006-12-11 20:39:15 +00:00
|
|
|
// FIXME The aspell documentation says to use "lang"
|
2003-03-26 01:20:25 +00:00
|
|
|
aspell_config_replace(config, "language-tag", lang.c_str());
|
2006-12-12 08:17:22 +00:00
|
|
|
// Set the encoding to utf-8.
|
|
|
|
// aspell does also understand "ucs-4", so we would not need a
|
|
|
|
// conversion in theory, but if this is used it expects all
|
|
|
|
// char const * arguments to be a cast from uint const *, and it
|
|
|
|
// seems that this uint is not compatible with our char_type on some
|
|
|
|
// platforms (cygwin, OS X). Therefore we use utf-8, that does
|
|
|
|
// always work.
|
2006-12-11 20:39:15 +00:00
|
|
|
aspell_config_replace(config, "encoding", "utf-8");
|
2003-03-26 01:20:25 +00:00
|
|
|
AspellCanHaveError * err = new_aspell_speller(config);
|
|
|
|
if (spell_error_object)
|
|
|
|
delete_aspell_can_have_error(spell_error_object);
|
|
|
|
spell_error_object = 0;
|
|
|
|
|
|
|
|
if (aspell_error_number(err) == 0) {
|
|
|
|
Speller m;
|
|
|
|
m.speller = to_aspell_speller(err);
|
|
|
|
m.config = config;
|
|
|
|
spellers_[lang] = m;
|
|
|
|
} else {
|
|
|
|
spell_error_object = err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-05 20:21:27 +00:00
|
|
|
ASpell::Result ASpell::check(WordLangTuple const & word)
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
2005-01-20 16:17:37 +00:00
|
|
|
Result res = UNKNOWN_WORD;
|
2003-03-26 01:20:25 +00:00
|
|
|
|
|
|
|
Spellers::iterator it = spellers_.find(word.lang_code());
|
|
|
|
if (it == spellers_.end()) {
|
|
|
|
addSpeller(word.lang_code());
|
|
|
|
it = spellers_.find(word.lang_code());
|
|
|
|
// FIXME
|
|
|
|
if (it == spellers_.end())
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
AspellSpeller * m = it->second.speller;
|
|
|
|
|
2006-12-08 19:46:16 +00:00
|
|
|
int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
|
2003-09-09 17:25:35 +00:00
|
|
|
BOOST_ASSERT(word_ok != -1);
|
2003-03-26 01:20:25 +00:00
|
|
|
|
2007-11-29 18:29:57 +00:00
|
|
|
if (word_ok)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
AspellWordList const * sugs =
|
|
|
|
aspell_speller_suggest(m, to_utf8(word.word()).c_str(), -1);
|
|
|
|
BOOST_ASSERT(sugs != 0);
|
|
|
|
els = aspell_word_list_elements(sugs);
|
|
|
|
if (aspell_word_list_empty(sugs))
|
|
|
|
res = UNKNOWN_WORD;
|
|
|
|
else
|
|
|
|
res = SUGGESTED_WORDS;
|
|
|
|
|
2003-03-26 01:20:25 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASpell::insert(WordLangTuple const & word)
|
|
|
|
{
|
|
|
|
Spellers::iterator it = spellers_.find(word.lang_code());
|
|
|
|
if (it != spellers_.end())
|
2006-12-08 19:46:16 +00:00
|
|
|
aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASpell::accept(WordLangTuple const & word)
|
|
|
|
{
|
|
|
|
Spellers::iterator it = spellers_.find(word.lang_code());
|
|
|
|
if (it != spellers_.end())
|
2006-12-08 19:46:16 +00:00
|
|
|
aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-08 19:46:16 +00:00
|
|
|
docstring const ASpell::nextMiss()
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
|
|
|
char const * str = 0;
|
|
|
|
|
|
|
|
if (els)
|
|
|
|
str = aspell_string_enumeration_next(els);
|
2005-01-05 20:21:27 +00:00
|
|
|
|
2006-12-08 19:46:16 +00:00
|
|
|
return (str ? from_utf8(str) : docstring());
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring const ASpell::error()
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
|
|
|
char const * err = 0;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-11-29 18:29:57 +00:00
|
|
|
if (spell_error_object && aspell_error_number(spell_error_object) != 0)
|
2003-03-26 01:20:25 +00:00
|
|
|
err = aspell_error_message(spell_error_object);
|
|
|
|
|
2006-12-08 19:46:16 +00:00
|
|
|
// FIXME UNICODE: err is not in UTF8, but probably the locale encoding
|
2006-10-21 00:16:43 +00:00
|
|
|
return (err ? from_utf8(err) : docstring());
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|