File: qdocumentcommand.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (1064 lines) | stat: -rw-r--r-- 30,144 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qdocumentcommand.h"

/*!
	\file qdocumentcommand.cpp
	\brief Implementation of the QDocumentCommand class and its basic heirs.
*/

#include "qdocument_p.h"

#include "stdint.h"

/*!
	\ingroup document
	@{
*/

/*!
	\class QDocumentCommand
	\brief The base class for document editing command
*/


/*!
	\brief ctor
*/
QDocumentCommand::QDocumentCommand(Command c, QDocument *d, QDocumentCommand *p)
 : QUndoCommand(p),
	m_state(false), m_first(true), m_doc(d),
	m_redoOffset(0), m_undoOffset(0),
	m_silent(false), m_keepAnchor(false), m_command(c), m_cursor(0)
{

}

QStringList QDocumentCommand::debugRepresentation() const {
	return QStringList() << "UNKNOWN COMMAND";
}

/*!
	\brief dtor
*/
QDocumentCommand::~QDocumentCommand()
{
	if ( m_cursor )
	{
		// release the handle
		m_cursor->deref();
	}
}

/*!
	\return command identifier
*/
int QDocumentCommand::id() const
{
	return m_command;
}

/*!
	\brief Attempts to merge with another command

	Command merging is not implemented.
*/
bool QDocumentCommand::mergeWith(const QUndoCommand */*command*/)
{
	return false;
}

/*!
	\brief Redo the command
*/
void QDocumentCommand::redo()
{
	QUndoCommand::redo();
}

/*!
	\brief Undo the command
*/
void QDocumentCommand::undo()
{
	QUndoCommand::undo();
}

/*!
	\return whether the command is silent

	Silent command do not update the editing cursor of the host document.
*/
bool QDocumentCommand::isSilent() const
{
	return m_silent;
}

/*!
	\brief Set whether the command is silent
*/
void QDocumentCommand::setSilent(bool y)
{
	m_silent = y;
}

/*!
	\return whether the command preserve selection of the target cursor
	
	When this property is true, cursor adjustement upon command execution
	will preserve the anchor of target cursor and only alter its position
	thus keeping a selection.
	
	\note This is disabled by default
*/
bool QDocumentCommand::keepAnchor() const
{
	return m_keepAnchor;
}

/*!
	\brief Set whether the command preserve selection of the target cursor
	
	\note This is disabled by default
*/
void QDocumentCommand::setKeepAnchor(bool y)
{
	m_keepAnchor = y;
}

/*!
	\brief Set the target cursor

	The position of the target cursor is update upon undo() and redo()
*/
void QDocumentCommand::setTargetCursor(QDocumentCursorHandle *h)
{
	if ( m_cursor )
	{
		// release the handle
		m_cursor->deref();
	}

	m_cursor = h;

	if ( m_cursor )
	{
		// make sure the handle does not get deleted while the command knows it
		m_cursor->ref();
	}
}

/*!
	\brief ?
*/
void QDocumentCommand::setRedoOffset(int off)
{
	m_redoOffset = off;
}

/*!
	\brief ?
*/
void QDocumentCommand::setUndoOffset(int off)
{
	m_undoOffset = off;
}

/*!
	\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();
}

/*!
	\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();
}

/*!
	\brief Insert some lines in the host document
	\param after where to insert lines (line number)
	\param l list of lines to insert

	This helper method is provided so that subclasses may actually
	modify the document contents without using too much private API
	(QDocumentLineHandle is part of the private API...)
*/
void QDocumentCommand::insertLines(int after, const QList<QDocumentLineHandle*>& l)
{
	if ( l.isEmpty() || !m_doc->impl()->at(after) )
		return;

	m_doc->impl()->insertLines(after, l);
}

inline void growPosOnInsertion(int *moveLine, int *moveOffset, int line, int column, int prefixLength, int numLines, int suffixLength){
	if ( numLines )
	{
		if ( *moveLine == line )
			*moveOffset -= column;
		*moveLine += numLines;
		*moveOffset += suffixLength;
	} else if ( *moveLine == line ) {
		*moveOffset += prefixLength;
	}
}

