bool pt_PieceTable::_lastUndoIsThisFmtMark(PT_DocPosition dpos)
{
	// look backwards thru the undo from this point and see
	// if we have <InsertFmtMark>[<ChangeFmtMark>*]

	PX_ChangeRecord * pcr;
	UT_uint32 undoNdx = 0;

	while (1)
	{
		bool bHaveUndo = m_history.getNthUndo(&pcr,undoNdx);

		if (!bHaveUndo)
			return false;
		if (!pcr)
			return false;
		if (pcr->getPosition() != dpos)
			return false;

		switch (pcr->getType())
		{
		default:
			return false;
		case PX_ChangeRecord::PXT_InsertFmtMark:
			return true;
		case PX_ChangeRecord::PXT_ChangeFmtMark:
			undoNdx++;
			break;
		}
	}

	UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
	return false;
}
void px_ChangeHistory::_printHistory(UT_sint32 iPrev) const
{
	UT_sint32 i = 0;
	UT_sint32 iStop = 0;
	UT_sint32 iStart = 0;
	if(iPrev>0)
	{
		iStop =m_undoPosition-1 - iPrev;
		iStart = m_undoPosition-1;
	}
	else
	{
		iStart = m_vecChangeRecords.getItemCount() -1;
		iStop = iStart + iPrev;
	}
	if(iStop <0)
		iStop =0;
	for(i=iStart; i>= iStop;i--)
	{
			PX_ChangeRecord * pcr = m_vecChangeRecords.getNthItem(i);
			if(i != (m_undoPosition-m_iAdjustOffset-1))
			{
					UT_DEBUGMSG((" loc %d pos %d type %d isLocal %d \n",i,pcr->getPosition(),pcr->getType(),pcr->isFromThisDoc()));
			}
			else
			{
					UT_DEBUGMSG((" loc %d pos %d type %d isLocal %d <- Current undo record \n",i,pcr->getPosition(),pcr->getType(),pcr->isFromThisDoc()));
			}
	}
}
void px_ChangeHistory::coalesceHistory(const PX_ChangeRecord * pcr)
{
	// coalesce this record with the current undo record.

	UT_sint32 iAdj = m_iAdjustOffset;
	PX_ChangeRecord * pcrUndo = m_vecChangeRecords.getNthItem(m_undoPosition-1);
	UT_return_if_fail (pcrUndo);
	UT_return_if_fail (pcr->getType() == pcrUndo->getType());

	switch (pcr->getType())
	{
		case PX_ChangeRecord::PXT_InsertSpan:
		case PX_ChangeRecord::PXT_DeleteSpan:
			{
				const PX_ChangeRecord_Span * pcrSpan = static_cast<const PX_ChangeRecord_Span *>(pcr);
				PX_ChangeRecord_Span * pcrSpanUndo = static_cast<PX_ChangeRecord_Span *>(pcrUndo);

				if(pcr->isFromThisDoc())
				{
				  _invalidateRedo();
				  m_iAdjustOffset = 0;
				}
				else if(iAdj > 0) 
				{
				    m_iAdjustOffset = iAdj - 1;
				    xxx_UT_DEBUGMSG(("AdjustOffset decremented - 3 %d ", m_iAdjustOffset));

				}
				pcrSpanUndo->coalesce(pcrSpan);
			}
			return;

		default:
			UT_ASSERT_HARMLESS(0);
			return;
	}
}
bool pt_PieceTable::_canCoalesceInsertSpan(PX_ChangeRecord_Span * pcrSpan) const
{
	// see if this record can be coalesced with the most recent undo record.

	UT_return_val_if_fail (pcrSpan->getType() == PX_ChangeRecord::PXT_InsertSpan, false);

	PX_ChangeRecord * pcrUndo;
	if (!m_history.getUndo(&pcrUndo,true))
		return false;
	if (pcrSpan->getType() != pcrUndo->getType())
		return false;
	if (pcrSpan->getIndexAP() != pcrUndo->getIndexAP())
		return false;

	PX_ChangeRecord_Span * pcrUndoSpan = static_cast<PX_ChangeRecord_Span *>(pcrUndo);
	if((pcrUndoSpan->isFromThisDoc() != pcrSpan->isFromThisDoc()))
	   return false;

	UT_uint32 lengthUndo = pcrUndoSpan->getLength();

	if ((pcrUndo->getPosition() + lengthUndo) != pcrSpan->getPosition())
		return false;

	PT_BufIndex biUndo = pcrUndoSpan->getBufIndex();
	PT_BufIndex biSpan = pcrSpan->getBufIndex();

	if (m_varset.getBufIndex(biUndo,lengthUndo) != biSpan)
		return false;

	// "Coalescing not allowed across a save." - PL
	// So, if we're clean, make us dirty.
	if (!m_history.isDirty())
		return false;

	return true;
}
bool px_ChangeHistory::getUndo(PX_ChangeRecord ** ppcr, bool bStatic) const
{
	if (m_bOverlap)
	{
		*ppcr = NULL;
		return false;
	}
	UT_sint32 iGLOB = 0;
	bool bGotOne = false;
	PX_ChangeRecord * pcr = NULL;
	PX_ChangeRecord * pcrFirst = NULL;
	bool bCorrect = false;
	UT_sint32 iAdjust = m_iAdjustOffset;
	UT_sint32 iLoop = 0;
	//	_printHistory(50);
	while (!bGotOne)
	{
		if ((m_undoPosition - m_iAdjustOffset -iLoop) <= m_iMinUndo)
		{
			if (bStatic)
				m_iAdjustOffset = iAdjust;
			return false;
		}
		
		pcr = m_vecChangeRecords.getNthItem(m_undoPosition-m_iAdjustOffset-1-iLoop);
		UT_return_val_if_fail(pcr, false); // just bail out, everything seems wrong

		//
		// Do Adjustments for blocks of remote CR's. Scan through local globs
		// to check for remote CR's which overlap it.
		//
		if((iGLOB== 0) && !pcr->isFromThisDoc())
		{
			bCorrect = true;
			m_iAdjustOffset++;
			UT_DEBUGMSG(("Doing undo iAdjust incremented to %d \n",m_iAdjustOffset));
		}
		else if ((iGLOB==0) && (pcr->getType() == PX_ChangeRecord::PXT_GlobMarker) && pcr->isFromThisDoc() && !isScanningUndoGLOB() && (m_iAdjustOffset > 0))
		{
			iGLOB++;
			pcrFirst = pcr;
			iLoop++;
			setScanningUndoGLOB(true);
		}
		else if((iGLOB>0) && (pcr->getType() == PX_ChangeRecord::PXT_GlobMarker) &&  pcr->isFromThisDoc())
		{
			if(isScanningUndoGLOB())
				pcr = pcrFirst;
			bGotOne = true;
		}
		else if(iGLOB == 0)
		{
			bGotOne = true;
			if(m_iAdjustOffset > 0)
				bCorrect = true;
		}
		//
		// we're here if we've started scanning through a glob in the local
		// document to see if it overlaps a later remote change.
		//
		else
		{
			PT_DocPosition low, high;
			PT_DocPosition lowWork = 0;
            PT_DocPosition highWork;
			UT_sint32 iAccumOffset = 0;
			getCRRange(pcr, low, high);
			for (UT_sint32 i = 0; i<m_iAdjustOffset;i++)
			{
				PX_ChangeRecord *pcrTmp = m_vecChangeRecords.getNthItem(m_undoPosition-i-1);
				if (!pcrTmp->isFromThisDoc())
				{
					UT_sint32 iCur = getDoc()->getAdjustmentForCR(pcrTmp);
					if(pcrTmp->getPosition() <= lowWork+iCur)
					{
						iAccumOffset += iCur;
					}
					lowWork = low + iAccumOffset;
					highWork = high + iAccumOffset;
					PT_DocPosition p1,p2;
					getCRRange(pcrTmp,p1,p2);
					bool bZero = (p1 == p2);
					if(bZero)
						lowWork++;
					if (doesOverlap(pcrTmp,lowWork,highWork))
					{
						*ppcr = NULL;
						//
						// OK now we have to invalidate the undo stack
						// to just before the first pcr we pulled off.
						//
						if(m_undoPosition-iAdjust > 0)
						{
							m_iMinUndo = m_undoPosition-iAdjust-1;
						}
						else
						{
							m_iMinUndo = 0;
						}
						m_iAdjustOffset = iAdjust;
						m_iAdjustOffset++;
						return false;
					}
				}
			}
			
			iLoop++;
		}
	}

	PX_ChangeRecord * pcrOrig = pcr;
	if (bCorrect)
	{
	    pcr->setAdjustment(0);
	    PT_DocPosition pos = pcr->getPosition();
	    UT_sint32 iAdj = 0;
		UT_sint32 iCurrAdj  = 0;
	    PT_DocPosition low, high;
	    getCRRange(pcr, low, high);
	    for (UT_sint32 i = m_iAdjustOffset-1; i>=0;i--)
	    {
			pcr = m_vecChangeRecords.getNthItem(m_undoPosition-i-1);
			if (!pcr->isFromThisDoc())
			{
				iCurrAdj = getDoc()->getAdjustmentForCR(pcr);
			    if(pcr->getPosition() <= static_cast<PT_DocPosition>(static_cast<UT_sint32>(pos) + iAdj + iCurrAdj))
			    {
					iAdj += iCurrAdj;
					low += iCurrAdj;
					high += iCurrAdj;
			    }
				PT_DocPosition p1,p2;
				getCRRange(pcr,p1,p2);
				bool bZero = (p1 == p2);
				PT_DocPosition low1 = low;
				if(bZero)
					low1++;
			    if (doesOverlap(pcr,low1,high))
			    {
					UT_DEBUGMSG(("CR Type %d adj pos %d Overlaps found with CR pos %d \n",pcrOrig->getType(),pcrOrig->getPosition()+iAdj,pcr->getPosition()));
					UT_DEBUGMSG((" Orig Adj low %d high %d \n",low,high));

					*ppcr = NULL;
					m_iMinUndo = m_undoPosition-m_iAdjustOffset-1;
					return false;
			    }
			}
	    }
	    pcrOrig->setAdjustment(iAdj);
	    m_iAdjustOffset++;
	}

	UT_ASSERT(pcrOrig->isFromThisDoc());
	*ppcr = pcrOrig;
	if(bStatic)
	    m_iAdjustOffset = iAdjust;
	return true;
}