/*!
        \brief Insert some text
        \param line target line
        \param pos target text position within line
        \param s text to insert
        
        This helper method is provided so that subclasses may actually
        modify the document contents without using private API.
*/
void QDocumentCommand::insertText(int line, int pos, const QString& s)
{
        if ( !m_doc )
                return;
        
        QDocumentPrivate *pd = m_doc->impl();
        QDocumentLineHandle *h = pd->m_lines.at(line);
        
        if ( !h )
                return;
        
        h->textBuffer().insert(pos, s);
        h->shiftOverlays(pos, s.length());
        /*
        foreach ( QDocumentCursorHandle *ch, m_autoUpdated )
        {
                if ( ch == m_cursor || ch->document() != m_doc )
                        continue;
                
                if ( (pd->at(ch->m_begLine) == h) && (ch->m_begOffset >= pos) )
                {
                        if ( !(pd->at(ch->m_endLine) == h) || !(ch->m_endOffset >= pos) )
                                ch->m_begOffset += s.length();
                }
                
                if ( (pd->at(ch->m_endLine) == h) && (ch->m_endOffset >= pos) )
                {
                        ch->m_endOffset += s.length();
                }
        }
        */
        pd->adjustWidth(line);
}
/*!
	\brief Remove some text
	\param line target line
	\param pos target text position within line
	\param length length of the text to remove

	This helper method is provided so that subclasses may actually
	modify the document contents without using private API.
*/
void QDocumentCommand::removeText(int line, int pos, int length)
{
	if ( !m_doc )
		return;

	QDocumentPrivate *pd = m_doc->impl();
	QDocumentLineHandle *h = pd->m_lines.at(line);

	if ( !h || !length )
		return;

	h->lockForWriteText();
	h->textBuffer().remove(pos, length);
	h->shiftOverlays(pos, -length);
	h->unlock();

	pd->adjustWidth(line);
}
/*!
	\brief Insert some text
	\param line target line
	\param pos target text position within line
	\param s text to insert

	This helper method is provided so that subclasses may actually
	modify the document contents without using private API.
*/
void QDocumentCommand::insertText(int line, int pos, const QString& s)
{
	if ( !m_doc )
		return;

	QDocumentPrivate *pd = m_doc->impl();
	QDocumentLineHandle *h = pd->m_lines.at(line);

	if ( !h )
		return;
	
	h->lockForWriteText();
	h->textBuffer().insert(pos, s);
	h->shiftOverlays(pos, s.length());
	h->unlock();
	
	pd->adjustWidth(line);
}
/*!
        \brief Remove some text
        \param line target line
        \param pos target text position within line
        \param length length of the text to remove
        
        This helper method is provided so that subclasses may actually
        modify the document contents without using private API.
*/
void QDocumentCommand::removeText(int line, int pos, int length)
{
        if ( !m_doc )
                return;
        
        QDocumentPrivate *pd = m_doc->impl();
        QDocumentLineHandle *h = pd->m_lines.at(line);
        
        if ( !h || !length )
                return;
        
        h->textBuffer().remove(pos, length);
        h->shiftOverlays(pos, -length);
        /*
        QList<QDocumentCursorHandle*> m_del;
        
        foreach ( QDocumentCursorHandle *ch, m_autoUpdated )
        {
                if ( ch == m_cursor || ch->document() != m_doc )
                        continue;
                
                if ( ch->hasSelection() )
                {
                        int lbeg = line, cbeg = pos, lend = line, cend = pos + length;
                        
                        ch->substractBoundaries(lbeg, cbeg, lend, cend);
                } else {
                        if ( pd->at(ch->m_begLine) == h )
                        {
                                if ( ch->m_begOffset < pos )
                                        continue;
                                
                                if ( ch->m_begOffset >= (pos + length) )
                                {
                                        ch->m_begOffset -= length;
                                } else {
                                        // TODO : what if only part of a selection disappeared???
                                        // cursor caught in a deleted text..
                                        ch->moveTo(QDocumentCursor());
                                        m_del << ch;
                                }
                        }
                        
                        if ( pd->at(ch->m_endLine) == h )
                        {
                                if ( ch->m_endOffset < pos )
                                        continue;
                                
                                if ( ch->m_endOffset >= (pos + length) )
                                {
                                        ch->m_endOffset -= length;
                                } else {
                                        // TODO : what if only part of a selection disappeared???
                                        // cursor caught in a deleted text..
                                        ch->moveTo(QDocumentCursor());
                                        m_del << ch;
                                }
                        }
                }
        }
        
        foreach ( QDocumentCursorHandle *ch, m_del )
        {
                //qDebug("hard-no-up(0x%x)", h);
                //m_autoUpdated.removeAll(ch);
        }
        */

        pd->adjustWidth(line);
}