File: qdocumentcursor.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 (1056 lines) | stat: -rw-r--r-- 26,018 bytes parent folder | download
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
 /****************************************************************************
**
** 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.
**
****************************************************************************/

/*!
	\file qdocumentcursor.cpp
	\brief Implementation of the QDocumentCursor class
*/

#include "qdocumentcursor.h"

/*!
	\ingroup document
	@{
*/

#include "qdocument_p.h"

#include "qdocumentline.h"

/*!
	\class QDocumentCursor
	
	\brief A cursor to navigate within documents and edit them
	
	QDocumentCursor is a central class of the public API.
	
	It is the best (as in fastest and easiest) way to iterate over
	the content of a document.
	
	It is also the only class that allows to perform editing operations.
	
	A cursor position is defined by a line number and a text position
	within the line.
	
	Every cursor can have one or two cursor positions. In the later
	case, they delimit a selection. The first position set (before
	selecting) is referred to as the "anchor" and the other (if it
	is different from the anchor) is the actual cursor position.
	
	When the cursor does not have a selection, querying informations about
	the anchor has the same result as querying informations about the cursor
	position.
	
	Informations you can get about both the anchor and the posiotion :
	<ul>
	<li>the line number : the logical line to which the cursor position points inside the document
	<li>the column number : the logical text column to which the cursor position points to, inside the pointed line.
	<li>the wrapped line offset : when a cursor resides on a wrapped line, this indicates in which part of
	the wrapped line it does
	<li>the document position : document (x, y) coordinates corresponding to the place the cursor is drawn
	</ul>
	
	The visual line to which a given cursor resides can be obtained as follows :
	
	\code
	int visual_line = cursor.document()->visualLine(cursor.lineNumber()) + cursor.wrappedLineOffset();
	\endcode
	
	\note The line and column numbers passed to/returned by a cursor method
	always start at zero.

	\note Quick overview of the various coordinate systems :
	<ul>
		<li>document coordinates aka viewport coordinates : (x, y) coords, in pixels, origin at the top left corner of
		the rectangle occupied by the very first line of the document
		<li>text coordinates : (line, column) in logical units (number of lines, number of characters)
		<li>visual text coordinates : (line, column) in logical units but with a different mapping
	</ul>
*/

QDocumentCursor::QDocumentCursor(QDocument *doc)
 : QObject(0),m_handle(new QDocumentCursorHandle(doc))
{
	m_handle->ref();
}

QDocumentCursor::QDocumentCursor(const QDocumentCursor& from, const QDocumentCursor& to): QObject(0){
	Q_ASSERT(from.document() == to.document());
	Q_ASSERT(from.handle());
	Q_ASSERT(to.handle());
	Q_ASSERT(from.hasSelection() == false);
	Q_ASSERT(to.hasSelection() == false);

	m_handle = new QDocumentCursorHandle(from.document());
	m_handle->m_endLine = from.lineNumber();
	m_handle->m_endOffset = from.columnNumber();
	m_handle->m_begLine = to.lineNumber();
	m_handle->m_begOffset = to.columnNumber();
}

QDocumentCursor::QDocumentCursor(const QDocumentCursor& cursor)
 : QObject(0),m_handle(0)
{
	if ( cursor.m_handle )
	{
		m_handle = cursor.m_handle->clone(true);
		m_handle->ref();
	}
}

QDocumentCursor::QDocumentCursor(const QDocumentCursor& cursor, const bool cloneAutoUpdateFlag)
 : QObject(0),m_handle(0)
{
	if ( cursor.m_handle )
	{
		m_handle = cursor.m_handle->clone(cloneAutoUpdateFlag);
		m_handle->ref();
	}
}



QDocumentCursor::QDocumentCursor(QDocument *doc, int line, int column, int lineTo, int columnTo)
 : QObject(0),m_handle(new QDocumentCursorHandle(doc, line, column, lineTo, columnTo))
{
	m_handle->ref();
}

