#7170 add id() method for language; add variety of language to hunspell dictionary lookup; use dicts as subdirectory to search for in system_support and user_support directories

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37716 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Stephan Witt 2011-02-17 17:09:06 +00:00
parent e2329914ec
commit 4ba2c362cb
3 changed files with 40 additions and 36 deletions

View File

@ -61,9 +61,6 @@ struct AspellChecker::Private
/// ///
AspellSpeller * speller(Language const * lang); AspellSpeller * speller(Language const * lang);
/// create a unique ID from lang code and variety
string const spellerID(Language const * lang);
bool isValidDictionary(AspellConfig * config, bool isValidDictionary(AspellConfig * config,
string const & lang, string const & variety); string const & lang, string const & variety);
bool checkAspellData(AspellConfig * config, bool checkAspellData(AspellConfig * config,
@ -88,7 +85,7 @@ struct AspellChecker::Private
/// the location below system/user directory /// the location below system/user directory
/// there the rws files lookup will happen /// there the rws files lookup will happen
const string dictDirectory(void) { return "dict"; } const string dictDirectory(void) { return "dicts"; }
/// there the dat+cmap files lookup will happen /// there the dat+cmap files lookup will happen
const string dataDirectory(void) { return "data"; } const string dataDirectory(void) { return "data"; }
/// os package directory constants /// os package directory constants
@ -273,14 +270,14 @@ AspellSpeller * AspellChecker::Private::addSpeller(Language const * lang)
initSessionDictionary(m, pd); initSessionDictionary(m, pd);
} }
spellers_[spellerID(lang)] = m; spellers_[lang->id()] = m;
return m.e_speller ? to_aspell_speller(m.e_speller) : 0; return m.e_speller ? to_aspell_speller(m.e_speller) : 0;
} }
AspellSpeller * AspellChecker::Private::speller(Language const * lang) AspellSpeller * AspellChecker::Private::speller(Language const * lang)
{ {
Spellers::iterator it = spellers_.find(spellerID(lang)); Spellers::iterator it = spellers_.find(lang->id());
if (it != spellers_.end()) if (it != spellers_.end())
return to_aspell_speller(it->second.e_speller); return to_aspell_speller(it->second.e_speller);
@ -288,12 +285,6 @@ AspellSpeller * AspellChecker::Private::speller(Language const * lang)
} }
string const AspellChecker::Private::spellerID(Language const * lang)
{
return lang->code() + "-" + lang->variety();
}
SpellChecker::Result AspellChecker::Private::check( SpellChecker::Result AspellChecker::Private::check(
AspellSpeller * m, string const & word) AspellSpeller * m, string const & word)
const const
@ -316,7 +307,7 @@ void AspellChecker::Private::remove(WordLangTuple const & word)
if (!pd) if (!pd)
return; return;
pd->remove(word.word()); pd->remove(word.word());
Spellers::iterator it = spellers_.find(spellerID(word.lang())); Spellers::iterator it = spellers_.find(word.lang()->id());
if (it != spellers_.end()) { if (it != spellers_.end()) {
initSessionDictionary(it->second, pd); initSessionDictionary(it->second, pd);
} }
@ -325,7 +316,7 @@ void AspellChecker::Private::remove(WordLangTuple const & word)
void AspellChecker::Private::insert(WordLangTuple const & word) void AspellChecker::Private::insert(WordLangTuple const & word)
{ {
Spellers::iterator it = spellers_.find(spellerID(word.lang())); Spellers::iterator it = spellers_.find(word.lang()->id());
if (it != spellers_.end()) { if (it != spellers_.end()) {
AspellSpeller * speller = to_aspell_speller(it->second.e_speller); AspellSpeller * speller = to_aspell_speller(it->second.e_speller);
aspell_speller_add_to_session(speller, to_utf8(word.word()).c_str(), -1); aspell_speller_add_to_session(speller, to_utf8(word.word()).c_str(), -1);
@ -389,7 +380,7 @@ void AspellChecker::insert(WordLangTuple const & word)
void AspellChecker::accept(WordLangTuple const & word) void AspellChecker::accept(WordLangTuple const & word)
{ {
Spellers::iterator it = d->spellers_.find(d->spellerID(word.lang())); Spellers::iterator it = d->spellers_.find(word.lang()->id());
if (it != d->spellers_.end()) { if (it != d->spellers_.end()) {
AspellSpeller * speller = to_aspell_speller(it->second.e_speller); AspellSpeller * speller = to_aspell_speller(it->second.e_speller);
aspell_speller_add_to_session(speller, to_utf8(word.word()).c_str(), -1); aspell_speller_add_to_session(speller, to_utf8(word.word()).c_str(), -1);

View File

@ -57,9 +57,9 @@ struct HunspellChecker::Private
const string dictPath(int selector); const string dictPath(int selector);
bool haveLanguageFiles(string const & hpath); bool haveLanguageFiles(string const & hpath);
bool haveDictionary(string const & lang, string & hpath); bool haveDictionary(Language const * lang, string & hpath);
bool haveDictionary(string const & lang); bool haveDictionary(Language const * lang);
Hunspell * addSpeller(string const & lang, string & hpath); Hunspell * addSpeller(Language const * lang, string & hpath);
Hunspell * addSpeller(Language const * lang); Hunspell * addSpeller(Language const * lang);
Hunspell * speller(Language const * lang); Hunspell * speller(Language const * lang);
/// ignored words /// ignored words
@ -77,7 +77,7 @@ struct HunspellChecker::Private
/// the location below system/user directory /// the location below system/user directory
/// there the aff+dic files lookup will happen /// there the aff+dic files lookup will happen
const string dictDirectory(void) const { return "dict"; } const string dictDirectory(void) const { return "dicts"; }
int maxLookupSelector(void) const { return 3; } int maxLookupSelector(void) const { return 3; }
}; };
@ -132,43 +132,53 @@ const string HunspellChecker::Private::dictPath(int selector)
} }
bool HunspellChecker::Private::haveDictionary(string const & lang, string & hpath) bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
{ {
if (hpath.empty()) { if (hpath.empty()) {
return false; return false;
} }
LYXERR(Debug::FILES, "check hunspell path: " << hpath << " for language " << lang); LYXERR(Debug::FILES, "check hunspell path: " << hpath << " for language " << lang);
string h_path = addName(hpath, lang); string h_path = addName(hpath, lang->code() + "-" + lang->variety());
// first we try lang code+variety
if (!lang->variety().empty() && haveLanguageFiles(h_path)) {
hpath = h_path;
return true;
}
// next we try lang code only
h_path = addName(hpath, lang->code());
if (haveLanguageFiles(h_path)) {
hpath = h_path;
return true;
}
// last try with '_' replaced by '-'
h_path = addName(hpath, subst(lang->code(), '_', '-'));
if (!haveLanguageFiles(h_path)) { if (!haveLanguageFiles(h_path)) {
// try with '_' replaced by '-' return false;
h_path = addName(hpath, subst(lang, '_', '-'));
if (!haveLanguageFiles(h_path)) {
// FIXME: We should indicate somehow that this language is not
// supported, probably by popping a warning. But we'll need to
// remember which warnings we've issued.
return false;
}
} }
hpath = h_path; hpath = h_path;
return true; return true;
} }
bool HunspellChecker::Private::haveDictionary(string const & lang) bool HunspellChecker::Private::haveDictionary(Language const * lang)
{ {
bool result = false; bool result = false;
for ( int p = 0; !result && p < maxLookupSelector(); p++ ) { for ( int p = 0; !result && p < maxLookupSelector(); p++ ) {
string lpath = dictPath(p); string lpath = dictPath(p);
result = haveDictionary(lang, lpath); result = haveDictionary(lang, lpath);
} }
// FIXME: if result is false...
// we should indicate somehow that this language is not
// supported, probably by popping a warning. But we'll need to
// remember which warnings we've issued.
return result; return result;
} }
Hunspell * HunspellChecker::Private::speller(Language const * lang) Hunspell * HunspellChecker::Private::speller(Language const * lang)
{ {
Spellers::iterator it = spellers_.find(lang->code()); Spellers::iterator it = spellers_.find(lang->id());
if (it != spellers_.end()) if (it != spellers_.end())
return it->second; return it->second;
@ -176,10 +186,10 @@ Hunspell * HunspellChecker::Private::speller(Language const * lang)
} }
Hunspell * HunspellChecker::Private::addSpeller(string const & lang,string & path) Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
{ {
if (!haveDictionary(lang, path)) { if (!haveDictionary(lang, path)) {
spellers_[lang] = 0; spellers_[lang->id()] = 0;
return 0; return 0;
} }
@ -187,7 +197,7 @@ Hunspell * HunspellChecker::Private::addSpeller(string const & lang,string & pat
FileName const dict(path + ".dic"); FileName const dict(path + ".dic");
Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str()); Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found"); LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
spellers_[lang] = h; spellers_[lang->id()] = h;
return h; return h;
} }
@ -197,7 +207,7 @@ Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
Hunspell * h = 0; Hunspell * h = 0;
for ( int p = 0; p < maxLookupSelector() && 0 == h; p++ ) { for ( int p = 0; p < maxLookupSelector() && 0 == h; p++ ) {
string lpath = dictPath(p); string lpath = dictPath(p);
h = addSpeller(lang->code(), lpath); h = addSpeller(lang, lpath);
} }
if (0 != h) { if (0 != h) {
string const encoding = h->get_dic_encoding(); string const encoding = h->get_dic_encoding();
@ -360,7 +370,7 @@ bool HunspellChecker::hasDictionary(Language const * lang) const
{ {
if (!lang) if (!lang)
return false; return false;
return (d->haveDictionary(lang->code())); return (d->haveDictionary(lang));
} }

View File

@ -57,6 +57,9 @@ public:
std::string const & variety() const { return variety_; } std::string const & variety() const { return variety_; }
/// set variety (needed for rc.spellchecker_alt_lang) /// set variety (needed for rc.spellchecker_alt_lang)
void setVariety(std::string const v) { variety_ = v; } void setVariety(std::string const v) { variety_ = v; }
/// create a unique ID from lang code and variety
std::string const id() const {
return variety_.empty() ? code_ : code_ + "-" + variety_; }
/// preamble settings after babel was called /// preamble settings after babel was called
std::string const & babel_postsettings() const { return babel_postsettings_; } std::string const & babel_postsettings() const { return babel_postsettings_; }
/// preamble settings before babel is called /// preamble settings before babel is called