diff --git a/src/Buffer.cpp b/src/Buffer.cpp index bd6b7f7fc7..de4b9ccd2e 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -204,9 +204,6 @@ public: /// A cache for the bibfiles (including bibfiles of loaded child /// documents), needed for appropriate update of natbib labels. mutable EmbeddedFileList bibfilesCache_; - - /// - WordList words_; }; /// Creates the per buffer temporary directory @@ -2668,16 +2665,4 @@ void Buffer::bufferErrors(TeXErrors const & terr, ErrorList & errorList) const } } - -void Buffer::registerWord(docstring const & word) -{ - d->words_.insert(word); -} - - -WordList const & Buffer::registeredWords() const -{ - return d->words_; -} - } // namespace lyx diff --git a/src/Buffer.h b/src/Buffer.h index ace9986f23..c35a2479c0 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -50,7 +50,6 @@ class TeXErrors; class TexRow; class TocBackend; class Undo; -class WordList; namespace frontend { class GuiBufferDelegate; @@ -452,11 +451,6 @@ public: bool isExportable(std::string const & format) const; /// std::vector exportableFormats(bool only_viewable) const; - - /// Register word for completion word list. - void registerWord(docstring const & word); - /// - WordList const & registeredWords() const; private: /// search for macro in local (buffer) table or in children diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index 5d773ee34c..08fb7c527d 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -39,7 +39,9 @@ #include "sgml.h" #include "TextClass.h" #include "TexRow.h" +#include "Text.h" #include "VSpace.h" +#include "WordList.h" #include "frontends/alert.h" @@ -196,6 +198,10 @@ public: typedef docstring TextContainer; /// TextContainer text_; + + typedef std::set Words; + /// + Words words_; }; @@ -1057,6 +1063,7 @@ Paragraph & Paragraph::operator=(Paragraph const & par) Paragraph::~Paragraph() { + deregisterWords(); delete d; } @@ -2678,4 +2685,63 @@ bool Paragraph::isSeparator(pos_type pos) const } +void Paragraph::deregisterWords() +{ + // deregister words + Private::Words::const_iterator it; + WordList & wl = theWordList(); + for (it = d->words_.begin(); it != d->words_.end(); ++it) + wl.remove(*it); + d->words_.clear(); +} + + +void Paragraph::registerWords(Cursor const & cur) +{ + // find new words + bool inword = false; + + lyxerr << "Words: "; + pos_type n = size(); + for (pos_type pos = 0; pos != n; ++pos) { + if (isDeleted(pos)) + continue; + + if (!isLetter(pos)) { + inword = false; + continue; + } + + if (inword) + continue; + + inword = true; + CursorSlice from = cur.top(); + CursorSlice to = cur.top(); + from.pos() = pos; + to.pos() = pos; + from.text()->getWord(from, to, NEXT_WORD); + if (to.pos() - from.pos() < 6) + continue; + docstring word + = asString(cur.buffer(), from.pos(), to.pos(), false); + d->words_.insert(word); + lyxerr << word << " "; + } + lyxerr << std::endl; + + // register words + Private::Words::const_iterator it; + WordList & wl = theWordList(); + for (it = d->words_.begin(); it != d->words_.end(); ++it) + wl.insert(*it); +} + + +void Paragraph::updateWords(Cursor const & cur) +{ + deregisterWords(); + registerWords(cur); +} + } // namespace lyx diff --git a/src/Paragraph.h b/src/Paragraph.h index 3c9e533562..eb24df8dbc 100644 --- a/src/Paragraph.h +++ b/src/Paragraph.h @@ -31,6 +31,7 @@ class Buffer; class BufferParams; class Change; class Counters; +class Cursor; class Inset; class InsetBibitem; class LaTeXFeatures; @@ -367,8 +368,16 @@ public: bool mw, ///< pos_type pos, ///< start from here. bool del = true) const; + + /// + void updateWords(Cursor const & cur); private: + /// + void deregisterWords(); + /// + void registerWords(Cursor const & cur); + /// Pimpl away stuff class Private; /// diff --git a/src/Text.cpp b/src/Text.cpp index 98aef7a70e..9066213668 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -581,7 +581,8 @@ void Text::charInserted(Cursor & cur) && !par.isLetter(cur.pos() - 1)) { // get the word in front of cursor BOOST_ASSERT(this == cur.text()); - CursorSlice focus = cur.top(); + cur.paragraph().updateWords(cur); + /* CursorSlice focus = cur.top(); focus.backwardPos(); CursorSlice from = focus; CursorSlice to = focus; @@ -593,7 +594,7 @@ void Text::charInserted(Cursor & cur) // register words longer than 5 characters if (word.length() > 5) - cur.buffer().registerWord(word); + cur.buffer().registerWord(word);*/ } } @@ -1129,7 +1130,7 @@ bool Text::dissolveInset(Cursor & cur) { void Text::getWord(CursorSlice & from, CursorSlice & to, - word_location const loc) + word_location const loc) const { Paragraph const & from_par = pars_[from.pit()]; switch (loc) { @@ -1161,7 +1162,7 @@ void Text::getWord(CursorSlice & from, CursorSlice & to, break; } to = from; - Paragraph & to_par = pars_[to.pit()]; + Paragraph const & to_par = pars_[to.pit()]; while (to.pos() < to_par.size() && to_par.isLetter(to.pos())) ++to.pos(); } diff --git a/src/Text.h b/src/Text.h index 0f8ba1c7f0..323bdabaa9 100644 --- a/src/Text.h +++ b/src/Text.h @@ -134,7 +134,7 @@ public: * @param from return here the start of the word * @param to return here the end of the word */ - void getWord(CursorSlice & from, CursorSlice & to, word_location const); + void getWord(CursorSlice & from, CursorSlice & to, word_location const) const; /// just selects the word the cursor is in void selectWord(Cursor & cur, word_location loc); diff --git a/src/WordList.cpp b/src/WordList.cpp index a5a8f4a78f..5d4debf1e9 100644 --- a/src/WordList.cpp +++ b/src/WordList.cpp @@ -21,12 +21,20 @@ namespace lyx { +/// +WordList theGlobalWordList; + +WordList & theWordList() +{ + return theGlobalWordList; +} + /// struct WordList::Impl { /// size_t c_; /// - typedef stx::weighted_btree Words; + typedef stx::weighted_btree Words; /// Words words_; }; @@ -67,8 +75,22 @@ size_t WordList::size() const void WordList::insert(docstring const & w) { - d->words_.insert(w, size_t(1), stx::Void()); + Impl::Words::iterator it = d->words_.find(w); + if (it == d->words_.end()) + d->words_.insert(w, size_t(1), 1); + else + it.data()++; +} + + +void WordList::remove(docstring const & w) +{ + Impl::Words::iterator it = d->words_.find(w); + if (it != d->words_.end()) { + it.data()--; + if (it.data() == 0) + d->words_.erase(w); + } } - } // namespace lyx diff --git a/src/WordList.h b/src/WordList.h index 263c4d40da..d02b0b7b88 100644 --- a/src/WordList.h +++ b/src/WordList.h @@ -29,12 +29,16 @@ public: size_t size() const; /// void insert(docstring const & w); - + /// + void remove(docstring const & w); + private: struct Impl; Impl * d; }; +WordList & theWordList(); + } // namespace lyx #endif // WORDLIST_H diff --git a/src/insets/InsetText.cpp b/src/insets/InsetText.cpp index 42d5cff3ce..f3404281bc 100644 --- a/src/insets/InsetText.cpp +++ b/src/insets/InsetText.cpp @@ -80,12 +80,14 @@ public: /// virtual size_t size() const { - return buf_.registeredWords().size(); +// return buf_.registeredWords().size(); + return theWordList().size(); } /// virtual docstring const & data(size_t idx) const { - return buf_.registeredWords().word(idx); +// return buf_.registeredWords().word(idx); + return theWordList().word(idx); } private: diff --git a/src/support/weighted_btree.h b/src/support/weighted_btree.h index 4418a83136..d5d6c300c1 100644 --- a/src/support/weighted_btree.h +++ b/src/support/weighted_btree.h @@ -2547,7 +2547,7 @@ private: left->weights[left->slotuse + i] = right->weights[i]; } left->slotuse += right->slotuse; - left->weight += right.weight; + left->weight += right->weight; left->nextleaf = right->nextleaf; if (left->nextleaf) @@ -2600,7 +2600,7 @@ private: left->childid[left->slotuse + i] = right->childid[i]; } left->slotuse += right->slotuse; - left->weight += right.weight; + left->weight += right->weight; left->childid[left->slotuse] = right->childid[right->slotuse];