Georg Baum wrote:
Am Montag, 21. Februar 2005 09:22 schrieb Andre Poenitz:
The only change that seems to make all the difference is this:

#ifdef SLOW_forwardPar
 ++tip.pos();
#else
 tip.pos() = lastp;
#endif

Yes, but this is not safe if there insets in the paragraph before lastp, I think.

Here is a simpler try that does mostly what you propose, but uses
forwardPos() to do the hard work. I kept the old code around (see the
#if 0) to be able to compare the results with the old slow method.

I will continue some testing, but basically it sees to work.

Testing welcome.

JMarc



Index: lib/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/ChangeLog,v
retrieving revision 1.676
diff -u -r1.676 ChangeLog
--- lib/ChangeLog	22 Feb 2005 09:15:06 -0000	1.676
+++ lib/ChangeLog	23 Feb 2005 22:24:15 -0000
@@ -1,3 +1,7 @@
+2005-02-20  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
+
+	* layouts/stdlayouts.inc: change labelstring to "Senseless!"
+
 2005-02-21  Angus Leeming  <[EMAIL PROTECTED]>
 
 	* CREDITS: add some missing pretty printing info.
Index: lib/layouts/stdlayouts.inc
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/layouts/stdlayouts.inc,v
retrieving revision 1.8
diff -u -r1.8 stdlayouts.inc
--- lib/layouts/stdlayouts.inc	13 Oct 2003 09:50:10 -0000	1.8
+++ lib/layouts/stdlayouts.inc	23 Feb 2005 22:24:16 -0000
@@ -71,7 +71,7 @@
 	Align                 Center
 	AlignPossible         Center
 	LabelType             Sensitive
-	LabelString   Caption
+	LabelString	      "Senseless!"
 	OptionalArgs          1
 	LabelFont
 	  Series              Bold
Index: src/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ChangeLog,v
retrieving revision 1.2128
diff -u -r1.2128 ChangeLog
--- src/ChangeLog	23 Feb 2005 16:56:40 -0000	1.2128
+++ src/ChangeLog	23 Feb 2005 22:25:08 -0000
@@ -1,3 +1,21 @@
+2005-02-23  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
+
+	* cursor.C: remove unused variable
+
+	* text3.C (dispatch): 
+	* text.C (breakParagraph, backspace): 
+	* CutAndPaste.C (cutSelection, pasteSelection): pass a buffer to
+	updateCounters.
+
+	* text2.C (updateCounters): turn into a free standing function and
+	add a buffer and dociterator parameters. Remove dead code for
+	tracking labelstring change.
+	(init, setLayout, changeDepth): pass a buffer to updateCounters.
+	(setCounter): change into a free-standing function which gets a
+	dociterator as argument. Use this iterator to fix captions in a
+	simple way. When no float is found above the caption, use the
+	labelstring of the caption layout as default.
+
 2005-02-23  Georg Baum  <[EMAIL PROTECTED]>
 
 	* LaTeX.C (scanLogFile): recognize pdfTeX warnings
Index: src/CutAndPaste.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/CutAndPaste.C,v
retrieving revision 1.148
diff -u -r1.148 CutAndPaste.C
--- src/CutAndPaste.C	8 Feb 2005 13:17:56 -0000	1.148
+++ src/CutAndPaste.C	23 Feb 2005 22:25:08 -0000
@@ -503,7 +503,7 @@
 
 		// need a valid cursor. (Lgb)
 		cur.clearSelection();
-		text->updateCounters();
+		updateCounters(cur.buffer());
 	}
 
 	if (cur.inMathed()) {
@@ -596,7 +596,7 @@
 		cur.resetAnchor();
 		text->setCursor(cur, ppp.first, ppp.second);
 		cur.setSelection();
-		text->updateCounters();
+		updateCounters(cur.buffer());
 	}
 
 	if (cur.inMathed()) {
Index: src/cursor.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/cursor.C,v
retrieving revision 1.122
diff -u -r1.122 cursor.C
--- src/cursor.C	15 Feb 2005 17:34:53 -0000	1.122
+++ src/cursor.C	23 Feb 2005 22:25:11 -0000
@@ -123,8 +123,6 @@
 	{
 		BOOST_ASSERT(!cursor.empty());
 		CursorSlice bottom = cursor[0];
-		LyXText * text = bottom.text();
-		BOOST_ASSERT(text);
 
 		DocIterator it = doc_iterator_begin(bottom.inset());
 		DocIterator const et = doc_iterator_end(bottom.inset());
Index: src/dociterator.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/dociterator.C,v
retrieving revision 1.24
diff -u -r1.24 dociterator.C
--- src/dociterator.C	14 Feb 2005 08:17:23 -0000	1.24
+++ src/dociterator.C	23 Feb 2005 22:25:15 -0000
@@ -382,8 +382,27 @@
 void DocIterator::forwardPar()
 {
 	forwardPos();
-	while (!empty() && (!inTexted() || pos() != 0))
+
+#if 0
+	DocIterator cmp(*this);
+#endif
+	
+	while (!empty() && (!inTexted() || pos() != 0)) {
+		if (inTexted()) {
+			pos_type const lastp = lastpos();
+			Paragraph const & par = paragraph();
+			pos_type & pos = top().pos();
+			while (pos < lastp && !par.isInset(pos))
+				++pos;
+		}
 		forwardPos();
+	}
+
+#if 0
+	while (!cmp.empty() && (!cmp.inTexted() || cmp.pos() != 0))
+		cmp.forwardPos();
+	BOOST_ASSERT(cmp == *this);
+#endif
 }
 
 
Index: src/lyxtext.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxtext.h,v
retrieving revision 1.319
diff -u -r1.319 lyxtext.h
--- src/lyxtext.h	22 Feb 2005 11:41:22 -0000	1.319
+++ src/lyxtext.h	23 Feb 2005 22:25:15 -0000
@@ -261,8 +261,6 @@
 	/// current text heigth
 	int height() const;
 
-	/// updates all counters
-	void updateCounters();
 	/// Returns an inset if inset was hit, or 0 if not.
 	InsetBase * checkInsetHit(int x, int y) const;
 
@@ -375,8 +373,6 @@
 	bool deleteEmptyParagraphMechanism(LCursor & cur, LCursor const & old);
 
 	///
-	void setCounter(Buffer const &, pit_type pit);
-	///
 	void deleteWordForward(LCursor & cur);
 	///
 	void deleteWordBackward(LCursor & cur);
@@ -405,6 +401,10 @@
 ///
 std::string expandLabel(LyXTextClass const & textclass,
 		LyXLayout_ptr const & layout, bool appendix);
+
+/// updates all counters
+void updateCounters(Buffer const &);
+
 
 
 #endif // LYXTEXT_H
Index: src/text.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text.C,v
retrieving revision 1.594
diff -u -r1.594 text.C
--- src/text.C	8 Feb 2005 02:06:35 -0000	1.594
+++ src/text.C	23 Feb 2005 22:25:21 -0000
@@ -1074,7 +1074,7 @@
 	while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
 		pars_[next_par].erase(0);
 
-	updateCounters();
+	updateCounters(cur.buffer());
 
 	// This check is necessary. Otherwise the new empty paragraph will
 	// be deleted automatically. And it is more friendly for the user!
@@ -1609,7 +1609,7 @@
 				--cur.pos();
 
 			// the counters may have changed
-			updateCounters();
+			updateCounters(cur.buffer());
 			setCursor(cur, cur.pit(), cur.pos(), false);
 		}
 	} else {
Index: src/text2.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text2.C,v
retrieving revision 1.600
diff -u -r1.600 text2.C
--- src/text2.C	14 Feb 2005 08:17:23 -0000	1.600
+++ src/text2.C	23 Feb 2005 22:25:24 -0000
@@ -45,6 +45,7 @@
 #include "paragraph.h"
 #include "paragraph_funcs.h"
 #include "ParagraphParameters.h"
+#include "pariterator.h"
 #include "undo.h"
 #include "vspace.h"
 
@@ -67,6 +68,7 @@
 using lyx::support::bformat;
 
 using std::endl;
+using std::min;
 using std::ostringstream;
 using std::string;
 
@@ -93,7 +95,7 @@
 		pars_[pit].rows().clear();
 
 	current_font = getFont(pars_[0], 0);
-	updateCounters();
+	updateCounters(*bv->buffer());
 }
 
 
