implement the paragraph check feature for apple speller

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35276 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Stephan Witt 2010-09-04 07:48:15 +00:00
parent d7c90fd841
commit 4bde2bc7bc
5 changed files with 63 additions and 28 deletions

View File

@ -78,8 +78,12 @@ string AppleSpellChecker::Private::toString(SpellCheckResult status)
SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word) SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word)
{ {
string const word_str = to_utf8(word.word()); string const word_str = to_utf8(word.word());
SpellCheckResult result = checkAppleSpeller(d->speller, word_str.c_str(), word.lang()->code().c_str()); SpellCheckResult result =
LYXERR(Debug::GUI, "spellCheck: \"" << word.word() << "\" = " << d->toString(result)) ; AppleSpeller_check(d->speller,
word_str.c_str(), word.lang()->code().c_str());
LYXERR(Debug::GUI, "spellCheck: \"" <<
word.word() << "\" = " << d->toString(result) <<
", lang = " << word.lang()->code()) ;
return d->toResult(result); return d->toResult(result);
} }
@ -88,7 +92,7 @@ SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word)
void AppleSpellChecker::insert(WordLangTuple const & word) void AppleSpellChecker::insert(WordLangTuple const & word)
{ {
string const word_str = to_utf8(word.word()); string const word_str = to_utf8(word.word());
learnAppleSpeller(d->speller, word_str.c_str()); AppleSpeller_learn(d->speller, word_str.c_str());
LYXERR(Debug::GUI, "learn word: \"" << word.word() << "\"") ; LYXERR(Debug::GUI, "learn word: \"" << word.word() << "\"") ;
} }
@ -97,7 +101,7 @@ void AppleSpellChecker::insert(WordLangTuple const & word)
void AppleSpellChecker::remove(WordLangTuple const & word) void AppleSpellChecker::remove(WordLangTuple const & word)
{ {
string const word_str = to_utf8(word.word()); string const word_str = to_utf8(word.word());
unlearnAppleSpeller(d->speller, word_str.c_str()); AppleSpeller_unlearn(d->speller, word_str.c_str());
LYXERR(Debug::GUI, "unlearn word: \"" << word.word() << "\"") ; LYXERR(Debug::GUI, "unlearn word: \"" << word.word() << "\"") ;
} }
@ -106,7 +110,7 @@ void AppleSpellChecker::remove(WordLangTuple const & word)
void AppleSpellChecker::accept(WordLangTuple const & word) void AppleSpellChecker::accept(WordLangTuple const & word)
{ {
string const word_str = to_utf8(word.word()); string const word_str = to_utf8(word.word());
ignoreAppleSpeller(d->speller, word_str.c_str()); AppleSpeller_ignore(d->speller, word_str.c_str());
} }
@ -115,9 +119,9 @@ void AppleSpellChecker::suggest(WordLangTuple const & wl,
{ {
suggestions.clear(); suggestions.clear();
string const word_str = to_utf8(wl.word()); string const word_str = to_utf8(wl.word());
size_t num = makeSuggestionAppleSpeller(d->speller, word_str.c_str(), wl.lang()->code().c_str()); size_t num = AppleSpeller_makeSuggestion(d->speller, word_str.c_str(), wl.lang()->code().c_str());
for (size_t i = 0; i < num; i++) { for (size_t i = 0; i < num; i++) {
char const * next = getSuggestionAppleSpeller(d->speller, i); char const * next = AppleSpeller_getSuggestion(d->speller, i);
if (!next) break; if (!next) break;
suggestions.push_back(from_utf8(next)); suggestions.push_back(from_utf8(next));
} }
@ -126,7 +130,19 @@ void AppleSpellChecker::suggest(WordLangTuple const & wl,
bool AppleSpellChecker::hasDictionary(Language const * lang) const bool AppleSpellChecker::hasDictionary(Language const * lang) const
{ {
return hasLanguageAppleSpeller(d->speller,lang->code().c_str()); return AppleSpeller_hasLanguage(d->speller,lang->code().c_str());
}
int AppleSpellChecker::numMisspelledWords() const
{
return AppleSpeller_numMisspelledWords(d->speller);
}
void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
{
AppleSpeller_misspelledWord(d->speller, index, &start, &length);
} }

View File

@ -30,6 +30,9 @@ public:
void remove(WordLangTuple const &); void remove(WordLangTuple const &);
void accept(WordLangTuple const &); void accept(WordLangTuple const &);
bool hasDictionary(Language const * lang) const; bool hasDictionary(Language const * lang) const;
bool canCheckParagraph() const { return true; }
int numMisspelledWords() const;
void misspelledWord(int index, int & start, int & length) const;
docstring const error(); docstring const error();
//@} //@}

View File

@ -55,7 +55,7 @@ public:
/// check the given word of the given lang code and return the result /// check the given word of the given lang code and return the result
virtual enum Result check(WordLangTuple const &) = 0; virtual enum Result check(WordLangTuple const &) = 0;
/// Gives suggestions. /// Gives suggestions.
virtual void suggest(WordLangTuple const &, docstring_list & suggestions) = 0; virtual void suggest(WordLangTuple const &, docstring_list & suggestions) = 0;
@ -71,6 +71,22 @@ public:
/// check if dictionary exists /// check if dictionary exists
virtual bool hasDictionary(Language const *) const = 0; virtual bool hasDictionary(Language const *) const = 0;
/// if speller can spell check whole paragraph return true
virtual bool canCheckParagraph() const { return false; }
/// count of misspelled words
virtual int numMisspelledWords() const { return 0; }
/// start position and length of misspelled word at index
virtual void misspelledWord(
int /* index */,
int & start, int & length) const
{
/// index is used here to make the compiler happy
start = 0;
length = 0;
}
/// give an error message on messy exit /// give an error message on messy exit
virtual docstring const error() = 0; virtual docstring const error() = 0;
}; };

View File

@ -28,15 +28,15 @@ typedef struct AppleSpellerRec * AppleSpeller ;
AppleSpeller newAppleSpeller(void); AppleSpeller newAppleSpeller(void);
void freeAppleSpeller(AppleSpeller speller); void freeAppleSpeller(AppleSpeller speller);
SpellCheckResult checkAppleSpeller(AppleSpeller speller, const char * word, const char * lang); SpellCheckResult AppleSpeller_check(AppleSpeller speller, const char * word, const char * lang);
void ignoreAppleSpeller(AppleSpeller speller, const char * word); void AppleSpeller_ignore(AppleSpeller speller, const char * word);
size_t makeSuggestionAppleSpeller(AppleSpeller speller, const char * word, const char * lang); size_t AppleSpeller_makeSuggestion(AppleSpeller speller, const char * word, const char * lang);
const char * getSuggestionAppleSpeller(AppleSpeller speller, size_t pos); const char * AppleSpeller_getSuggestion(AppleSpeller speller, size_t pos);
void learnAppleSpeller(AppleSpeller speller, const char * word); void AppleSpeller_learn(AppleSpeller speller, const char * word);
void unlearnAppleSpeller(AppleSpeller speller, const char * word); void AppleSpeller_unlearn(AppleSpeller speller, const char * word);
int hasLanguageAppleSpeller(AppleSpeller speller, const char * lang); int AppleSpeller_hasLanguage(AppleSpeller speller, const char * lang);
int numMisspelledWordsAppleSpeller(AppleSpeller speller); int AppleSpeller_numMisspelledWords(AppleSpeller speller);
void misspelledWordAppleSpeller(AppleSpeller speller, int const position, int * start, int * length); void AppleSpeller_misspelledWord(AppleSpeller speller, int index, int * start, int * length);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"

View File

@ -84,7 +84,7 @@ static NSString * toLanguage(AppleSpeller speller, const char * lang)
} }
SpellCheckResult checkAppleSpeller(AppleSpeller speller, const char * word, const char * lang) SpellCheckResult AppleSpeller_check(AppleSpeller speller, const char * word, const char * lang)
{ {
if (!speller->checker || !lang || !word) if (!speller->checker || !lang || !word)
return SPELL_CHECK_FAILED; return SPELL_CHECK_FAILED;
@ -132,7 +132,7 @@ SpellCheckResult checkAppleSpeller(AppleSpeller speller, const char * word, cons
} }
void ignoreAppleSpeller(AppleSpeller speller, const char * word) void AppleSpeller_ignore(AppleSpeller speller, const char * word)
{ {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * word_ = toString(word); NSString * word_ = toString(word);
@ -144,7 +144,7 @@ void ignoreAppleSpeller(AppleSpeller speller, const char * word)
} }
size_t makeSuggestionAppleSpeller(AppleSpeller speller, const char * word, const char * lang) size_t AppleSpeller_makeSuggestion(AppleSpeller speller, const char * word, const char * lang)
{ {
if (!speller->checker || !word || !lang) if (!speller->checker || !word || !lang)
return 0; return 0;
@ -184,7 +184,7 @@ size_t makeSuggestionAppleSpeller(AppleSpeller speller, const char * word, const
} }
const char * getSuggestionAppleSpeller(AppleSpeller speller, size_t pos) const char * AppleSpeller_getSuggestion(AppleSpeller speller, size_t pos)
{ {
const char * result = 0; const char * result = 0;
if (pos < [speller->suggestions count]) { if (pos < [speller->suggestions count]) {
@ -194,7 +194,7 @@ const char * getSuggestionAppleSpeller(AppleSpeller speller, size_t pos)
} }
void learnAppleSpeller(AppleSpeller speller, const char * word) void AppleSpeller_learn(AppleSpeller speller, const char * word)
{ {
#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050) #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
@ -209,7 +209,7 @@ void learnAppleSpeller(AppleSpeller speller, const char * word)
} }
void unlearnAppleSpeller(AppleSpeller speller, const char * word) void AppleSpeller_unlearn(AppleSpeller speller, const char * word)
{ {
#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050) #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
@ -224,21 +224,21 @@ void unlearnAppleSpeller(AppleSpeller speller, const char * word)
} }
int numMisspelledWordsAppleSpeller(AppleSpeller speller) int AppleSpeller_numMisspelledWords(AppleSpeller speller)
{ {
return [speller->misspelled count]; return [speller->misspelled count];
} }
void misspelledWordAppleSpeller(AppleSpeller speller, int const position, int * start, int * length) void AppleSpeller_misspelledWord(AppleSpeller speller, int index, int * start, int * length)
{ {
NSRange range = [[speller->misspelled objectAtIndex:position] rangeValue]; NSRange range = [[speller->misspelled objectAtIndex:index] rangeValue];
*start = range.location; *start = range.location;
*length = range.length; *length = range.length;
} }
int hasLanguageAppleSpeller(AppleSpeller speller, const char * lang) int AppleSpeller_hasLanguage(AppleSpeller speller, const char * lang)
{ {
return toLanguage(speller, lang) != nil; return toLanguage(speller, lang) != nil;
} }