File: TextModelImpl.cpp

package info (click to toggle)
subcommander 2.0.0~b5p2-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,344 kB
  • sloc: cpp: 63,594; sh: 4,050; xml: 1,992; makefile: 1,134; ansic: 786; ruby: 251; lisp: 24
file content (1157 lines) | stat: -rw-r--r-- 22,406 bytes parent folder | download | duplicates (4)
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
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
/* ====================================================================
 * Copyright (c) 2003-2006, 2008  Martin Hauner
 *                                http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */

// sc
#include "TextModelImpl.h"
#include "Cursor.h"
#include "Line.h"
#include "Tab.h"
#include "util/String.h"

// sys
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <list>
#include "util/max.h"

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

class TextCmd
{
public:
  virtual ~TextCmd() {}

  virtual void run() = 0;
  virtual void undo() = 0;
};

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

typedef std::list< Line >                        Lines;
typedef std::map< svn::Offset, Lines::iterator > LineNrs;

typedef std::list< TextCmd* >                    Cmds;



class TextModelImpl::Member
{
public:
  // \todo setTabWidth()
  Member() : _lineEnds(leNone), _tabWidth(2), _lastCol(0), _maxCol(0) 
    //, maxLine(0) 
  {
    _cmdIt = _cmds.end();
  }

  ~Member()
  {
    for( Cmds::iterator it = _cmds.begin(); it != _cmds.end(); it++ )
    {
      delete (*it);
    }
  }

  LineEnd getLineEnd()
  {
    return _lineEnds;
  }

  LineEnd checkLineEnd( const Line& line )
  {
    if( _lineEnds == leNone )
    {
      sc::Size bytes = line.getBytes();

      if( bytes >= 1 && line.getStr()[bytes-1] == '\r' )
      {
        _lineEnds = leCR;
      }
      else if( bytes >= 1 && line.getStr()[bytes-1] == '\n' )
      {
        _lineEnds = leLF;

        if( bytes >= 2 && line.getStr()[bytes-2] == '\r' )
        {
          _lineEnds = leCRLF;
        }
      }
    }
    return _lineEnds;
  }

  const char* getLineEndStr()
  {
    if( _lineEnds == leLF )
    {
      return sLF;
    }
    else if( _lineEnds == leCR )
    {
      return sCR;
    }
    else if( _lineEnds == leCRLF )
    {
      return sCRLF;
    }
    else
    {
      // \todo default value
      return sLF;
    }
  }

  Lines::iterator addLine( const Line& line )
  {
    Lines::iterator it = _lines.insert( _lines.end(), line );
    _lineNrs.insert( LineNrs::value_type(_lineNrs.size(),it) );

    Tab tab(_tabWidth);
    _maxCol = std::max( _maxCol, (size_t)tab.calcColumns(line.getStr()) );

    return it;
  }

  Lines::iterator getLine( svn::Offset lineNr )
  {
    LineNrs::iterator it = _lineNrs.find( lineNr );
    if( it == _lineNrs.end() )
    {
      return _lines.end();
    }
    return (*it).second;
  }

  Lines::iterator getLine()
  {
    return getLine( _cursor.line() );
  }

  const Line& getLine( Lines::iterator it )
  {
    if( it == _lines.end() )
    {
      return Line::getEmpty();
    }
    return *it;
  }

  bool nextLine( const Cursor& c, int& down )
  {
    down = 0;

    for( Lines::iterator it = getLine( c.line()+1 ); it != _lines.end(); it++ )
    {
      down++;

      if( ! (*it).isEmpty() )
      {
        return true;
      }
    }
    return false;
  }

  bool prevLine( const Cursor& c, int& up )
  {
    up = 0;

    // since we move up check against begin and end (not found)
    for( Lines::iterator it = getLine( c.line()-1 ); it != _lines.end(); it-- )
    {
      up++;

      if( ! (*it).isEmpty() )
      {
        return true;
      }
    }
    return false;
  }