@@ -337,7 +339,7 @@
 	pit_type start = cur.selBegin().pit();
 	pit_type end = cur.selEnd().pit() + 1;
 	setLayout(start, end, layout);
-	updateCounters();
+	updateCounters(cur.buffer());
 }
 
 
@@ -398,7 +400,7 @@
 	}
 	// this handles the counter labels, and also fixes up
 	// depth values for follow-on (child) paragraphs
-	updateCounters();
+	updateCounters(cur.buffer());
 }
 
 
@@ -677,13 +679,11 @@
 	}
 }
 
-} // anon namespace
-
 
 // set the counter of a paragraph. This includes the labels
-void LyXText::setCounter(Buffer const & buf, pit_type pit)
+void setCounter(Buffer const & buf, ParIterator & it)
 {
-	Paragraph & par = pars_[pit];
+	Paragraph & par = *it;
 	BufferParams const & bufparams = buf.params();
 	LyXTextClass const & textclass = bufparams.getLyXTextClass();
 	LyXLayout_ptr const & layout = par.layout();
@@ -692,10 +692,10 @@
 	// Always reset
 	par.itemdepth = 0;
 
-	if (pit == 0) {
+	if (it.pit() == 0) {
 		par.params().appendix(par.params().startOfAppendix());
 	} else {
-		par.params().appendix(pars_[pit - 1].params().appendix());
+		par.params().appendix(it.plist()[it.pit() - 1].params().appendix());
 		if (!par.params().appendix() &&
 		    par.params().startOfAppendix()) {
 			par.params().appendix(true);
@@ -703,7 +703,7 @@
 		}
 
 		// Maybe we have to increment the item depth.
-		incrementItemDepth(pars_, pit, 0);
+		incrementItemDepth(it.plist(), it.pit(), 0);
 	}
 
 	// erase what was there before
@@ -718,8 +718,6 @@
 
 	// is it a layout that has an automatic label?
 	if (layout->labeltype == LABEL_COUNTER) {
-		BufferParams const & bufparams = buf.params();
-		LyXTextClass const & textclass = bufparams.getLyXTextClass();
 		counters.step(layout->counter);
 		string label = expandLabel(textclass, layout, par.params().appendix());
 		par.params().labelString(label);
@@ -748,7 +746,7 @@
 		par.params().labelString(itemlabel);
 	} else if (layout->labeltype == LABEL_ENUMERATE) {
 		// Maybe we have to reset the enumeration counter.
-		resetEnumCounterIfNeeded(pars_, pit, 0, counters);
+		resetEnumCounterIfNeeded(it.plist(), it.pit(), 0, counters);
 
 		// FIXME
 		// Yes I know this is a really, really! bad solution
@@ -782,100 +780,56 @@
 			par.params().labelString(layout->labelstring());
 		}
 		// In biblio should't be following counters but...
-	} else {
-		string s = buf.B_(layout->labelstring());
-
-		// the caption hack:
-		if (layout->labeltype == LABEL_SENSITIVE) {
-			pit_type end = paragraphs().size();
-			pit_type tmppit = pit;
-			InsetBase * in = 0;
-			bool isOK = false;
-			while (tmppit != end) {
-				in = pars_[tmppit].inInset();
-				// FIXME: in should be always valid.
-				if (in &&
-				    (in->lyxCode() == InsetBase::FLOAT_CODE ||
-				     in->lyxCode() == InsetBase::WRAP_CODE)) {
-					isOK = true;
-					break;
-				}
-#ifdef WITH_WARNINGS
-#warning replace this code by something that works
-// This code does not work because we have currently no way to move up
-// in the hierarchy of insets (JMarc 16/08/2004)
-#endif
-#if 0
-/* I think this code is supposed to be useful when one has a caption
- * in a minipage in a figure inset. We need to go up to be able to see
- * that the caption should use "Figure" as label
- */
-				else {
-					Paragraph const * owner = &ownerPar(buf, in);
-					tmppit = 0;
-					for ( ; tmppit != end; ++tmppit)
-						if (&pars_[tmppit] == owner)
-							break;
-				}
-#else
-				++tmppit;
-#endif
-			}
-
-			if (isOK) {
-				string type;
+	} else if (layout->labeltype == LABEL_SENSITIVE) {
+		// Search for the first float or wrap inset in the iterator
+		string type;
+		size_t i = it.depth();
+		while (i > 0) {
+			--i;
+			InsetBase * const in = &it[i].inset();
+			if (in->lyxCode() == InsetBase::FLOAT_CODE
+			    || in->lyxCode() == InsetBase::WRAP_CODE) 
+				type = in->getInsetName();
+				break;
+		}
 
-				if (in->lyxCode() == InsetBase::FLOAT_CODE)
-					type = static_cast<InsetFloat*>(in)->params().type;
-				else if (in->lyxCode() == InsetBase::WRAP_CODE)
-					type = static_cast<InsetWrap*>(in)->params().type;
-				else
-					BOOST_ASSERT(false);
-
-				Floating const & fl = textclass.floats().getType(type);
-
-				counters.step(fl.type());
-
-				// Doesn't work... yet.
-				s = bformat(_("%1$s #:"), buf.B_(fl.name()));
-			} else {
-				// par->SetLayout(0);
-				// s = layout->labelstring;
-				s = _("Senseless: ");
-			}
+		string s;
+		if (!type.empty()) {
+			Floating const & fl = textclass.floats().getType(type);
+
+			counters.step(fl.type());
+
+			// Doesn't work... yet.
+			s = bformat(_("%1$s #:"), buf.B_(fl.name()));
+		} else {
+			// par->SetLayout(0);
+			s = buf.B_(layout->labelstring());
 		}
-		par.params().labelString(s);
 
-	}
+		par.params().labelString(s);
+	} else
+		par.params().labelString(buf.B_(layout->labelstring()));
 }
 
