From 3e80b0b318343341c230b3fd828a5b8597c636d5 Mon Sep 17 00:00:00 2001 From: Alfredo Braunstein Date: Sun, 28 Mar 2004 22:00:22 +0000 Subject: [PATCH] the pariterator stuff git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8554 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.C | 20 +- src/BufferView_pimpl.C | 2 +- src/ChangeLog | 17 + src/CutAndPaste.C | 21 +- src/Makefile.am | 4 +- src/buffer.C | 10 +- src/bufferview_funcs.C | 2 +- src/cursor.C | 2 +- src/dociterator.C | 68 +--- src/frontends/controllers/ChangeLog | 5 + src/frontends/controllers/ControlDocument.C | 7 +- src/frontends/controllers/ControlErrorList.C | 2 +- src/insets/ChangeLog | 10 + src/insets/insetcharstyle.C | 4 +- src/insets/insetert.C | 12 +- src/insets/insetfloat.C | 6 +- src/insets/insetlabel.C | 2 +- src/insets/insettext.C | 14 +- src/insets/insettext.h | 6 +- src/insets/insetwrap.C | 4 +- src/iterators.C | 350 ------------------- src/lyxfind.C | 2 +- src/lyxfunc.C | 2 +- src/paragraph_funcs.C | 2 +- src/pariterator.C | 163 +++++++++ src/{iterators.h => pariterator.h} | 86 ++--- src/toc.C | 2 +- 27 files changed, 303 insertions(+), 522 deletions(-) delete mode 100644 src/iterators.C create mode 100644 src/pariterator.C rename src/{iterators.h => pariterator.h} (51%) diff --git a/src/BufferView.C b/src/BufferView.C index 633234ab43..dd64c90a40 100644 --- a/src/BufferView.C +++ b/src/BufferView.C @@ -25,13 +25,13 @@ #include "funcrequest.h" #include "gettext.h" #include "insetiterator.h" -#include "iterators.h" #include "language.h" #include "lyxlayout.h" #include "lyxtext.h" #include "lyxtextclass.h" #include "paragraph.h" #include "paragraph_funcs.h" +#include "pariterator.h" #include "texrow.h" #include "undo.h" #include "WordLangTuple.h" @@ -368,13 +368,7 @@ LyXText * BufferView::text() const void BufferView::setCursor(ParIterator const & par, lyx::pos_type pos) { LCursor & cur = cursor(); - cur.reset(buffer()->inset()); - ParIterator::PosHolder const & positions = par.positions(); - int const last = par.size() - 1; - for (int i = 0; i < last; ++i) - (*positions[i].it)->inset->edit(cur, true); - cur.resetAnchor(); - par.text(*buffer())->setCursor(cur, par.pit(), pos); + cur.setCursor(makeDocumentIterator(par, pos), false); } @@ -393,20 +387,20 @@ Ab. */ void BufferView::putSelectionAt(DocumentIterator const & cur, - int length, bool backwards) + int length, bool backwards) { ParIterator par(cur); cursor().clearSelection(); - LyXText * text = par.text(*buffer()); + LyXText & text = *par.text(); setCursor(par, cur.pos()); // hack for the chicken and egg problem - if (par.inset()) - top_y(text->getPar(par.outerPar()).y); + top_y(text.getPar(par.outerPar()).y); + update(); - text->setCursor(cursor(), cur.par(), cur.pos()); + text.setCursor(cursor(), cur.par(), cur.pos()); cursor().updatePos(); if (length) { diff --git a/src/BufferView_pimpl.C b/src/BufferView_pimpl.C index 24f6dba230..e1010d8cda 100644 --- a/src/BufferView_pimpl.C +++ b/src/BufferView_pimpl.C @@ -31,7 +31,6 @@ #include "funcrequest.h" #include "gettext.h" #include "intl.h" -#include "iterators.h" #include "insetiterator.h" #include "lyx_cb.h" // added for Dispatch functions #include "lyx_main.h" @@ -43,6 +42,7 @@ #include "paragraph.h" #include "paragraph_funcs.h" #include "ParagraphParameters.h" +#include "pariterator.h" #include "undo.h" #include "vspace.h" diff --git a/src/ChangeLog b/src/ChangeLog index 943ad1dfbb..6a6e39b49d 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,4 +1,21 @@ +2004-03-28 Alfredo Braunstein + + * dociterator.C: implement forwardPar + * iterators.[Ch]: remove, replaced by + * pariterator.[Ch]: this. derive ParIterator from DocumentIterator + * BufferView.C: + * BufferView_pimpl.C: + * CutAndPaste.C: + * buffer.C: + * bufferview_funcs.C: + * cursor.C: + * lyxfind.C + * lyxfunc.C + * paragraph_funcs.C + * toc.C: + * Makefile.am: adjust + 2004-03-28 Alfredo Braunstein * CutAndPaste.C (pasteSelection): fix 2 crashes diff --git a/src/CutAndPaste.C b/src/CutAndPaste.C index e3063113dc..4d291f38d8 100644 --- a/src/CutAndPaste.C +++ b/src/CutAndPaste.C @@ -21,13 +21,13 @@ #include "cursor.h" #include "errorlist.h" #include "gettext.h" -#include "iterators.h" #include "lyxtext.h" #include "lyxtextclasslist.h" #include "paragraph.h" #include "paragraph_funcs.h" #include "ParagraphParameters.h" #include "ParagraphList_fwd.h" +#include "pariterator.h" #include "undo.h" #include "insets/insettabular.h" @@ -81,8 +81,12 @@ int SwitchLayoutsBetweenClasses(textclass_type c1, textclass_type c2, LyXTextClass const & tclass1 = textclasslist[c1]; LyXTextClass const & tclass2 = textclasslist[c2]; - ParIterator end = ParIterator(pars.size(), pars); - for (ParIterator it = ParIterator(0, pars); it != end; ++it) { + + InsetText in; + std::swap(in.paragraphs(), pars); + + ParIterator end = ParIterator(in, pars.size()); + for (ParIterator it = ParIterator(in, 0); it != end; ++it) { string const name = it->layout()->name(); bool hasLayout = tclass2.hasLayout(name); @@ -103,6 +107,7 @@ int SwitchLayoutsBetweenClasses(textclass_type c1, textclass_type c2, it->size())); } } + std::swap(in.paragraphs(), pars); return ret; } @@ -301,8 +306,11 @@ pasteSelection(Buffer const & buffer, ParagraphList & pars, // Prepare the paragraphs and insets for insertion. // A couple of insets store buffer references so need updating. - ParIterator fpit(0, insertion); - ParIterator fend(insertion.size(), insertion); + InsetText in; + std::swap(in.paragraphs(), insertion); + + ParIterator fpit(in, 0); + ParIterator fend(in, insertion.size()); for (; fpit != fend; ++fpit) { InsetList::iterator lit = fpit->insetlist.begin(); @@ -321,7 +329,8 @@ pasteSelection(Buffer const & buffer, ParagraphList & pars, } } } - + std::swap(in.paragraphs(), insertion); + // Split the paragraph for inserting the buf if necessary. bool did_split = false; if (pars[pit].size() || pit + 1 == par_type(pars.size())) { diff --git a/src/Makefile.am b/src/Makefile.am index 3575c6ff09..20d66a71a9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -165,8 +165,6 @@ lyx_SOURCES = \ importer.h \ intl.C \ intl.h \ - iterators.C \ - iterators.h \ insetiterator.C \ insetiterator.h \ kbmap.C \ @@ -243,6 +241,8 @@ lyx_SOURCES = \ paragraph_funcs.h \ paragraph_pimpl.C \ paragraph_pimpl.h \ + pariterator.C \ + pariterator.h \ SpellBase.h \ ispell.C \ ispell.h \ diff --git a/src/buffer.C b/src/buffer.C index cde602284f..e0010c2247 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -26,7 +26,6 @@ #include "funcrequest.h" #include "gettext.h" #include "insetiterator.h" -#include "iterators.h" #include "language.h" #include "LaTeX.h" #include "LaTeXFeatures.h" @@ -44,6 +43,7 @@ #include "paragraph.h" #include "paragraph_funcs.h" #include "ParagraphParameters.h" +#include "pariterator.h" #include "sgml.h" #include "texrow.h" #include "undo.h" @@ -1344,25 +1344,25 @@ bool Buffer::hasParWithID(int id) const ParIterator Buffer::par_iterator_begin() { - return ParIterator(0, paragraphs()); + return ParIterator(inset(), 0); } ParIterator Buffer::par_iterator_end() { - return ParIterator(paragraphs().size(), paragraphs()); + return ParIterator(DocumentIterator()); } ParConstIterator Buffer::par_iterator_begin() const { - return ParConstIterator(0, paragraphs()); + return ParConstIterator(inset(), 0); } ParConstIterator Buffer::par_iterator_end() const { - return ParConstIterator(paragraphs().size(), paragraphs()); + return ParConstIterator(DocumentIterator()); } diff --git a/src/bufferview_funcs.C b/src/bufferview_funcs.C index 2e1c6212c9..0bcc2e892d 100644 --- a/src/bufferview_funcs.C +++ b/src/bufferview_funcs.C @@ -27,7 +27,7 @@ #include "lyxrow.h" #include "paragraph.h" #include "ParagraphParameters.h" -#include "iterators.h" +#include "pariterator.h" #include "frontends/Alert.h" #include "frontends/LyXView.h" diff --git a/src/cursor.C b/src/cursor.C index 52d971d211..35ccc5ad3c 100644 --- a/src/cursor.C +++ b/src/cursor.C @@ -20,7 +20,6 @@ #include "dispatchresult.h" #include "encoding.h" #include "funcrequest.h" -#include "iterators.h" #include "language.h" #include "lfuns.h" #include "lyxfont.h" @@ -30,6 +29,7 @@ #include "lyxtext.h" #include "paragraph.h" #include "paragraph_funcs.h" +#include "pariterator.h" #include "insets/updatableinset.h" #include "insets/insettabular.h" diff --git a/src/dociterator.C b/src/dociterator.C index 6c3b43b3f3..c03d345e63 100644 --- a/src/dociterator.C +++ b/src/dociterator.C @@ -294,11 +294,19 @@ void DocumentIterator::forwardPos() } +void DocumentIterator::forwardPar() +{ + forwardPos(); + while (!empty() && (!inTexted() || pos() != 0)) + forwardPos(); +} + + void DocumentIterator::forwardChar() { - forwardPos(); + forwardPos(); while (size() != 0 && pos() == lastpos()) - forwardPos(); + forwardPos(); } @@ -317,62 +325,6 @@ void DocumentIterator::backwardChar() } -void DocumentIterator::forwardPar() -{ - CursorSlice & top = back(); - lyxerr << "XXX " << *this << endl; - - // move into an inset to the right if possible - InsetBase * n = 0; - if (top.pos() != lastpos()) { - // this is impossible for pos() == size() - if (inMathed()) { - n = (top.cell().begin() + top.pos())->nucleus(); - } else { - if (paragraph().isInset(top.pos())) - n = paragraph().getInset(top.pos()); - } - } - - if (n && n->isActive()) { - lyxerr << "... descend" << endl; - push_back(CursorSlice(*n)); - return; - } - - // otherwise move on one cell back if possible - if (top.pos() < lastpos()) { - lyxerr << "... next pos" << endl; - ++top.pos(); - return; - } - - // otherwise move on one cell back if possible - if (top.par() < lastpar()) { - lyxerr << "... next par" << endl; - ++top.par(); - top.pos() = 0; - return; - } - - // otherwise try to move on one cell if possible - while (top.idx() < top.lastidx()) { - lyxerr << "... next idx" - << " was: " << top.idx() << " max: " << top.lastidx() << endl; - ++top.idx(); - top.par() = 0; - top.pos() = 0; - return; - } - - // otherwise leave inset an jump over inset as a whole - pop_back(); - // 'top' is invalid now... - if (size()) - ++back().pos(); -} - - std::ostream & operator<<(std::ostream & os, DocumentIterator const & dit) { for (size_t i = 0, n = dit.size(); i != n; ++i) diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 35552fe3c1..4cbfa0fd8e 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,8 @@ +2004-03-28 Alfredo Braunstein + + * ControlDocument.C: + * ControlErrorList.C: adjust to ParIterator change + 2004-03-28 Angus Leeming * ControlPrint.[Ch]: converted to the dialog-based scheme. diff --git a/src/frontends/controllers/ControlDocument.C b/src/frontends/controllers/ControlDocument.C index c5c51c5d99..d10194032e 100644 --- a/src/frontends/controllers/ControlDocument.C +++ b/src/frontends/controllers/ControlDocument.C @@ -20,14 +20,15 @@ #include "BufferView.h" #include "CutAndPaste.h" #include "errorlist.h" +#include "funcrequest.h" #include "gettext.h" -#include "iterators.h" #include "language.h" #include "LColor.h" #include "lyxtextclasslist.h" -#include "paragraph.h" -#include "funcrequest.h" #include "lfuns.h" +#include "paragraph.h" +#include "ParagraphList_fwd.h" +#include "pariterator.h" #include "frontends/Alert.h" #include "frontends/LyXView.h" diff --git a/src/frontends/controllers/ControlErrorList.C b/src/frontends/controllers/ControlErrorList.C index 005378f481..64dab7aa3a 100644 --- a/src/frontends/controllers/ControlErrorList.C +++ b/src/frontends/controllers/ControlErrorList.C @@ -14,9 +14,9 @@ #include "buffer.h" #include "BufferView.h" #include "debug.h" -#include "iterators.h" #include "lyxtext.h" #include "paragraph.h" +#include "pariterator.h" using std::endl; using std::string; diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 4e3ff26253..fc31ccc483 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,4 +1,14 @@ +2004-03-28 Alfredo Braunstein + + * insettext.[Ch]: add a default ctor. implement const version of + paragraphs() + * insetcharstyle.C: + * insetert.C: + * insetfloat.C: + * insetlabel.C: + * insetwrap.C: adjust + 2004-03-28 Alfredo Braunstein * insettext.h: add insetAllowed returning true by default (fixing diff --git a/src/insets/insetcharstyle.C b/src/insets/insetcharstyle.C index 8f64168a34..dc5d78a9c9 100644 --- a/src/insets/insetcharstyle.C +++ b/src/insets/insetcharstyle.C @@ -166,8 +166,8 @@ namespace { int outputVerbatim(std::ostream & os, InsetText const & inset) { int lines = 0; - ParagraphList::iterator par = inset.paragraphs().begin(); - ParagraphList::iterator end = inset.paragraphs().end(); + ParagraphList::const_iterator par = inset.paragraphs().begin(); + ParagraphList::const_iterator end = inset.paragraphs().end(); while (par != end) { lyx::pos_type siz = par->size(); for (lyx::pos_type i = 0; i < siz; ++i) { diff --git a/src/insets/insetert.C b/src/insets/insetert.C index 086e32f8fd..40e8a94fa8 100644 --- a/src/insets/insetert.C +++ b/src/insets/insetert.C @@ -116,8 +116,8 @@ string const InsetERT::editMessage() const int InsetERT::latex(Buffer const &, ostream & os, OutputParams const &) const { - ParagraphList::iterator par = paragraphs().begin(); - ParagraphList::iterator end = paragraphs().end(); + ParagraphList::const_iterator par = paragraphs().begin(); + ParagraphList::const_iterator end = paragraphs().end(); int lines = 0; while (par != end) { @@ -155,8 +155,8 @@ int InsetERT::plaintext(Buffer const &, ostream &, int InsetERT::linuxdoc(Buffer const &, ostream & os, OutputParams const &) const { - ParagraphList::iterator par = paragraphs().begin(); - ParagraphList::iterator end = paragraphs().end(); + ParagraphList::const_iterator par = paragraphs().begin(); + ParagraphList::const_iterator end = paragraphs().end(); int lines = 0; while (par != end) { @@ -183,8 +183,8 @@ int InsetERT::linuxdoc(Buffer const &, ostream & os, int InsetERT::docbook(Buffer const &, ostream & os, OutputParams const &) const { - ParagraphList::iterator par = paragraphs().begin(); - ParagraphList::iterator end = paragraphs().end(); + ParagraphList::const_iterator par = paragraphs().begin(); + ParagraphList::const_iterator end = paragraphs().end(); int lines = 0; while (par != end) { diff --git a/src/insets/insetfloat.C b/src/insets/insetfloat.C index ed173ac63b..2a1a485ff7 100644 --- a/src/insets/insetfloat.C +++ b/src/insets/insetfloat.C @@ -23,12 +23,12 @@ #include "FloatList.h" #include "funcrequest.h" #include "gettext.h" -#include "iterators.h" #include "LaTeXFeatures.h" #include "LColor.h" #include "lyxlex.h" #include "outputparams.h" #include "paragraph.h" +#include "pariterator.h" #include "support/lstrings.h" #include "support/tostr.h" @@ -379,8 +379,8 @@ void InsetFloat::wide(bool w, BufferParams const & bp) void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const { - ParIterator pit(0, paragraphs()); - ParIterator end(paragraphs().size(), paragraphs()); + ParConstIterator pit(*this, 0); + ParConstIterator end = ParConstIterator(DocumentIterator()); // Find a caption layout in one of the (child inset's) pars for (; pit != end; ++pit) { diff --git a/src/insets/insetlabel.C b/src/insets/insetlabel.C index e915f787ff..c96ea56b81 100644 --- a/src/insets/insetlabel.C +++ b/src/insets/insetlabel.C @@ -17,9 +17,9 @@ #include "dispatchresult.h" #include "funcrequest.h" #include "InsetList.h" -#include "iterators.h" #include "lyxtext.h" #include "paragraph.h" +#include "pariterator.h" #include "frontends/LyXView.h" diff --git a/src/insets/insettext.C b/src/insets/insettext.C index a17764b90e..69eef1534a 100644 --- a/src/insets/insettext.C +++ b/src/insets/insettext.C @@ -88,6 +88,10 @@ InsetText::InsetText(InsetText const & in) } +InsetText::InsetText() : text_(0) +{} + + void InsetText::operator=(InsetText const & in) { UpdatableInset::operator=(in); @@ -514,7 +518,13 @@ void InsetText::addPreview(PreviewLoader & loader) const } -ParagraphList & InsetText::paragraphs() const +ParagraphList const & InsetText::paragraphs() const { - return const_cast(text_.paragraphs()); + return text_.paragraphs(); +} + + +ParagraphList & InsetText::paragraphs() +{ + return text_.paragraphs(); } diff --git a/src/insets/insettext.h b/src/insets/insettext.h index 88e7b0c305..9766c6bfa6 100644 --- a/src/insets/insettext.h +++ b/src/insets/insettext.h @@ -50,6 +50,8 @@ public: /// explicit InsetText(BufferParams const &); /// + explicit InsetText(); + /// InsetText(InsetText const &); /// virtual std::auto_ptr clone() const; @@ -141,7 +143,9 @@ public: /// number of cells in this inset size_t nargs() const { return 1; } /// - ParagraphList & paragraphs() const; + ParagraphList & paragraphs(); + /// + ParagraphList const & paragraphs() const; /// bool insetAllowed(Code) const { return true; } diff --git a/src/insets/insetwrap.C b/src/insets/insetwrap.C index 2163b026da..59e8f57aa6 100644 --- a/src/insets/insetwrap.C +++ b/src/insets/insetwrap.C @@ -223,8 +223,8 @@ bool InsetWrap::showInsetDialog(BufferView * bv) const void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const { // Now find the caption in the float... - ParagraphList::iterator tmp = paragraphs().begin(); - ParagraphList::iterator end = paragraphs().end(); + ParagraphList::const_iterator tmp = paragraphs().begin(); + ParagraphList::const_iterator end = paragraphs().end(); for (; tmp != end; ++tmp) { if (tmp->layout()->name() == caplayout) { diff --git a/src/iterators.C b/src/iterators.C deleted file mode 100644 index f4536bd025..0000000000 --- a/src/iterators.C +++ /dev/null @@ -1,350 +0,0 @@ -/* \file iterators.C - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author unknown - * \author Lars Gullik Bjønnes - * - * Full author contact details are available in file CREDITS. - */ - - -#include - -#include "iterators.h" -#include "paragraph.h" -#include "cursor.h" -#include "buffer.h" -#include "BufferView.h" -#include "dispatchresult.h" - -#include "insets/inset.h" -#include "insets/updatableinset.h" -#include "insets/insettext.h" - -#include - -using lyx::par_type; - -using boost::next; - -/// -/// ParPosition -/// - - -ParPosition::ParPosition(par_type p, ParagraphList const & pl) - : pit(p), plist(&pl) -{ - if (p != par_type(pl.size())) - it.reset(const_cast(pl[p].insetlist).begin()); -} - - -bool operator==(ParPosition const & pos1, ParPosition const & pos2) -{ - return pos1.pit == pos2.pit; -} - - -bool operator!=(ParPosition const & pos1, ParPosition const & pos2) -{ - return !(pos1 == pos2); -} - - -/// -/// ParIterator -/// - -ParIterator::ParIterator(par_type pit, ParagraphList const & pl) -{ - positions_.push_back(ParPosition(pit, pl)); -} - - -ParIterator::~ParIterator() -{} - - -ParIterator::ParIterator(ParIterator const & pi) - : positions_(pi.positions_) -{} - - -void ParIterator::operator=(ParIterator const & pi) -{ - ParIterator tmp(pi); - swap(positions_, tmp.positions_); -} - - -ParIterator & ParIterator::operator++() -{ - while (!positions_.empty()) { - ParPosition & p = positions_.back(); - - // Does the current inset contain more "cells" ? - if (p.index) { - ++(*p.index); - if (LyXText * text = (*p.it)->inset->getText(*p.index)) { - ParagraphList & plist = text->paragraphs(); - if (!plist.empty()) { - positions_.push_back(ParPosition(0, plist)); - return *this; - } - } - ++(*p.it); - } else { - // The following line is needed because the value of - // p.it may be invalid if inset was added/removed to - // the paragraph pointed by the iterator - p.it.reset(const_cast((*p.plist)[p.pit].insetlist).begin()); - } - - // Try to find the next inset that contains paragraphs - InsetList::iterator end = - const_cast((*p.plist)[p.pit].insetlist).end(); - for (; *p.it != end; ++(*p.it)) { - if (LyXText * text = (*p.it)->inset->getText(0)) { - ParagraphList & plist = text->paragraphs(); - if (!plist.empty()) { - p.index.reset(0); - positions_.push_back(ParPosition(0, plist)); - return *this; - } - } - } - - // Try to go to the next paragarph - if (p.pit + 1 != par_type(p.plist->size()) || - positions_.size() == 1) { - ++p.pit; - p.index.reset(); - p.it.reset(); - return *this; - } - - // Drop end and move up in the stack. - positions_.pop_back(); - } - return *this; -} - - -LyXText * ParIterator::text(Buffer & buf) const -{ - //lyxerr << "positions.size: " << positions.size() << std::endl; - if (positions_.size() <= 1) - return &buf.text(); - - ParPosition const & pos = positions_[positions_.size() - 2]; - return (*pos.it)->inset->getText(*pos.index); -} - - -InsetBase * ParIterator::inset() const -{ - //lyxerr << "positions.size: " << positions.size() << std::endl; - if (positions_.size() <= 1) - return 0; - - ParPosition const & pos = positions_[positions_.size() - 2]; - return (*pos.it)->inset; -} - - -int ParIterator::index() const -{ - if (positions_.size() <= 1) - return 0; - - return *(positions_[positions_.size() - 2].index); -} - - -Paragraph & ParIterator::operator*() const -{ - return plist()[positions_.back().pit]; -} - - -par_type ParIterator::pit() const -{ - return positions_.back().pit; -} - - -Paragraph * ParIterator::operator->() const -{ - return &plist()[positions_.back().pit]; -} - - -par_type ParIterator::outerPar() const -{ - return positions_[0].pit; -} - - -size_t ParIterator::size() const -{ - return positions_.size(); -} - - -ParagraphList & ParIterator::plist() const -{ - return *const_cast(positions_.back().plist); -} - - -ParIterator::ParIterator(DocumentIterator const & cur) -{ - int const size = cur.size(); - - for (int i = 0; i < size; ++i) { - CursorSlice sl = cur[i]; - ParPosition pp(sl.par(), sl.text()->paragraphs()); - if (i < size - 1) { - Paragraph & par = sl.text()->paragraphs()[sl.par()]; - InsetBase * inset = par.getInset(sl.pos()); - BOOST_ASSERT(inset); - InsetList::iterator beg = par.insetlist.begin(); - InsetList::iterator end = par.insetlist.end(); - for ( ; beg != end && beg->inset != inset; ++beg) - ; - pp.it.reset(beg); - pp.index.reset(sl.idx() - 1); - } - positions_.push_back(pp); - } -} - - -bool operator==(ParIterator const & iter1, ParIterator const & iter2) -{ - return iter1.positions() == iter2.positions(); -} - - -bool operator!=(ParIterator const & iter1, ParIterator const & iter2) -{ - return !(iter1 == iter2); -} - - -/// -/// ParConstIterator -/// - - -ParConstIterator::ParConstIterator(par_type pit, ParagraphList const & pl) -{ - positions_.push_back(ParPosition(pit, pl)); -} - - -ParConstIterator::~ParConstIterator() -{} - - -ParConstIterator::ParConstIterator(ParConstIterator const & pi) - : positions_(pi.positions_) -{} - - -ParConstIterator & ParConstIterator::operator++() -{ - while (!positions_.empty()) { - ParPosition & p = positions_.back(); - - // Does the current inset contain more "cells" ? - if (p.index) { - ++(*p.index); - if (LyXText * text = (*p.it)->inset->getText(*p.index)) { - ParagraphList & plist = text->paragraphs(); - if (!plist.empty()) { - positions_.push_back(ParPosition(0, plist)); - return *this; - } - } - ++(*p.it); - } else { - // The following line is needed because the value of - // p.it may be invalid if inset was added/removed to - // the paragraph pointed by the iterator - p.it.reset(const_cast((*p.plist)[p.pit].insetlist).begin()); - } - - // Try to find the next inset that contains paragraphs - InsetList::iterator end = - const_cast((*p.plist)[p.pit].insetlist).end(); - for (; *p.it != end; ++(*p.it)) { - if (LyXText * text = (*p.it)->inset->getText(0)) { - ParagraphList & plist = text->paragraphs(); - if (!plist.empty()) { - p.index.reset(0); - positions_.push_back(ParPosition(0, plist)); - return *this; - } - } - } - - // Try to go to the next paragarph - if (p.pit + 1 != par_type(p.plist->size()) || - positions_.size() == 1) { - ++p.pit; - p.index.reset(); - p.it.reset(); - return *this; - } - - // Drop end and move up in the stack. - positions_.pop_back(); - } - - return *this; -} - - -Paragraph const & ParConstIterator::operator*() const -{ - return plist()[positions_.back().pit]; -} - - -par_type ParConstIterator::pit() const -{ - return positions_.back().pit; -} - - -Paragraph const * ParConstIterator::operator->() const -{ - return &plist()[positions_.back().pit]; -} - - -ParagraphList const & ParConstIterator::plist() const -{ - return *positions_.back().plist; -} - - -size_t ParConstIterator::size() const -{ - return positions_.size(); -} - - -bool operator==(ParConstIterator const & iter1, ParConstIterator const & iter2) -{ - return iter1.positions() == iter2.positions(); -} - - -bool operator!=(ParConstIterator const & iter1, ParConstIterator const & iter2) -{ - return !(iter1 == iter2); -} diff --git a/src/lyxfind.C b/src/lyxfind.C index 7f2d5bfee8..dc12c17d39 100644 --- a/src/lyxfind.C +++ b/src/lyxfind.C @@ -20,11 +20,11 @@ #include "CutAndPaste.h" #include "BufferView.h" #include "debug.h" -#include "iterators.h" #include "funcrequest.h" #include "gettext.h" #include "lyxtext.h" #include "paragraph.h" +#include "pariterator.h" #include "undo.h" #include "frontends/Alert.h" diff --git a/src/lyxfunc.C b/src/lyxfunc.C index eab75513a7..54c68f6acd 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -37,7 +37,6 @@ #include "gettext.h" #include "importer.h" #include "intl.h" -#include "iterators.h" #include "kbmap.h" #include "LColor.h" #include "lyx_cb.h" @@ -48,6 +47,7 @@ #include "lyxserver.h" #include "lyxvc.h" #include "paragraph.h" +#include "pariterator.h" #include "ParagraphParameters.h" #include "undo.h" diff --git a/src/paragraph_funcs.C b/src/paragraph_funcs.C index 01a404ffe0..1cac51313c 100644 --- a/src/paragraph_funcs.C +++ b/src/paragraph_funcs.C @@ -18,11 +18,11 @@ #include "debug.h" #include "encoding.h" #include "gettext.h" -#include "iterators.h" #include "language.h" #include "lyxtext.h" #include "outputparams.h" #include "paragraph_pimpl.h" +#include "pariterator.h" #include "sgml.h" #include "texrow.h" #include "vspace.h" diff --git a/src/pariterator.C b/src/pariterator.C new file mode 100644 index 0000000000..d864d659e7 --- /dev/null +++ b/src/pariterator.C @@ -0,0 +1,163 @@ +/* \file iterators.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author unknown + * \author Lars Gullik Bjønnes + * + * Full author contact details are available in file CREDITS. + */ + + +#include + + +#include "pariterator.h" + +#include "ParagraphList_fwd.h" +#include "paragraph.h" +#include "lyxtext.h" + +#include "insets/inset.h" + +using lyx::par_type; + +/// +/// ParIterator +/// + +ParIterator::ParIterator(DocumentIterator const & cur) : DocumentIterator(cur) +{} + + +ParIterator::ParIterator(InsetBase & in, par_type pit) : DocumentIterator(in) +{ + par() = pit; +} + + +ParIterator::ParIterator(ParIterator const & pi) + : DocumentIterator(DocumentIterator(pi)) +{} + + +ParIterator & ParIterator::operator++() +{ + forwardPar(); + return *this; +} + + +ParIterator & ParIterator::operator--() +{ +// DocumentIterator::backwardPar(); + return *this; +} + + +Paragraph & ParIterator::operator*() const +{ + return text()->getPar(par()); +} + + +par_type ParIterator::pit() const +{ + return par(); +} + + +Paragraph * ParIterator::operator->() const +{ + return &text()->getPar(par()); +} + + +par_type ParIterator::outerPar() const +{ + return bottom().par(); +} + + +ParagraphList & ParIterator::plist() const +{ + return text()->paragraphs(); +} + + +bool operator==(ParIterator const & iter1, ParIterator const & iter2) +{ + return DocumentIterator(iter1) == DocumentIterator(iter2); +} + + +bool operator!=(ParIterator const & iter1, ParIterator const & iter2) +{ + return !(iter1 == iter2); +} + +DocumentIterator +makeDocumentIterator(ParIterator const & par, lyx::pos_type pos) +{ + DocumentIterator dit(par); + dit.pos() = pos; + return dit; +} + +/// +/// ParConstIterator +/// + + +ParConstIterator::ParConstIterator(InsetBase const & in, par_type pit) + : DocumentIterator(const_cast(in)) +{ + par() = pit; +} + + +ParConstIterator::ParConstIterator(DocumentIterator const & dit) + : DocumentIterator(dit) +{} + + +ParConstIterator::ParConstIterator(ParConstIterator const & pi) + : DocumentIterator(DocumentIterator(pi)) +{} + + +ParConstIterator & ParConstIterator::operator++() +{ + DocumentIterator::forwardPar(); + return *this; +} + + +Paragraph const & ParConstIterator::operator*() const +{ + return text()->getPar(par()); +} + + +Paragraph const * ParConstIterator::operator->() const +{ + return &text()->getPar(par()); +} + + +ParagraphList const & ParConstIterator::plist() const +{ + return text()->paragraphs(); +} + + +bool operator==(ParConstIterator const & iter1, ParConstIterator const & iter2) +{ + return DocumentIterator(iter1) == DocumentIterator(iter2); +} + + +bool operator!=(ParConstIterator const & iter1, ParConstIterator const & iter2) +{ + return !(iter1 == iter2); +} diff --git a/src/iterators.h b/src/pariterator.h similarity index 51% rename from src/iterators.h rename to src/pariterator.h index b6f99ce897..c1f9812e39 100644 --- a/src/iterators.h +++ b/src/pariterator.h @@ -12,8 +12,7 @@ #ifndef ITERATORS_H #define ITERATORS_H -#include "InsetList.h" -#include "ParagraphList_fwd.h" // only for ParagraphList::value_type +#include "dociterator.h" #include "support/types.h" @@ -21,45 +20,31 @@ #include -class Buffer; -class Cursor; + + class InsetBase; class LyXText; -class DocumentIterator; +class ParagraphList; -class ParPosition { +class ParIterator : public std::iterator, + public DocumentIterator +{ public: /// - ParPosition(lyx::par_type p, ParagraphList const & pl); - /// - lyx::par_type pit; - /// - ParagraphList const * plist; - /// - boost::optional it; - /// - boost::optional index; -}; - - -class ParIterator : public std::iterator< - std::forward_iterator_tag, - ParagraphList::value_type> { -public: - /// - ParIterator(lyx::par_type pit, ParagraphList const & pl); - /// - ~ParIterator(); + ParIterator(InsetBase &, lyx::par_type pit); /// ParIterator(ParIterator const &); /// ParIterator(DocumentIterator const &); + /// void operator=(ParIterator const &); /// ParIterator & operator++(); /// + ParIterator & operator--(); + /// Paragraph & operator*() const; /// Paragraph * operator->() const; @@ -69,24 +54,14 @@ public: lyx::par_type pit() const; /// ParagraphList & plist() const; - /// returns 'innermost' LyXText - LyXText * text(Buffer &) const; - /// returns innermost inset - InsetBase * inset() const; - /// returns index of cell in innermost inset - int index() const; - /// - size_t size() const; - - typedef std::vector PosHolder; - PosHolder const & positions() const - { - return positions_; - } -private: - PosHolder positions_; }; + +DocumentIterator makeDocumentIterator(ParIterator const &, lyx::pos_type); + + + + /// bool operator==(ParIterator const & iter1, ParIterator const & iter2); @@ -94,37 +69,28 @@ bool operator==(ParIterator const & iter1, ParIterator const & iter2); bool operator!=(ParIterator const & iter1, ParIterator const & iter2); -class ParConstIterator : public std::iterator< - std::forward_iterator_tag, - ParagraphList::value_type> { +class ParConstIterator : public std::iterator, + public DocumentIterator +{ public: /// - ParConstIterator(lyx::par_type pit, ParagraphList const & pl); - /// - ~ParConstIterator(); + ParConstIterator(InsetBase const &, lyx::par_type pit); /// ParConstIterator(ParConstIterator const &); /// + ParConstIterator(DocumentIterator const &); + /// + ParConstIterator & operator++(); /// - lyx::par_type pit() const; + ParConstIterator & operator--(); /// Paragraph const & operator*() const; /// Paragraph const * operator->() const; /// ParagraphList const & plist() const; - - /// depth of nesting - size_t size() const; - /// - typedef std::vector PosHolder; - /// - PosHolder const & positions() const { return positions_; } - -private: - /// - PosHolder positions_; }; bool operator==(ParConstIterator const & iter1, diff --git a/src/toc.C b/src/toc.C index 65bcc5673b..d18c4b0387 100644 --- a/src/toc.C +++ b/src/toc.C @@ -16,9 +16,9 @@ #include "buffer.h" #include "bufferparams.h" #include "funcrequest.h" -#include "iterators.h" #include "LyXAction.h" #include "paragraph.h" +#include "pariterator.h" #include "frontends/LyXView.h"