From 4bde2bc7bc7f19ac3ff0e223a0e87ba842a62e10 Mon Sep 17 00:00:00 2001 From: Stephan Witt Date: Sat, 4 Sep 2010 07:48:15 +0000 Subject: [PATCH] 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 --- src/AppleSpellChecker.cpp | 32 ++++++++++++++++++++++++-------- src/AppleSpellChecker.h | 3 +++ src/SpellChecker.h | 18 +++++++++++++++++- src/support/AppleSpeller.h | 18 +++++++++--------- src/support/AppleSpeller.m | 20 ++++++++++---------- 5 files changed, 63 insertions(+), 28 deletions(-) diff --git a/src/AppleSpellChecker.cpp b/src/AppleSpellChecker.cpp index 003b8051ec..71f7e1e25b 100644 --- a/src/AppleSpellChecker.cpp +++ b/src/AppleSpellChecker.cpp @@ -78,8 +78,12 @@ string AppleSpellChecker::Private::toString(SpellCheckResult status) SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word) { string const word_str = to_utf8(word.word()); - SpellCheckResult result = checkAppleSpeller(d->speller, word_str.c_str(), word.lang()->code().c_str()); - LYXERR(Debug::GUI, "spellCheck: \"" << word.word() << "\" = " << d->toString(result)) ; + SpellCheckResult 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); } @@ -88,7 +92,7 @@ SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word) void AppleSpellChecker::insert(WordLangTuple const & 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() << "\"") ; } @@ -97,7 +101,7 @@ void AppleSpellChecker::insert(WordLangTuple const & word) void AppleSpellChecker::remove(WordLangTuple const & 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() << "\"") ; } @@ -106,7 +110,7 @@ void AppleSpellChecker::remove(WordLangTuple const & word) void AppleSpellChecker::accept(WordLangTuple const & 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(); 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++) { - char const * next = getSuggestionAppleSpeller(d->speller, i); + char const * next = AppleSpeller_getSuggestion(d->speller, i); if (!next) break; suggestions.push_back(from_utf8(next)); } @@ -126,7 +130,19 @@ void AppleSpellChecker::suggest(WordLangTuple const & wl, 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); } diff --git a/src/AppleSpellChecker.h b/src/AppleSpellChecker.h index e406325a70..9d1aeeee3c 100644 --- a/src/AppleSpellChecker.h +++ b/src/AppleSpellChecker.h @@ -30,6 +30,9 @@ public: void remove(WordLangTuple const &); void accept(WordLangTuple 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(); //@} diff --git a/src/SpellChecker.h b/src/SpellChecker.h index 58a1fb2032..04b657cbd2 100644 --- a/src/SpellChecker.h +++ b/src/SpellChecker.h @@ -55,7 +55,7 @@ public: /// check the given word of the given lang code and return the result virtual enum Result check(WordLangTuple const &) = 0; - + /// Gives suggestions. virtual void suggest(WordLangTuple const &, docstring_list & suggestions) = 0; @@ -71,6 +71,22 @@ public: /// check if dictionary exists 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 virtual docstring const error() = 0; }; diff --git a/src/support/AppleSpeller.h b/src/support/AppleSpeller.h index 647471a52b..5910fba142 100644 --- a/src/support/AppleSpeller.h +++ b/src/support/AppleSpeller.h @@ -28,15 +28,15 @@ typedef struct AppleSpellerRec * AppleSpeller ; AppleSpeller newAppleSpeller(void); void freeAppleSpeller(AppleSpeller speller); -SpellCheckResult checkAppleSpeller(AppleSpeller speller, const char * word, const char * lang); -void ignoreAppleSpeller(AppleSpeller speller, const char * word); -size_t makeSuggestionAppleSpeller(AppleSpeller speller, const char * word, const char * lang); -const char * getSuggestionAppleSpeller(AppleSpeller speller, size_t pos); -void learnAppleSpeller(AppleSpeller speller, const char * word); -void unlearnAppleSpeller(AppleSpeller speller, const char * word); -int hasLanguageAppleSpeller(AppleSpeller speller, const char * lang); -int numMisspelledWordsAppleSpeller(AppleSpeller speller); -void misspelledWordAppleSpeller(AppleSpeller speller, int const position, int * start, int * length); +SpellCheckResult AppleSpeller_check(AppleSpeller speller, const char * word, const char * lang); +void AppleSpeller_ignore(AppleSpeller speller, const char * word); +size_t AppleSpeller_makeSuggestion(AppleSpeller speller, const char * word, const char * lang); +const char * AppleSpeller_getSuggestion(AppleSpeller speller, size_t pos); +void AppleSpeller_learn(AppleSpeller speller, const char * word); +void AppleSpeller_unlearn(AppleSpeller speller, const char * word); +int AppleSpeller_hasLanguage(AppleSpeller speller, const char * lang); +int AppleSpeller_numMisspelledWords(AppleSpeller speller); +void AppleSpeller_misspelledWord(AppleSpeller speller, int index, int * start, int * length); #ifdef __cplusplus } // extern "C" diff --git a/src/support/AppleSpeller.m b/src/support/AppleSpeller.m index 810ee97602..feba78c1a1 100644 --- a/src/support/AppleSpeller.m +++ b/src/support/AppleSpeller.m @@ -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) 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]; 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) 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; 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) 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) 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]; } -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; *length = range.length; } -int hasLanguageAppleSpeller(AppleSpeller speller, const char * lang) +int AppleSpeller_hasLanguage(AppleSpeller speller, const char * lang) { return toLanguage(speller, lang) != nil; }