inline void movePosOnInsertion(int *moveLine, int *moveOffset, int line, int column, int prefixLength, int numLines, int suffixLength){
	if ( *moveLine > line )
	{
		*moveLine += numLines;
	} else if ( *moveLine == line && *moveOffset >= column ) {
		if ( numLines )
		{
			*moveLine += numLines;
			*moveOffset -= column;
			*moveOffset += suffixLength;
		} else {
			*moveOffset += prefixLength;
		}
	}
}

void QDocumentCommand::updateCursorsOnInsertion(int line, int column, int prefixLength, int numLines, int suffixLength)
{
	//qDebug("inserting %i lines at (%i, %i) with (%i : %i) bounds", numLines, line, column, prefixLength, suffixLength);
	foreach ( QDocumentCursorHandle *ch, m_doc->impl()->m_autoUpdatedCursorList )
	{
		if ( ch == m_cursor || ch->document() != m_doc )
			continue;

		//printf("[[watch:0x%x(%i, %i)]]", ch, ch->m_begLine, ch->m_begOffset);

		// TODO : better selection handling
		if ( ch->hasSelection() )
		{
			if (ch->hasFlag(QDocumentCursorHandle::AutoUpdateKeepBegin) &&
			    line == ch->m_begLine && column == ch->m_begOffset) {
				//movePosOnInsertion(&ch->m_begLine, &ch->m_begOffset, line, column, prefixLength, numLines, suffixLength);
				//movePosOnInsertion(&ch->m_endLine, &ch->m_endOffset, line, column, prefixLength, numLines, suffixLength);
				continue;
			}
			if (ch->hasFlag(QDocumentCursorHandle::AutoUpdateKeepEnd) &&
			    line == ch->m_endLine && column == ch->m_endOffset) {
				movePosOnInsertion(&ch->m_begLine, &ch->m_begOffset, line, column, prefixLength, numLines, suffixLength);
				movePosOnInsertion(&ch->m_endLine, &ch->m_endOffset, line, column, prefixLength, numLines, suffixLength);
				continue;
			}
			int lbeg = line, cbeg = column, lend = line, cend = column;

			ch->intersectBoundaries(lbeg, cbeg, lend, cend);

			if ( lbeg == line && cbeg == column )
			{
				//qDebug("expand (%i, %i : %i, %i)", ch->m_begLine, ch->m_begOffset, ch->m_endLine, ch->m_endOffset);
				if ( (ch->m_begLine > ch->m_endLine) || (ch->m_begLine == ch->m_endLine && ch->m_begOffset >= ch->m_endOffset) )
					growPosOnInsertion(&ch->m_begLine, &ch->m_begOffset, line, column, prefixLength, numLines, suffixLength);
				 else
					growPosOnInsertion(&ch->m_endLine, &ch->m_endOffset, line, column, prefixLength, numLines, suffixLength);
				//qDebug("into (%i, %i : %i, %i)", ch->m_begLine, ch->m_begOffset, ch->m_endLine, ch->m_endOffset);
				continue;
			}
		}

		// move
		movePosOnInsertion(&ch->m_begLine, &ch->m_begOffset, line, column, prefixLength, numLines, suffixLength);
		movePosOnInsertion(&ch->m_endLine, &ch->m_endOffset, line, column, prefixLength, numLines, suffixLength);
	}
}