  Cursor calcNearestCursorPos( const Cursor& c )
  {
    Cursor nc = c;

    Lines::iterator it = getLine( c.line() );

    if( it == _lines.end() )
    {
      // cursor is in empty space below all files
      nc.setLine((int)(_lines.size()-1));
    }
    else if( (*it).isEmpty() )
    {
      int up;
      int down;
      /*bool bup   =*/ prevLine( nc, up );
      /*bool bdown =*/ nextLine( nc, down );

      if( up <= down )
      {
        nc.up(up);
      }
      else
      {
        nc.down(down);
      }
    }

    const Line& l = getLine( getLine(nc.line()) );

    Tab tab(_tabWidth);
    nc.maxColumn( tab.calcColumns(l.getStr()) );
    nc.setColumn( tab.calcColumnForColumn(l.getStr(),nc.column()) );

    return nc;
  }

  Cursor moveCursorRight( bool moveC2 )
  {
    Cursor nc = _cursor;
    nc.setOn();

    // find next line, this may be more than 1 line down if we step
    // over "nop" lines.
    int  down  = 0;
    bool bdown = nextLine(nc,down);

    const Line& l = getLine( getLine(nc.line()) );

    Tab tab(_tabWidth);
    int curcol    = nc.column();
    int nextcol   = tab.calcColumnForNextChar( l.getStr(), curcol );
    int linewidth = tab.calcColumns( l.getStr() );

    // cursor right DOES change the line
    // if we check bdown we can't 'entf' the last char in the last line
    if( bdown && (curcol == linewidth) )
    {
      nc.down(down);
      nc.setColumn(0);
    }
    else // cursor right DOES NOT change the line
    {
      // move cursor right but never behind the last column in the current line,
      // this is (only) necessary if we are on the last possible line
      nc.setColumn( nextcol );
      nc.maxColumn( linewidth );
    }

    _lastCol = nc.column();
    _cursor  = nc;
    if( moveC2 )
    {
      _cursor2 = _cursor;
    }
    return nc;
  }

  Cursor moveCursorLeft( bool moveC2 )
  {
    Cursor nc = _cursor;
    nc.setOn();

    // find previous line
    int  up  = 0;
    bool bup = prevLine(nc,up);

    // already leftmost cursor position?
    if( ! bup && nc.column() == 0 )
    {
      // yes
      return nc;
    }

    const Line& l  = getLine( getLine( nc.line() ) );
    const Line& lp = getLine( getLine( nc.line()-up ) );

    Tab tab(_tabWidth);
    int curcol     = nc.column();
    int prevcol    = tab.calcColumnForPrevChar( l.getStr(), nc.column() );
    int linewidthp = tab.calcColumns( lp.getStr() );

    // cursor left DOES change the line
    if( (prevcol == 0) && (curcol == 0) )
    {
      nc.up(up);
      nc.setColumn( linewidthp );
    }
    // cursor left DOES NOT change the line
    else
    {
      nc.setColumn(prevcol);
      nc.minColumn(0);
    }

    _lastCol = nc.column();
    _cursor  = nc;
    if( moveC2 )
    {
      _cursor2 = _cursor;
    }
    return nc;
  }

  Cursor moveCursorDown( bool moveC2 )
  {
    Cursor nc = _cursor;
    nc.setOn();

    // find next line
    int  down  = 0;
    bool bdown = nextLine(nc,down);
    if( ! bdown )
    {
      return nc;
    }

    const Line& l = getLine( getLine( nc.line()+down ) );

    Tab tab(_tabWidth);
    int linewidth = tab.calcColumns( l.getStr() );

    nc.down(down);

    if( _lastCol < linewidth )
    {
      nc.setColumn(_lastCol);
    }
    else
    {
      nc.setColumn(linewidth);
    }

    _cursor = nc;
    if( moveC2 )
    {
      _cursor2 = _cursor;
    }
    return nc;
  }

  Cursor moveCursorUp( bool moveC2 )
  {
    Cursor nc = _cursor;
    nc.setOn();

    // find previous line
    int  up  = 0;
    bool bup = prevLine(nc,up);
    if( ! bup )
    {
      return nc;
    }

    const Line& l = getLine( getLine( nc.line()-up ) );

    Tab tab(_tabWidth);
    int linewidth = tab.calcColumns( l.getStr() );

    nc.up(up);

    if( _lastCol < linewidth )
    {
      nc.setColumn(_lastCol);
    }
    else
    {
      nc.setColumn(linewidth);
    }

    _cursor = nc;
    if( moveC2 )
    {
      _cursor2 = _cursor;
    }
    return nc;
  }

