From 204a070130a12ce1ff1aac97547ccddba840bf24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Thu, 22 May 2003 08:01:41 +0000 Subject: [PATCH] return a ParIterator from getParFromID git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6996 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.C | 6 +- src/BufferView_pimpl.C | 8 +- src/ChangeLog | 12 ++ src/buffer.C | 16 +-- src/buffer.h | 3 +- src/frontends/controllers/ControlErrorList.C | 10 +- src/iterators.C | 8 +- src/iterators.h | 9 +- src/lyxfunc.C | 12 +- src/undo_funcs.C | 125 ++++++++++--------- 10 files changed, 115 insertions(+), 94 deletions(-) diff --git a/src/BufferView.C b/src/BufferView.C index 730e30ac97..fc1ac3a97e 100644 --- a/src/BufferView.C +++ b/src/BufferView.C @@ -509,13 +509,13 @@ void BufferView::setCursorFromRow(int row) buffer()->texrow.getIdFromRow(row, tmpid, tmppos); - Paragraph * texrowpar; + ParagraphList::iterator texrowpar; if (tmpid == -1) { - texrowpar = &*text->ownerParagraphs().begin(); + texrowpar = text->ownerParagraphs().begin(); tmppos = 0; } else { - texrowpar = &*buffer()->getParFromID(tmpid); + texrowpar = *buffer()->getParFromID(tmpid); } text->setCursor(texrowpar, tmppos); } diff --git a/src/BufferView_pimpl.C b/src/BufferView_pimpl.C index 9787625304..7a7790b3da 100644 --- a/src/BufferView_pimpl.C +++ b/src/BufferView_pimpl.C @@ -642,12 +642,12 @@ void BufferView::Pimpl::restorePosition(unsigned int i) if (b != 0) buffer(b); } - Paragraph * par = &*buffer_->getParFromID(saved_positions[i].par_id); - if (!par) + ParIterator par = buffer_->getParFromID(saved_positions[i].par_id); + if (par == buffer_->par_iterator_end()) return; - bv_->text->setCursor(par, - min(par->size(), saved_positions[i].par_pos)); + bv_->text->setCursor(*par, + min((*par)->size(), saved_positions[i].par_pos)); update(BufferView::SELECT); if (i > 0) diff --git a/src/ChangeLog b/src/ChangeLog index cae91f9913..dc95b4fada 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,15 @@ + +2003-05-22 André Pönitz + + * BufferView.C: + * BufferView_pimpl.C: + * buffer.[Ch]: + * lyxfunc.C: + * undo_funcs.C: return a ParIterator from getParFromID. + + * iterators.[Ch]: add two const's + + 2003-05-21 Lars Gullik Bjønnes * toc.C (getTocList): adjust diff --git a/src/buffer.C b/src/buffer.C index 3bb3d78951..e8c8f9de84 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -2203,7 +2203,7 @@ Inset * Buffer::getInsetFromID(int id_arg) const } -ParagraphList::iterator Buffer::getParFromID(int id) const +ParIterator Buffer::getParFromID(int id) const { #warning FIXME: const correctness! (Andre) ParIterator it(const_cast(this)->par_iterator_begin()); @@ -2213,18 +2213,14 @@ ParagraphList::iterator Buffer::getParFromID(int id) const if (id < 0) { // John says this is called with id == -1 from undo lyxerr << "getParFromID(), id: " << id << endl; - return 0; + return end; } - for (; it != end; ++it) { - // go on then, show me how to remove - // the cast - if ((*it)->id() == id) { - return *it; - } - } + for (; it != end; ++it) + if ((*it)->id() == id) + return it; - return 0; + return end; } diff --git a/src/buffer.h b/src/buffer.h index c6a2cb8901..c7dd087ba7 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -23,6 +23,7 @@ #include "ParagraphList.h" #include "paragraph.h" #include "author.h" +#include "iterators.h" #include #include @@ -122,7 +123,7 @@ public: void insertStringAsLines(ParagraphList::iterator &, lyx::pos_type &, LyXFont const &, string const &); /// - ParagraphList::iterator getParFromID(int id) const; + ParIterator getParFromID(int id) const; /// do we have a paragraph with this id? bool hasParWithID(int id) const; diff --git a/src/frontends/controllers/ControlErrorList.C b/src/frontends/controllers/ControlErrorList.C index 128dc2334d..4613989f7b 100644 --- a/src/frontends/controllers/ControlErrorList.C +++ b/src/frontends/controllers/ControlErrorList.C @@ -63,23 +63,23 @@ void ControlErrorList::goTo(int item) if (err.par_id == -1) return; - ParagraphList::iterator pit = buf->getParFromID(err.par_id); + ParIterator pit = buf->getParFromID(err.par_id); - if (pit == bv->text->ownerParagraphs().end()) { + if (pit == buf->par_iterator_end()) { lyxerr << "par id not found" << endl; return; } int range = err.pos_end - err.pos_start; - if (err.pos_end > pit->size() || range <= 0) - range = pit->size() - err.pos_start; + if (err.pos_end > (*pit)->size() || range <= 0) + range = (*pit)->size() - err.pos_start; // Now make the selection. bv->insetUnlock(); bv->toggleSelection(); bv->text->clearSelection(); - bv->text->setCursor(pit, err.pos_start); + bv->text->setCursor(*pit, err.pos_start); bv->text->setSelectionRange(range); bv->toggleSelection(false); bv->fitCursor(); diff --git a/src/iterators.C b/src/iterators.C index 7f067728d1..aa89cba5a8 100644 --- a/src/iterators.C +++ b/src/iterators.C @@ -136,13 +136,13 @@ ParIterator & ParIterator::operator++() } -ParagraphList::iterator ParIterator::operator*() +ParagraphList::iterator ParIterator::operator*() const { return pimpl_->positions.top().pit; } -ParagraphList::iterator ParIterator::operator->() +ParagraphList::iterator ParIterator::operator->() const { return pimpl_->positions.top().pit; } @@ -243,13 +243,13 @@ ParConstIterator & ParConstIterator::operator++() } -ParagraphList::iterator ParConstIterator::operator*() +ParagraphList::iterator ParConstIterator::operator*() const { return pimpl_->positions.top().pit; } -ParagraphList::iterator ParConstIterator::operator->() +ParagraphList::iterator ParConstIterator::operator->() const { return pimpl_->positions.top().pit; } diff --git a/src/iterators.h b/src/iterators.h index 9bc322ec6b..5cca3f717d 100644 --- a/src/iterators.h +++ b/src/iterators.h @@ -27,9 +27,9 @@ public: /// ParIterator & operator++(); /// - ParagraphList::iterator operator*(); + ParagraphList::iterator operator*() const; /// - ParagraphList::iterator operator->(); + ParagraphList::iterator operator->() const; /// size_t size() const; /// @@ -58,10 +58,9 @@ public: /// ParConstIterator & operator++(); /// - ParagraphList::iterator operator*(); - + ParagraphList::iterator operator*() const; /// - ParagraphList::iterator operator->(); + ParagraphList::iterator operator->() const; /// size_t size() const; diff --git a/src/lyxfunc.C b/src/lyxfunc.C index 7cc5f9424c..c00c24694b 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -1353,24 +1353,24 @@ void LyXFunc::dispatch(FuncRequest const & ev, bool verbose) int id; istr >> id; - Paragraph * par = &*owner->buffer()->getParFromID(id); - if (par == 0) { + ParIterator par = owner->buffer()->getParFromID(id); + if (par == owner->buffer()->par_iterator_end()) { lyxerr[Debug::INFO] << "No matching paragraph found! [" << id << ']' << endl; break; } else { - lyxerr[Debug::INFO] << "Paragraph " << par->id() + lyxerr[Debug::INFO] << "Paragraph " << (*par)->id() << " found." << endl; } if (view()->theLockingInset()) view()->unlockInset(view()->theLockingInset()); - if (par->inInset()) { + if ((*par)->inInset()) { FuncRequest cmd(view(), LFUN_INSET_EDIT, "left"); - par->inInset()->localDispatch(cmd); + (*par)->inInset()->localDispatch(cmd); } // Set the cursor - view()->getLyXText()->setCursor(par, 0); + view()->getLyXText()->setCursor(*par, 0); view()->switchKeyMap(); owner->view_state_changed(); diff --git a/src/undo_funcs.C b/src/undo_funcs.C index 93ad12cfaf..3d3f659c2e 100644 --- a/src/undo_funcs.C +++ b/src/undo_funcs.C @@ -78,12 +78,13 @@ bool textHandleUndo(BufferView * bv, Undo & undo) { Buffer * b = bv->buffer(); - Paragraph * const before = &*b->getParFromID(undo.number_of_before_par); - Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par); + ParIterator const before = b->getParFromID(undo.number_of_before_par); + ParIterator const behind = b->getParFromID(undo.number_of_behind_par); + ParIterator const end = b->par_iterator_end(); // If there's no before take the beginning // of the document for redoing. - if (!before) { + if (before == end) { LyXText * t = bv->text; int num = undo.number_of_inset_id; if (undo.number_of_inset_id >= 0) { @@ -100,7 +101,7 @@ bool textHandleUndo(BufferView * bv, Undo & undo) // Set the right(new) inset-owner of the paragraph if there is any. if (!undo.pars.empty()) { Inset * in = 0; - if (before) + if (before != end) in = before->inInset(); else if (undo.number_of_inset_id >= 0) in = bv->buffer()->getInsetFromID(undo.number_of_inset_id); @@ -112,17 +113,20 @@ bool textHandleUndo(BufferView * bv, Undo & undo) vector deletelist; // Now add old paragraphs to be deleted. - if (before != behind || (!behind && !before)) { - Paragraph * deletepar; - if (before) - deletepar = before->next(); - else - deletepar = &undoParagraphs(bv, undo.number_of_inset_id).front(); + if (before != behind || (behind == end && before == end)) { + ParagraphList::iterator deletepar; + if (before != end) { + deletepar = *before; + ++deletepar; + } else { + deletepar = undoParagraphs(bv, undo.number_of_inset_id).begin(); + } // this surprisingly fills the undo! (Andre') size_t par = 0; - while (deletepar && deletepar != behind) { - deletelist.push_back(deletepar); - deletepar = deletepar->next(); + //while (deletepar && deletepar != *behind) { + while (deletepar != *behind) { + deletelist.push_back(&*deletepar); + ++deletepar; // A memory optimization for edit: // Only layout information @@ -141,21 +145,21 @@ bool textHandleUndo(BufferView * bv, Undo & undo) // Thread the end of the undo onto the par in front if any. if (!undo.pars.empty()) { - undo.pars.back()->next(behind); - if (behind) - behind->previous(undo.pars.back()); + undo.pars.back()->next(&**behind); + if (behind != end) + (&**behind)->previous(undo.pars.back()); } // Put the new stuff in the list if there is one. Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars.front(); if (!undo.pars.empty()) { - undo.pars.front()->previous(before); - if (before) - before->next(undopar); + undo.pars.front()->previous(&**before); + if (before != end) + (&**before)->next(undopar); else { int id = undoParagraphs(bv, undo.number_of_inset_id).front().id(); - Paragraph * op = &*bv->buffer()->getParFromID(id); - if (op && op->inInset()) { + ParIterator op = bv->buffer()->getParFromID(id); + if (op != end && op->inInset()) { static_cast(op->inInset())->paragraph(undopar); } else { bv->buffer()->paragraphs.set(undopar); @@ -165,27 +169,27 @@ bool textHandleUndo(BufferView * bv, Undo & undo) // We enter here on DELETE undo operations where we // have to substitue the second paragraph with the // first if the removed one is the first. - if (!before && behind) { + if (before == end && behind != end) { int id = undoParagraphs(bv, undo.number_of_inset_id).front().id(); - Paragraph * op = &*bv->buffer()->getParFromID(id); - if (op && op->inInset()) { - static_cast(op->inInset())->paragraph(behind); + ParIterator op = bv->buffer()->getParFromID(id); + if (op != end && op->inInset()) { + static_cast(op->inInset())->paragraph(&**behind); } else { - bv->buffer()->paragraphs.set(behind); + bv->buffer()->paragraphs.set(&**behind); } - undopar = behind; + undopar = &**behind; } } // Set the cursor for redoing. // If we have a par before the undopar. - if (before) { + if (before != end) { Inset * it = before->inInset(); if (it) - it->getLyXText(bv)->setCursorIntern(before, 0); + it->getLyXText(bv)->setCursorIntern(*before, 0); else - bv->text->setCursorIntern(before, 0); + bv->text->setCursorIntern(*before, 0); } // we are not ready for this we cannot set the cursor for a paragraph @@ -200,24 +204,28 @@ bool textHandleUndo(BufferView * bv, Undo & undo) } #endif - Paragraph * endpar = 0; - - // Calculate the endpar for redoing the paragraphs. - if (behind) - endpar = behind->next(); - UpdatableInset * it = 0; if (undopar) it = static_cast(undopar->inInset()); + + LyXText * text = it ? it->getLyXText(bv) : bv->text; + + ParagraphList::iterator endpar = text->ownerParagraphs().end(); + + // Calculate the endpar for redoing the paragraphs. + if (behind != end) { + endpar = *behind; + ++endpar; + } + + text->redoParagraphs(text->cursor, endpar); + ParIterator tmppar = + bv->buffer()->getParFromID(undo.number_of_cursor_par); + if (it) { - it->getLyXText(bv)->redoParagraphs( - it->getLyXText(bv)->cursor, - endpar); - Paragraph * tmppar = - &*bv->buffer()->getParFromID(undo.number_of_cursor_par); - if (tmppar) { - it = static_cast(tmppar->inInset()); + if (tmppar != end) { LyXText * t; + Inset * it = tmppar->inInset(); if (it) { FuncRequest cmd(bv, LFUN_INSET_EDIT, "left"); it->localDispatch(cmd); @@ -225,7 +233,7 @@ bool textHandleUndo(BufferView * bv, Undo & undo) } else { t = bv->text; } - t->setCursorIntern(tmppar, undo.cursor_pos); + t->setCursorIntern(*tmppar, undo.cursor_pos); // Clear any selection and set the selection // cursor for an evt. new selection. t->clearSelection(); @@ -237,10 +245,7 @@ bool textHandleUndo(BufferView * bv, Undo & undo) bv->text->setCursorIntern(bv->text->cursor.par(), bv->text->cursor.pos()); } else { - bv->text->redoParagraphs(bv->text->cursor, endpar); - Paragraph * tmppar = - &*bv->buffer()->getParFromID(undo.number_of_cursor_par); - if (tmppar) { + if (tmppar != end) { LyXText * t; Inset * it = tmppar->inInset(); if (it) { @@ -250,7 +255,7 @@ bool textHandleUndo(BufferView * bv, Undo & undo) } else { t = bv->text; } - t->setCursorIntern(tmppar, undo.cursor_pos); + t->setCursorIntern(*tmppar, undo.cursor_pos); // Clear any selection and set the selection // cursor for an evt. new selection. t->clearSelection(); @@ -381,17 +386,25 @@ bool textUndoOrRedo(BufferView * bv, finishUndo(); if (!undo_frozen) { - Paragraph * first = &*b->getParFromID(undo->number_of_before_par); - if (first && first->next()) - first = first->next(); - else if (!first) - first = &*undoParagraphs(bv, undo->number_of_inset_id).begin(); +/* + ParIterator p = b->getParFromID(undo->number_of_before_par); + bool ok = false; + ParagraphList::iterator first; + // default constructed? + ParIterator const end = b->par_iterator_end(); + if (p != end) { + first = p.par(); + if (first->next()) + first = first->next(); + } else + first = undoParagraphs(bv, undo->number_of_inset_id).begin(); if (first) { shared_ptr u; - if (createUndo(bv, undo->kind, first, - b->getParFromID(undo->number_of_behind_par), u)) + ParIterator behind = b->getParFromID(undo->number_of_behind_par); + if (createUndo(bv, undo->kind, first, behind.par(), u)) otherstack.push(u); } +*/ } // Now we can unlock the inset for saftey because the inset