From: Dekel Tsur Date: Sat, 1 Sep 2001 21:26:34 +0000 (+0000) Subject: Paragraph iterators X-Git-Tag: 1.6.10~20697 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=0233b6753d5a7ef67263c3111411be04765c629b;p=features.git Paragraph iterators git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2649 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/BufferView2.C b/src/BufferView2.C index 3aac225fed..b58b1c3ca6 100644 --- a/src/BufferView2.C +++ b/src/BufferView2.C @@ -32,6 +32,7 @@ #include "gettext.h" #include "undo_funcs.h" #include "debug.h" +#include "iterators.h" extern BufferList bufferlist; @@ -494,18 +495,20 @@ bool BufferView::ChangeInsets(Inset::Code code, string const & from, string const & to) { bool flag = false; - Paragraph * par = buffer()->paragraph; LyXCursor cursor = text->cursor; LyXCursor tmpcursor = cursor; cursor.par(tmpcursor.par()); cursor.pos(tmpcursor.pos()); - while (par) { + ParIterator end = buffer()->par_iterator_end(); + for (ParIterator it = buffer()->par_iterator_begin(); + it != end; ++it) { + Paragraph * par = *it; bool flag2 = false; - for (Paragraph::inset_iterator it = par->inset_iterator_begin(); - it != par->inset_iterator_end(); ++it) { - if ((*it)->lyxCode() == code) { - InsetCommand * inset = static_cast(*it); + for (Paragraph::inset_iterator it2 = par->inset_iterator_begin(); + it2 != par->inset_iterator_end(); ++it2) { + if ((*it2)->lyxCode() == code) { + InsetCommand * inset = static_cast(*it2); if (inset->getContents() == from) { inset->setContents(to); flag2 = true; @@ -514,14 +517,16 @@ bool BufferView::ChangeInsets(Inset::Code code, } if (flag2) { flag = true; - // this is possible now, since SetCursor takes - // care about footnotes - text->setCursorIntern(this, par, 0); - text->redoParagraphs(this, text->cursor, - text->cursor.par()->next()); - text->fullRebreak(this); +#warning Fix me + // The test it.size()==1 was needed to prevent crashes. + // How to set the cursor corretly when it.size()>1 ?? + if (it.size() == 1) { + text->setCursorIntern(this, par, 0); + text->redoParagraphs(this, text->cursor, + text->cursor.par()->next()); + text->fullRebreak(this); + } } - par = par->next(); } text->setCursorIntern(this, cursor.par(), cursor.pos()); return flag; diff --git a/src/ChangeLog b/src/ChangeLog index 2ad6f9ee4d..aa6d9b67f5 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,12 @@ +2001-09-02 Dekel Tsur + + * iterators.[Ch]: New files. Provide paragraph iterators. + + * buffer.C (changeLanguage): Use paragraph iterators. + (isMultiLingual): ditto + + * BufferView2.C (ChangeInsets): Use paragraph iterators. + 2001-09-01 Dekel Tsur * FontLoader.C: Support for cmr font. diff --git a/src/Makefile.am b/src/Makefile.am index 64b6772746..1c281449c6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -124,6 +124,8 @@ lyx_SOURCES = \ importer.h \ intl.C \ intl.h \ + iterators.C \ + iterators.h \ kbmap.C \ kbmap.h \ kbsequence.C \ diff --git a/src/buffer.C b/src/buffer.C index e99a8a01b8..de4866e3b9 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -107,6 +107,7 @@ #include "converter.h" #include "BufferView.h" #include "ParagraphParameters.h" +#include "iterators.h" using std::ostream; using std::ofstream; @@ -3755,22 +3756,19 @@ void Buffer::redraw() void Buffer::changeLanguage(Language const * from, Language const * to) { - Paragraph * par = paragraph; - while (par) { - par->changeLanguage(params, from, to); - par = par->next(); - } + ParIterator end = par_iterator_end(); + for (ParIterator it = par_iterator_begin(); it != end; ++it) + (*it)->changeLanguage(params, from, to); } bool Buffer::isMultiLingual() { - Paragraph * par = paragraph; - while (par) { - if (par->isMultiLingual(params)) + ParIterator end = par_iterator_end(); + for (ParIterator it = par_iterator_begin(); it != end; ++it) + if ((*it)->isMultiLingual(params)) return true; - par = par->next(); - } + return false; } @@ -3831,3 +3829,15 @@ Paragraph * Buffer::getParFromID(int id) const } return 0; } + + +ParIterator Buffer::par_iterator_begin() +{ + return ParIterator(paragraph); +} + + +ParIterator Buffer::par_iterator_end() +{ + return ParIterator(); +} diff --git a/src/buffer.h b/src/buffer.h index a68acbc263..0a34563cf0 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -30,6 +30,7 @@ class LyXRC; class TeXErrors; class LaTeXFeatures; class Language; +class ParIterator; // When lyx 1.3.x starts we should enable this // btw. we should also test this with 1.2 so that we @@ -451,6 +452,12 @@ public: inset_iterator inset_const_iterator_end() const { return inset_iterator(); } + + /// + ParIterator par_iterator_begin(); + /// + ParIterator par_iterator_end(); + /// Inset * getInsetFromID(int id_arg) const; }; @@ -600,4 +607,5 @@ bool operator!=(Buffer::inset_iterator const & iter1, Buffer::inset_iterator const & iter2) { return !(iter1 == iter2); } + #endif diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 993018c589..53a723b5d0 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,7 @@ +2001-09-02 Dekel Tsur + + * inset.h (getFirstParagraph): New virtual method. + 2001-08-20 Herbert Voss * insetbib.C: added a option bibtotoc which is from "BIB to TOC" in the the bibtex-database-gui for inserting a line diff --git a/src/insets/inset.h b/src/insets/inset.h index 82d84034bc..7a36f61686 100644 --- a/src/insets/inset.h +++ b/src/insets/inset.h @@ -264,6 +264,12 @@ public: virtual Paragraph * firstParagraph() const { return 0; } + + /// + virtual Paragraph * getFirstParagraph(int /*num*/) const { + return 0; + } + /// return the cursor if we own one otherwise giv'em just the /// BufferView cursor to work with. virtual LyXCursor const & cursor(BufferView * bview) const; diff --git a/src/insets/insetcollapsable.C b/src/insets/insetcollapsable.C index b1905c679c..5a9926cff8 100644 --- a/src/insets/insetcollapsable.C +++ b/src/insets/insetcollapsable.C @@ -577,6 +577,12 @@ Paragraph * InsetCollapsable::firstParagraph() const } +Paragraph * InsetCollapsable::getFirstParagraph(int i) const +{ + return inset.getFirstParagraph(i); +} + + LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const { return inset.cursor(bv); diff --git a/src/insets/insetcollapsable.h b/src/insets/insetcollapsable.h index c595acef08..3c9a29aca3 100644 --- a/src/insets/insetcollapsable.h +++ b/src/insets/insetcollapsable.h @@ -165,6 +165,8 @@ public: /// Paragraph * firstParagraph() const; /// + Paragraph * getFirstParagraph(int) const; + /// LyXCursor const & cursor(BufferView *) const; /// bool isOpen() const { return !collapsed_; } diff --git a/src/insets/insettabular.C b/src/insets/insettabular.C index f54ef6e196..0da1e92eb0 100644 --- a/src/insets/insettabular.C +++ b/src/insets/insettabular.C @@ -2458,6 +2458,14 @@ Paragraph * InsetTabular::firstParagraph() const } +Paragraph * InsetTabular::getFirstParagraph(int i) const +{ + return (i < tabular->GetNumberOfCells()) + ? tabular->GetCellInset(i)->getFirstParagraph(0) + : 0; +} + + LyXCursor const & InsetTabular::cursor(BufferView * bv) const { if (the_locking_inset) diff --git a/src/insets/insettabular.h b/src/insets/insettabular.h index 4c4b64a81b..40c2b07360 100644 --- a/src/insets/insettabular.h +++ b/src/insets/insettabular.h @@ -208,6 +208,8 @@ public: /// Paragraph * firstParagraph() const; /// + Paragraph * getFirstParagraph(int) const; + /// LyXCursor const & cursor(BufferView *) const; /// string const selectNextWord(BufferView *, float & value) const; diff --git a/src/insets/insettext.C b/src/insets/insettext.C index bc431bcada..ba50705075 100644 --- a/src/insets/insettext.C +++ b/src/insets/insettext.C @@ -2122,6 +2122,12 @@ Paragraph * InsetText::firstParagraph() const } +Paragraph * InsetText::getFirstParagraph(int i) const +{ + return (i == 0) ? par : 0; +} + + LyXCursor const & InsetText::cursor(BufferView * bv) const { if (the_locking_inset) diff --git a/src/insets/insettext.h b/src/insets/insettext.h index a654c47b1e..f611bdbec2 100644 --- a/src/insets/insettext.h +++ b/src/insets/insettext.h @@ -221,6 +221,8 @@ public: /// Paragraph * firstParagraph() const; /// + Paragraph * getFirstParagraph(int) const; + /// LyXCursor const & cursor(BufferView *) const; /// Paragraph * paragraph() const; diff --git a/src/iterators.C b/src/iterators.C new file mode 100644 index 0000000000..e94479dd04 --- /dev/null +++ b/src/iterators.C @@ -0,0 +1,39 @@ +#include + +#include "iterators.h" + +ParIterator & ParIterator::operator++() +{ + while (!positions.empty()) { + ParPosition & p = positions.back(); + + // Does the current inset contain more "cells" ? + if (p.index >= 0) { + ++p.index; + Paragraph * par = (*p.it)->getFirstParagraph(p.index); + if (par) { + positions.push_back(ParPosition(par)); + return *this; + } + ++p.it; + } + + // Try to find the next inset that contains paragraphs + for ( ; p.it != p.par->inset_iterator_end(); ++p.it) { + Paragraph * par = (*p.it)->getFirstParagraph(0); + if (par) { + p.index = 0; + positions.push_back(ParPosition(par)); + return *this; + } + } + // Try to go to the next paragarph + if (p.par->next()) { + p = ParPosition(p.par->next()); + return *this; + } + + positions.pop_back(); + } + return *this; +} diff --git a/src/iterators.h b/src/iterators.h new file mode 100644 index 0000000000..148eda029c --- /dev/null +++ b/src/iterators.h @@ -0,0 +1,72 @@ +// -*- C++ -*- + +#ifndef ITERATORS_H +#define ITERATORS_H + +#include + +#include "paragraph.h" + +class ParPosition { +public: + ParPosition(Paragraph * p) + : par(p), it(p->inset_iterator_begin()), index(-1) {} + /// + Paragraph * par; + /// + Paragraph::inset_iterator it; + /// + int index; +}; + + +inline +bool operator==(ParPosition const & pos1, ParPosition const & pos2) { + return pos1.par == pos2.par && + pos1.it == pos2.it && + pos1.index == pos2.index; +} + +inline +bool operator!=(ParPosition const & pos1, ParPosition const & pos2) { + return !(pos1 == pos2); +} + + +class ParIterator { +public: + /// + ParIterator() {} + // + ParIterator(Paragraph * par) + : positions(1, ParPosition(par)) {} + /// + ParIterator & operator++(); + /// + Paragraph * operator*() { return positions.back().par; } + /// + vector::size_type size() const + { return positions.size(); } + /// + friend + bool operator==(ParIterator const & iter1, ParIterator const & iter2); +private: + /// + std::vector positions; +}; + + +/// +inline +bool operator==(ParIterator const & iter1, ParIterator const & iter2) { + return iter1.positions == iter2.positions; +} + + +/// +inline +bool operator!=(ParIterator const & iter1, ParIterator const & iter2) { + return !(iter1 == iter2); +} + +#endif