  void addCmd( TextCmd* cmd )
  {
    for( Cmds::iterator it = _cmdIt; it != _cmds.end(); )
    {
      delete (*it);
      it = _cmds.erase(it);
    }

    _cmds.push_back( cmd );
    _cmdIt = _cmds.end();
  }

  TextCmd* getUndo()
  {
    if( _cmdIt == _cmds.begin() )
    {
      return 0;
    }
    return *(--_cmdIt);
  }

  TextCmd* getRedo()
  {
    if( _cmdIt == _cmds.end() )
    {
      return 0;
    }

    return *(_cmdIt++);
  }

  void calcMaxColumn()
  {
    Tab tab(_tabWidth);

    _maxCol = 0;
    for( Lines::iterator it = _lines.begin(); it != _lines.end(); it++ )
    {
      _maxCol = std::max( _maxCol, (size_t)tab.calcColumns((*it).getStr()) );
    }
  }

  void rebuildLineNrs()
  {
    _lineNrs.clear();

    svn::Offset cnt = 0;
    for( Lines::iterator it = _lines.begin(); it != _lines.end(); it++, cnt++ )
    {
      _lineNrs.insert( LineNrs::value_type(cnt,it) );
    }
  }

  void setCursor( const Cursor& c, bool moveC2 )
  {
    _cursor = c;
    if( moveC2 )
    {
      _cursor2 = _cursor;
    }
  }

public:
  sc::String     _sourceName;
  LineEnd        _lineEnds;
  int            _tabWidth;

  Cursor         _cursor;
  Cursor         _cursor2;
  int            _lastCol;  // last column for cursor up/down

  Lines          _lines;
  LineNrs        _lineNrs;

  size_t         _maxCol;
  //size_t         maxLine;

  Cmds           _cmds;
  Cmds::iterator _cmdIt;
};

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

class CompositeTextCmd : public TextCmd
{
public:
  CompositeTextCmd( const Cmds& cmds ) : _cmds(cmds)
  {
  }

  void run()
  {
    for( Cmds::iterator it = _cmds.begin(); it != _cmds.end(); it++ )
    {
      (*it)->run();
    }
  }

  void undo()
  {
    for( Cmds::reverse_iterator it = _cmds.rbegin(); it != _cmds.rend(); it++ )
    {
      (*it)->undo();
    }
  }

private:
  Cmds _cmds;
};

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

// The input text can be a string with more than one character but without line
// feed. line feeds will come as a string that only contains the line feed.

class AddTextCmd : public TextCmd
{
public:
  AddTextCmd( TextModelImpl::Member* m, const sc::String& text )
    : M(m), _text(text)
  {
  }

  void run()
  {
    // store current cursor position
    _cursor = M->_cursor;

    Lines::iterator it = M->getLine( _cursor.line() );

    if( it == M->_lines.end() )
    {
      it = M->addLine( Line::getEmpty2() );
    }

    Line& line = *it;

    Tab tab(M->_tabWidth);
    int choff = tab.calcCharOffsetForColumn( line.getStr(), _cursor.column() );

    sc::String src = line.getLine();
    sc::String dst = src.left( choff );

    if( isReturn() )
    {
      dst += M->getLineEndStr();

      sc::String dst2;
      dst2 += src.right( src.getCharCnt() - choff );

      Line l  = Line( dst, line.getBlockNr(), line.getType() );
      Line l2 = Line( dst2, line.getBlockNr(), line.getType() );

      *it = l;
      M->_lines.insert( ++it, l2 );

      M->rebuildLineNrs();
      M->moveCursorRight(true);
    }
    else
    {
      dst += _text;
      dst += src.right( src.getCharCnt() - choff );

      Line l = Line( dst, line.getBlockNr(), line.getType() );
      *it    = l;

      for( int r = 0; r < (int)_text.getCharCnt(); r++ )
      {
        M->moveCursorRight(true);
      }
    }

    M->calcMaxColumn();
  }

