From 5702f22ef0ea3d167fc5f327002d7006eef657b4 Mon Sep 17 00:00:00 2001 From: Dekel Tsur Date: Fri, 7 Dec 2001 18:40:24 +0000 Subject: [PATCH] Use paragraph iterators in CutAndPaste::SwitchLayoutsBetweenClasses and in BufferView::removeAutoInsets git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3166 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView2.C | 31 ++++++++++++++++++------------- src/ChangeLog | 9 +++++++++ src/CutAndPaste.C | 12 +++++++----- src/iterators.C | 8 ++++++-- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/BufferView2.C b/src/BufferView2.C index 822bfde0b7..05b40ef781 100644 --- a/src/BufferView2.C +++ b/src/BufferView2.C @@ -102,23 +102,28 @@ bool BufferView::insertLyXFile(string const & filen) bool BufferView::removeAutoInsets() { - Paragraph * par = buffer()->paragraph; - LyXCursor tmpcursor = text->cursor; LyXCursor cursor; + bool found = false; - bool a = false; - - while (par) { + ParIterator end = buffer()->par_iterator_end(); + for (ParIterator it = buffer()->par_iterator_begin(); + it != end; ++it) { + Paragraph * par = *it; // this has to be done before the delete - text->setCursor(this, cursor, par, 0); - if (par->autoDeleteInsets()){ - a = true; - text->redoParagraphs(this, cursor, - cursor.par()->next()); - text->fullRebreak(this); + if (par->autoDeleteInsets()) { + found = true; +#ifdef WITH_WARNINGS +#warning FIXME +#endif + // The test it.size()==1 was needed to prevent crashes. + if (it.size() == 1) { + text->setCursor(this, cursor, par, 0); + text->redoParagraphs(this, cursor, + cursor.par()->next()); + text->fullRebreak(this); + } } - par = par->next(); } // avoid forbidden cursor positions caused by error removing @@ -127,7 +132,7 @@ bool BufferView::removeAutoInsets() text->setCursorIntern(this, tmpcursor.par(), tmpcursor.pos()); - return a; + return found; } diff --git a/src/ChangeLog b/src/ChangeLog index b76f096beb..6f2bc66fec 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,12 @@ +2001-12-07 Dekel Tsur + + * iterators.C (operator++): Make the iterator more robust + + * BufferView2.C (removeAutoInsets): Use paragraph iterators + (John's patch) + * CutAndPaste.C (SwitchLayoutsBetweenClasses): Ditto + + 2001-12-05 John Levon * lyxtext.h: diff --git a/src/CutAndPaste.C b/src/CutAndPaste.C index 8e9c574c4f..4990bb234d 100644 --- a/src/CutAndPaste.C +++ b/src/CutAndPaste.C @@ -17,6 +17,7 @@ #include "lyx_gui_misc.h" #include "lyxcursor.h" #include "gettext.h" +#include "iterators.h" #ifdef __GNUG__ #pragma implementation @@ -366,16 +367,18 @@ int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1, int ret = 0; if (!par || c1 == c2) return ret; - - while (par) { + + ParIterator end = ParIterator(); + for (ParIterator it = ParIterator(par); it != end; ++it) { + par = *it; string const name = textclasslist.NameOfLayout(c1, par->layout); int lay = 0; pair pp = textclasslist.NumberOfLayout(c2, name); if (pp.first) { lay = pp.second; - } else { // layout not found - // use default layout "Standard" (0) + } else { + // not found: use default layout "Standard" (0) lay = 0; } par->layout = lay; @@ -391,7 +394,6 @@ int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1, InsetError * new_inset = new InsetError(s); par->insertInset(0, new_inset); } - par = par->next(); } return ret; } diff --git a/src/iterators.C b/src/iterators.C index e94479dd04..86c1218e85 100644 --- a/src/iterators.C +++ b/src/iterators.C @@ -2,7 +2,7 @@ #include "iterators.h" -ParIterator & ParIterator::operator++() +ParIterator & ParIterator::operator++() { while (!positions.empty()) { ParPosition & p = positions.back(); @@ -16,7 +16,11 @@ ParIterator & ParIterator::operator++() 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 = p.par->inset_iterator_begin(); // Try to find the next inset that contains paragraphs for ( ; p.it != p.par->inset_iterator_end(); ++p.it) { -- 2.39.2