summaryrefslogtreecommitdiffstats
path: root/kate/part/katesupercursor.cpp
blob: 8a2689014781db5398c6df69fe00ea091732b5c9 (plain)
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
/* This file is part of the KDE libraries
   Copyright (C) 2003 Hamish Rodda <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "katesupercursor.h"
#include "katesupercursor.moc"

#include "katedocument.h"

#include <kdebug.h>

#include <tqobjectlist.h>

KateSuperCursor::KateSuperCursor(KateDocument* doc, bool privateC, const KateTextCursor& cursor, TQObject* parent, const char* name)
  : TQObject(parent, name)
  , KateDocCursor(cursor.line(), cursor.col(), doc)
  , Kate::Cursor ()
  , m_doc (doc)
{
  m_moveOnInsert = false;
  m_lineRemoved = false;
  m_privateCursor = privateC;

  m_doc->addSuperCursor (this, privateC);
}

KateSuperCursor::KateSuperCursor(KateDocument* doc, bool privateC, int lineNum, int col, TQObject* parent, const char* name)
  : TQObject(parent, name)
  , KateDocCursor(lineNum, col, doc)
  , Kate::Cursor ()
  , m_doc (doc)
{
  m_moveOnInsert = false;
  m_lineRemoved = false;
  m_privateCursor = privateC;

  m_doc->addSuperCursor (this, privateC);
}

KateSuperCursor::~KateSuperCursor ()
{
  m_doc->removeSuperCursor (this, m_privateCursor);
}

void KateSuperCursor::position(uint *pline, uint *pcol) const
{
  KateDocCursor::position(pline, pcol);
}

bool KateSuperCursor::setPosition(uint line, uint col)
{
  if (line == uint(-2) && col == uint(-2)) { delete this; return true; }
  return KateDocCursor::setPosition(line, col);
}

bool KateSuperCursor::insertText(const TQString& s)
{
  return KateDocCursor::insertText(s);
}

bool KateSuperCursor::removeText(uint nbChar)
{
  return KateDocCursor::removeText(nbChar);
}

TQChar KateSuperCursor::currentChar() const
{
  return KateDocCursor::currentChar();
}

bool KateSuperCursor::atStartOfLine() const
{
  return col() == 0;
}

bool KateSuperCursor::atEndOfLine() const
{
  return col() >= (int)m_doc->kateTextLine(line())->length();
}

bool KateSuperCursor::moveOnInsert() const
{
  return m_moveOnInsert;
}

void KateSuperCursor::setMoveOnInsert(bool moveOnInsert)
{
  m_moveOnInsert = moveOnInsert;
}

void KateSuperCursor::setLine(int lineNum)
{
  int tempLine = line(), tempcol = col();
  KateDocCursor::setLine(lineNum);

  if (tempLine != line() || tempcol != col())
    emit positionDirectlyChanged();
}

void KateSuperCursor::setCol(int colNum)
{
  KateDocCursor::setCol(colNum);
}

void KateSuperCursor::setPos(const KateTextCursor& pos)
{
  KateDocCursor::setPos(pos);
}

void KateSuperCursor::setPos(int lineNum, int colNum)
{
  KateDocCursor::setPos(lineNum, colNum);
}

void KateSuperCursor::editTextInserted(uint line, uint col, uint len)
{
  if (m_line == int(line))
  {
    if ((m_col > int(col)) || (m_moveOnInsert && (m_col == int(col))))
    {
      bool insertedAt = m_col == int(col);

      m_col += len;

      if (insertedAt)
        emit charInsertedAt();

      emit positionChanged();
      return;
    }
  }

  emit positionUnChanged();
}

void KateSuperCursor::editTextRemoved(uint line, uint col, uint len)
{
  if (m_line == int(line))
  {
    if (m_col > int(col))
    {
      if (m_col > int(col + len))
      {
        m_col -= len;
      }
      else
      {
        bool prevCharDeleted = m_col == int(col + len);

        m_col = col;

        if (prevCharDeleted)
          emit charDeletedBefore();
        else
          emit positionDeleted();
      }

      emit positionChanged();
      return;

    }
    else if (m_col == int(col))
    {
      emit charDeletedAfter();
    }
  }

  emit positionUnChanged();
}

void KateSuperCursor::editLineWrapped(uint line, uint col, bool newLine)
{
  if (newLine)
  {
    if (m_line > int(line) || (m_line == int(line) && m_col >= int(col)))
    {
      if(m_line == int(line))
         m_col -= col;
      m_line++;

      emit positionChanged();
      return;
    }
  }
  else if ( (m_line == int(line)) && (m_col > int(col)) || (m_moveOnInsert && (m_col == int(col))) )
  {
    m_line++;
    m_col -= col;

    emit positionChanged();
    return;
  }

  emit positionUnChanged();
}

void KateSuperCursor::editLineUnWrapped(uint line, uint col, bool removeLine, uint length)
{
  if (removeLine && (m_line > int(line+1)))
  {
    m_line--;

    emit positionChanged();
    return;
  }
  else if ( (m_line == int(line+1)) && (removeLine || (m_col < int(length))) )
  {
    m_line = line;
    m_col += col;

    emit positionChanged();
    return;
  }
  else if ( (m_line == int(line+1)) && (m_col >= int(length)) )
  {
    m_col -= length;

    emit positionChanged();
    return;
  }

  emit positionUnChanged();
}

void KateSuperCursor::editLineInserted (uint line)
{
  if (m_line >= int(line))
  {
    m_line++;

    emit positionChanged();
    return;
  }

  emit positionUnChanged();
}

void KateSuperCursor::editLineRemoved(uint line)
{
  if (m_line > int(line))
  {
    m_line--;

    emit positionChanged();
    return;
  }
  else if (m_line == int(line))
  {
    m_line = (line <= m_doc->lastLine()) ? line : (line - 1);
    m_col = 0;

    emit positionDeleted();

    emit positionChanged();
    return;
  }

  emit positionUnChanged();
}

KateSuperCursor::operator TQString()
{
  return TQString("[%1,%1]").arg(line()).arg(col());
}

KateSuperRange::KateSuperRange(KateSuperCursor* start, KateSuperCursor* end, TQObject* parent, const char* name)
  : TQObject(parent, name)
  , m_start(start)
  , m_end(end)
  , m_evaluate(false)
  , m_startChanged(false)
  , m_endChanged(false)
  , m_deleteCursors(false)
  , m_allowZeroLength(false)
{
  init();
}

KateSuperRange::KateSuperRange(KateDocument* doc, const KateRange& range, TQObject* parent, const char* name)
  : TQObject(parent, name)
  , m_start(new KateSuperCursor(doc, true, range.start()))
  , m_end(new KateSuperCursor(doc, true, range.end()))
  , m_evaluate(false)
  , m_startChanged(false)
  , m_endChanged(false)
  , m_deleteCursors(true)
  , m_allowZeroLength(false)
{
  init();
}

KateSuperRange::KateSuperRange(KateDocument* doc, const KateTextCursor& start, const KateTextCursor& end, TQObject* parent, const char* name)
  : TQObject(parent, name)
  , m_start(new KateSuperCursor(doc, true, start))
  , m_end(new KateSuperCursor(doc, true, end))
  , m_evaluate(false)
  , m_startChanged(false)
  , m_endChanged(false)
  , m_deleteCursors(true)
  , m_allowZeroLength(false)
{
  init();
}

void KateSuperRange::init()
{
  Q_ASSERT(isValid());
  if (!isValid())
    kdDebug(13020) << superStart() << " " << superEnd() << endl;

  insertChild(m_start);
  insertChild(m_end);

  setBehaviour(DoNotExpand);

  // Not necessarily the best implementation
  connect(m_start, TQT_SIGNAL(positionDirectlyChanged()),  TQT_SIGNAL(contentsChanged()));
  connect(m_end, TQT_SIGNAL(positionDirectlyChanged()),  TQT_SIGNAL(contentsChanged()));

  connect(m_start, TQT_SIGNAL(positionChanged()),  TQT_SLOT(slotEvaluateChanged()));
  connect(m_end, TQT_SIGNAL(positionChanged()),  TQT_SLOT(slotEvaluateChanged()));
  connect(m_start, TQT_SIGNAL(positionUnChanged()), TQT_SLOT(slotEvaluateUnChanged()));
  connect(m_end, TQT_SIGNAL(positionUnChanged()), TQT_SLOT(slotEvaluateUnChanged()));
  connect(m_start, TQT_SIGNAL(positionDeleted()), TQT_SIGNAL(boundaryDeleted()));
  connect(m_end, TQT_SIGNAL(positionDeleted()), TQT_SIGNAL(boundaryDeleted()));
}

KateSuperRange::~KateSuperRange()
{
  if (m_deleteCursors)
  {
    //insertChild(m_start);
    //insertChild(m_end);
    delete m_start;
    delete m_end;
  }
}

KateTextCursor& KateSuperRange::start()
{
  return *m_start;
}

const KateTextCursor& KateSuperRange::start() const
{
  return *m_start;
}

KateTextCursor& KateSuperRange::end()
{
  return *m_end;
}

const KateTextCursor& KateSuperRange::end() const
{
  return *m_end;
}

KateSuperCursor& KateSuperRange::superStart()
{
  return *m_start;
}

const KateSuperCursor& KateSuperRange::superStart() const
{
  return *m_start;
}

KateSuperCursor& KateSuperRange::superEnd()
{
  return *m_end;
}

const KateSuperCursor& KateSuperRange::superEnd() const
{
  return *m_end;
}

int KateSuperRange::behaviour() const
{
  return (m_start->moveOnInsert() ? DoNotExpand : ExpandLeft) | (m_end->moveOnInsert() ? ExpandRight : DoNotExpand);
}

void KateSuperRange::setBehaviour(int behaviour)
{
  m_start->setMoveOnInsert(behaviour & ExpandLeft);
  m_end->setMoveOnInsert(!(behaviour & ExpandRight));
}

bool KateSuperRange::isValid() const
{
  return superStart() <= superEnd();
}

bool KateSuperRange::owns(const KateTextCursor& cursor) const
{
  if (!includes(cursor)) return false;

  if (children())
    for (TQObjectListIt it(*children()); *it; ++it)
      if ((*it)->inherits("KateSuperRange"))
        if (static_cast<KateSuperRange*>(*it)->owns(cursor))
          return false;

  return true;
}

bool KateSuperRange::includes(const KateTextCursor& cursor) const
{
  return isValid() && cursor >= superStart() && cursor < superEnd();
}

bool KateSuperRange::includes(uint lineNum) const
{
  return isValid() && (int)lineNum >= superStart().line() && (int)lineNum <= superEnd().line();
}

bool KateSuperRange::includesWholeLine(uint lineNum) const
{
  return isValid() && ((int)lineNum > superStart().line() || ((int)lineNum == superStart().line() && superStart().atStartOfLine())) && ((int)lineNum < superEnd().line() || ((int)lineNum == superEnd().line() && superEnd().atEndOfLine()));
}

bool KateSuperRange::boundaryAt(const KateTextCursor& cursor) const
{
  return isValid() && (cursor == superStart() || cursor == superEnd());
}

bool KateSuperRange::boundaryOn(uint lineNum) const
{
  return isValid() && (superStart().line() == (int)lineNum || superEnd().line() == (int)lineNum);
}

void KateSuperRange::slotEvaluateChanged()
{
  if (sender() == static_cast<TQObject*>(m_start)) {
    if (m_evaluate) {
      if (!m_endChanged) {
        // Only one was changed
        evaluateEliminated();

      } else {
        // Both were changed
        evaluatePositionChanged();
        m_endChanged = false;
      }

    } else {
      m_startChanged = true;
    }

  } else {
    if (m_evaluate) {
      if (!m_startChanged) {
        // Only one was changed
        evaluateEliminated();

      } else {
        // Both were changed
        evaluatePositionChanged();
        m_startChanged = false;
      }

    } else {
      m_endChanged = true;
    }
  }

  m_evaluate = !m_evaluate;
}

void KateSuperRange::slotEvaluateUnChanged()
{
  if (sender() == static_cast<TQObject*>(m_start)) {
    if (m_evaluate) {
      if (m_endChanged) {
        // Only one changed
        evaluateEliminated();
        m_endChanged = false;

      } else {
        // Neither changed
        emit positionUnChanged();
      }
    }

  } else {
    if (m_evaluate) {
      if (m_startChanged) {
        // Only one changed
        evaluateEliminated();
        m_startChanged = false;

      } else {
        // Neither changed
        emit positionUnChanged();
      }
    }
  }

  m_evaluate = !m_evaluate;
}

void KateSuperRange::slotTagRange()
{
  emit tagRange(this);
}

void KateSuperRange::evaluateEliminated()
{
  if (superStart() == superEnd()) {
      if (!m_allowZeroLength) emit eliminated();
    }
  else
    emit contentsChanged();
}

void KateSuperRange::evaluatePositionChanged()
{
  if (superStart() == superEnd())
    emit eliminated();
  else
    emit positionChanged();
}

int KateSuperCursorList::compareItems(TQPtrCollection::Item item1, TQPtrCollection::Item item2)
{
  if (*(static_cast<KateSuperCursor*>(item1)) == *(static_cast<KateSuperCursor*>(item2)))
    return 0;

  return *(static_cast<KateSuperCursor*>(item1)) < *(static_cast<KateSuperCursor*>(item2)) ? -1 : 1;
}

KateSuperRangeList::KateSuperRangeList(bool autoManage, TQObject* parent, const char* name)
  : TQObject(parent, name)
  , m_autoManage(autoManage)
  , m_connect(true)
  , m_trackingBoundaries(false)
{
  setAutoManage(autoManage);
}

KateSuperRangeList::KateSuperRangeList(const TQPtrList<KateSuperRange>& rangeList, TQObject* parent, const char* name)
  : TQObject(parent, name)
  , m_autoManage(false)
  , m_connect(false)
  , m_trackingBoundaries(false)
{
  appendList(rangeList);
}

void KateSuperRangeList::appendList(const TQPtrList<KateSuperRange>& rangeList)
{
  for (TQPtrListIterator<KateSuperRange> it = rangeList; *it; ++it)
    append(*it);
}

void KateSuperRangeList::clear()
{
  for (KateSuperRange* range = first(); range; range = next())
    emit rangeEliminated(range);

  TQPtrList<KateSuperRange>::clear();
}

void KateSuperRangeList::connectAll()
{
  if (!m_connect) {
    m_connect = true;
    for (KateSuperRange* range = first(); range; range = next()) {
      connect(range, TQT_SIGNAL(destroyed(TQObject*)), TQT_SLOT(slotDeleted(TQObject*)));
      connect(range, TQT_SIGNAL(eliminated()), TQT_SLOT(slotEliminated()));
    }
  }
}

bool KateSuperRangeList::autoManage() const
{
  return m_autoManage;
}

void KateSuperRangeList::setAutoManage(bool autoManage)
{
  m_autoManage = autoManage;
  setAutoDelete(m_autoManage);
}

TQPtrList<KateSuperRange> KateSuperRangeList::rangesIncluding(const KateTextCursor& cursor)
{
  sort();

  TQPtrList<KateSuperRange> ret;

  for (KateSuperRange* r = first(); r; r = next())
    if (r->includes(cursor))
      ret.append(r);

  return ret;
}

TQPtrList<KateSuperRange> KateSuperRangeList::rangesIncluding(uint line)
{
  sort();

  TQPtrList<KateSuperRange> ret;

  for (KateSuperRange* r = first(); r; r = next())
    if (r->includes(line))
      ret.append(r);

  return ret;
}

bool KateSuperRangeList::rangesInclude(const KateTextCursor& cursor)
{
  for (KateSuperRange* r = first(); r; r = next())
    if (r->includes(cursor))
      return true;

  return false;
}

void KateSuperRangeList::slotEliminated()
{
  if (sender()) {
    KateSuperRange* range = static_cast<KateSuperRange*>(const_cast<TQObject*>(sender()));
    emit rangeEliminated(range);

    if (m_trackingBoundaries) {
      m_columnBoundaries.removeRef(range->m_start);
      m_columnBoundaries.removeRef(range->m_end);
    }

    if (m_autoManage)
      removeRef(range);

    if (!count())
      emit listEmpty();
  }
}

void KateSuperRangeList::slotDeleted(TQObject* range)
{
  //kdDebug(13020)<<"KateSuperRangeList::slotDeleted"<<endl;
  KateSuperRange* r = static_cast<KateSuperRange*>(range);

  if (m_trackingBoundaries) {
      m_columnBoundaries.removeRef(r->m_start);
      m_columnBoundaries.removeRef(r->m_end);
  }

  int index = findRef(r);
  if (index != -1)
    take(index);
  //else kdDebug(13020)<<"Range not found in list"<<endl;

  if (!count())
      emit listEmpty();
}

KateSuperCursor* KateSuperRangeList::firstBoundary(const KateTextCursor* start)
{
  if (!m_trackingBoundaries) {
    m_trackingBoundaries = true;

    for (KateSuperRange* r = first(); r; r = next()) {
      m_columnBoundaries.append(&(r->superStart()));
      m_columnBoundaries.append(&(r->superEnd()));
    }
  }

  m_columnBoundaries.sort();

  if (start)
    // OPTIMISE: TQMap with TQPtrList for each line? (==> sorting issues :( )
    for (KateSuperCursor* c = m_columnBoundaries.first(); c; c = m_columnBoundaries.next())
      if (*start <= *c)
        break;

  return m_columnBoundaries.current();
}

KateSuperCursor* KateSuperRangeList::nextBoundary()
{
  KateSuperCursor* current = m_columnBoundaries.current();

  // make sure the new cursor is after the current cursor; multiple cursors with the same position can be in the list.
  if (current)
    while (m_columnBoundaries.next())
      if (*(m_columnBoundaries.current()) != *current)
        break;

  return m_columnBoundaries.current();
}

KateSuperCursor* KateSuperRangeList::currentBoundary()
{
  return m_columnBoundaries.current();
}

int KateSuperRangeList::compareItems(TQPtrCollection::Item item1, TQPtrCollection::Item item2)
{
  if (static_cast<KateSuperRange*>(item1)->superStart() == static_cast<KateSuperRange*>(item2)->superStart()) {
    if (static_cast<KateSuperRange*>(item1)->superEnd() == static_cast<KateSuperRange*>(item2)->superEnd()) {
      return 0;
    } else {
      return static_cast<KateSuperRange*>(item1)->superEnd() < static_cast<KateSuperRange*>(item2)->superEnd() ? -1 : 1;
    }
  }

  return static_cast<KateSuperRange*>(item1)->superStart() < static_cast<KateSuperRange*>(item2)->superStart() ? -1 : 1;
}

TQPtrCollection::Item KateSuperRangeList::newItem(TQPtrCollection::Item d)
{
  if (m_connect) {
    connect(static_cast<KateSuperRange*>(d), TQT_SIGNAL(destroyed(TQObject*)), TQT_SLOT(slotDeleted(TQObject*)));
    connect(static_cast<KateSuperRange*>(d), TQT_SIGNAL(eliminated()), TQT_SLOT(slotEliminated()));
    connect(static_cast<KateSuperRange*>(d), TQT_SIGNAL(tagRange(KateSuperRange*)), TQT_SIGNAL(tagRange(KateSuperRange*)));

    // HACK HACK
    static_cast<KateSuperRange*>(d)->slotTagRange();
  }

  if (m_trackingBoundaries) {
    m_columnBoundaries.append(&(static_cast<KateSuperRange*>(d)->superStart()));
    m_columnBoundaries.append(&(static_cast<KateSuperRange*>(d)->superEnd()));
  }

  return TQPtrList<KateSuperRange>::newItem(d);
}

// kate: space-indent on; indent-width 2; replace-tabs on;