  void undo()
  {
    Lines::iterator it = M->getLine( _cursor.line() );
    Line& line         = *it;

    Tab tab(M->_tabWidth);
    int choff = tab.calcCharOffsetForColumn( line.getStr(), _cursor.column() );

    sc::String src = line.getLine();
    sc::String dst = src.left( choff );

    if( isReturn() )
    {
      Lines::iterator itn = M->getLine( _cursor.line()+1 );
      Line& linen         = *itn;

      dst += linen.getStr();

      M->_lines.erase(itn);
      M->rebuildLineNrs();
    }
    else
    {
      dst += src.right( src.getCharCnt() - _text.getCharCnt() - choff );
    }

    Line l = Line( dst, line.getBlockNr(), line.getType() );
    *it    = l;

    M->calcMaxColumn();
    M->setCursor( _cursor, true );
  }

  bool isReturn()
  {
    static sc::String LF(sLF);
    static sc::String CR(sCR);
    static sc::String CRLF(sCRLF);

    if( _text == LF )
    {
      return true;
    }
    else if( _text == CR )
    {
      return true;
    }
    else if( _text == CRLF )
    {
      return true;
    }
    else
    {
      return false;
    }
  }

private:
  TextModelImpl::Member* M;
  Cursor                 _cursor;
  sc::String             _text;
};

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

class RemoveTextLeftCmd : public TextCmd
{
public:
  RemoveTextLeftCmd( TextModelImpl::Member* m )
    : M(m), _cursor(m->_cursor)
  {
  }

  void run()
  {
    if( _cursor.equalPos(Cursor(0,0)) )
    {
      return;
    }

    Lines::iterator it = M->getLine( _cursor.line() );

    if( it == M->_lines.end() )
    {
      return;
    }

    Line& line = *it;

    Tab tab(M->_tabWidth);
    int choff = tab.calcCharOffsetForColumn( line.getStr(), _cursor.column() );

    if( choff == 0 )
    {
      Lines::iterator itp = M->getLine( _cursor.line()-1 );
      Line& linep         = *itp;

      int lf = 0;
      sc::String s;
      
      s = linep.getLine().right(1);
      if( s == sc::String(sCR) )
      {
        lf = 1;
      }
      else if( s == sc::String(sLF) )
      {
        lf = 1;
      }
      
      s = linep.getLine().right(2);
      if( s == sc::String(sCRLF) )
      {
        lf = 2;
      }

      int len = (int)linep.getLine().getCharCnt()-lf;
      _text = linep.getLine().right( lf );
      sc::String p;
      p += linep.getLine().left( len );
      p += line.getLine();

      Line l = Line( p, linep.getBlockNr(), linep.getType() );
      *itp   = l;

      M->_lines.erase(it);
      M->rebuildLineNrs();

      _cursor = Cursor( _cursor.line()-1, len );
      M->setCursor( _cursor, true );

      _removedLine = true;
    }
    else
    {
      M->moveCursorLeft(true);

      sc::String src = line.getLine();
      sc::String dst;
      dst += src.left( choff-1 );
      dst += src.right( src.getCharCnt() - choff );
      _text = src.mid( choff-1, 1 );

      Line l = Line( dst, line.getBlockNr(), line.getType() );
      *it    = l;

      _removedLine = false;
    }

    M->calcMaxColumn();
  }

  void undo()
  {
    Lines::iterator it = M->getLine( _cursor.line() );

    if( it == M->_lines.end() )
    {
      return;
    }

    Line& line = *it;

    Tab tab(M->_tabWidth);
    int choff = tab.calcCharOffsetForColumn( line.getStr(), _cursor.column() );

    if( _removedLine )
    {
      sc::String src = line.getLine();
      sc::String dst;
      dst += src.left( choff );
      dst += _text;

      sc::String dst2;
      dst2 += src.right( src.getCharCnt() - choff );

      Line l  = Line( dst, line.getBlockNr(), line.getType() );
      Line l2 = Line( dst2, line.getBlockNr(), line.getType() );

      *it = l;
      M->_lines.insert( ++it, l2 );

      M->rebuildLineNrs();

      _cursor = Cursor( _cursor.line()+1, 0 );
    }
    else
    {
      sc::String src = line.getLine();
      sc::String dst;
      dst += src.left( choff-1 );
      dst += _text;
      dst += src.right( src.getCharCnt() - (choff - 1) );

      Line l = Line( dst, line.getBlockNr(), line.getType() );
      *it    = l;
    }

    M->calcMaxColumn();
    M->setCursor( _cursor, true );
  }

protected:
  TextModelImpl::Member* M;
  Cursor                 _cursor;
  bool                   _removedLine;
  sc::String             _text;
};

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

