this patch 

1) makes DocumentIterator (btw, too long name) store an inset * member. With
this we can come back from the past-the-last position (and go forward from
the one-before-start one, which is the same one)

I've introduced then stale functions DocumentIterator{Begin,End}(InsetBase
&) that return iterators. Please note that the end iterator now is
DocumentIterator(inset) or DocumentIteratorEnd(inset), which was the
begin() iterator before.

Added Begin,End also for {Par,ParConst,Inset}Iterator.

2) implements DocumentIterator::backwardPos.

Comments?

Alfredo

? DviOut-1.4.0.lyx
? Markers.C
? Markers.h
? PosIterator.C-bold
? PosIterator.h-bold
? lyxrow_funcs.C-bad1
? paragraph.C-bad1
? text.C-bad1
? text.C-good
? text.C-goood
? text.C-save
? undo.C-save
Index: BufferView.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView.C,v
retrieving revision 1.242
diff -u -p -r1.242 BufferView.C
--- BufferView.C	28 Mar 2004 22:00:19 -0000	1.242
+++ BufferView.C	29 Mar 2004 11:00:29 -0000
@@ -311,7 +311,7 @@ void BufferView::setCursorFromRow(int ro
 
 void BufferView::gotoLabel(string const & label)
 {
-	for (InsetIterator it(buffer()->inset()); it; ++it) {
+	for (InsetIterator it = InsetIteratorBegin(buffer()->inset()); it; ++it) {
 		vector<string> labels;
 		it->getLabelList(*buffer(), labels);
 		if (find(labels.begin(),labels.end(),label) != labels.end()) {
Index: BufferView_pimpl.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.C,v
retrieving revision 1.529
diff -u -p -r1.529 BufferView_pimpl.C
--- BufferView_pimpl.C	28 Mar 2004 22:00:19 -0000	1.529
+++ BufferView_pimpl.C	29 Mar 2004 11:00:30 -0000
@@ -723,7 +723,7 @@ InsetBase * BufferView::Pimpl::getInsetB
 #warning FIXME
 #if 0
 	Buffer * buf = bv_->buffer();
-	InsetIterator beg(buf->inset());
+	InsetIterator beg = InsetIteratorBegin(buf->inset());
 
 	bool cursor_par_seen = false;
 
Index: CutAndPaste.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/CutAndPaste.C,v
retrieving revision 1.132
diff -u -p -r1.132 CutAndPaste.C
--- CutAndPaste.C	29 Mar 2004 09:46:05 -0000	1.132
+++ CutAndPaste.C	29 Mar 2004 11:00:31 -0000
@@ -85,8 +85,8 @@ int SwitchLayoutsBetweenClasses(textclas
 	InsetText in;
 	std::swap(in.paragraphs(), pars);
 	
-	ParIterator end = ParIterator(DocumentIterator());
-	for (ParIterator it = ParIterator(in, 0); it != end; ++it) {
+	ParIterator end = ParIteratorEnd(in);
+	for (ParIterator it = ParIteratorBegin(in); it != end; ++it) {
 		string const name = it->layout()->name();
 		bool hasLayout = tclass2.hasLayout(name);
 
@@ -309,8 +309,8 @@ pasteSelection(Buffer const & buffer, Pa
 	InsetText in;
 	std::swap(in.paragraphs(), insertion);
 
-	ParIterator fpit = ParIterator(in, 0);
-	ParIterator fend = ParIterator(DocumentIterator());
+	ParIterator fpit = ParIteratorBegin(in);
+	ParIterator fend = ParIteratorEnd(in);
 
 	for (; fpit != fend; ++fpit) {
 		InsetList::iterator lit = fpit->insetlist.begin();
Index: buffer.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer.C,v
retrieving revision 1.565
diff -u -p -r1.565 buffer.C
--- buffer.C	28 Mar 2004 22:00:20 -0000	1.565
+++ buffer.C	29 Mar 2004 11:00:32 -0000
@@ -1192,7 +1192,7 @@ void Buffer::getLabelList(std::vector<st
 		return;
 	}
 
-	for (InsetIterator it(inset()); it; ++it)
+	for (InsetIterator it = InsetIteratorBegin(inset()); it; ++it)
 		it.nextInset()->getLabelList(*this, list);
 }
 
@@ -1210,7 +1210,7 @@ void Buffer::fillWithBibKeys(std::vector
 		return;
 	}
 
-	for (InsetIterator it(inset()); it; ++it) {
+	for (InsetIterator it = InsetIteratorBegin(inset()); it; ++it) {
 		if (it->lyxCode() == InsetOld::BIBTEX_CODE) {
 			InsetBibtex const & inset =
 				dynamic_cast<InsetBibtex const &>(*it);
@@ -1304,11 +1304,9 @@ bool Buffer::isMultiLingual() const
 
 ParIterator Buffer::getParFromID(int id) const
 {
-#warning FIXME: const correctness! (Andre)
-	ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
-	ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
+	ParConstIterator it = par_iterator_begin();
+	ParConstIterator end = par_iterator_end();
 
-#warning FIXME, perhaps this func should return a ParIterator? (Lgb)
 	if (id < 0) {
 		// John says this is called with id == -1 from undo
 		lyxerr << "getParFromID(), id: " << id << endl;
@@ -1344,25 +1342,25 @@ bool Buffer::hasParWithID(int id) const
 
 ParIterator Buffer::par_iterator_begin()
 {
-	return ParIterator(inset(), 0);
+	return ParIteratorBegin(inset());
 }
 
 
 ParIterator Buffer::par_iterator_end()
 {
-	return ParIterator(DocumentIterator());
+	return ParIteratorEnd(inset());
 }
 
 
 ParConstIterator Buffer::par_iterator_begin() const
 {
-	return ParConstIterator(inset(), 0);
+	return ParConstIteratorBegin(inset());
 }
 
 
 ParConstIterator Buffer::par_iterator_end() const
 {
-	return ParConstIterator(DocumentIterator());
+	return ParConstIteratorEnd(inset());
 }
 
 
Index: cursor.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/cursor.C,v
retrieving revision 1.82
diff -u -p -r1.82 cursor.C
--- cursor.C	28 Mar 2004 22:00:21 -0000	1.82
+++ cursor.C	29 Mar 2004 11:00:33 -0000
@@ -84,8 +84,9 @@ void region(CursorSlice const & i1, Curs
 
 
 LCursor::LCursor(BufferView & bv)
-	: DocumentIterator(), bv_(&bv), anchor_(),
-	  cached_y_(0), x_target_(-1), selection_(false), mark_(false)
+	: DocumentIterator(), bv_(&bv),
+	  anchor_(), cached_y_(0), x_target_(-1),
+	  selection_(false), mark_(false)
 {}
 
 
@@ -93,7 +94,7 @@ void LCursor::reset(InsetBase & inset)
 {
 	clear();
 	push_back(CursorSlice(inset));
-	anchor_.clear();
+	anchor_ = DocumentIterator(inset);
 	cached_y_ = 0;
 	clearTargetX();
 	selection_ = false;
@@ -1117,8 +1118,8 @@ bool LCursor::bruteFind(int x, int y, in
 	BOOST_ASSERT(text);
 	getParsInRange(text->paragraphs(), ylow, yhigh, beg, end);
 
-	DocumentIterator it(bv().buffer()->inset());
-	DocumentIterator et;
+	DocumentIterator it = DocumentIteratorBegin(bv().buffer()->inset());
+	DocumentIterator et = DocumentIteratorEnd(bv().buffer()->inset());
 	//lyxerr << "x: " << x << " y: " << y << endl;
 	//lyxerr << "xlow: " << xlow << " ylow: " << ylow << endl;
 	//lyxerr << "xhigh: " << xhigh << " yhigh: " << yhigh << endl;
Index: dociterator.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/dociterator.C,v
retrieving revision 1.9
diff -u -p -r1.9 dociterator.C
--- dociterator.C	28 Mar 2004 22:00:21 -0000	1.9
+++ dociterator.C	29 Mar 2004 11:00:33 -0000
@@ -14,16 +14,30 @@
 using std::endl;
 
 
-DocumentIterator::DocumentIterator()
+//we could be able to get rid of this if only every BufferView were
+//associated to a buffer on construction
+DocumentIterator::DocumentIterator() : inset_(0)
 {}
 
 
-DocumentIterator::DocumentIterator(InsetBase & inset)
+DocumentIterator DocumentIteratorBegin(InsetBase & inset)
 {
-	push_back(CursorSlice(inset));
+	DocumentIterator dit(inset);
+	dit.forwardPos();
+	return dit;
 }
 
 
+DocumentIterator DocumentIteratorEnd(InsetBase & inset)
+{
+	return DocumentIterator(inset);
+}
+
+
+DocumentIterator::DocumentIterator(InsetBase & inset) : inset_(&inset)
+{}
+
+
 InsetBase * DocumentIterator::nextInset()
 {
 	BOOST_ASSERT(!empty());
@@ -234,6 +248,12 @@ InsetBase * DocumentIterator::innerInset
 
 void DocumentIterator::forwardPos()
 {
+	//this dog bites his tail
+	if (empty()) {
+		push_back(CursorSlice(*inset_));
+		return;
+	}
+
 	CursorSlice & top = back();
 	//lyxerr << "XXX\n" << *this << endl;
 
@@ -320,8 +340,62 @@ void DocumentIterator::forwardInset()
 
 void DocumentIterator::backwardChar()
 {
-	lyxerr << "not implemented" << endl;
-	BOOST_ASSERT(false);
+	backwardPos();
+	while (size() != 0 && pos() == lastpos())
+		backwardPos(); 
+}
+
+
+void DocumentIterator::backwardPos()
+{
+	//this dog bites his tail
+	if (empty()) {
+		push_back(CursorSlice(*inset_));
+		back().idx() = lastidx();
+		back().par() = lastpar();
+		back().pos() = lastpos();
+		return;
+	}
+
+	CursorSlice & top = back();
+
+	if (top.pos() != 0) {
+		--top.pos();
+	} else if (top.par() != 0) {
+		//lyxerr << "... prev par" << endl;
+		--top.par();
+		top.pos() = lastpos();
+		return;
+	} else if (top.idx() != 0) {
+		// otherwise try to move on one cell if possible
+		//lyxerr << "... next idx" << endl;
+		--top.idx();
+		top.par() = lastpar();
+		top.pos() = lastpos();
+		return;
+	} else {
+		pop_back();
+		return;
+	}
+
+	// move into an inset to the left if possible
+	InsetBase * n = 0;
+	
+	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));
+		back().idx() = lastidx();
+		back().par() = lastpar();
+		back().pos() = lastpos();
+		return;
+	}
 }
 
 
@@ -349,7 +423,7 @@ StableDocumentIterator::asDocumentIterat
 {
 	// this function re-creates the cache of inset pointers
 	//lyxerr << "converting:\n" << *this << endl;
-	DocumentIterator dit;
+	DocumentIterator dit = DocumentIteratorEnd(*inset);
 	for (size_t i = 0, n = data_.size(); i != n; ++i) {
 		dit.push_back(data_[i]);
 		dit.back().inset_ = inset;
Index: dociterator.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/dociterator.h,v
retrieving revision 1.8
diff -u -p -r1.8 dociterator.h
--- dociterator.h	27 Mar 2004 12:37:41 -0000	1.8
+++ dociterator.h	29 Mar 2004 11:00:33 -0000
@@ -188,7 +188,13 @@ public:
 	/// output
 	friend std::ostream &
 	operator<<(std::ostream & os, DocumentIterator const & cur);
+private:
+	InsetBase * inset_;
 };
+
+
+DocumentIterator DocumentIteratorBegin(InsetBase & inset);
+DocumentIterator DocumentIteratorEnd(InsetBase & inset);
 
 
 // The difference to a ('non stable') DocumentIterator is the removed
Index: insetiterator.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insetiterator.C,v
retrieving revision 1.1
diff -u -p -r1.1 insetiterator.C
--- insetiterator.C	27 Mar 2004 12:33:53 -0000	1.1
+++ insetiterator.C	29 Mar 2004 11:00:33 -0000
@@ -7,6 +7,18 @@
 InsetIterator::InsetIterator(InsetBase & inset)
 	: DocumentIterator(inset)
 {
-	if (size() && !nextInset())
-		forwardInset();
+}
+
+
+InsetIterator InsetIteratorBegin(InsetBase & inset)
+{
+	InsetIterator it = InsetIterator(inset);
+	it.forwardInset();
+	return it;
+}
+
+
+InsetIterator InsetIteratorEnd(InsetBase & inset)
+{
+	return InsetIterator(inset);
 }
Index: insetiterator.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insetiterator.h,v
retrieving revision 1.1
diff -u -p -r1.1 insetiterator.h
--- insetiterator.h	27 Mar 2004 12:33:53 -0000	1.1
+++ insetiterator.h	29 Mar 2004 11:00:33 -0000
@@ -19,8 +19,6 @@ class InsetIterator : public DocumentIte
 {
 public:
 	///
-	InsetIterator() {}
-	///
 	explicit InsetIterator(InsetBase & inset);
 	///
 	void operator++() { forwardInset(); }
@@ -31,5 +29,10 @@ public:
 	///
 	InsetBase & operator*() { return *nextInset(); }
 };
+
+
+InsetIterator InsetIteratorBegin(InsetBase & inset);
+InsetIterator InsetIteratorEnd(InsetBase & inset);
+
 
 #endif
Index: pariterator.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/pariterator.C,v
retrieving revision 1.1
diff -u -p -r1.1 pariterator.C
--- pariterator.C	28 Mar 2004 22:00:21 -0000	1.1
+++ pariterator.C	29 Mar 2004 11:00:34 -0000
@@ -30,9 +30,15 @@ ParIterator::ParIterator(DocumentIterato
 {}
 
 
-ParIterator::ParIterator(InsetBase & in, par_type pit) : DocumentIterator(in)
+ParIterator ParIteratorBegin(InsetBase & inset)
 {
-	par() = pit;
+	return ParIterator(DocumentIteratorBegin(inset));
+}
+
+
+ParIterator ParIteratorEnd(InsetBase & inset)
+{
+	return ParIterator(DocumentIteratorEnd(inset));
 }
 
 
@@ -109,13 +115,6 @@ makeDocumentIterator(ParIterator const &
 ///
 
 
-ParConstIterator::ParConstIterator(InsetBase const & in, par_type pit)
-	: DocumentIterator(const_cast<InsetBase &>(in))
-{
-	par() = pit;
-}
-
-
 ParConstIterator::ParConstIterator(DocumentIterator const & dit)
 	: DocumentIterator(dit)
 {}
@@ -160,4 +159,18 @@ bool operator==(ParConstIterator const &
 bool operator!=(ParConstIterator const & iter1, ParConstIterator const & iter2)
 {
 	return !(iter1 == iter2);
+}
+
+
+#warning const correctness!
+
+ParConstIterator ParConstIteratorBegin(InsetBase const & inset)
+{
+	return ParConstIterator(DocumentIteratorBegin(const_cast<InsetBase &>(inset)));
+}
+
+
+ParConstIterator ParConstIteratorEnd(InsetBase const & inset)
+{
+	return ParConstIterator(DocumentIteratorEnd(const_cast<InsetBase &>(inset)));
 }
Index: pariterator.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/pariterator.h,v
retrieving revision 1.1
diff -u -p -r1.1 pariterator.h
--- pariterator.h	28 Mar 2004 22:00:21 -0000	1.1
+++ pariterator.h	29 Mar 2004 11:00:34 -0000
@@ -59,7 +59,8 @@ public:
 
 DocumentIterator makeDocumentIterator(ParIterator const &, lyx::pos_type);
 
-
+ParIterator ParIteratorBegin(InsetBase & inset);
+ParIterator ParIteratorEnd(InsetBase & inset);
 
 
 ///
@@ -75,8 +76,6 @@ class ParConstIterator : public std::ite
 {
 public:
 	///
-	ParConstIterator(InsetBase const &, lyx::par_type pit);
-	///
 	ParConstIterator(ParConstIterator const &);
 	///
 	ParConstIterator(DocumentIterator const &);
@@ -98,5 +97,10 @@ bool operator==(ParConstIterator const &
 
 bool operator!=(ParConstIterator const & iter1,
 		ParConstIterator const & iter2);
+
+
+ParConstIterator ParConstIteratorBegin(InsetBase const & inset);
+ParConstIterator ParConstIteratorEnd(InsetBase const & inset);
+
 
 #endif
Index: text3.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text3.C,v
retrieving revision 1.238
diff -u -p -r1.238 text3.C
--- text3.C	25 Mar 2004 09:16:26 -0000	1.238
+++ text3.C	29 Mar 2004 11:00:35 -0000
@@ -460,7 +460,7 @@ void LyXText::dispatch(LCursor & cur, Fu
 		break;
 
 	case LFUN_RIGHT:
-	case LFUN_RIGHTSEL:
+	case LFUN_RIGHTSEL: 
 		//lyxerr << "handle LFUN_RIGHT[SEL]:\n" << cur << endl;
 		cur.selHandle(cmd.action == LFUN_RIGHTSEL);
 		if (isRTL(cur.paragraph()))
Index: insets/insetfloat.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetfloat.C,v
retrieving revision 1.123
diff -u -p -r1.123 insetfloat.C
--- insets/insetfloat.C	28 Mar 2004 22:00:22 -0000	1.123
+++ insets/insetfloat.C	29 Mar 2004 11:00:37 -0000
@@ -379,8 +379,8 @@ void InsetFloat::wide(bool w, BufferPara
 
 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
 {
-	ParConstIterator pit(*this, 0);
-	ParConstIterator end = ParConstIterator(DocumentIterator());
+	ParConstIterator pit = ParConstIteratorBegin(*this);
+	ParConstIterator end = ParConstIteratorEnd(*this);
 
 	// Find a caption layout in one of the (child inset's) pars
 	for (; pit != end; ++pit) {

Reply via email to