void QDocumentCommand::updateCursorsOnDeletion(int line, int column, int prefixLength, int numLines, int suffixLength)
{
//	qDebug("removing %i lines at (%i, %i) with (%i : %i) bounds", numLines, line, column, prefixLength, suffixLength);
//erstes zeichen einer zeile: warning: removing 0 lines at (12, 0) with (1 : -1) bounds
//zeile mit 10 textzeichen: warning: removing 1 lines at (13, 0) with (10 : 0) bounds
	foreach ( QDocumentCursorHandle *ch, m_doc->impl()->m_autoUpdatedCursorList )
	{
		if ( ch == m_cursor || ch->document() != m_doc)
			continue;

	//	qDebug("[[watch:0x%x(%i, %i)]]", ch, ch->m_begLine, ch->m_begOffset);
		int columnEnd = (numLines >= 1) ? suffixLength : column + prefixLength;
		// TODO : better selection handling
		if ( ch->hasSelection() )
		{
			int lbeg = line;
			int cbeg = column;
			int lend = line + numLines;
			int cend = columnEnd;

			
			ch->intersectBoundaries(lbeg, cbeg, lend, cend);
			//qDebug("intersection (%i, %i : %i, %i)", lbeg, cbeg, lend, cend);
			if ( lbeg != -1 && cbeg != -1 && lend != -1 && cend != -1 )
			{
				//qDebug("shrink (%i, %i : %i, %i)", ch->m_begLine, ch->m_begOffset, ch->m_endLine, ch->m_endOffset);
				//qDebug("of intersection (%i, %i : %i, %i)", lbeg, cbeg, lend, cend);
				if (ch->isAutoErasable() && 
				    (lbeg > line || (lbeg == line && cbeg > column)) &&
					(lend < line+numLines || (lend == line+numLines && cend < columnEnd))) 
				{	
					//intersection is complete within the removed range without touching the bordes
					//=> cursor is completely removed and thus invalid
					ch->m_begLine = ch->m_endLine = -1;
					ch->m_begOffset = ch->m_endOffset = -1;
				} else {				
					ch->substractBoundaries(line, column, line+numLines,columnEnd);
					//lbeg, cbeg, lend, cend);
				}
				//qDebug("into (%i, %i : %i, %i)", ch->m_begLine, ch->m_begOffset, ch->m_endLine, ch->m_endOffset);
				continue;
			}
		}
		
		int cend = columnEnd;
		// move
		if ( ch->m_begLine > line + numLines )
		{
			ch->m_begLine -= numLines;
		} else if ( ch->m_begLine == line + numLines && ch->m_begOffset >= cend ) {
			if ( numLines )
			{
				ch->m_begLine -= numLines;
				ch->m_begOffset -= suffixLength;
				ch->m_begOffset += column;
			} else {
				ch->m_begOffset -= prefixLength;
			}
		} else if ( ch->m_begLine > line || (ch->m_begLine == line && ch->m_begOffset > column) ) {
			// cursor will become invalid in an unrecoverable way...

		}

		if ( ch->m_endLine > line + numLines )
		{
			ch->m_endLine -= numLines;
		} else if ( ch->m_endLine == line + numLines && ch->m_endOffset >= cend ) {
			if ( numLines )
			{
				ch->m_endLine -= numLines;
				ch->m_endOffset -= suffixLength;
				ch->m_endOffset += column;
			} else {
				ch->m_endOffset -= prefixLength;
			}
		} else if ( ch->m_endLine > line || (ch->m_endLine == line && ch->m_endOffset > column) ) {
			// cursor will become invalid in an unrecoverable way...
			// except that it should have intersected in the first place...
		}
	}
}


/*!
	\brief Remove some lines from the host document
	\param after where to remove lines (line number)
	\param n number of lines to remove

	This helper method is provided so that subclasses may actually
	modify the document contents without using the private API.
*/
void QDocumentCommand::removeLines(int after, int n)
{
	if ( n <= 0 || !m_doc->impl()->at(after) || !m_doc->impl()->at(after + n) )
		return;

	m_doc->impl()->removeLines(after, n);
}

/*!
	\brief Update the target cursor
	\param l target line
	\param offset target text position within target line
*/
void QDocumentCommand::updateTarget(int l, int offset)
{
	//QDocumentLineHandle *h = m_doc->impl()->at(l);
	
	// update command sender if any
	if ( m_cursor )
	{
//		qDebug("moving cursor [0x%x:beg] from (%i, %i) to line (%i, %i) as updating",
//					m_cursor,
//					m_cursor->m_begLine,
//					m_cursor->m_begOffset,
//					l,
//					offset
//					);
//
		while ( l && (offset < 0) )
		{
			--l;
			offset += m_doc->line(l).length() + 1;
		}

		while ( (l + 1) < m_doc->lines() && m_doc->line(l).length() < offset )
		{
			offset -= m_doc->line(l).length() + 1;
			++l;
		}

		if ( !m_keepAnchor )
		{
		m_cursor->m_endLine = -1;
		m_cursor->m_endOffset = -1;
		} else if ( m_cursor->m_endLine == -1 ) {
			m_cursor->m_endLine = m_cursor->m_begLine;
			m_cursor->m_endOffset = m_cursor->m_begOffset;
		}

		m_cursor->m_begLine = qMax(0, l);
		m_cursor->m_begOffset = qMax(0, offset);
		
		m_cursor->refreshColumnMemory();
	}
}