class RemoveTextRightCmd : public RemoveTextLeftCmd
{
public:
  RemoveTextRightCmd( TextModelImpl::Member* m )
    : RemoveTextLeftCmd( m )
  {
  }

  void run()
  {
    _cursor = M->moveCursorRight(true);

    RemoveTextLeftCmd::run();
  }

  void undo()
  {
    RemoveTextLeftCmd::undo();
    
    _cursor = M->moveCursorLeft(true);
  }
};

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

class TextCmdFactory
{
public:
  static TextCmd* createAddTextCmd( TextModelImpl::Member* m, const sc::String& text )
  {
    Cmds cmds;

    const char* start = text.getStr();
    const char* curr  = start;

    while( *curr != 0 )
    {
      if( *curr == '\r' || *curr == '\n' )
      {
        // add a normal string if we have something to add
        if( curr > start )
        {
          sc::String part( start, curr-start );
          TextCmd* cmdPart = new AddTextCmd( m, part );
          cmds.push_back(cmdPart);
        }

        char lineEnd[2] = {};
        lineEnd[0] = *curr;
        curr++;

        // check if it is a CRLF
        if( *curr != 0 && lineEnd[0] == '\r' && *curr == '\n' )
        {
          lineEnd[1] = *curr;
          curr++;
        }

        sc::String le( lineEnd );
        TextCmd* cmdLE = new AddTextCmd( m, le );
        cmds.push_back(cmdLE);


        // next none line end string part starts here
        start = curr;
        continue;
      }

      curr++;
    }

    // nothing in there? then the string didn't contain any line end.
    if( cmds.size() == 0 )
    {
      TextCmd* cmd = new AddTextCmd( m, text );
      cmds.push_back(cmd);
    }
    // any chars left? then the last line in the string didn't end with
    // a line end.
    else if( curr > start )
    {
      sc::String part( start, curr-start );
      TextCmd* cmdPart = new AddTextCmd( m, part );
      cmds.push_back(cmdPart);
    }

    if( cmds.size() == 1 )
    {
      return *(cmds.begin());
    }
    else
    {
      return new CompositeTextCmd( cmds );
    }
  }
};

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

TextModelImpl::TextModelImpl( const sc::String& sourceName )
{
  M = new Member();
  M->_sourceName = sourceName;
}

TextModelImpl::~TextModelImpl()
{
  delete M;
}

void TextModelImpl::clear()
{
  sc::String name = M->_sourceName;

  delete M;

  M = new Member();
  M->_sourceName = name;
}

const Line& TextModelImpl::getLine( sc::Size lineNr )
{
  return M->getLine( M->getLine( lineNr ) );
}

BlockInfo TextModelImpl::getBlockInfo( int block )
{
  svn::Offset cnt    = 0;
  svn::Offset start  = 0;
  svn::Offset length = 0;
  bool foundStart    = false;

  for( Lines::iterator it = M->_lines.begin(); it != M->_lines.end(); it++, cnt++ )
  {
    if( (*it).getBlockNr() == block )
    {
      if( ! foundStart )
      {
        start      = cnt;
        foundStart = true;
      }
      length++;
    }
  }

  return BlockInfo( start, length, 0, 0 );
}

size_t TextModelImpl::getLineCnt()
{
  return M->_lines.size();
}

size_t TextModelImpl::getColumnCnt()
{
  return M->_maxCol; 
}

LineEnd TextModelImpl::getLineEnd()
{
  return M->_lineEnds;
}

unsigned int TextModelImpl::getTabWidth()
{
  return M->_tabWidth;
}

const sc::String& TextModelImpl::getSourceName()
{
  return M->_sourceName;
}

bool TextModelImpl::addLine( const Line& line )
{
  M->checkLineEnd( line );
  M->addLine( line );

  return true;
}

