From 6b5c9696b6669b48062bec96707c78d605e1af58 Mon Sep 17 00:00:00 2001 From: John Levon Date: Tue, 6 Aug 2002 22:38:44 +0000 Subject: [PATCH] Remove LYX_LAYOUT_DEFAULT - it is very very old, even klyx doesn't use it. Commit the multi-language spellcheck patch. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4882 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.h | 5 +- src/BufferView2.C | 4 +- src/ChangeLog | 18 +++++ src/SpellBase.h | 10 ++- src/WordLangTuple.h | 43 ++++++++++ src/buffer.C | 62 +++++++-------- src/frontends/controllers/ChangeLog | 5 ++ .../controllers/ControlSpellchecker.C | 17 ++-- .../controllers/ControlSpellchecker.h | 5 +- src/insets/ChangeLog | 13 ++++ src/insets/inset.C | 4 +- src/insets/inset.h | 3 +- src/insets/insetcollapsable.C | 8 +- src/insets/insetcollapsable.h | 3 +- src/insets/insetert.C | 4 +- src/insets/insetert.h | 3 +- src/insets/insettabular.C | 26 +++---- src/insets/insettabular.h | 4 +- src/insets/insettext.C | 18 ++--- src/insets/insettext.h | 2 +- src/ispell.C | 12 +-- src/ispell.h | 6 +- src/layout.h | 6 -- src/lyxtext.h | 3 +- src/pspell.C | 78 +++++++++++++------ src/pspell.h | 33 +++++--- src/text.C | 17 ++-- src/text2.C | 6 +- 28 files changed, 275 insertions(+), 143 deletions(-) create mode 100644 src/WordLangTuple.h diff --git a/src/BufferView.h b/src/BufferView.h index 2603dadf1b..fb7b298341 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -19,6 +19,7 @@ #include "commandtags.h" #include "insets/inset.h" +#include "WordLangTuple.h" #include @@ -124,8 +125,8 @@ public: void endOfSpellCheck(); /// void selectLastWord(); - /// - string const nextWord(float & value); + /// return the next word + WordLangTuple const nextWord(float & value); /// bool gotoLabel(string const & label); /// diff --git a/src/BufferView2.C b/src/BufferView2.C index 389201ce5f..f09e7bfeb8 100644 --- a/src/BufferView2.C +++ b/src/BufferView2.C @@ -443,11 +443,11 @@ void BufferView::paste() /* these functions are for the spellchecker */ -string const BufferView::nextWord(float & value) +WordLangTuple const BufferView::nextWord(float & value) { if (!available()) { value = 1; - return string(); + return WordLangTuple(); } return text->selectNextWordToSpellcheck(this, value); diff --git a/src/ChangeLog b/src/ChangeLog index a91b09a966..49c4851369 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,21 @@ +2002-08-06 John Levon + + * WordLangTuple.h: new file for word + language code tuple + + * SpellBase.h: + * pspell.h: + * pspell.C: + * ispell.h: + * ispell.C: + * lyxtext.h: + * text.C: + * text2.C: + * BufferView.h: + * BufferView2.C: use WordLangTuple + + * layout.h: + * buffer.C: remove very dead LYX_LAYOUT_DEFAULT + 2002-08-06 John Levon * lyx_main.C: fix cmdline batch handling diff --git a/src/SpellBase.h b/src/SpellBase.h index 0120a0bb07..1348c1e24c 100644 --- a/src/SpellBase.h +++ b/src/SpellBase.h @@ -12,6 +12,8 @@ #include "LString.h" // can't forward declare... +#include "WordLangTuple.h" + class BufferParams; /** @@ -38,17 +40,17 @@ public: /// clean up on messy exit virtual void cleanUp() = 0; - /// check the given word and return the result - virtual enum Result check(string const &) = 0; + /// check the given word of the given lang code and return the result + virtual enum Result check(WordLangTuple const &) = 0; /// finish this spellchecker instance virtual void close() = 0; /// insert the given word into the personal dictionary - virtual void insert(string const &) = 0; + virtual void insert(WordLangTuple const &) = 0; /// accept the given word temporarily - virtual void accept(string const &) = 0; + virtual void accept(WordLangTuple const &) = 0; /// return the next near miss after a MISSED result virtual string const nextMiss() = 0; diff --git a/src/WordLangTuple.h b/src/WordLangTuple.h new file mode 100644 index 0000000000..d58ca4a7eb --- /dev/null +++ b/src/WordLangTuple.h @@ -0,0 +1,43 @@ +/** + * \file WordLangTuple.h + * Copyright 2002 the LyX Team + * Read the file COPYING + * + * \author John Levon + */ + +#ifndef WORD_LANG_TUPLE_H +#define WORD_LANG_TUPLE_H + +#include +#include "LString.h" + +/** + * A word and its given language code ("en_US"). + * This is used for spellchecking. + */ +class WordLangTuple { +public: + WordLangTuple() {}; + + WordLangTuple(string const & w, string const & c) + : word_(w), code_(c) {} + + /// return the word + string const word() const { + return word_; + } + + /// return its language code + string const lang_code() const { + return code_; + } + +private: + /// the word + string word_; + /// language code of word + string code_; +}; + +#endif // WORD_LANG_TUPLE_H diff --git a/src/buffer.C b/src/buffer.C index 2b3371ab52..c5d5110e8e 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -948,9 +948,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par, int tmpret = lex.findToken(string_paragraph_separation); if (tmpret == -1) ++tmpret; - if (tmpret != LYX_LAYOUT_DEFAULT) - params.paragraph_separation = - static_cast(tmpret); + params.paragraph_separation = + static_cast(tmpret); } else if (token == "\\defskip") { lex.nextToken(); params.defskip = VSpace(lex.getString()); @@ -965,31 +964,29 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par, int tmpret = lex.findToken(string_quotes_language); if (tmpret == -1) ++tmpret; - if (tmpret != LYX_LAYOUT_DEFAULT) { - InsetQuotes::quote_language tmpl = - InsetQuotes::EnglishQ; - switch (tmpret) { - case 0: - tmpl = InsetQuotes::EnglishQ; - break; - case 1: - tmpl = InsetQuotes::SwedishQ; - break; - case 2: - tmpl = InsetQuotes::GermanQ; - break; - case 3: - tmpl = InsetQuotes::PolishQ; - break; - case 4: - tmpl = InsetQuotes::FrenchQ; - break; - case 5: - tmpl = InsetQuotes::DanishQ; - break; - } - params.quotes_language = tmpl; + InsetQuotes::quote_language tmpl = + InsetQuotes::EnglishQ; + switch (tmpret) { + case 0: + tmpl = InsetQuotes::EnglishQ; + break; + case 1: + tmpl = InsetQuotes::SwedishQ; + break; + case 2: + tmpl = InsetQuotes::GermanQ; + break; + case 3: + tmpl = InsetQuotes::PolishQ; + break; + case 4: + tmpl = InsetQuotes::FrenchQ; + break; + case 5: + tmpl = InsetQuotes::DanishQ; + break; } + params.quotes_language = tmpl; } else if (token == "\\quotes_times") { lex.nextToken(); switch (lex.getInteger()) { @@ -1029,8 +1026,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par, int tmpret = lex.findToken(string_orientation); if (tmpret == -1) ++tmpret; - if (tmpret != LYX_LAYOUT_DEFAULT) - params.orientation = static_cast(tmpret); + params.orientation = + static_cast(tmpret); } else if (token == "\\paperwidth") { lex.next(); params.paperwidth = lex.getString(); @@ -1173,11 +1170,8 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, Paragraph *& par, } else if (token == "\\align") { int tmpret = lex.findToken(string_align); if (tmpret == -1) ++tmpret; - if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ??? - int const tmpret2 = int(pow(2.0, tmpret)); - //lyxerr << "Tmpret2 = " << tmpret2 << endl; - par->params().align(LyXAlignment(tmpret2)); - } + int const tmpret2 = int(pow(2.0, tmpret)); + par->params().align(LyXAlignment(tmpret2)); } else if (token == "\\added_space_top") { lex.nextToken(); VSpace value = VSpace(lex.getString()); diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 0b35cd27ff..58bf5ef657 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,8 @@ +2002-08-06 John Levon + + * ControlSpellchecker.C: + * ControlSpellchecker.h: use WordLangTuple + 2002-08-06 John Levon * ControlSpellchecker.C: show an alert with the diff --git a/src/frontends/controllers/ControlSpellchecker.C b/src/frontends/controllers/ControlSpellchecker.C index 10420d6c65..0a341353b3 100644 --- a/src/frontends/controllers/ControlSpellchecker.C +++ b/src/frontends/controllers/ControlSpellchecker.C @@ -91,7 +91,7 @@ void ControlSpellchecker::check() while ((res == SpellBase::OK || res == SpellBase::IGNORE) && !stop_) { word_ = lv_.view()->nextWord(newval_); - if (word_.empty()) { + if (word_.word().empty()) { clearParams(); break; } @@ -99,19 +99,23 @@ void ControlSpellchecker::check() ++count_; // Update slider if and only if value has changed - newvalue_ = int(100.0*newval_); + newvalue_ = int(100.0 * newval_); if (newvalue_!= oldval_) { oldval_ = newvalue_; // set progress bar view().partialUpdate(0); } - if (!speller_->alive()) clearParams(); + if (!speller_->alive()) { + clearParams(); + stop(); + return; + } res = speller_->check(word_); } - if (!stop_ && !word_.empty()) + if (!stop_ && !word_.word().empty()) lv_.view()->selectLastWord(); // set suggestions @@ -124,6 +128,8 @@ void ControlSpellchecker::check() void ControlSpellchecker::replace(string const & replacement) { lv_.view()->replaceWord(replacement); + // fix up the count + --count_; check(); } @@ -155,7 +161,7 @@ string ControlSpellchecker::getSuggestion() string ControlSpellchecker::getWord() { - string tmp = word_; + string tmp = word_.word(); if (rtl_) std::reverse(tmp.begin(), tmp.end()); return tmp; @@ -214,7 +220,6 @@ void ControlSpellchecker::clearParams() // reset values to initial rtl_ = false; - word_.erase(); newval_ = 0.0; oldval_ = 0; newvalue_ = 0; diff --git a/src/frontends/controllers/ControlSpellchecker.h b/src/frontends/controllers/ControlSpellchecker.h index 75b58b51ee..65a515f12a 100644 --- a/src/frontends/controllers/ControlSpellchecker.h +++ b/src/frontends/controllers/ControlSpellchecker.h @@ -21,6 +21,7 @@ #include "ControlDialog_impl.h" #include "LString.h" +#include "WordLangTuple.h" class SpellBase; @@ -78,8 +79,8 @@ private: /// right to left bool rtl_; - /// current word being checked - string word_; + /// current word being checked and lang code + WordLangTuple word_; /// values for progress float newval_; diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 016b120f4e..316749adfa 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,16 @@ +2002-08-06 John Levon + + * inset.h: + * inset.C: + * insetcollapsable.h: + * insetcollapsable.C: + * insetert.h: + * insetert.C: + * insettabular.h: + * insettabular.C: + * insettext.h: + * insettext.C: use WordLangTuple for spellcheck + 2002-08-06 Angus Leeming * insetinclude.C: add a monitor to the previewed image, so that the diff --git a/src/insets/inset.C b/src/insets/inset.C index 4fa75c48d9..4429c12cb8 100644 --- a/src/insets/inset.C +++ b/src/insets/inset.C @@ -359,13 +359,13 @@ LyXCursor const & Inset::cursor(BufferView * bv) const } -string const UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, +WordLangTuple UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const { // we have to unlock ourself in this function by default! bv->unlockInset(const_cast(this)); value = 0; - return string(); + return WordLangTuple(); } diff --git a/src/insets/inset.h b/src/insets/inset.h index fa81f39f00..48ff4741fc 100644 --- a/src/insets/inset.h +++ b/src/insets/inset.h @@ -19,6 +19,7 @@ #include "LString.h" #include "commandtags.h" #include "frontends/mouse_state.h" +#include "WordLangTuple.h" #include "LColor.h" class LyXFont; @@ -536,7 +537,7 @@ public: /// virtual bool allowSpellcheck() { return false; } /// - virtual string const selectNextWordToSpellcheck(BufferView *, float & value) const; + virtual WordLangTuple selectNextWordToSpellcheck(BufferView *, float & value) const; /// virtual void selectSelectedWord(BufferView *) { return; } /// diff --git a/src/insets/insetcollapsable.C b/src/insets/insetcollapsable.C index de8e98f740..ffba580c7f 100644 --- a/src/insets/insetcollapsable.C +++ b/src/insets/insetcollapsable.C @@ -667,14 +667,14 @@ bool InsetCollapsable::searchBackward(BufferView * bv, string const & str, } -string const InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, +WordLangTuple InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const { - string const str = inset.selectNextWordToSpellcheck(bv, value); - if (first_after_edit && str.empty()) + WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value); + if (first_after_edit && word.word().empty()) close(bv); first_after_edit = false; - return str; + return word; } diff --git a/src/insets/insetcollapsable.h b/src/insets/insetcollapsable.h index b9fca1d3d3..8ae7d2a593 100644 --- a/src/insets/insetcollapsable.h +++ b/src/insets/insetcollapsable.h @@ -176,7 +176,8 @@ public: void close(BufferView *) const; /// bool allowSpellcheck() { return inset.allowSpellcheck(); } - string const selectNextWordToSpellcheck(BufferView *, float &) const; + + WordLangTuple selectNextWordToSpellcheck(BufferView *, float &) const; void selectSelectedWord(BufferView * bv) { inset.selectSelectedWord(bv); diff --git a/src/insets/insetert.C b/src/insets/insetert.C index e7a3a8f980..a05d684bef 100644 --- a/src/insets/insetert.C +++ b/src/insets/insetert.C @@ -674,11 +674,11 @@ void InsetERT::close(BufferView * bv) const } -string const InsetERT::selectNextWordToSpellcheck(BufferView * bv, +WordLangTuple InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const { bv->unlockInset(const_cast(this)); - return string(); + return WordLangTuple(); } diff --git a/src/insets/insetert.h b/src/insets/insetert.h index 4675049f8e..3b580ed938 100644 --- a/src/insets/insetert.h +++ b/src/insets/insetert.h @@ -114,7 +114,8 @@ public: void close(BufferView *) const; /// bool allowSpellcheck() { return false; } - string const selectNextWordToSpellcheck(BufferView *, float &) const; + + WordLangTuple selectNextWordToSpellcheck(BufferView *, float &) const; /// int ascent(BufferView *, LyXFont const &) const; /// diff --git a/src/insets/insettabular.C b/src/insets/insettabular.C index f88e9dbb83..2749679a35 100644 --- a/src/insets/insettabular.C +++ b/src/insets/insettabular.C @@ -2675,20 +2675,20 @@ Inset * InsetTabular::getInsetFromID(int id_arg) const } -string const +WordLangTuple InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const { nodraw(true); if (the_locking_inset) { - string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value)); - if (!str.empty()) { + WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value)); + if (!word.word().empty()) { nodraw(false); - return str; + return word; } if (tabular->IsLastCell(actcell)) { bv->unlockInset(const_cast(this)); nodraw(false); - return string(); + return WordLangTuple(); } ++actcell; } @@ -2696,26 +2696,26 @@ InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const UpdatableInset * inset = static_cast(tabular->GetCellInset(actcell)); inset->edit(bv, 0, 0, mouse_button::none); - string const str(selectNextWordInt(bv, value)); + WordLangTuple word(selectNextWordInt(bv, value)); nodraw(false); - if (!str.empty()) + if (!word.word().empty()) resetPos(bv); - return str; + return word; } -string InsetTabular::selectNextWordInt(BufferView * bv, float & value) const +WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const { // when entering this function the inset should be ALWAYS locked! lyx::Assert(the_locking_inset); - string const str(the_locking_inset->selectNextWordToSpellcheck(bv, value)); - if (!str.empty()) - return str; + WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value)); + if (!word.word().empty()) + return word; if (tabular->IsLastCell(actcell)) { bv->unlockInset(const_cast(this)); - return string(); + return WordLangTuple(); } // otherwise we have to lock the next inset and ask for it's selecttion diff --git a/src/insets/insettabular.h b/src/insets/insettabular.h index 298790526c..329a67df7c 100644 --- a/src/insets/insettabular.h +++ b/src/insets/insettabular.h @@ -216,7 +216,7 @@ public: LyXCursor const & cursor(BufferView *) const; /// bool allowSpellcheck() { return true; } - string const selectNextWordToSpellcheck(BufferView *, float & value) const; + WordLangTuple selectNextWordToSpellcheck(BufferView *, float & value) const; void selectSelectedWord(BufferView *); void toggleSelection(BufferView *, bool kill_selection); /// @@ -316,7 +316,7 @@ private: void getSelection(int & scol, int & ecol, int & srow, int & erow) const; /// - string selectNextWordInt(BufferView *, float & value) const; + WordLangTuple selectNextWordInt(BufferView *, float & value) const; /// bool insertAsciiString(BufferView *, string const & buf, bool usePaste); diff --git a/src/insets/insettext.C b/src/insets/insettext.C index c3b93db510..b191f313ea 100644 --- a/src/insets/insettext.C +++ b/src/insets/insettext.C @@ -2581,34 +2581,34 @@ Inset * InsetText::getInsetFromID(int id_arg) const } -string const InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const +WordLangTuple InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const { bool clear = false; - string str; + WordLangTuple word; if (!lt) { lt = getLyXText(bv); clear = true; } if (the_locking_inset) { - str = the_locking_inset->selectNextWordToSpellcheck(bv, value); - if (!str.empty()) { + word = the_locking_inset->selectNextWordToSpellcheck(bv, value); + if (!word.word().empty()) { value += cy(bv); if (clear) lt = 0; - return str; + return word; } - // we have to go on checking so move cusor to the next char + // we have to go on checking so move cursor to the next char lt->cursor.pos(lt->cursor.pos() + 1); } - str = lt->selectNextWordToSpellcheck(bv, value); - if (str.empty()) + word = lt->selectNextWordToSpellcheck(bv, value); + if (word.word().empty()) bv->unlockInset(const_cast(this)); else value = cy(bv); if (clear) lt = 0; - return str; + return word; } diff --git a/src/insets/insettext.h b/src/insets/insettext.h index eaeba72355..7b1723daa4 100644 --- a/src/insets/insettext.h +++ b/src/insets/insettext.h @@ -230,7 +230,7 @@ public: /// bool allowSpellcheck() { return true; } /// - string const selectNextWordToSpellcheck(BufferView *, + WordLangTuple selectNextWordToSpellcheck(BufferView *, float & value) const; void selectSelectedWord(BufferView *); /// diff --git a/src/ispell.C b/src/ispell.C index 95dfcf2eb3..c8e8721ee0 100644 --- a/src/ispell.C +++ b/src/ispell.C @@ -303,13 +303,13 @@ void ISpell::cleanUp() } -enum ISpell::Result ISpell::check(string const & word) +enum ISpell::Result ISpell::check(WordLangTuple const & word) { // FIXME Please rewrite to use string. Result res; - ::fputs(word.c_str(), out); + ::fputs(word.word().c_str(), out); ::fputc('\n', out); char buf[1024]; @@ -374,18 +374,18 @@ void ISpell::close() } -void ISpell::insert(string const & word) +void ISpell::insert(WordLangTuple const & word) { ::fputc('*', out); // Insert word in personal dictionary - ::fputs(word.c_str(), out); + ::fputs(word.word().c_str(), out); ::fputc('\n', out); } -void ISpell::accept(string const & word) +void ISpell::accept(WordLangTuple const & word) { ::fputc('@', out); // Accept in this session - ::fputs(word.c_str(), out); + ::fputs(word.word().c_str(), out); ::fputc('\n', out); } diff --git a/src/ispell.h b/src/ispell.h index 477cef9ae0..cb1affed2c 100644 --- a/src/ispell.h +++ b/src/ispell.h @@ -30,16 +30,16 @@ public: virtual void cleanUp(); /// check the given word and return the result - virtual enum Result check(string const & word); + virtual enum Result check(WordLangTuple const & word); /// finish this spellchecker instance virtual void close(); /// insert the given word into the personal dictionary - virtual void insert(string const & word); + virtual void insert(WordLangTuple const & word); /// accept the given word temporarily - virtual void accept(string const & word); + virtual void accept(WordLangTuple const & word); /// return the next near miss after a MISSED result virtual string const nextMiss(); diff --git a/src/layout.h b/src/layout.h index 83a6228936..fd86e9910f 100644 --- a/src/layout.h +++ b/src/layout.h @@ -12,12 +12,6 @@ #ifndef LAYOUT_H #define LAYOUT_H -enum layout_default { - /// - LYX_LAYOUT_DEFAULT = 99 -}; - - /// The different output types enum OutputType { /// diff --git a/src/lyxtext.h b/src/lyxtext.h index 949681298d..076562856b 100644 --- a/src/lyxtext.h +++ b/src/lyxtext.h @@ -21,6 +21,7 @@ #include "layout.h" #include "LColor.h" #include "insets/inset.h" +#include "WordLangTuple.h" class Buffer; class BufferParams; @@ -302,7 +303,7 @@ public: to the beginning of this word. With SelectSelectedWord can this be highlighted really */ - string const selectNextWordToSpellcheck(BufferView *, float & value) const; + WordLangTuple selectNextWordToSpellcheck(BufferView *, float & value) const; /// void selectSelectedWord(BufferView *); /// returns true if par was empty and was removed diff --git a/src/pspell.C b/src/pspell.C index cba210de6c..f31605d02a 100644 --- a/src/pspell.C +++ b/src/pspell.C @@ -28,16 +28,9 @@ extern "C" { using std::endl; PSpell::PSpell(BufferParams const &, string const & lang) - : sc(0), els(0), spell_error_object(0), alive_(false) + : els(0), spell_error_object(0) { - PspellConfig * config = new_pspell_config(); - pspell_config_replace(config, "language-tag", lang.c_str()); - spell_error_object = new_pspell_manager(config); - if (pspell_error_number(spell_error_object) == 0) { - sc = to_pspell_manager(spell_error_object); - spell_error_object = 0; - alive_ = true; - } + addManager(lang); } @@ -47,6 +40,13 @@ PSpell::~PSpell() close(); if (els) delete_pspell_string_emulation(els); + Managers::iterator it = managers_.begin(); + Managers::iterator end = managers_.end(); + + for (; it != end; ++it) { + delete_pspell_manager(it->second.manager); + delete_pspell_config(it->second.config); + } } @@ -59,21 +59,49 @@ void PSpell::cleanUp() } -enum PSpell::Result PSpell::check(string const & word) +void PSpell::addManager(string const & lang) +{ + PspellConfig * config = new_pspell_config(); + pspell_config_replace(config, "language-tag", lang.c_str()); + PspellCanHaveError * err = new_pspell_manager(config); + if (spell_error_object) + delete_pspell_can_have_error(spell_error_object); + spell_error_object = 0; + + if (pspell_error_number(err) == 0) { + Manager m; + m.manager = to_pspell_manager(err); + m.config = config; + managers_[lang] = m; + } else { + spell_error_object = err; + } +} + + +enum PSpell::Result PSpell::check(WordLangTuple const & word) { Result res = UNKNOWN; - if (!sc) - return res; + Managers::iterator it = managers_.find(word.lang_code()); + if (it == managers_.end()) { + addManager(word.lang_code()); + it = managers_.find(word.lang_code()); + // FIXME + if (it == managers_.end()) + return res; + } - int word_ok = pspell_manager_check(sc, word.c_str()); + PspellManager * m = it->second.manager; + + int word_ok = pspell_manager_check(m, word.word().c_str()); lyx::Assert(word_ok != -1); if (word_ok) { res = OK; } else { PspellWordList const * sugs = - pspell_manager_suggest(sc, word.c_str()); + pspell_manager_suggest(m, word.word().c_str()); lyx::Assert(sugs != 0); els = pspell_word_list_elements(sugs); if (pspell_word_list_empty(sugs)) @@ -87,22 +115,28 @@ enum PSpell::Result PSpell::check(string const & word) void PSpell::close() { - if (sc) - pspell_manager_save_all_word_lists(sc); + Managers::iterator it = managers_.begin(); + Managers::iterator end = managers_.end(); + + for (; it != end; ++it) { + pspell_manager_save_all_word_lists(it->second.manager); + } } -void PSpell::insert(string const & word) +void PSpell::insert(WordLangTuple const & word) { - if (sc) - pspell_manager_add_to_personal(sc, word.c_str()); + Managers::iterator it = managers_.find(word.lang_code()); + if (it != managers_.end()) + pspell_manager_add_to_personal(it->second.manager, word.word().c_str()); } -void PSpell::accept(string const & word) +void PSpell::accept(WordLangTuple const & word) { - if (sc) - pspell_manager_add_to_session(sc, word.c_str()); + Managers::iterator it = managers_.find(word.lang_code()); + if (it != managers_.end()) + pspell_manager_add_to_session(it->second.manager, word.word().c_str()); } diff --git a/src/pspell.h b/src/pspell.h index dd393ba4ce..6ab6dabb9c 100644 --- a/src/pspell.h +++ b/src/pspell.h @@ -10,11 +10,14 @@ #ifndef LYX_PSPELL_H #define LYX_PSPELL_H +#include + #include "SpellBase.h" class PspellManager; class PspellStringEmulation; class PspellCanHaveError; +class PspellConfig; class BufferParams; @@ -28,23 +31,26 @@ public: virtual ~PSpell(); - /// return true if the spellchecker instance still exists - virtual bool alive() { return alive_; } + /** + * return true if the spellchecker instance still exists + * Always true for pspell, since there is no separate process + */ + virtual bool alive() { return true; } /// clean up on messy exit virtual void cleanUp(); /// check the given word and return the result - virtual enum Result check(string const & word); + virtual enum Result check(WordLangTuple const &); /// finish this spellchecker instance virtual void close(); /// insert the given word into the personal dictionary - virtual void insert(string const & word); + virtual void insert(WordLangTuple const &); /// accept the given word temporarily - virtual void accept(string const & word); + virtual void accept(WordLangTuple const &); /// return the next near miss after a MISSED result virtual string const nextMiss(); @@ -53,14 +59,23 @@ public: virtual string const error(); private: - /// main manager - PspellManager * sc; + /// add a manager of the given language + void addManager(string const & lang); + + struct Manager { + PspellManager * manager; + PspellConfig * config; + }; + + typedef std::map Managers; + + /// the managers + Managers managers_; + /// FIXME PspellStringEmulation * els; /// FIXME PspellCanHaveError * spell_error_object; - /// initialised properly ? - bool alive_; }; #endif // PSPELL_H diff --git a/src/text.C b/src/text.C index 7f35a5fadb..466185d29e 100644 --- a/src/text.C +++ b/src/text.C @@ -2393,19 +2393,19 @@ bool LyXText::selectWordWhenUnderCursor(BufferView * bview, // This function is only used by the spellchecker for NextWord(). // It doesn't handle LYX_ACCENTs and probably never will. -string const LyXText::selectNextWordToSpellcheck(BufferView * bview, +WordLangTuple LyXText::selectNextWordToSpellcheck(BufferView * bview, float & value) const { if (the_locking_inset) { - string str = the_locking_inset->selectNextWordToSpellcheck(bview, value); - if (!str.empty()) { + WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bview, value); + if (!word.word().empty()) { value += float(cursor.y())/float(height); - return str; + return word; } - // we have to go on checking so move cusor to the next char + // we have to go on checking so move cursor to the next char if (cursor.pos() == cursor.par()->size()) { if (!cursor.par()->next()) - return str; + return word; cursor.par(cursor.par()->next()); cursor.pos(0); } else @@ -2457,6 +2457,9 @@ string const LyXText::selectNextWordToSpellcheck(BufferView * bview, // Start the selection from here selection.cursor = cursor; + string lang_code( + getFont(bview->buffer(), cursor.par(), cursor.pos()) + .language()->code()); // and find the end of the word (insets like optional hyphens // and ligature break are part of a word) while (cursor.pos() < cursor.par()->size() @@ -2472,7 +2475,7 @@ string const LyXText::selectNextWordToSpellcheck(BufferView * bview, str += cursor.par()->getChar(i); } } - return str; + return WordLangTuple(str, lang_code); } diff --git a/src/text2.C b/src/text2.C index c41e4f74fb..4d725b75df 100644 --- a/src/text2.C +++ b/src/text2.C @@ -1092,8 +1092,8 @@ string LyXText::getStringToIndex(BufferView * bview) // Try implicit word selection // If there is a change in the language the implicit word selection // is disabled. - LyXCursor resetCursor = cursor; - bool implicitSelection = selectWordWhenUnderCursor(bview, PREVIOUS_WORD); + LyXCursor const reset_cursor = cursor; + bool const implicitSelection = selectWordWhenUnderCursor(bview, PREVIOUS_WORD); if (!selection.set()) { bview->owner()->message(_("Nothing to index!")); @@ -1110,7 +1110,7 @@ string LyXText::getStringToIndex(BufferView * bview) //and cursor is set to the original position. if (implicitSelection) { clearSelection(); - cursor = resetCursor; + cursor = reset_cursor; setCursor(bview, cursor.par(), cursor.pos()); selection.cursor = cursor; } -- 2.39.5