+} // anon namespace
+
 
-// Updates all counters.
-void LyXText::updateCounters()
+void updateCounters(Buffer const & buf)
 {
 	// start over
-	bv()->buffer()->params().getLyXTextClass().counters().reset();
+	buf.params().getLyXTextClass().counters().reset();
 
-	bool update_pos = false;
+	for (ParIterator it = par_iterator_begin(buf.inset()); it; ++it) {
+		// reduce depth if necessary
+		if (it.pit()) {
+			Paragraph const & prevpar = it.plist()[it.pit() - 1];
+			it->params().depth(min(it->params().depth(),
+					       prevpar.getMaxDepthAfter()));
+		} else
+			it->params().depth(0);
 
-	pit_type end = paragraphs().size();
-	for (pit_type pit = 0; pit != end; ++pit) {
-		string const oldLabel = pars_[pit].params().labelString();
-		size_t maxdepth = 0;
-		if (pit != 0)
-			maxdepth = pars_[pit - 1].getMaxDepthAfter();
-
-		if (pars_[pit].params().depth() > maxdepth)
-			pars_[pit].params().depth(maxdepth);
-
-		// setCounter can potentially change the labelString.
-		setCounter(*bv()->buffer(), pit);
-		string const & newLabel = pars_[pit].params().labelString();
-		if (oldLabel != newLabel) {
-			//lyxerr[Debug::DEBUG] << "changing labels: old: " << oldLabel << " new: "
-			//	<< newLabel << endl;
-			update_pos = true;
-		}
+		// set the counter for this paragraph
+		setCounter(buf, it);
 	}
 }
 
Index: src/text3.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/text3.C,v
retrieving revision 1.284
diff -u -r1.284 text3.C
--- src/text3.C	22 Feb 2005 11:41:22 -0000	1.284
+++ src/text3.C	23 Feb 2005 22:25:26 -0000
@@ -297,6 +297,10 @@
 		Paragraph & par = cur.paragraph();
 		bool start = !par.params().startOfAppendix();
 
+#ifdef WITH_WARNINGS
+#warning The code below only makes sense a top level.
+// Should LFUN_APPENDIX be restricted to top-level paragraphs?
+#endif
 		// ensure that we have only one start_of_appendix in this document
 		for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
 			if (pars_[tmp].params().startOfAppendix()) {
@@ -310,7 +314,7 @@
 		par.params().startOfAppendix(start);
 
 		// we can set the refreshing parameters now
-		updateCounters();
+		updateCounters(cur.buffer());
 		break;
 	}
 

Reply via email to