/*
QDocumentCursor::QDocumentCursor(const QDocumentLine& line, int column)
 : m_handle(new QDocumentCursorHandle(line.document(), line.lineNumber()))
{
	m_handle->ref();
	
	m_handle->setColumnNumber(column);
	//movePosition(qMin(column, line.length()));
}
*/

QDocumentCursor::QDocumentCursor(QDocumentCursorHandle *handle)
 : QObject(0),m_handle(handle)
{
	if ( m_handle )
		m_handle->ref();
}

QDocumentCursor::~QDocumentCursor()
{
	if ( m_handle )
		m_handle->deref();
}

QDocumentCursor QDocumentCursor::clone(bool cloneAutoUpdatedFlag) const
{
	return m_handle ? QDocumentCursor(m_handle->clone(cloneAutoUpdatedFlag)) : QDocumentCursor();
}

QDocumentCursor& QDocumentCursor::operator = (const QDocumentCursor& c)
{
	#if 0
	if ( m_handle )
		m_handle->deref();
	
	m_handle = c.m_handle ? c.m_handle->clone() : 0;
	//m_handle = c.m_handle;
	
	if ( m_handle )
		m_handle->ref();
	#endif
	
	if ( c.m_handle )
	{
		if ( m_handle )
		{
			m_handle->copy(c.m_handle); //warning: this is inconsistent, a copied cursor is never auto updated,
		} else {                            //but the clones is. (there was however a reason for this behaviour)
			m_handle = c.m_handle->clone(true);
			m_handle->ref();
		}
	} else if ( m_handle ) {
		
		//qWarning("Setting a cursor to null");
		
		m_handle->deref();
		m_handle = 0;
	}
	
	return *this;
}

/*!
	\brief comparision operator
	
	\note If any of the operand is an invalid cursor, false is returned
*/
bool QDocumentCursor::operator == (const QDocumentCursor& c) const
{
	if ( !m_handle || !c.m_handle )
		return false;
	
	return m_handle->eq(c.m_handle);
}

/*!
	\brief comparision operator
	
	\note If any of the operand is an invalid cursor, true is returned (to preserve logical consistency with == )
*/
bool QDocumentCursor::operator != (const QDocumentCursor& c) const
{
	if ( !m_handle || !c.m_handle )
		return true;
	
	return !m_handle->eq(c.m_handle);
}

/*!
	\brief comparision operator
	
	\note If any of the operand is an invalid cursor, false is returned
*/
bool QDocumentCursor::operator < (const QDocumentCursor& c) const
{
	if ( !m_handle || !c.m_handle )
		return false;
	
	return m_handle->lt(c.m_handle);
}

/*!
	\brief comparision operator
	
	\note If any of the operand is an invalid cursor, false is returned
*/
bool QDocumentCursor::operator > (const QDocumentCursor& c) const
{
	if ( !m_handle || !c.m_handle )
		return false;
	
	return m_handle->gt(c.m_handle);
}

/*!
	\brief comparision operator
	
	\note If any of the operand is an invalid cursor, false is returned
*/
bool QDocumentCursor::operator <= (const QDocumentCursor& c) const
{
	if ( !m_handle || !c.m_handle )
		return false;
	
	return m_handle->lt(c.m_handle) || m_handle->eq(c.m_handle);
}

/*!
	\brief comparision operator
	
	\note If any of the operand is an invalid cursor, false is returned
*/
bool QDocumentCursor::operator >= (const QDocumentCursor& c) const
{
	if ( !m_handle || !c.m_handle )
		return false;
	
	return m_handle->gt(c.m_handle) || m_handle->eq(c.m_handle);
}

/*!
	\brief comparision operator
*/
bool QDocumentCursor::isNull() const
{
	return !m_handle || !m_handle->document() || !line().isValid();
}