/*!
	\brief Change the modification status of a line
*/
void QDocumentCommand::markRedone(QDocumentLineHandle *h, bool firstTime)
{
	QHash<QDocumentLineHandle*, QPair<int, int> >::iterator it = m_doc->impl()->m_status.find(h);

	if ( it != m_doc->impl()->m_status.end() )
	{
		if ( firstTime && it->first < it->second )
			it->second = -1;

		++it->first;
	} else {
		m_doc->impl()->m_status[h] = qMakePair(1, 0);
	}
}

/*!
	\brief Change the modifiaction status of a line
*/
void QDocumentCommand::markUndone(QDocumentLineHandle *h)
{
	QHash<QDocumentLineHandle*, QPair<int, int> >::iterator it = m_doc->impl()->m_status.find(h);

	if ( it != m_doc->impl()->m_status.end() )
	{
		--it->first;
	} else {
		qDebug("warning: status data and/or undo stack corrupted...");
		m_doc->impl()->m_status[h] = qMakePair(-1, 0);
	}
}

////////////////////////////

/*!
	\class QDocumentInsertCommand
	\brief A specialized command to insert text
*/

/*!
	\brief ctor
	\param l target line
	\param offset target text position within target line
	\param text text to insert (can contain line feeds, "\n", which will result in the creation of new lines)
	\param doc host document
	\param p parent command
*/
QDocumentInsertCommand::QDocumentInsertCommand(	int l, int offset,
												const QString& text,
												QDocument *doc,
												QDocumentCommand *p)
 : QDocumentCommand(Insert, doc, p)
{
    QStringList lines;
    if (!text.contains("\n") && text.contains("\r"))  //mac line ending
      lines = text.split(QLatin1Char('\r'), QString::KeepEmptyParts);
     else
      lines = text.split(QLatin1Char('\n'), QString::KeepEmptyParts);

	if ( !m_doc || text.isEmpty() )
		qFatal("Invalid insert command");

	m_data.lineNumber = l;
	m_data.startOffset = offset;

	m_data.begin = lines.takeAt(0);
	m_data.endOffset = lines.count() ? lines.last().length() : -1;

	foreach ( const QString& s, lines )
		m_data.handles << new QDocumentLineHandle(s, m_doc);

	QDocumentLine bl = m_doc->line(l);

	if ( m_data.handles.count() && (bl.length() > offset) )
	{
		m_data.end = bl.text().mid(offset);
		m_data.handles.last()->lockForWriteText();
		m_data.handles.last()->textBuffer().append(m_data.end);
		m_data.handles.last()->unlock();
	}

	/*
	if ( (text == "\n") && m_data.handles.isEmpty() )
		qWarning("Go fix it by hand...");
	*/
}

/*!
	\brief dtor
*/
QDocumentInsertCommand::~QDocumentInsertCommand()
{
	if ( m_state )
		return;

	foreach ( QDocumentLineHandle *h, m_data.handles )
		h->deref();

}

bool QDocumentInsertCommand::mergeWith(const QUndoCommand *command)
{
	const QDocumentInsertCommand* cmd = static_cast<const QDocumentInsertCommand*>(command);
	int new_line=cmd->m_data.lineNumber;
	int new_startOffset=cmd->m_data.startOffset;
	int myLenght=m_data.begin.length();
	if(new_line==m_data.lineNumber && new_startOffset==m_data.startOffset+myLenght && 
	   cmd->m_data.handles.count()==0 && m_data.handles.count()==0) //don't know exactly why this line is needed but otherwise undo after insertion with several lines with autoindent doesn't work
	{
		m_data.begin+=cmd->m_data.begin;
		return true;
	}
	return false;
}

