From 3efb1572a24043c07a8e54689bd58c5fa7db97e1 Mon Sep 17 00:00:00 2001 From: Vincent van Ravesteijn Date: Fri, 18 Dec 2009 14:48:56 +0000 Subject: [PATCH] Fix bug #6378: Word completion (in texted) should be language-sensitive. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@32581 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Language.h | 9 +++++++++ src/Paragraph.cpp | 30 ++++++++++++++++++++---------- src/Text.cpp | 13 ++++++++----- src/WordList.cpp | 17 ++++++++++++++--- src/WordList.h | 4 +++- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/Language.h b/src/Language.h index 575dd33392..14700710b7 100644 --- a/src/Language.h +++ b/src/Language.h @@ -51,6 +51,8 @@ public: bool internalFontEncoding() const; /// bool read(Lexer & lex); + // for the use in std::map + friend bool operator<(Language const & p, Language const & q); private: /// std::string lang_; @@ -70,6 +72,13 @@ private: std::string latex_options_; }; + +inline bool operator<(Language const & p, Language const & q) +{ + return q.lang() > p.lang(); +} + + class Languages { public: diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index 0266c2e6ff..0711d2492b 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -208,9 +208,9 @@ public: /// TextContainer text_; - typedef std::set Words; + typedef set Words; /// - Words words_; + map words_; /// Layout const * layout_; }; @@ -2401,7 +2401,7 @@ docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf, bool emph_flag = false; bool bold_flag = false; - std::string closing_tag; + string closing_tag; Layout const & style = *d->layout_; @@ -3022,10 +3022,13 @@ bool Paragraph::isSeparator(pos_type pos) const void Paragraph::deregisterWords() { + map::const_iterator itl; Private::Words::const_iterator it; - WordList & wl = theWordList(); - for (it = d->words_.begin(); it != d->words_.end(); ++it) - wl.remove(*it); + for (itl = d->words_.begin(); itl != d->words_.end(); ++itl) { + WordList * wl = theWordList(itl->first); + for (it = (itl->second).begin(); it != (itl->second).end(); ++it) + wl->remove(*it); + } d->words_.clear(); } @@ -3077,7 +3080,11 @@ void Paragraph::collectWords() locateWord(from, pos, WHOLE_WORD); if (pos - from >= 6) { docstring word = asString(from, pos, AS_STR_NONE); - d->words_.insert(word); + FontList::const_iterator cit = d->fontlist_.fontIterator(pos); + if (cit == d->fontlist_.end()) + return; + Language const * lang = cit->font().language(); + d->words_[*lang].insert(word); } } } @@ -3085,10 +3092,13 @@ void Paragraph::collectWords() void Paragraph::registerWords() { + map::const_iterator itl; Private::Words::const_iterator it; - WordList & wl = theWordList(); - for (it = d->words_.begin(); it != d->words_.end(); ++it) - wl.insert(*it); + for (itl = d->words_.begin(); itl != d->words_.end(); ++itl) { + WordList * wl = theWordList(itl->first); + for (it = (itl->second).begin(); it != (itl->second).end(); ++it) + wl->insert(*it); + } } diff --git a/src/Text.cpp b/src/Text.cpp index 16816f3414..5803c2d4ad 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -485,8 +485,8 @@ class TextCompletionList : public CompletionList { public: /// - TextCompletionList(Cursor const & cur) - : buffer_(cur.buffer()), pos_(0) + TextCompletionList(Cursor const & cur, WordList const * list) + : buffer_(cur.buffer()), pos_(0), list_(list) {} /// virtual ~TextCompletionList() {} @@ -496,12 +496,12 @@ public: /// virtual size_t size() const { - return theWordList().size(); + return list_->size(); } /// virtual docstring const & data(size_t idx) const { - return theWordList().word(idx); + return list_->word(idx); } private: @@ -509,6 +509,8 @@ private: Buffer const * buffer_; /// size_t pos_; + /// + WordList const * list_; }; @@ -1988,7 +1990,8 @@ bool Text::completionSupported(Cursor const & cur) const CompletionList const * Text::createCompletionList(Cursor const & cur) const { - return new TextCompletionList(cur); + WordList const * list = theWordList(*cur.getFont().language()); + return new TextCompletionList(cur, list); } diff --git a/src/WordList.cpp b/src/WordList.cpp index 7dc620e089..2f8ce2bd52 100644 --- a/src/WordList.cpp +++ b/src/WordList.cpp @@ -12,6 +12,8 @@ #include "WordList.h" +#include "Language.h" + #include "support/convert.h" #include "support/debug.h" #include "support/docstring.h" @@ -19,14 +21,23 @@ #include "support/lassert.h" +#include + +using namespace std; + namespace lyx { /// -WordList theGlobalWordList; +map theGlobalWordList; -WordList & theWordList() +WordList * theWordList(Language const & lang) { - return theGlobalWordList; + map::iterator it = theGlobalWordList.find(lang); + if (it != theGlobalWordList.end()) + return it->second; + else + theGlobalWordList[lang] = new WordList(); + return theGlobalWordList[lang]; } /// diff --git a/src/WordList.h b/src/WordList.h index d02b0b7b88..7bef7f34aa 100644 --- a/src/WordList.h +++ b/src/WordList.h @@ -14,6 +14,8 @@ #include "support/docstring.h" +#include "Language.h" + namespace lyx { class WordList { @@ -37,7 +39,7 @@ private: Impl * d; }; -WordList & theWordList(); +WordList * theWordList(Language const & lang); } // namespace lyx