bool QDocumentCursor::beginBoundaryLarger (const QDocumentCursor& c) const{
	if (!m_handle || !c.m_handle) return false;
	int line1, col1;
	m_handle->beginBoundary(line1,col1);
	int line2, col2;
	c.m_handle->beginBoundary(line2,col2);
	if (line1==line2)
		return col1>col2;
	else 
		return line1>line2;	
}

bool QDocumentCursor::equalBoundaries(const QDocumentCursor& c) const{
	if (!m_handle) return false;
	if (m_handle == c.m_handle) return true;
	return m_handle->equalBoundaries(c.m_handle);
}

bool QDocumentCursor::equal(const QDocumentCursor& c) const{
	if (!m_handle) return false;
	if (m_handle == c.m_handle) return true;
	return m_handle->equal(c.m_handle);
}


/*!
  Returns if my right boundary is larger (line/offset) than c
*/
bool QDocumentCursor::endBoundaryLarger (const QDocumentCursor& c) const{
	if (!m_handle || !c.m_handle) return false;
	int line1, col1;
	m_handle->endBoundary(line1,col1);
	int line2, col2;
	c.m_handle->endBoundary(line2,col2);
	if (line1==line2)
		return col1>col2;
	else 
		return line1>line2;	
}

void QDocumentCursor::beginBoundary(int& begline, int& begcol) const{
	if (!m_handle) {
		begline=-1;
		begcol=-1;
		return;
	}
	m_handle->beginBoundary(begline, begcol);
}
void QDocumentCursor::endBoundary(int& endline, int& endcol) const{
	if (!m_handle) {
		endline=-1;
		endcol=-1;
		return;
	}
	m_handle->endBoundary(endline, endcol);
}
void QDocumentCursor::boundaries(int& begline, int& begcol, int& endline, int& endcol) const{
	if (!m_handle) {
		begline=begcol=endline=endcol=-1;
		return;
	}
	m_handle->boundaries(begline,begcol,endline,endcol);
}


/*!
	\brief comparision operator
*/
bool QDocumentCursor::isValid() const
{
	return m_handle && m_handle->document() && line().isValid();
}

/*!
	\return whether the cursor is at the end of the document
*/
bool QDocumentCursor::atEnd() const
{
	return m_handle ? m_handle->atEnd() : false;
}

/*!
	\return whether the cursor is at the begining of the document
*/
bool QDocumentCursor::atStart() const
{
	return m_handle ? m_handle->atStart() : false;
}

/*!
	\return whether the cursor is at the end of a block
*/
bool QDocumentCursor::atBlockEnd() const
{
	return m_handle ? m_handle->atBlockEnd() : false;
}

/*!
	\return whether the cursor is at the start of a block
*/
bool QDocumentCursor::atBlockStart() const
{
	return m_handle ? m_handle->atBlockStart() : false;
}

/*!
	\return whether the cursor is at the end of a line
	
	\note this may only differ from atBlockEnd() on wrapped lines
*/
bool QDocumentCursor::atLineEnd() const
{
	return m_handle ? m_handle->atLineEnd() : false;
}

/*!
	\return whether the cursor is at the start of a line
	
	\note this may only differ from atBlockStart() on wrapped lines
*/
bool QDocumentCursor::atLineStart() const
{
	return m_handle ? m_handle->atLineStart() : false;
}

/*!
	\return the document on which this cursor operates
*/
QDocument* QDocumentCursor::document() const
{
	return m_handle ? m_handle->document() : 0;
}

/*!
	\return the text column of the anchor
*/
int QDocumentCursor::anchorColumnNumber() const
{
	return m_handle ? m_handle->anchorColumnNumber() : -1;
}

int QDocumentCursor::startLineNumber() const{
	return m_handle ? m_handle->startLineNumber() : -1;
}
int QDocumentCursor::startColumnNumber() const{
	return m_handle ? m_handle->startColumnNumber() : -1;
}

int QDocumentCursor::endLineNumber() const{
	return m_handle ? m_handle->endLineNumber() : -1;
}
int QDocumentCursor::endColumnNumber() const{
	return m_handle ? m_handle->endColumnNumber() : -1;
}


