Hi All, As a first hesitant step towards cleaning up the spellcheck code (and move it to frontends) this patch moves some functions out of BufferView into the spellcheck code. It's perhaps not very pretty but I don't see why we have these very specialized functions that are called only once and don't need any private members. Do you agree? Some questions: find/replace and spellcheck share some needs. find/replace wants to: -find string -find word=string -find casesensitive string -find casesensitiveword=string both forward and backward spellcheck wants to: -find word=next word only forward they both want to: -replace range start.pos to end.pos with string Have you got any ideas on how to implement this? Is is possible to define an iterator that allows me to "walk" through the text, and use function objects as predicates for the find code? Your thoughts are appreciated, please be detailed :-) gr.ed. ps. I also noticed the new member Last() in LyXParagraph. As far as I can see Last() is now called where before size() was called. So my question: why add a new member Last() instead of #ifndef'fing size()?
Index: src/BufferView.h =================================================================== RCS file: /tmp/cvs/initial/src/BufferView.h,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 BufferView.h --- src/BufferView.h 2001/03/20 09:57:54 1.1.1.1 +++ src/BufferView.h 2001/03/21 08:43:58 @@ -132,14 +132,6 @@ public: /// void insetWakeup(); /// - void replaceWord(string const & replacestring); - /// - void endOfSpellCheck(); - /// - void selectLastWord(); - /// - string const nextWord(float & value); - /// void insertCorrectQuote(); /// bool gotoLabel(string const & label); Index: src/BufferView2.C =================================================================== RCS file: /tmp/cvs/initial/src/BufferView2.C,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 BufferView2.C --- src/BufferView2.C 2001/03/20 09:57:54 1.1.1.1 +++ src/BufferView2.C 2001/03/21 08:43:58 @@ -695,68 +695,6 @@ void BufferView::insertCorrectQuote() } -/* these functions are for the spellchecker */ -string const BufferView::nextWord(float & value) -{ - if (!available()) { - value = 1; - return string(); - } - - return text->SelectNextWord(this, value); -} - - -void BufferView::selectLastWord() -{ - if (!available()) return; - - hideCursor(); - beforeChange(text); - text->SelectSelectedWord(this); - toggleSelection(false); - update(text, BufferView::SELECT|BufferView::FITCUR); -} - - -void BufferView::endOfSpellCheck() -{ - if (!available()) return; - - hideCursor(); - beforeChange(text); - text->SelectSelectedWord(this); - text->ClearSelection(this); - update(text, BufferView::SELECT|BufferView::FITCUR); -} - - -void BufferView::replaceWord(string const & replacestring) -{ - if (!available()) return; - - hideCursor(); - update(text, BufferView::SELECT|BufferView::FITCUR); - - /* clear the selection (if there is any) */ - toggleSelection(false); - update(text, BufferView::SELECT|BufferView::FITCUR); - - /* clear the selection (if there is any) */ - toggleSelection(false); - text->ReplaceSelectionWithString(this, replacestring); - - text->SetSelectionOverString(this, replacestring); - - // Go back so that replacement string is also spellchecked - for (string::size_type i = 0; i < replacestring.length() + 1; ++i) { - text->CursorLeft(this); - } - update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE); -} -// End of spellchecker stuff - - bool BufferView::lockInset(UpdatableInset * inset) { if (!theLockingInset() && inset) { Index: src/spellchecker.C =================================================================== RCS file: /tmp/cvs/initial/src/spellchecker.C,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 spellchecker.C --- src/spellchecker.C 2001/03/20 09:57:54 1.1.1.1 +++ src/spellchecker.C 2001/03/21 09:17:41 @@ -64,6 +64,7 @@ #include "language.h" #include "encoding.h" #include "support/lstrings.h" +#include "lyxtext.h" #ifdef USE_PSPELL # include <pspell/pspell.h> @@ -795,7 +796,15 @@ void ShowSpellChecker(BufferView * bv) if (obj == fd_form_spell_check->done) break; } fl_hide_form(fd_form_spell_check->form_spell_check); - bv->endOfSpellCheck(); + + if (bv->available()) { + bv->hideCursor(); + bv->beforeChange(bv->text); + bv->text->SelectSelectedWord(bv); + bv->text->ClearSelection(bv); + bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR); + } + return; } @@ -844,8 +853,13 @@ bool RunSpellChecker(BufferView * bv) unsigned int word_count = 0; + /* initialize empty string */ + string word = ""; + while (true) { - string const word = bv->nextWord(newval); + + word = bv->text->SelectNextWord(bv, newval); + if (word.empty()) break; ++word_count; @@ -878,7 +892,12 @@ bool RunSpellChecker(BufferView * bv) case ISP_UNKNOWN: case ISP_MISSED: { - bv->selectLastWord(); + bv->hideCursor(); + bv->beforeChange(bv->text); + bv->text->SelectSelectedWord(bv); + bv->toggleSelection(false); + bv->update(bv->text, BufferView::SELECT|BufferView::FITCUR); + if (rtl) { string tmp = word; reverse(tmp.begin(),tmp.end()); @@ -912,20 +931,27 @@ bool RunSpellChecker(BufferView * bv) break; } if (obj == fd_form_spell_check->replace || - obj == fd_form_spell_check->input) { + obj == fd_form_spell_check->input || + ((obj == fd_form_spell_check->browser) && + (clickline == +fl_get_browser(fd_form_spell_check->browser)))) { sc_store_replacement(word, fl_get_input(fd_form_spell_check->input)); - bv->replaceWord(fl_get_input(fd_form_spell_check->input)); + + string const replacestring = +fl_get_input(fd_form_spell_check->input); + bv->hideCursor(); + bv->toggleSelection(false); + bv->text->ReplaceSelectionWithString(bv, +replacestring); + bv->text->SetSelectionOverString(bv, +replacestring); + // Go back so that replacement string is also +spellchecked + for (string::size_type i = 0; i < +replacestring.length() + 1; ++i) { + bv->text->CursorLeft(bv); + } + bv->update(bv->text, +BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE); + break; } if (obj == fd_form_spell_check->browser) { // implements double click in the browser window. // sent to lyx@via by Mark Burton <[EMAIL PROTECTED]> - if (clickline == - fl_get_browser(fd_form_spell_check->browser)) { - sc_store_replacement(word, fl_get_input(fd_form_spell_check->input)); - bv->replaceWord(fl_get_input(fd_form_spell_check->input)); - break; - } clickline = fl_get_browser(fd_form_spell_check->browser); /// Why not use /// fl_set_input(fd_form_spell_check->input, result->misses[clickline-1]); ? Index: src/text.C =================================================================== RCS file: /tmp/cvs/initial/src/text.C,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 text.C --- src/text.C 2001/03/20 09:57:54 1.1.1.1 +++ src/text.C 2001/03/21 09:52:10 @@ -2576,7 +2576,7 @@ bool LyXText::SelectWordWhenUnderCursor( } -// This function is only used by the spellchecker for NextWord(). +// This function is only used by the spellchecker. // It doesn't handle LYX_ACCENTs and probably never will. string const LyXText::SelectNextWord(BufferView * bview, float & value) const @@ -2586,40 +2586,27 @@ string const LyXText::SelectNextWord(Buf // If this is not the very first word, skip rest of // current word because we are probably in the middle // of a word if there is text here. + #ifndef NEW_INSETS + LyXParagraph::size_type end = cursor.par()->Last(); if (cursor.pos() || cursor.par()->previous_) { - while (cursor.pos() < cursor.par()->Last() - && cursor.par()->IsLetter(cursor.pos())) - cursor.pos(cursor.pos() + 1); - } #else + LyXParagraph::size_type end = cursor.par()->size(); if (cursor.pos() || cursor.par()->previous()) { - while (cursor.pos() < cursor.par()->size() +#endif + while (cursor.pos() < end && cursor.par()->IsLetter(cursor.pos())) cursor.pos(cursor.pos() + 1); } -#endif // Now, skip until we have real text (will jump paragraphs) -#ifndef NEW_INSETS - while ((cursor.par()->Last() > cursor.pos() -#else - while ((cursor.par()->size() > cursor.pos() -#endif + while ((end > cursor.pos() && (!cursor.par()->IsLetter(cursor.pos()) || cursor.par()->getFont(bview->buffer()->params, cursor.pos()) .latex() == LyXFont::ON)) -#ifndef NEW_INSETS - || (cursor.par()->Last() == cursor.pos() -#else - || (cursor.par()->size() == cursor.pos() -#endif + || (end == cursor.pos() && cursor.par()->next())){ -#ifndef NEW_INSETS - if (cursor.pos() == cursor.par()->Last()) { -#else - if (cursor.pos() == cursor.par()->size()) { -#endif + if (cursor.pos() == end) { cursor.par(cursor.par()->next()); cursor.pos(0); } else @@ -2639,11 +2626,7 @@ string const LyXText::SelectNextWord(Buf // and find the end of the word // (optional hyphens are part of a word) -#ifndef NEW_INSETS - while (cursor.pos() < cursor.par()->Last() -#else - while (cursor.pos() < cursor.par()->size() -#endif + while (cursor.pos() < end && (cursor.par()->IsLetter(cursor.pos())) || (cursor.par()->GetChar(cursor.pos()) == LyXParagraph::META_INSET && cursor.par()->GetInset(cursor.pos()) != 0