void QDocumentInsertCommand::redo()
{
	// state : handles used by doc
	m_state = true;

	//QDocumentIterator it = m_doc->impl()->index(m_data.lineNumber);

	//qDebug("inserting %i lines after %i", m_data.handles.count(), m_data.lineNumber);

	bool commandAffectsFolding = m_doc->linesPartiallyFolded(m_data.lineNumber, m_data.lineNumber);

	QDocumentLineHandle *hl = m_doc->impl()->at(m_data.lineNumber);

	if ( m_data.handles.count() )
	{
		removeText(m_data.lineNumber, m_data.startOffset, m_data.end.count());
	}

	insertText(m_data.lineNumber, m_data.startOffset, m_data.begin);

	insertLines(m_data.lineNumber, m_data.handles);

	if ( m_data.handles.count() )
	{
		QDocumentLineHandle *h = m_data.handles.last();

		//updateTarget(h, h->text().length() - m_data.end.length());
		updateTarget(m_data.lineNumber + m_data.handles.count(), h->text().length() - m_data.end.length() + m_redoOffset);
	} else {
		updateTarget(m_data.lineNumber, m_data.startOffset + m_data.begin.length() + m_redoOffset);
	}

	updateCursorsOnInsertion(m_data.lineNumber, m_data.startOffset, m_data.begin.length(), m_data.handles.count(), m_data.endOffset);

	m_doc->impl()->emitContentsChange(m_data.lineNumber, m_data.handles.count() + 1);
    m_doc->setProposedPosition(QDocumentCursor(m_doc,m_data.lineNumber+m_data.handles.size(),m_data.endOffset));

	markRedone(hl, m_first);

	foreach ( QDocumentLineHandle *h, m_data.handles )
		markRedone(h, m_first);

	if (commandAffectsFolding)
		m_doc->correctFolding(m_data.lineNumber, m_data.lineNumber+m_data.handles.count());

	//m_doc->impl()->emitContentsChanged();
	m_first = false;
}

void QDocumentInsertCommand::undo()
{
	// state : handles !used by doc
	m_state = false;

	bool commandAffectsFolding = m_doc->linesPartiallyFolded(m_data.lineNumber, m_data.lineNumber+m_data.handles.count());

	//QDocumentIterator it = m_doc->impl()->index(m_data.line);

	QDocumentLineHandle *hl = m_doc->impl()->at(m_data.lineNumber);

	removeLines(m_data.lineNumber, m_data.handles.count());
	removeText(m_data.lineNumber, m_data.startOffset, m_data.begin.count());

	if ( m_data.handles.count() )
	{
		insertText(m_data.lineNumber, m_data.startOffset, m_data.end);
	}

	updateTarget(m_data.lineNumber, m_data.startOffset + m_undoOffset);

	updateCursorsOnDeletion(m_data.lineNumber, m_data.startOffset, m_data.begin.length(), m_data.handles.count(), m_data.endOffset);

	m_doc->impl()->emitContentsChange(m_data.lineNumber, m_data.handles.count() + 1);
    m_doc->setProposedPosition(QDocumentCursor(m_doc,m_data.lineNumber,m_data.startOffset));

	markUndone(hl);

	foreach ( QDocumentLineHandle *h, m_data.handles )
		markUndone(h);

	if (commandAffectsFolding)
		m_doc->correctFolding(m_data.lineNumber, m_data.lineNumber+m_data.handles.count());
	//m_doc->impl()->emitContentsChanged();
}

///////////////////////////

/*!
	\class QDocumentEraseCommand
	\brief A specialized command to erase text
*/