/*!
	\return the "visual" text column of the cursor
	
	\note this may only differ from columnNumber() when there are tabs on the line
*/
int QDocumentCursor::visualColumnNumber() const
{
	return m_handle ? m_handle->visualColumnNumber() : -1;
}

/*!
	\return the text column of the cursor
*/
int QDocumentCursor::columnNumber() const
{
	return m_handle ? m_handle->columnNumber() : -1;
}


void QDocumentCursor::setLineNumber(int l, MoveMode m)
{
	if ( m_handle )
		m_handle->setLineNumber(l, m);
}

/*!
	\brief Set the text column of the cursor
	\param c text column to set
	\param m move mode (determines whether text will be selected)
*/
void QDocumentCursor::setColumnNumber(int c, MoveMode m)
{
	if ( m_handle )
		m_handle->setColumnNumber(c, m);
}

void QDocumentCursor::setAnchorLineNumber(int c) const{
	if ( m_handle )
		m_handle->setAnchorLineNumber(c);
}

void QDocumentCursor::setAnchorColumnNumber(int c) const{
	if ( m_handle )
		m_handle->setAnchorColumnNumber(c);
}


/*!
	\return The line number to which the cursor points
*/
int QDocumentCursor::lineNumber() const
{
	return m_handle ? m_handle->lineNumber() : -1;
}

/*!
	\return The line number to which the cursor points
*/
int QDocumentCursor::anchorLineNumber() const
{
	return m_handle ? m_handle->anchorLineNumber() : -1;
}

/*!
	\return The wrapped line on which the cursor resides
	
	Wrapped line are "sublines" of logical lines.
*/
int QDocumentCursor::wrappedLineOffset() const
{
	return line().wrappedLineForCursor(columnNumber());
}

/*!
	\return The line number on which the anchor resides
*/
int QDocumentCursor::anchorWrappedLineOffset() const
{
	return anchorLine().wrappedLineForCursor(anchorColumnNumber());
}

/*!
	\return the document position at which the cursor is
	
	Document position and viewport position are two terms used interchangeably.
	The only difference is the former refers to model perception (QDocument)
	whereas the later refers to view perception (QEditor)
*/
QPoint QDocumentCursor::documentPosition() const
{
	return m_handle ? m_handle->documentPosition() : QPoint();
}

/*!
	\return the document position of the anchor
*/
QPoint QDocumentCursor::anchorDocumentPosition() const
{
	return m_handle ? m_handle->anchorDocumentPosition() : QPoint();
}

QPolygon QDocumentCursor::documentRegion() const
{
	return m_handle ? m_handle->documentRegion() : QPolygon();
}

/*!
	\return The line object on which the cursor resides
*/
QDocumentLine QDocumentCursor::line() const
{
	return m_handle ? m_handle->line() : QDocumentLine();
}

/*!
	\return The line object on which the anchor resides
*/
QDocumentLine QDocumentCursor::anchorLine() const
{
	return m_handle ? m_handle->anchorLine() : QDocumentLine();
}

/*!
	\brief Shift cursor position (text column) by a number of columns (characters)
*/
void QDocumentCursor::shift(int offset)
{
	if ( m_handle )
		m_handle->shift(offset);
}

/*!
	\brief Set the text position of the cursor (within the whole document)
	
	Remark made about position() applies.
*/
void QDocumentCursor::setPosition(int pos, MoveMode m)
{
	if ( m_handle )
		m_handle->setPosition(pos, m);
}

/*!
	\brief Moves the cursor position
	\param offset number of times the selected move will be done
	\param op movement type
	\param m movement mode (whether to select)
	\return true on succes
*/
bool QDocumentCursor::movePosition(int offset, MoveOperation op, MoveMode m)
{
	return m_handle ? m_handle->movePosition(offset, op, m) : false;
}

