>>>>> "Juergen" == Juergen Spitzmueller <[EMAIL PROTECTED]> writes:

Juergen> It seems that the special casing for empty paragraphs can
Juergen> indeed just be removed (see attached patch). But I'm not 100%
Juergen> sure if it doesn't have any side cases, and I don't have much
Juergen> time for testing.

OK, let's try a complete rewrite of the method backspacePos0 :)

I think the code is much clearer now, and would appreciate comments.
There are a few things that still need changing:

- does not work with change tracking. I need to look at that.

- I think we should get rid of the test

                if (prevcur.pos() != 0 
                    && prevpar.isSeparator(prevcur.pos() - 1))
                        --prevcur.pos();

  Does anyone know why it could be useful?

- something needs to be done for erase too.

I'd like to see lots of testing on that, since this is fragile code.
Note however, that the new code is much easier to understand, so I am
rather confident that it is correct.

JMarc

Index: src/text.C
===================================================================
--- src/text.C	(revision 15295)
+++ src/text.C	(working copy)
@@ -1622,73 +1622,48 @@ bool LyXText::Delete(LCursor & cur)
 bool LyXText::backspacePos0(LCursor & cur)
 {
 	BOOST_ASSERT(this == cur.text());
+	if (cur.pit() == 0)
+		return false;
+
 	bool needsUpdate = false;
 
-	Paragraph & par = cur.paragraph();
+	BufferParams const & bufparams = cur.buffer().params();
+	LyXTextClass const & tclass = bufparams.getLyXTextClass();
+	ParagraphList & plist = cur.text()->paragraphs();
+	Paragraph const & par = cur.paragraph();
+	LCursor prevcur = cur;
+	--prevcur.pit();
+	prevcur.pos() = prevcur.lastpos();
+	Paragraph const & prevpar = prevcur.paragraph();
+
 	// is it an empty paragraph?
-	pos_type lastpos = cur.lastpos();
+	const pos_type lastpos = cur.lastpos();
 	if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
-		// This is an empty paragraph and we delete it just
-		// by moving the cursor one step
-		// left and let the DeleteEmptyParagraphMechanism
-		// handle the actual deletion of the paragraph.
-
-		if (cur.pit() != 0) {
-			// For KeepEmpty layouts we need to get
-			// rid of the keepEmpty setting first.
-			// And the only way to do this is to
-			// reset the layout to something
-			// else: f.ex. the default layout.
-			if (par.allowEmpty()) {
-				Buffer & buf = cur.buffer();
-				BufferParams const & bparams = buf.params();
-				par.layout(bparams.getLyXTextClass().defaultLayout());
-			}
-                                
-			cursorLeft(cur);
-			return true;
-		}
-	}
-
-	if (cur.pit() != 0)
-		recordUndo(cur, Undo::DELETE, cur.pit() - 1);
-
-	pit_type tmppit = cur.pit();
-	// We used to do cursorLeftIntern() here, but it is
-	// not a good idea since it triggers the auto-delete
-	// mechanism. So we do a cursorLeftIntern()-lite,
-	// without the dreaded mechanism. (JMarc)
-	if (cur.pit() != 0) {
-		// steps into the above paragraph.
-		setCursorIntern(cur, cur.pit() - 1,
-				pars_[cur.pit() - 1].size(),
-				false);
+		recordUndo(cur, Undo::ATOMIC, prevcur.pit());
+		plist.erase(boost::next(plist.begin(), cur.pit()));
+		needsUpdate = true;
 	}
-
 	// Pasting is not allowed, if the paragraphs have different
 	// layout. I think it is a real bug of all other
 	// word processors to allow it. It confuses the user.
 	// Correction: Pasting is always allowed with standard-layout
-	// Correction (Jug 20050717): Remove check about alignment!
-	Buffer & buf = cur.buffer();
-	BufferParams const & bufparams = buf.params();
-	LyXTextClass const & tclass = bufparams.getLyXTextClass();
-	pit_type const cpit = cur.pit();
-
-	if (cpit != tmppit
-	    && (pars_[cpit].layout() == pars_[tmppit].layout()
-	        || pars_[tmppit].layout() == tclass.defaultLayout()))
-	{
-		mergeParagraph(bufparams, pars_, cpit);
+	else if (par.layout() == prevpar.layout()
+		 || par.layout() == tclass.defaultLayout()) {
+		recordUndo(cur, Undo::ATOMIC, prevcur.pit());
+		mergeParagraph(bufparams, plist, prevcur.pit());
 		needsUpdate = true;
+	}
 
-		if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
-				--cur.pos();
+	if (needsUpdate) {
+		if (prevcur.pos() != 0 
+		    && prevpar.isSeparator(prevcur.pos() - 1))
+			--prevcur.pos();
 
 		// the counters may have changed
 		updateCounters(cur.buffer());
-		setCursor(cur, cur.pit(), cur.pos(), false);
+		setCursorIntern(cur, prevcur.pit(), prevcur.pos(), false);
 	}
+
 	return needsUpdate;
 }
 
@@ -1705,7 +1680,7 @@ bool LyXText::backspace(LCursor & cur)
 		// the the backspace will collapse two paragraphs into
 		// one.
 
-		if (cur.pit() != 0 && cur.buffer().params().tracking_changes) {
+		if (cur.buffer().params().tracking_changes) {
 			// Previous paragraph, mark "carriage return" as
 			// deleted:
 			Paragraph & par = pars_[cur.pit() - 1];

Reply via email to