/*!
	\brief ctor
	\param bl begin line of the target area
	\param bo begin text position of the target area
	\param el end line of the target area
	\param eo end text position of the target area
	\param doc host document
	\param p parent command
*/
QDocumentEraseCommand::QDocumentEraseCommand(	int bl, int bo,
												int el, int eo,
												QDocument *doc,
												QDocumentCommand *p)
 : QDocumentCommand(Erase, doc, p)
{
	if (el>m_doc->lines()-1) {
		el=m_doc->lines()-1;
		eo=m_doc->impl()->at(el)->text().length();
	}
	QDocumentLineHandle *start = m_doc->impl()->at(bl),
						*end = m_doc->impl()->at(el);

	QDocumentConstIterator it = m_doc->impl()->begin() + bl; //index(start);

	m_data.lineNumber = bl;
	m_data.startOffset = bo;

	if ( start == end )
	{
		m_data.begin = start->text().mid(bo, eo - bo);

		m_data.end = QString();
		m_data.endOffset = -1;

	} else {
		m_data.begin = start->text().mid(bo);

		m_data.endOffset = eo;
		m_data.end = end->text().mid(eo);

		do
		{
			m_data.handles << *(++it);
		} while ( *it != end );
	}

	m_state = true;
    m_mergedLines=false;
}

QStringList QDocumentInsertCommand::debugRepresentation() const{
	QStringList result;
	result << QString("INSERT COMMAND: %1:%2").arg(m_data.lineNumber).arg(m_data.startOffset).arg(m_data.lineNumber+m_data.handles.size()).arg(m_data.endOffset);
	result << QString("     Inserted text: \"%1\"").arg(m_data.begin);
	if (m_data.handles.size()) {
		result << ("     Inserted lines:");
		for (int i=0; i < m_data.handles.size(); i++)
			result << (QString("     %1").arg((intptr_t)(m_data.handles[i]),8,16) + (m_doc->indexOf(m_data.handles[i]) >= 0?"\""+m_data.handles[i]->text()+"\"":"<ERASED>") );
	}
	return result;
}


/*!
	\brief dtor
*/
QDocumentEraseCommand::~QDocumentEraseCommand()
{
	if ( m_state )
		return;

	foreach ( QDocumentLineHandle *h, m_data.handles )
		h->deref();
	//qDeleteAll(m_data.handles);
}

bool QDocumentEraseCommand::mergeWith(const QUndoCommand *command)
{
	int new_line=static_cast<const QDocumentEraseCommand*>(command)->m_data.lineNumber;
	int new_startOffset=static_cast<const QDocumentEraseCommand*>(command)->m_data.startOffset;
	int myLenght=static_cast<const QDocumentEraseCommand*>(command)->m_data.begin.length();
	if(static_cast<const QDocumentEraseCommand*>(command)->m_data.endOffset!=-1) return false;
	if(m_data.endOffset!=-1) return false;
	if(new_line==m_data.lineNumber && new_startOffset+myLenght==m_data.startOffset){ //backspace
		m_data.begin=static_cast<const QDocumentEraseCommand*>(command)->m_data.begin+m_data.begin;
		m_data.startOffset=new_startOffset;
		m_undoOffset+=static_cast<const QDocumentEraseCommand*>(command)->m_undoOffset;
		return true;
	}
	if(new_line==m_data.lineNumber && new_startOffset==m_data.startOffset){ //delete char
		m_data.begin+=static_cast<const QDocumentEraseCommand*>(command)->m_data.begin;
		m_undoOffset+=static_cast<const QDocumentEraseCommand*>(command)->m_undoOffset;
		//m_data.startOffset=new_startOffset;
		return true;
	}
	return false;
}