/*!
	\brief Jump to another cursor position
	\param line target line number
	\param colum target text column
*/
void QDocumentCursor::moveTo(int line, int column, MoveMode m)
{
	if ( m_handle )
		m_handle->moveTo(line, column,m);
}


/*!
	\brief Jump to the position of another cursor
	\param c target cursor
*/
void QDocumentCursor::moveTo(const QDocumentCursor &c, MoveMode m)
{
	if ( m_handle )
		m_handle->moveTo(c,m);
}

/*!
	\brief Jump to another cursor position
	\param l target line
	\param column target text column
	
	\note Calls QDocumentLine::lineNumber() => SLOW : avoid whenever possible
*/
/*void QDocumentCursor::moveTo(const QDocumentLine &l, int column, MoveMode m)
{
	if ( m_handle )
		m_handle->moveTo(l.lineNumber(), column, m);
}*/

/*!
	\return the character at the position immediately after the cursor
*/
QChar QDocumentCursor::nextChar() const
{
	return m_handle ? m_handle->nextChar() : QChar();
}

/*!
	\return the character at the position immediately before the cursor
*/
QChar QDocumentCursor::previousChar() const
{
	return m_handle ? m_handle->previousChar() : QChar();
}

/*!
	\brief Delete the character at the position immediately after the cursor
*/
void QDocumentCursor::deleteChar()
{
	if ( m_handle )
		m_handle->deleteChar();
}

/*!
	\brief Delete the character at the position immediately before the cursor
*/
void QDocumentCursor::deletePreviousChar()
{
	if ( m_handle )
		m_handle->deletePreviousChar();
}

/*!
	\brief erase the whole line the cursor is on, newline included
*/
void QDocumentCursor::eraseLine()
{
	if ( m_handle )
		m_handle->eraseLine();
}

/*!
	\brief insert a new line at the cursor position
*/
void QDocumentCursor::insertLine(bool keepAnchor)
{
	if ( m_handle )
		m_handle->insertText("\n", keepAnchor);
}

/*!
	\brief insert some text at the cursor position
	
	Selected text will be removed before insertion happens.
	
	\note Nothing happens if \a s is empty
*/
void QDocumentCursor::insertText(const QString& s, bool keepAnchor)
{
	if ( m_handle )
		m_handle->insertText(s, keepAnchor);
}

/*!
	\return A cursor pointing at the position of the selection start.
	
	Selection start is the position of the selection that is nearest to document start.
	
	\note an invalid cursor is returned when the cursor does not have a selection
*/
QDocumentCursor QDocumentCursor::selectionStart() const
{
	return m_handle ? m_handle->selectionStart() : QDocumentCursor();
}

/*!
	\return A cursor pointing at the position of the selection end.
	
	Selection end is the position of the selection that is nearest to document end.
	
	\note an invalid cursor is returned when the cursor does not have a selection
*/
QDocumentCursor QDocumentCursor::selectionEnd() const
{
	return m_handle ? m_handle->selectionEnd() : QDocumentCursor();
}

/*!
	\return A cursor pointing at the position of the anchor.
*/
QDocumentCursor QDocumentCursor::anchorCursor() const
{
	return m_handle ? m_handle->anchorCursor() : QDocumentCursor();
}

/*!
	\return The selected text
*/
QString QDocumentCursor::selectedText() const
{
	return m_handle ? m_handle->selectedText() : QString();
}


/*!
	\brief Remove the selected text
*/
void QDocumentCursor::removeSelectedText()
{
	if ( m_handle )
		m_handle->removeSelectedText();
}

/*!
	\brief Replace the selected text
	
	This method differs from insertText() in two ways :
	<ul>
		<li>if \a text is empty the selection WILL be removed
		<li>after the replacement happens this command will
		cause the cursor to select the new text (note that this
		information is NOT preserved by the undo/redo stack
		however).
	</ul>
*/
void QDocumentCursor::replaceSelectedText(const QString& text)
{
	if ( m_handle )
		m_handle->replaceSelectedText(text);
}

