-
Notifications
You must be signed in to change notification settings - Fork 262
/
Fl_Text_Buffer.cxx
1900 lines (1632 loc) · 47.3 KB
/
Fl_Text_Buffer.cxx
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
//
// "$Id$"
//
// Copyright 2001-2017 by Bill Spitzak and others.
// Original code Copyright Mark Edel. Permission to distribute under
// the LGPL for the FLTK library granted by Mark Edel.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
//
#include <stdio.h>
#include <stdlib.h>
#include <FL/fl_utf8.h>
#include "flstring.h"
#include <ctype.h>
#include <FL/Fl.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/fl_ask.H>
/*
This file is based on a port of NEdit to FLTK many years ago. NEdit at that
point was already stretched beyond the task it was designed for which explains
why the source code is sometimes pretty convoluted. It still is a very useful
widget for FLTK, and we are thankful that the nedit team allowed us to
integrate their code.
With the introduction of Unicode and UTF-8, Fl_Text_... has to go into a whole
new generation of code. Originally designed for monospaced fonts only, many
features make less sense in the multibyte and multiwidth world of UTF-8.
Columns are a good example. There is simply no such thing. The new Fl_Text_...
widget converts columns to pixels by multiplying them with the average
character width for a given font.
Rectangular selections were rarely used (if at all) and make little sense when
using variable width fonts. They have been removed.
Using multiple spaces to emulate tab stops has been replaced by pixel counting
routines. They are slower, but give the expected result for proportional fonts.
And constantly recalculating character widths is just much too expensive. Lines
of text are now subdivided into blocks of text which are measured at once
instead of individual characters.
*/
#ifndef min
static int max(int i1, int i2)
{
return i1 >= i2 ? i1 : i2;
}
static int min(int i1, int i2)
{
return i1 <= i2 ? i1 : i2;
}
#endif
static char *undobuffer;
static int undobufferlength;
static Fl_Text_Buffer *undowidget;
static int undoat; // points after insertion
static int undocut; // number of characters deleted there
static int undoinsert; // number of characters inserted
static int undoyankcut; // length of valid contents of buffer, even if undocut=0
/*
Resize the undo buffer to match at least the requested size.
*/
static void undobuffersize(int n)
{
if (n > undobufferlength) {
if (undobuffer) {
do {
undobufferlength *= 2;
} while (undobufferlength < n);
undobuffer = (char *) realloc(undobuffer, undobufferlength);
} else {
undobufferlength = n + 9;
undobuffer = (char *) malloc(undobufferlength);
}
}
}
static void def_transcoding_warning_action(Fl_Text_Buffer *text)
{
fl_alert("%s", text->file_encoding_warning_message);
}
/*
Initialize all variables.
*/
Fl_Text_Buffer::Fl_Text_Buffer(int requestedSize, int preferredGapSize)
{
mLength = 0;
mPreferredGapSize = preferredGapSize;
mBuf = (char *) malloc(requestedSize + mPreferredGapSize);
mGapStart = 0;
mGapEnd = requestedSize + mPreferredGapSize;
mTabDist = 8;
mPrimary.mSelected = 0;
mPrimary.mStart = mPrimary.mEnd = 0;
mSecondary.mSelected = 0;
mSecondary.mStart = mSecondary.mEnd = 0;
mHighlight.mSelected = 0;
mHighlight.mStart = mHighlight.mEnd = 0;
mModifyProcs = NULL;
mCbArgs = NULL;
mNModifyProcs = 0;
mNPredeleteProcs = 0;
mPredeleteProcs = NULL;
mPredeleteCbArgs = NULL;
mCursorPosHint = 0;
mCanUndo = 1;
input_file_was_transcoded = 0;
transcoding_warning_action = def_transcoding_warning_action;
}
/*
Free all resources.
*/
Fl_Text_Buffer::~Fl_Text_Buffer()
{
free(mBuf);
if (mNModifyProcs != 0) {
delete[]mModifyProcs;
delete[]mCbArgs;
}
if (mNPredeleteProcs > 0) {
delete[] mPredeleteProcs;
delete[] mPredeleteCbArgs;
}
}
/*
This function copies verbose whatever is in front and after the gap into a
single buffer.
*/
char *Fl_Text_Buffer::text() const {
char *t = (char *) malloc(mLength + 1);
memcpy(t, mBuf, mGapStart);
memcpy(t+mGapStart, mBuf+mGapEnd, mLength - mGapStart);
t[mLength] = '\0';
return t;
}
/*
Set the text buffer to a new string.
*/
void Fl_Text_Buffer::text(const char *t)
{
IS_UTF8_ALIGNED(t)
// if t is null then substitute it with an empty string
// then don't return so that internal cleanup can happen
if (!t) t="";
call_predelete_callbacks(0, length());
/* Save information for redisplay, and get rid of the old buffer */
const char *deletedText = text();
int deletedLength = mLength;
free((void *) mBuf);
/* Start a new buffer with a gap of mPreferredGapSize at the end */
int insertedLength = (int) strlen(t);
mBuf = (char *) malloc(insertedLength + mPreferredGapSize);
mLength = insertedLength;
mGapStart = insertedLength;
mGapEnd = mGapStart + mPreferredGapSize;
memcpy(mBuf, t, insertedLength);
/* Zero all of the existing selections */
update_selections(0, deletedLength, 0);
/* Call the saved display routine(s) to update the screen */
call_modify_callbacks(0, deletedLength, insertedLength, 0, deletedText);
free((void *) deletedText);
}
/*
Creates a range of text to a new buffer and copies verbose from around the gap.
*/
char *Fl_Text_Buffer::text_range(int start, int end) const {
IS_UTF8_ALIGNED2(this, (start))
IS_UTF8_ALIGNED2(this, (end))
char *s = NULL;
/* Make sure start and end are ok, and allocate memory for returned string.
If start is bad, return "", if end is bad, adjust it. */
if (start < 0 || start > mLength)
{
s = (char *) malloc(1);
s[0] = '\0';
return s;
}
if (end < start) {
int temp = start;
start = end;
end = temp;
}
if (end > mLength)
end = mLength;
int copiedLength = end - start;
s = (char *) malloc(copiedLength + 1);
/* Copy the text from the buffer to the returned string */
if (end <= mGapStart) {
memcpy(s, mBuf + start, copiedLength);
} else if (start >= mGapStart) {
memcpy(s, mBuf + start + (mGapEnd - mGapStart), copiedLength);
} else {
int part1Length = mGapStart - start;
memcpy(s, mBuf + start, part1Length);
memcpy(s + part1Length, mBuf + mGapEnd, copiedLength - part1Length);
}
s[copiedLength] = '\0';
return s;
}
/*
Return a UCS-4 character at the given index.
Pos must be at a character boundary.
*/
unsigned int Fl_Text_Buffer::char_at(int pos) const {
if (pos < 0 || pos >= mLength)
return '\0';
IS_UTF8_ALIGNED2(this, (pos))
const char *src = address(pos);
return fl_utf8decode(src, 0, 0);
}
/*
Return the raw byte at the given index.
This function ignores all unicode encoding.
*/
char Fl_Text_Buffer::byte_at(int pos) const {
if (pos < 0 || pos >= mLength)
return '\0';
const char *src = address(pos);
return *src;
}
/*
Insert some text at the given index.
Pos must be at a character boundary.
*/
void Fl_Text_Buffer::insert(int pos, const char *text)
{
IS_UTF8_ALIGNED2(this, (pos))
IS_UTF8_ALIGNED(text)
/* check if there is actually any text */
if (!text || !*text)
return;
/* if pos is not contiguous to existing text, make it */
if (pos > mLength)
pos = mLength;
if (pos < 0)
pos = 0;
/* Even if nothing is deleted, we must call these callbacks */
call_predelete_callbacks(pos, 0);
/* insert and redisplay */
int nInserted = insert_(pos, text);
mCursorPosHint = pos + nInserted;
IS_UTF8_ALIGNED2(this, (mCursorPosHint))
call_modify_callbacks(pos, 0, nInserted, 0, NULL);
}
/**
Can be used by subclasses that need their own printf() style functionality.
e.g. Fl_Simple_Terminal::printf() would wrap around this method.
\note The expanded string is currently limited to 1024 characters.
\param[in] fmt is a printf format string for the message text.
\param[in] ap is a va_list created by va_start() and closed with va_end(),
which the caller is responsible for handling.
*/
void Fl_Text_Buffer::vprintf(const char *fmt, va_list ap) {
char buffer[1024]; // XXX: 1024 should be user configurable
::vsnprintf(buffer, 1024, fmt, ap);
buffer[1024-1] = 0; // XXX: MICROSOFT
append(buffer);
}
/**
Appends printf formatted messages to the end of the buffer.
Example:
\code
#include <FL/Fl_Text_Display.H>
int main(..) {
:
// Create a text display widget and assign it a text buffer
Fl_Text_Display *tdsp = new Fl_Text_Display(..);
Fl_Text_Buffer *tbuf = new Fl_Text_Buffer();
tdsp->buffer(tbuf);
:
// Append three lines of formatted text to the buffer
tbuf->printf("The current date is: %s.\nThe time is: %s\n", date_str, time_str);
tbuf->printf("The current PID is %ld.\n", (long)getpid());
:
\endcode
\note The expanded string is currently limited to 1024 characters.
\param[in] fmt is a printf format string for the message text.
*/
void Fl_Text_Buffer::printf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
Fl_Text_Buffer::vprintf(fmt,ap);
va_end(ap);
}
/*
Replace a range of text with new text.
Start and end must be at a character boundary.
*/
void Fl_Text_Buffer::replace(int start, int end, const char *text)
{
// Range check...
if (!text)
return;
if (start < 0)
start = 0;
if (end > mLength)
end = mLength;
IS_UTF8_ALIGNED2(this, (start))
IS_UTF8_ALIGNED2(this, (end))
IS_UTF8_ALIGNED(text)
call_predelete_callbacks(start, end - start);
const char *deletedText = text_range(start, end);
remove_(start, end);
int nInserted = insert_(start, text);
mCursorPosHint = start + nInserted;
call_modify_callbacks(start, end - start, nInserted, 0, deletedText);
free((void *) deletedText);
}
/*
Remove a range of text.
Start and End must be at a character boundary.
*/
void Fl_Text_Buffer::remove(int start, int end)
{
/* Make sure the arguments make sense */
if (start > end) {
int temp = start;
start = end;
end = temp;
}
if (start > mLength)
start = mLength;
if (start < 0)
start = 0;
if (end > mLength)
end = mLength;
if (end < 0)
end = 0;
IS_UTF8_ALIGNED2(this, (start))
IS_UTF8_ALIGNED2(this, (end))
if (start == end)
return;
call_predelete_callbacks(start, end - start);
/* Remove and redisplay */
const char *deletedText = text_range(start, end);
remove_(start, end);
mCursorPosHint = start;
call_modify_callbacks(start, end - start, 0, 0, deletedText);
free((void *) deletedText);
}
/*
Copy a range of text from another text buffer.
fromStart, fromEnd, and toPos must be at a character boundary.
*/
void Fl_Text_Buffer::copy(Fl_Text_Buffer * fromBuf, int fromStart,
int fromEnd, int toPos)
{
IS_UTF8_ALIGNED2(fromBuf, fromStart)
IS_UTF8_ALIGNED2(fromBuf, fromEnd)
IS_UTF8_ALIGNED2(this, (toPos))
int copiedLength = fromEnd - fromStart;
/* Prepare the buffer to receive the new text. If the new text fits in
the current buffer, just move the gap (if necessary) to where
the text should be inserted. If the new text is too large, reallocate
the buffer with a gap large enough to accomodate the new text and a
gap of mPreferredGapSize */
if (copiedLength > mGapEnd - mGapStart)
reallocate_with_gap(toPos, copiedLength + mPreferredGapSize);
else if (toPos != mGapStart)
move_gap(toPos);
/* Insert the new text (toPos now corresponds to the start of the gap) */
if (fromEnd <= fromBuf->mGapStart) {
memcpy(&mBuf[toPos], &fromBuf->mBuf[fromStart], copiedLength);
} else if (fromStart >= fromBuf->mGapStart) {
memcpy(&mBuf[toPos],
&fromBuf->mBuf[fromStart + (fromBuf->mGapEnd - fromBuf->mGapStart)],
copiedLength);
} else {
int part1Length = fromBuf->mGapStart - fromStart;
memcpy(&mBuf[toPos], &fromBuf->mBuf[fromStart], part1Length);
memcpy(&mBuf[toPos + part1Length],
&fromBuf->mBuf[fromBuf->mGapEnd], copiedLength - part1Length);
}
mGapStart += copiedLength;
mLength += copiedLength;
update_selections(toPos, 0, copiedLength);
}
/*
Take the previous changes and undo them. Return the previous
cursor position in cursorPos. Returns 1 if the undo was applied.
CursorPos will be at a character boundary.
*/
int Fl_Text_Buffer::undo(int *cursorPos)
{
if (undowidget != this || (!undocut && !undoinsert && !mCanUndo))
return 0;
int ilen = undocut;
int xlen = undoinsert;
int b = undoat - xlen;
if (xlen && undoyankcut && !ilen) {
ilen = undoyankcut;
}
if (xlen && ilen) {
undobuffersize(ilen + 1);
undobuffer[ilen] = 0;
char *tmp = strdup(undobuffer);
replace(b, undoat, tmp);
if (cursorPos)
*cursorPos = mCursorPosHint;
free(tmp);
} else if (xlen) {
remove(b, undoat);
if (cursorPos)
*cursorPos = mCursorPosHint;
} else if (ilen) {
undobuffersize(ilen + 1);
undobuffer[ilen] = 0;
insert(undoat, undobuffer);
if (cursorPos)
*cursorPos = mCursorPosHint;
undoyankcut = 0;
}
return 1;
}
/*
Set a flag if undo function will work.
*/
void Fl_Text_Buffer::canUndo(char flag)
{
mCanUndo = flag;
// disabling undo also clears the last undo operation!
if (!mCanUndo && undowidget==this)
undowidget = 0;
}
/*
Change the tab width. This will cause a couple of callbacks and a complete
redisplay.
Matt: I am not entirely sure why we need to trigger callbacks because
tabs are only a graphical hint, not changing any text at all, but I leave
this in here for back compatibility.
*/
void Fl_Text_Buffer::tab_distance(int tabDist)
{
/* First call the pre-delete callbacks with the previous tab setting
still active. */
call_predelete_callbacks(0, mLength);
/* Change the tab setting */
mTabDist = tabDist;
/* Force any display routines to redisplay everything (unfortunately,
this means copying the whole buffer contents to provide "deletedText" */
const char *deletedText = text();
call_modify_callbacks(0, mLength, mLength, 0, deletedText);
free((void *) deletedText);
}
/*
Select a range of text.
Start and End must be at a character boundary.
*/
void Fl_Text_Buffer::select(int start, int end)
{
IS_UTF8_ALIGNED2(this, (start))
IS_UTF8_ALIGNED2(this, (end))
Fl_Text_Selection oldSelection = mPrimary;
mPrimary.set(start, end);
redisplay_selection(&oldSelection, &mPrimary);
}
/*
Clear the primary selection.
*/
void Fl_Text_Buffer::unselect()
{
Fl_Text_Selection oldSelection = mPrimary;
mPrimary.mSelected = 0;
redisplay_selection(&oldSelection, &mPrimary);
}
/*
Return the primary selection range.
*/
int Fl_Text_Buffer::selection_position(int *start, int *end)
{
return mPrimary.position(start, end);
}
/*
Return a copy of the selected text.
*/
char *Fl_Text_Buffer::selection_text()
{
return selection_text_(&mPrimary);
}
/*
Remove the selected text.
*/
void Fl_Text_Buffer::remove_selection()
{
remove_selection_(&mPrimary);
}
/*
Replace the selected text.
*/
void Fl_Text_Buffer::replace_selection(const char *text)
{
replace_selection_(&mPrimary, text);
}
/*
Select text.
Start and End must be at a character boundary.
*/
void Fl_Text_Buffer::secondary_select(int start, int end)
{
Fl_Text_Selection oldSelection = mSecondary;
mSecondary.set(start, end);
redisplay_selection(&oldSelection, &mSecondary);
}
/*
Deselect text.
*/
void Fl_Text_Buffer::secondary_unselect()
{
Fl_Text_Selection oldSelection = mSecondary;
mSecondary.mSelected = 0;
redisplay_selection(&oldSelection, &mSecondary);
}
/*
Return the selected range.
*/
int Fl_Text_Buffer::secondary_selection_position(int *start, int *end)
{
return mSecondary.position(start, end);
}
/*
Return a copy of the text in this selection.
*/
char *Fl_Text_Buffer::secondary_selection_text()
{
return selection_text_(&mSecondary);
}
/*
Remove the selected text.
*/
void Fl_Text_Buffer::remove_secondary_selection()
{
remove_selection_(&mSecondary);
}
/*
Replace selected text.
*/
void Fl_Text_Buffer::replace_secondary_selection(const char *text)
{
replace_selection_(&mSecondary, text);
}
/*
Highlight a range of text.
Start and End must be at a character boundary.
*/
void Fl_Text_Buffer::highlight(int start, int end)
{
Fl_Text_Selection oldSelection = mHighlight;
mHighlight.set(start, end);
redisplay_selection(&oldSelection, &mHighlight);
}
/*
Remove text highlighting.
*/
void Fl_Text_Buffer::unhighlight()
{
Fl_Text_Selection oldSelection = mHighlight;
mHighlight.mSelected = 0;
redisplay_selection(&oldSelection, &mHighlight);
}
/*
Return position of highlight.
*/
int Fl_Text_Buffer::highlight_position(int *start, int *end)
{
return mHighlight.position(start, end);
}
/*
Return a copy of highlighted text.
*/
char *Fl_Text_Buffer::highlight_text()
{
return selection_text_(&mHighlight);
}
/*
Add a callback that is called whenever text is modified.
*/
void Fl_Text_Buffer::add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB,
void *cbArg)
{
Fl_Text_Modify_Cb *newModifyProcs =
new Fl_Text_Modify_Cb[mNModifyProcs + 1];
void **newCBArgs = new void *[mNModifyProcs + 1];
for (int i = 0; i < mNModifyProcs; i++) {
newModifyProcs[i + 1] = mModifyProcs[i];
newCBArgs[i + 1] = mCbArgs[i];
}
if (mNModifyProcs != 0) {
delete[]mModifyProcs;
delete[]mCbArgs;
}
newModifyProcs[0] = bufModifiedCB;
newCBArgs[0] = cbArg;
mNModifyProcs++;
mModifyProcs = newModifyProcs;
mCbArgs = newCBArgs;
}
/*
Remove a callback.
*/
void Fl_Text_Buffer::remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB,
void *cbArg)
{
int i, toRemove = -1;
/* find the matching callback to remove */
for (i = 0; i < mNModifyProcs; i++) {
if (mModifyProcs[i] == bufModifiedCB && mCbArgs[i] == cbArg) {
toRemove = i;
break;
}
}
if (toRemove == -1) {
Fl::error
("Fl_Text_Buffer::remove_modify_callback(): Can't find modify CB to remove");
return;
}
/* Allocate new lists for remaining callback procs and args (if
any are left) */
mNModifyProcs--;
if (mNModifyProcs == 0) {
mNModifyProcs = 0;
delete[]mModifyProcs;
mModifyProcs = NULL;
delete[]mCbArgs;
mCbArgs = NULL;
return;
}
Fl_Text_Modify_Cb *newModifyProcs = new Fl_Text_Modify_Cb[mNModifyProcs];
void **newCBArgs = new void *[mNModifyProcs];
/* copy out the remaining members and free the old lists */
for (i = 0; i < toRemove; i++) {
newModifyProcs[i] = mModifyProcs[i];
newCBArgs[i] = mCbArgs[i];
}
for (; i < mNModifyProcs; i++) {
newModifyProcs[i] = mModifyProcs[i + 1];
newCBArgs[i] = mCbArgs[i + 1];
}
delete[]mModifyProcs;
delete[]mCbArgs;
mModifyProcs = newModifyProcs;
mCbArgs = newCBArgs;
}
/*
Add a callback that is called before deleting text.
*/
void Fl_Text_Buffer::add_predelete_callback(Fl_Text_Predelete_Cb bufPreDeleteCB,
void *cbArg)
{
Fl_Text_Predelete_Cb *newPreDeleteProcs =
new Fl_Text_Predelete_Cb[mNPredeleteProcs + 1];
void **newCBArgs = new void *[mNPredeleteProcs + 1];
for (int i = 0; i < mNPredeleteProcs; i++) {
newPreDeleteProcs[i + 1] = mPredeleteProcs[i];
newCBArgs[i + 1] = mPredeleteCbArgs[i];
}
if (mNPredeleteProcs > 0) {
delete[] mPredeleteProcs;
delete[] mPredeleteCbArgs;
}
newPreDeleteProcs[0] = bufPreDeleteCB;
newCBArgs[0] = cbArg;
mNPredeleteProcs++;
mPredeleteProcs = newPreDeleteProcs;
mPredeleteCbArgs = newCBArgs;
}
/*
Remove a callback.
*/
void Fl_Text_Buffer::remove_predelete_callback(Fl_Text_Predelete_Cb bufPreDeleteCB, void *cbArg)
{
int i, toRemove = -1;
/* find the matching callback to remove */
for (i = 0; i < mNPredeleteProcs; i++) {
if (mPredeleteProcs[i] == bufPreDeleteCB &&
mPredeleteCbArgs[i] == cbArg) {
toRemove = i;
break;
}
}
if (toRemove == -1) {
Fl::error
("Fl_Text_Buffer::remove_predelete_callback(): Can't find pre-delete CB to remove");
return;
}
/* Allocate new lists for remaining callback procs and args (if any are left) */
mNPredeleteProcs--;
if (mNPredeleteProcs == 0) {
delete[]mPredeleteProcs;
mPredeleteProcs = NULL;
delete[]mPredeleteCbArgs;
mPredeleteCbArgs = NULL;
return;
}
Fl_Text_Predelete_Cb *newPreDeleteProcs = new Fl_Text_Predelete_Cb[mNPredeleteProcs];
void **newCBArgs = new void *[mNPredeleteProcs];
/* copy out the remaining members and free the old lists */
for (i = 0; i < toRemove; i++) {
newPreDeleteProcs[i] = mPredeleteProcs[i];
newCBArgs[i] = mPredeleteCbArgs[i];
}
for (; i < mNPredeleteProcs; i++) {
newPreDeleteProcs[i] = mPredeleteProcs[i + 1];
newCBArgs[i] = mPredeleteCbArgs[i + 1];
}
delete[] mPredeleteProcs;
delete[] mPredeleteCbArgs;
mPredeleteProcs = newPreDeleteProcs;
mPredeleteCbArgs = newCBArgs;
}
/*
Return a copy of the line that contains a given index.
Pos must be at a character boundary.
*/
char *Fl_Text_Buffer::line_text(int pos) const {
return text_range(line_start(pos), line_end(pos));
}
/*
Find the beginning of the line.
*/
int Fl_Text_Buffer::line_start(int pos) const
{
if (!findchar_backward(pos, '\n', &pos))
return 0;
return pos + 1;
}
/*
Find the end of the line.
*/
int Fl_Text_Buffer::line_end(int pos) const {
if (!findchar_forward(pos, '\n', &pos))
pos = mLength;
return pos;
}
/** Returns whether character at position \p pos is a word separator.
Pos must be at a character boundary.
*/
bool Fl_Text_Buffer::is_word_separator(int pos) const {
int c = char_at(pos);
if (c < 128) {
return !(isalnum(c) || c == '_'); // non alphanumeric ASCII
}
return (c == 0xA0 || // NO-BREAK SPACE
(c >= 0x3000 && c <= 0x301F) // IDEOGRAPHIC punctuation
);
}
/*
Find the beginning of a word.
*/
int Fl_Text_Buffer::word_start(int pos) const {
while (pos > 0 && !is_word_separator(pos))
{
pos = prev_char(pos);
}
if (is_word_separator(pos))
pos = next_char(pos);
return pos;
}
/*
Find the end of a word.
*/
int Fl_Text_Buffer::word_end(int pos) const {
while (pos < length() && !is_word_separator(pos))
{
pos = next_char(pos);
}
return pos;
}
/*
Count the number of characters between two positions.
*/
int Fl_Text_Buffer::count_displayed_characters(int lineStartPos,
int targetPos) const
{
IS_UTF8_ALIGNED2(this, (lineStartPos))
IS_UTF8_ALIGNED2(this, (targetPos))
int charCount = 0;
int pos = lineStartPos;
while (pos < targetPos) {
pos = next_char(pos);
charCount++;
}
return charCount;
}
/*
Skip ahead a number of characters from a given index.
This function breaks early if it encounters a newline character.
*/
int Fl_Text_Buffer::skip_displayed_characters(int lineStartPos, int nChars)
{
IS_UTF8_ALIGNED2(this, (lineStartPos))
int pos = lineStartPos;
for (int charCount = 0; charCount < nChars && pos < mLength; charCount++) {
unsigned int c = char_at(pos);
if (c == '\n')
return pos;
pos = next_char(pos);
}
return pos;
}
/*
Count the number of newline characters between start and end.
startPos and endPos must be at a character boundary.
This function is optimized for speed by not using UTF-8 calls.
*/
int Fl_Text_Buffer::count_lines(int startPos, int endPos) const {
IS_UTF8_ALIGNED2(this, (startPos))
IS_UTF8_ALIGNED2(this, (endPos))
int gapLen = mGapEnd - mGapStart;
int lineCount = 0;
int pos = startPos;
while (pos < mGapStart)
{
if (pos == endPos)
return lineCount;
if (mBuf[pos++] == '\n')
lineCount++;
}
while (pos < mLength) {
if (pos == endPos)
return lineCount;
if (mBuf[pos++ + gapLen] == '\n')
lineCount++;
}
return lineCount;
}
/*
Skip to the first character, n lines ahead.
StartPos must be at a character boundary.
This function is optimized for speed by not using UTF-8 calls.
*/
int Fl_Text_Buffer::skip_lines(int startPos, int nLines)
{
IS_UTF8_ALIGNED2(this, (startPos))
if (nLines == 0)
return startPos;
int gapLen = mGapEnd - mGapStart;
int pos = startPos;
int lineCount = 0;
while (pos < mGapStart) {
if (mBuf[pos++] == '\n') {
lineCount++;
if (lineCount == nLines) {