void QDocumentEraseCommand::redo()
{
	// state : handles !used by doc
	m_state = false;

	bool commandAffectsFolding = m_doc->linesPartiallyFolded(m_data.lineNumber, m_data.lineNumber+m_data.handles.count());
	bool foldStart = false;
	if (commandAffectsFolding) 
		for (int i=m_data.lineNumber; i<=m_data.lineNumber+m_data.handles.count();i++)
			if (m_doc->line(i).hasFlag(QDocumentLine::CollapsedBlockStart)) {
				foldStart = true;
				break;
			}
	
	//QDocumentIterator it = m_doc->impl()->index(m_data.line);

	QDocumentLineHandle *hl = m_doc->impl()->at(m_data.lineNumber);

	if ( m_data.handles.isEmpty() )
	{
		removeText(m_data.lineNumber, m_data.startOffset, m_data.begin.count());
	} else {
		removeText(m_data.lineNumber, m_data.startOffset, m_data.begin.count());

        if ( m_data.endOffset != -1 ){
			insertText(m_data.lineNumber, m_data.startOffset, m_data.end);
            m_mergedLines=m_doc->linesMerged(hl,m_data.startOffset,m_data.handles.last());
        }

		removeLines(m_data.lineNumber, m_data.handles.count());
	}


	updateTarget(m_data.lineNumber, m_data.startOffset + m_redoOffset);

	updateCursorsOnDeletion(m_data.lineNumber, m_data.startOffset, m_data.begin.length(), m_data.handles.count(), m_data.endOffset);

	m_doc->impl()->emitContentsChange(m_data.lineNumber, m_data.handles.count() + 1);

	markRedone(hl, m_first);

	foreach ( QDocumentLineHandle *h, m_data.handles )
		markRedone(h, m_first);

	if (commandAffectsFolding)
		m_doc->correctFolding(m_data.lineNumber, m_data.lineNumber+m_data.handles.count(), foldStart);

	//m_doc->impl()->emitContentsChanged();
	m_first = false;
}

void QDocumentEraseCommand::undo()
{
	// state : handles used by doc
	m_state = true;

	bool commandAffectsFolding = m_doc->linesPartiallyFolded(m_data.lineNumber, m_data.lineNumber);

	//QDocumentIterator it = m_doc->impl()->index(m_data.line);

	QDocumentLineHandle *hl = m_doc->impl()->at(m_data.lineNumber);

	if ( m_data.handles.count() )
	{
		insertLines(m_data.lineNumber, m_data.handles);

		if ( m_data.endOffset != -1 )
			removeText(m_data.lineNumber, m_data.startOffset, m_data.end.count());

		insertText(m_data.lineNumber, m_data.startOffset, m_data.begin);

        if(m_mergedLines){
            m_doc->linesUnMerged(hl,m_data.handles.last());
        }

		m_doc->impl()->emitContentsChange(m_data.lineNumber, m_data.handles.count() + 1);
	} else {

		insertText(m_data.lineNumber, m_data.startOffset, m_data.begin);

		m_doc->impl()->emitContentsChange(m_data.lineNumber, 1);
	}

	if ( m_data.handles.count() )
	{
		QDocumentLineHandle *h = m_data.handles.last();

		//updateTarget(h, h->text().length() - m_data.end.length());
		updateTarget(m_data.lineNumber + m_data.handles.count(), h->text().length() - m_data.end.length() + m_undoOffset);
	} else {
		updateTarget(m_data.lineNumber, m_data.startOffset + m_data.begin.length() + m_undoOffset);
	}

	updateCursorsOnInsertion(m_data.lineNumber, m_data.startOffset, m_data.begin.length(), m_data.handles.count(), m_data.endOffset);

	markUndone(hl);

	foreach ( QDocumentLineHandle *h, m_data.handles )
		markUndone(h);

	if (commandAffectsFolding)
		m_doc->correctFolding(m_data.lineNumber,m_data.lineNumber+m_data.handles.count());

	//m_doc->impl()->emitContentsChanged();
}

QStringList QDocumentEraseCommand::debugRepresentation() const{
	QStringList result;
	result << QString("ERASE COMMAND: %1:%2 to %3:%4").arg(m_data.lineNumber).arg(m_data.startOffset).arg(m_data.lineNumber+m_data.handles.size()).arg(m_data.endOffset);
	result << QString("     Erased text: \"%1\", \"%2\"").arg(m_data.begin).arg(m_data.end);
	if (m_data.handles.size()) {
		result << ("     Erased lines:");
		for (int i=0; i < m_data.handles.size(); i++)
			result << (QString("     %1").arg((intptr_t)(m_data.handles[i]),8,16) + (m_doc->indexOf(m_data.handles[i]) >= 0?"\""+m_data.handles[i]->text()+"\"":"<ERASED>") );
	}
	return result;
}


///////////////////////////
/*
QDocumentReplaceCommand::QDocumentReplaceCommand(const QDocumentLine& l, int, int, const QString&)
 : QDocumentCommand(Replace, l.document())
{

}

QDocumentReplaceCommand::~QDocumentReplaceCommand()
{

}

bool QDocumentReplaceCommand::mergeWith(const QUndoCommand *)
{
	return false;
}

void QDocumentReplaceCommand::redo()
{

}

void QDocumentReplaceCommand::undo()
{

}
*/