/*!
	\brief Begin an edit block
	
	Edit blocks are command groups. All the commands in an edit block
	are executed in a row when the edit block is ended with endEditBlock().
	
	Edit blocks are considered as a single command as far as the undo/redo
	stack is concerned.
	
	Edit blocks can be nested but that isn't of much use
*/
void QDocumentCursor::beginEditBlock()
{
	if ( m_handle )
		m_handle->beginEditBlock();
}

/*!
	\brief End an edit block
*/
void QDocumentCursor::endEditBlock()
{
	if ( m_handle )
		m_handle->endEditBlock();
}

/*!
	\return Whether the cursor is silent
*/
bool QDocumentCursor::isSilent() const
{
	return m_handle ? m_handle->isSilent() : true;
}

/*!
	\brief Set whether the cursor is silent
*/
void QDocumentCursor::setSilent(bool y)
{
	if ( m_handle )
		m_handle->setSilent(y);
	
}

/*!
	\return whether the cursor is auto updated
	
	An auto updated cursor will remain on the actual line it points
	to when the document is modified.
	
	\code
	QDocumentCursor c1(10, 0, document), c2(10, 0, document), c(5, 0, document);
	
	c1.setAutoUpdated(true);
	
	c.insertLine();
	
	// at this point c2 still points to line 10 whereas c1 points to line 11
	\endcode
*/
bool QDocumentCursor::isAutoUpdated() const
{
	return m_handle ? m_handle->isAutoUpdated() : true;
}

/*!
	\brief Set whether the cursor is aut updated
*/
void QDocumentCursor::setAutoUpdated(bool y)
{
	if ( m_handle )
		m_handle->setAutoUpdated(y);
}

bool QDocumentCursor::isAutoErasable() const{
	return m_handle ? m_handle->isAutoErasable() : true;
}
void QDocumentCursor::setAutoErasable(bool y){
	if ( m_handle )
		m_handle->setAutoErasable(y);
}


/*!
	\brief Refresh the column memory of the cursor
	
	This set the current column memory to the current column position.
	
	\note It is not recommended to call that yourself. The various
	movement method should do that perfectly fine.
*/
void QDocumentCursor::refreshColumnMemory()
{
	if ( m_handle )
		m_handle->refreshColumnMemory();
	
}

/*!
	\return Whether the cursor has column memory
	
	The column memory is a feature that allow a cursor
	to remember its biggest column number so that moving
	back and forth (with movePosition()) on lines of
	different width does not result in the column to change.
	
*/
bool QDocumentCursor::hasColumnMemory() const
{
	return m_handle ? m_handle->hasColumnMemory() : false;
}

/*!
	\brief Set whether the cursor use column memory
*/
void QDocumentCursor::setColumnMemory(bool y)
{
	if ( m_handle )
		m_handle->setColumnMemory(y);
	
}

/*!
	\return whether the cursor has a selection
*/
bool QDocumentCursor::hasSelection() const
{
	return m_handle ? m_handle->hasSelection() : false;
}

/*!
 * \return true if there is an selection and the anchor is before the cursor
 */
bool QDocumentCursor::isForwardSelection() const
{
	return m_handle ? m_handle->isForwardSelection() : false;
}

/*!
	\brief clear the selection
*/
void QDocumentCursor::clearSelection()
{
	if ( m_handle )
		m_handle->clearSelection();
	
}

/*!
 * \brief exchanges cursor and anchor positions
 */
void QDocumentCursor::flipSelection() {
	if ( m_handle )
		m_handle->flipSelection();
}

/*!
	\brief Select something
*/
void QDocumentCursor::select(SelectionType t)
{
	if ( m_handle )
		m_handle->select(t);
	
}

/*!
	\brief Expand a current selection
*/
void QDocumentCursor::expandSelect(QDocumentCursor::SelectionType t)
{
	if ( m_handle )
		m_handle->expandSelect(t);
}

