This patch fixes bug #6378.

When working in multilingual documents, the word completion should be limited to the language at cursor. Currently, you get many irrelevant completion proposals from the other languages.

Any advices or objections ?

Vincent
Index: Language.h
===================================================================
--- Language.h  (revision 32374)
+++ Language.h  (working copy)
@@ -51,6 +51,11 @@
        bool internalFontEncoding() const;
        ///
        bool read(Lexer & lex);
+       // for the use in std::map
+       friend bool operator<(Language const & p, Language const & q)
+       {
+               return q.lang() > p.lang();
+       }
 private:
        ///
        std::string lang_;
Index: Text.cpp
===================================================================
--- Text.cpp    (revision 32374)
+++ Text.cpp    (working copy)
@@ -485,8 +485,8 @@
 {
 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 @@
        ///
        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 @@
        Buffer const * buffer_;
        ///
        size_t pos_;
+       ///
+       WordList const * list_;
 };
 
 
@@ -1986,7 +1988,8 @@
 
 CompletionList const * Text::createCompletionList(Cursor const & cur) const
 {
-       return new TextCompletionList(cur);
+       WordList const * list = theWordList(*cur.getFont().language());
+       return new TextCompletionList(cur, list);
 }
 
 
Index: Paragraph.cpp
===================================================================
--- Paragraph.cpp       (revision 32374)
+++ Paragraph.cpp       (working copy)
@@ -210,7 +210,7 @@
        
        typedef std::set<docstring> Words;
        ///
-       Words words_;
+       std::map<Language, Words> words_;
        ///
        Layout const * layout_;
 };
@@ -3011,10 +3011,13 @@
 
 void Paragraph::deregisterWords()
 {
+       std::map<Language, Private::Words>::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();
 }
 
@@ -3066,7 +3069,11 @@
                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);
                }
        }
 }
@@ -3074,10 +3081,13 @@
 
 void Paragraph::registerWords()
 {
+       std::map<Language, Private::Words>::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);
+       }
 }
 
 
Index: WordList.cpp
===================================================================
--- WordList.cpp        (revision 32374)
+++ WordList.cpp        (working copy)
@@ -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,21 @@
 
 #include "support/lassert.h"
 
+#include <map>
+
 namespace lyx {
 
 ///
-WordList theGlobalWordList;
+std::map<Language, WordList *> theGlobalWordList;
 
-WordList & theWordList()
+WordList * theWordList(Language const & lang)
 {
-       return theGlobalWordList;
+       std::map<Language, WordList *>::iterator it = 
theGlobalWordList.find(lang);
+       if (it != theGlobalWordList.end())
+               return it->second;
+       else
+               theGlobalWordList[lang] = new WordList();
+       return theGlobalWordList[lang];
 }
 
 ///
Index: WordList.h
===================================================================
--- WordList.h  (revision 32374)
+++ WordList.h  (working copy)
@@ -14,6 +14,8 @@
 
 #include "support/docstring.h"
 
+#include "Language.h"
+
 namespace lyx {
 
 class WordList {
@@ -37,7 +39,7 @@
        Impl * d;
 };
 
-WordList & theWordList();
+WordList * theWordList(Language const & lang);
 
 } // namespace lyx
 

Reply via email to