//////////////////////


/*!
	\class QDocumentCommandBlock
	\brief A meta command used for command grouping
*/

/*!
	\brief ctor
	\param d host document
*/
QDocumentCommandBlock::QDocumentCommandBlock(QDocument *d)
 : QDocumentCommand(Custom, d), m_weakLocked(false)
{

}

/*!
	\brief dtor
*/
QDocumentCommandBlock::~QDocumentCommandBlock()
{
	qDeleteAll(m_commands);
}

void QDocumentCommandBlock::redo()
{
	if ( isWeakLocked() )
	{
		setWeakLock(false);
		return;
	}

	//foreach ( QDocumentCommand *c, m_commands )
	//	c->redo();

	for ( int i = 0; i < m_commands.count(); ++i )
		m_commands.at(i)->redo();

}

void QDocumentCommandBlock::undo()
{
	//foreach ( QDocumentCommand *c, m_commands )
	//	c->undo();

	for ( int i = m_commands.count() - 1; i >= 0; --i )
		m_commands.at(i)->undo();

}

/*!
	\brief Set whether the block is weakly locked
*/
void QDocumentCommandBlock::setWeakLock(bool l)
{
	m_weakLocked = l;
}

/*!
	\return whether the block is weakly locked

	Weak locking of command block is an obscure internal feature
	which prevents the first redo() call from actually redo'ing
	the grouped commands
*/
bool QDocumentCommandBlock::isWeakLocked() const
{
	return m_weakLocked;
}

/*!
	\brief Add a command to the group

	\warning Doing that after having pushed the command on the undo/redo stack
	is likely to result in corruption of the undo/redo stack
*/
void QDocumentCommandBlock::addCommand(QDocumentCommand *c)
{
	m_commands << c;
}

/*!
	\brief Remove a command from the block

	\warning Doing that after having pushed the command on the undo/redo stack
	is likely to result in corruption of the undo/redo stack
*/
void QDocumentCommandBlock::removeCommand(QDocumentCommand *c)
{
	m_commands.removeAll(c);
}

QStringList QDocumentCommandBlock::debugRepresentation() const{
	QStringList result;
	result << "BLOCK:";
	for (int i=0;i<m_commands.size();i++) {
		QStringList child = m_commands[i]->debugRepresentation();
		foreach (const QString& s, child)
			result << "    " + s;
	}
	return result;
}


QDocumentChangeMetaDataCommand::QDocumentChangeMetaDataCommand(QDocument *d, QTextCodec* newCodec)
	:QDocumentCommand(Custom, d){
	init(newCodec, d->lineEnding());
}
QDocumentChangeMetaDataCommand::QDocumentChangeMetaDataCommand(QDocument *d, QDocument::LineEnding newLineEnding)
       :QDocumentCommand(Custom, d){
	init(d->codec(), newLineEnding);
}

void QDocumentChangeMetaDataCommand::redo(){
	m_doc->setCodecDirect(newCodec);
	m_doc->setLineEndingDirect(newLineEnding);
}

void QDocumentChangeMetaDataCommand::undo(){
	m_doc->setLineEndingDirect(oldLineEnding);	
	m_doc->setCodecDirect(oldCodec);
}

void QDocumentChangeMetaDataCommand::init(QTextCodec* newCodec, QDocument::LineEnding newLineEnding){
	this->oldCodec = m_doc->codec();
	this->newCodec = newCodec;
	this->oldLineEnding = m_doc->lineEnding();
	this->newLineEnding = newLineEnding;
}

QStringList QDocumentChangeMetaDataCommand::debugRepresentation() const{
	return QStringList() << QString("META DATA CHANGE: Encoding: %1=>%2; Line-Ending: %3=>%4").arg(oldCodec?oldCodec->name().data():"0").arg(newCodec?newCodec->name().data():"0").arg(oldLineEnding).arg(newLineEnding);
}

/*! @} */