/*!
	\brief Set the selection boundary
	
	Select text between the current cursor anchor and the one
	of \a c.
	
	\note We mean ANCHOR. If the cursor already has a selection the
	anchor will not change, but the position will be set to that of
	\a c.
*/
void QDocumentCursor::setSelectionBoundary(const QDocumentCursor& c)
{
	if ( m_handle )
		m_handle->setSelectionBoundary(c);	
}

void QDocumentCursor::select(int line, int column, int lineTo, int columnTo){
	if ( m_handle )
		m_handle->select(line, column, lineTo, columnTo);
}

void QDocumentCursor::selectColumns(int column, int columnTo){
	if ( m_handle )
		m_handle->select(lineNumber(), column, lineNumber(), columnTo);
}

/*!
	\return whether the given cursor c is within the selection of this (ignores selection of c)
*/
bool QDocumentCursor::isWithinSelection(const QDocumentCursor& c) const
{
	return m_handle ? m_handle->isWithinSelection(c) : false;
}

/*!
  Creates a new cursor whose selection is the largest region which is contained in the selection of both
  (returns c if c has no selection but is within the selection of c and returns an invalid cursor if
  this has no selection)
*/
QDocumentCursor QDocumentCursor::intersect(const QDocumentCursor& c) const{
	return m_handle ? m_handle->intersect(c) : QDocumentCursor();
}

/*!
  Sets the selection given by lbeg/cbeg/lend/cend to the largest selection which is contained in
  the selection of this cursor and the passed boundaries
  (it sets all to -1 if such a selection doesn't exists)
*/
void QDocumentCursor::intersectBoundaries(int& lbeg, int& cbeg, int& lend, int& cend) const{
	if (m_handle)
		m_handle->intersectBoundaries(lbeg, cbeg, lend, cend);
}



void QDocumentCursor::getMatchingPair(QDocumentCursor& from, QDocumentCursor& to, bool maximal) const{
	if (m_handle)
		m_handle->getMatchingPair(from, to, maximal);
	else
		from = to = QDocumentCursor();

}

/*!
	\return selection information
	
	\note The QDocumentSelection object is not updated if the selection
	changes later on : use it right away and do not store it.
*/
QDocumentSelection QDocumentCursor::selection() const
{
	QDocumentSelection s;
	
	if ( isNull() || !hasSelection() )
	{
		qDebug("NULL selection");
		
		s.startLine = -1;
		s.endLine = -1;
		
		s.start = -1;
		s.end = -1;
	} else if ( m_handle->m_begLine == m_handle->m_endLine ) {
		
		s.startLine = m_handle->m_begLine;
		s.endLine = m_handle->m_begLine;
		
		s.start = qMin(m_handle->m_begOffset, m_handle->m_endOffset);
		s.end = qMax(m_handle->m_begOffset, m_handle->m_endOffset);
		
	} else if ( m_handle->m_begLine > m_handle->m_endLine ) {
		
		s.startLine = m_handle->m_endLine;
		s.endLine = m_handle->m_begLine;
		
		s.start = m_handle->m_endOffset;
		s.end = m_handle->m_begOffset;
		
		//qDebug("[(%i,%i);(%i,%i)]", s.startLine.lineNumber(), s.start, s.endLine.lineNumber(), s.end);
	} else {
		s.startLine = m_handle->m_begLine;
		s.endLine = m_handle->m_endLine;
		
		s.start = m_handle->m_begOffset;
		s.end = m_handle->m_endOffset;
		
		//qDebug("[(%i,%i);(%i,%i)]", s.startLine.lineNumber(), s.start, s.endLine.lineNumber(), s.end);
	}
	
	return s;
}

bool QDocumentCursor::isRTL() const{
	return m_handle && m_handle->isRTL();
}

void QDocumentCursor::sort(QDocumentCursor& from, QDocumentCursor& to){
	if (from < to) return;
	QDocumentCursorHandle* temp = to.m_handle;
	to.m_handle = from.m_handle;
	from.m_handle = temp;
}

/*! @} */