int TextModelImpl::replaceBlock( int block, TextModel* src )
{
  bool foundBlock = false;
  Lines::iterator itStart;
  int cnt    = 0;
  int result = 0;

  for( Lines::iterator it = M->_lines.begin(); it != M->_lines.end(); cnt++ )
  {
    if( (*it).getBlockNr() == block )
    {
      if( ! foundBlock )
      {
        foundBlock = true;
        result     = cnt;
      }

      it      = M->_lines.erase(it);
      itStart = it;
      continue;
    }
    it++;
  }


  BlockInfo bi = src->getBlockInfo(block);

  for( sc::Size j = 0; j < (sc::Size)bi.getLength(); j++ )
  {
    const Line& sl = src->getLine( (sc::Size)bi.getStart() + j );

    if( (! sl.isEmpty()) || ((sl.getType() & ctFlagEmpty) == ctFlagEmpty) )
    {
      Line nl( sl.getLine(), sl.getBlockNr(), (ConflictType)(sl.getType() | ctFlagMerged) );
      M->_lines.insert( itStart, nl );
    }
  }

  M->calcMaxColumn();
  M->rebuildLineNrs();

  // first line of block, for scrolling...
  return result;
}

const Cursor& TextModelImpl::getCursor()
{
  return M->_cursor;
}

const Cursor& TextModelImpl::getCursor2()
{
  return M->_cursor2;
}

void TextModelImpl::setCursor( const Cursor& c )
{
  M->_cursor = c;
}

void TextModelImpl::setCursor2( const Cursor& c )
{
  M->_cursor2 = c;
}

Cursor TextModelImpl::moveCursorRight( bool moveC2 )
{
  return M->moveCursorRight(moveC2);
}

Cursor TextModelImpl::moveCursorLeft( bool moveC2 )
{
  return M->moveCursorLeft(moveC2);
}

Cursor TextModelImpl::moveCursorDown( bool moveC2 )
{
  return M->moveCursorDown(moveC2);
}

Cursor TextModelImpl::moveCursorUp( bool moveC2 )
{
  return M->moveCursorUp(moveC2);
}

int TextModelImpl::getLastColumn()
{
  return M->_lastCol;
}

void TextModelImpl::setLastColumn( int col )
{
  M->_lastCol = col;
}

Cursor TextModelImpl::calcNearestCursorPos( const Cursor& c )
{
  return M->calcNearestCursorPos(c);
}

void TextModelImpl::addText( const sc::String& s )
{
  if(s.isEmpty())
    return;
  
  TextCmd* cmd = TextCmdFactory::createAddTextCmd( M, s );
  M->addCmd( cmd );

  cmd->run();
}

void TextModelImpl::removeTextLeft()
{
  TextCmd* cmd = new RemoveTextLeftCmd( M );
  M->addCmd( cmd );

  cmd->run();
}

void TextModelImpl::removeTextRight()
{
  TextCmd* cmd = new RemoveTextRightCmd( M );
  M->addCmd( cmd );

  cmd->run();
}

sc::String TextModelImpl::getHighlightedText()
{
  CursorPair cp( M->_cursor, M->_cursor2 );
  cp.order();
  Cursor top = cp.getOne();
  Cursor bot = cp.getTwo();

  Tab tab(M->_tabWidth);
  sc::String copy;

  if( top.line() == bot.line() )
  {
    // handle single line, possibly partial line only
    const sc::String& s = getLine( top.line() ).getLine();

    int l = tab.calcCharOffsetForColumn( s.getStr(), top.column() );
    int r = tab.calcCharOffsetForColumn( s.getStr(), bot.column() );

    copy += s.mid( l, r-l );
  }
  else
  {
    // handle first line, possibly partial only
    const sc::String& first = getLine( top.line() ).getLine();
    int l = tab.calcCharOffsetForColumn( first.getStr(), top.column() );
    copy += first.right( first.getCharCnt() - l );

    // handle middle lines, always complete
    for( int i = top.line()+1; i < bot.line(); i++ )
    {
      copy += getLine(i).getLine();
    }

    // handle last line, possibly partial only
    const sc::String& last = getLine( bot.line() ).getLine();
    int r = tab.calcCharOffsetForColumn( last.getStr(), bot.column() );
    copy += last.left( r );
  }

  return copy;
}

void TextModelImpl::undo()
{
  TextCmd* cmd = M->getUndo();

  if( ! cmd )
  {
    return;
  }

  cmd->undo();
}

void TextModelImpl::redo()
{
  TextCmd* cmd = M->getRedo();

  if( ! cmd )
  {
    return;
  }

  cmd->run();
}

bool TextModelImpl::hasUndo()
{
  return M->_cmds.size() > 0;
}

void TextModelImpl::clearUndo()
{
}