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 #ifdef HAVE_CONFIG_H
32 #include "../config.h"
33 #endif
34
35 #include "textBuf.h"
36 #include "textDisp.h"
37 #include "rangeset.h"
38 #include "../util/nedit_malloc.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44
45 #ifdef HAVE_DEBUG_H
46 #include "../debug.h"
47 #endif
48
49
50
51 struct _Range {
52 int start, end;
53 };
54
55 typedef Rangeset *RangesetUpdateFn(Rangeset *p,
int pos,
int ins,
int del);
56
57 struct _Rangeset {
58 RangesetUpdateFn *update_fn;
59 char *update_name;
60 int maxpos;
61 int last_index;
62 int n_ranges;
63 Range *ranges;
64 unsigned char label;
65
66 signed char color_set;
67 char *color_name;
68 XftColor color;
69 textBuffer *buf;
70 char *name;
71 };
72
73 struct _RangesetTable {
74 int n_set;
75 textBuffer *buf;
76 Rangeset set[
N_RANGESETS];
77 unsigned char order[
N_RANGESETS];
78 unsigned char active[
N_RANGESETS];
79 unsigned char depth[
N_RANGESETS];
80 unsigned char list[
N_RANGESETS +
1];
81 };
82
83
84
85 #define SWAPval(
T,a,b) {
T t; t = *(a); *(a) = *(b); *(b) = t; }
86
87 static unsigned char rangeset_labels[
N_RANGESETS +
1] = {
88 58,
10,
15,
1,
27,
52,
14,
3,
61,
13,
31,
30,
45,
28,
41,
55,
89 33,
20,
62,
34,
42,
18,
57,
47,
24,
49,
19,
50,
25,
38,
40,
2,
90 21,
39,
59,
22,
60,
4,
6,
16,
29,
37,
48,
46,
54,
43,
32,
56,
91 51,
7,
9,
63,
5,
8,
36,
44,
26,
11,
23,
17,
53,
35,
12,
0
92 };
93
94
95
96 static RangesetUpdateFn rangesetInsDelMaintain;
97 static RangesetUpdateFn rangesetInclMaintain;
98 static RangesetUpdateFn rangesetDelInsMaintain;
99 static RangesetUpdateFn rangesetExclMaintain;
100 static RangesetUpdateFn rangesetBreakMaintain;
101
102 #define DEFAULT_UPDATE_FN_NAME "maintain"
103
104 static struct {
105 char *name;
106 RangesetUpdateFn *update_fn;
107 } RangesetUpdateMap[] = {
108 {
DEFAULT_UPDATE_FN_NAME, rangesetInsDelMaintain},
109 {
"ins_del", rangesetInsDelMaintain},
110 {
"include", rangesetInclMaintain},
111 {
"del_ins", rangesetDelInsMaintain},
112 {
"exclude", rangesetExclMaintain},
113 {
"break", rangesetBreakMaintain},
114 {(
char *)
0, (RangesetUpdateFn *)
0}
115 };
116
117
118
119 static Range *RangesNew(
int n)
120 {
121 Range *newRanges;
122
123 if (n !=
0) {
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 n = (n >=
256) ? ((n +
64) & ~
63) : ((n +
16) & ~
15);
147 newRanges = (Range *)NEditMalloc(n *
sizeof (Range));
148 return newRanges;
149 }
150
151 return NULL;
152 }
153
154
155
156 static Range* RangesRealloc(Range* ranges,
int n)
157 {
158 int size;
159 Range* newRanges;
160
161 if (n >
0)
162 {
163
164 n = (n >=
256) ? ((n +
64) & ~
63) : ((n +
16) & ~
15);
165 size = n *
sizeof (Range);
166 newRanges = (Range*)NEditRealloc(ranges,size);
167 return newRanges;
168 }
else {
169 NEditFree(ranges);
170 }
171
172 return NULL;
173 }
174
175
176
177 static Range *RangesFree(Range *ranges)
178 {
179 NEditFree(ranges);
180
181 return NULL;
182 }
183
184
185
186
187
188
189
190
191 void RangesetRefreshRange(Rangeset *rangeset,
int start,
int end)
192 {
193 if (rangeset->buf !=
NULL)
194 BufCheckDisplay(rangeset->buf, start, end);
195 }
196
197 static void rangesetRefreshAllRanges(Rangeset *rangeset)
198 {
199 int i;
200
201 for (i =
0; i < rangeset->n_ranges; i++)
202 RangesetRefreshRange(rangeset, rangeset->ranges[i].start, rangeset->ranges[i].end);
203 }
204
205
206
207
208
209
210
211 void RangesetEmpty(Rangeset *rangeset)
212 {
213 Range *ranges = rangeset->ranges;
214 int start, end;
215
216 if (rangeset->color_name && rangeset->color_set >
0) {
217
218 rangeset->color_set = -
1;
219
220 while (rangeset->n_ranges--) {
221 start = ranges[rangeset->n_ranges].start;
222 end = ranges[rangeset->n_ranges].end;
223 RangesetRefreshRange(rangeset, start, end);
224 }
225 }
226
227 NEditFree(rangeset->color_name);
228 NEditFree(rangeset->name);
229
230 rangeset->color_name = (
char *)
0;
231 rangeset->name = (
char *)
0;
232 rangeset->ranges = RangesFree(rangeset->ranges);
233 }
234
235
236
237
238
239
240
241 void RangesetInit(Rangeset *rangeset,
int label, textBuffer *buf)
242 {
243 rangeset->label = (
unsigned char)label;
244 rangeset->maxpos =
0;
245 rangeset->last_index =
0;
246 rangeset->n_ranges =
0;
247 rangeset->ranges = (Range *)
0;
248
249 rangeset->color_name = (
char *)
0;
250 rangeset->name = (
char *)
0;
251 rangeset->color_set =
0;
252 rangeset->buf = buf;
253
254 rangeset->maxpos = buf->length;
255
256 RangesetChangeModifyResponse(rangeset,
DEFAULT_UPDATE_FN_NAME);
257 }
258
259
260
261
262
263
264 int RangesetChangeModifyResponse(Rangeset *rangeset,
char *name)
265 {
266 int i;
267
268 if (name ==
NULL)
269 name =
DEFAULT_UPDATE_FN_NAME;
270
271 for (i =
0; RangesetUpdateMap[i].name; i++)
272 if (strcmp(RangesetUpdateMap[i].name, name) ==
0) {
273 rangeset->update_fn = RangesetUpdateMap[i].update_fn;
274 rangeset->update_name = RangesetUpdateMap[i].name;
275 return 1;
276 }
277
278 return 0;
279 }
280
281
282
283
284
285
286
287
288
289 static int at_or_before(
int *table,
int base,
int len,
int val)
290 {
291 int lo, mid =
0, hi;
292
293 if (base >= len)
294 return len;
295
296 lo = base;
297 hi = len -
1;
298
299 while (lo <= hi) {
300 mid = (lo + hi) /
2;
301 if (val == table[mid])
302 return mid;
303 if (val < table[mid])
304 hi = mid -
1;
305 else
306 lo = mid +
1;
307 }
308
309 if (val > table[mid])
310 mid++;
311
312 return mid;
313 }
314
315 static int weighted_at_or_before(
int *table,
int base,
int len,
int val)
316 {
317 int lo, mid =
0, hi;
318 int min, max;
319
320 if (base >= len)
321 return len;
322
323 lo = base;
324 hi = len -
1;
325
326 min = table[lo];
327 max = table[hi];
328
329 if (val <= min)
330 return lo;
331 else if (val > max)
332 return len;
333 else if (val == max)
334 return hi;
335
336 while (lo <= hi) {
337
338 mid = lo + (
int)((hi - lo) * (
double)(val - min) / (max - min));
339
340
341 if (val == table[mid])
342 return mid;
343 if (val < table[mid]) {
344 hi = mid -
1;
345 max = table[mid];
346 }
347 else {
348 lo = mid +
1;
349 min = table[mid];
350 }
351 }
352
353
354 if (val > table[mid])
355 return mid +
1;
356
357 return mid;
358 }
359
360
361
362
363
364
365
366
367
368 int RangesetFindRangeNo(Rangeset *rangeset,
int index,
int *start,
int *end)
369 {
370 if (!rangeset || index <
0 || rangeset->n_ranges <= index || !rangeset->ranges)
371 return 0;
372
373 *start = rangeset->ranges[index].start;
374 *end = rangeset->ranges[index].end;
375
376 return 1;
377 }
378
379
380
381
382
383
384
385 int RangesetFindRangeOfPos(Rangeset *rangeset,
int pos,
int incl_end)
386 {
387 int *ranges;
388 int len, ind;
389
390 if (!rangeset || !rangeset->n_ranges || !rangeset->ranges)
391 return -
1;
392
393 ranges = (
int *)rangeset->ranges;
394 len = rangeset->n_ranges *
2;
395 ind = at_or_before(ranges,
0, len, pos);
396
397 if (ind == len)
398 return -
1;
399
400 if (ind &
1) {
401 if (pos < ranges[ind] || (incl_end && pos == ranges[ind]))
402 return ind /
2;
403 }
404 else {
405 if (pos == ranges[ind])
406 return ind /
2;
407 }
408 return -
1;
409 }
410
411
412
413
414
415
416
417
418
419
420
421 int RangesetCheckRangeOfPos(Rangeset *rangeset,
int pos)
422 {
423 int *ranges;
424 int len, index, last;
425
426 len = rangeset->n_ranges;
427 if (len ==
0)
428 return -
1;
429
430 ranges = (
int *)rangeset->ranges;
431 last = rangeset->last_index;
432
433
434 if (last >= len || last <
0) {
435 last = (len >
0) ? len -
1 :
0;
436 rangeset->last_index = last;
437 }
438
439 len *=
2;
440 last *=
2;
441
442 if (pos >= ranges[last]) {
443 if (pos < ranges[last +
1])
444 return last /
2;
445 else
446 last +=
2;
447
448 if (last == len)
449 return -
1;
450
451
452 index = weighted_at_or_before(ranges, last, len, pos);
453 }
454 else if (last >
0) {
455 index = weighted_at_or_before(ranges,
0, last, pos);
456 }
457 else
458 index =
0;
459
460 rangeset->last_index = index /
2;
461
462 if (index == len)
463 return -
1;
464
465 if (index &
1) {
466 if (pos < ranges[index])
467 return index /
2;
468 }
469 else {
470 if (pos == ranges[index])
471 return index /
2;
472 }
473 return -
1;
474 }
475
476
477
478
479
480
481
482 int RangesetAdd(Rangeset *origSet, Rangeset *plusSet)
483 {
484 Range *origRanges, *plusRanges, *newRanges, *oldRanges;
485 int nOrigRanges, nPlusRanges;
486 int isOld;
487
488 origRanges = origSet->ranges;
489 nOrigRanges = origSet->n_ranges;
490
491 plusRanges = plusSet->ranges;
492 nPlusRanges = plusSet->n_ranges;
493
494 if (nPlusRanges ==
0)
495 return nOrigRanges;
496
497 newRanges = RangesNew(nOrigRanges + nPlusRanges);
498
499 if (nOrigRanges ==
0) {
500
501 memcpy(newRanges, plusRanges, nPlusRanges *
sizeof (Range));
502 RangesFree(origSet->ranges);
503 origSet->ranges = newRanges;
504 origSet->n_ranges = nPlusRanges;
505 for (nOrigRanges =
0; nOrigRanges < nPlusRanges; nOrigRanges++) {
506 RangesetRefreshRange(origSet, newRanges->start, newRanges->end);
507 newRanges++;
508 }
509 return nPlusRanges;
510 }
511
512 oldRanges = origRanges;
513 origSet->ranges = newRanges;
514 origSet->n_ranges =
0;
515
516
517
518
519
520 isOld =
1;
521
522 while (nOrigRanges >
0 || nPlusRanges >
0) {
523
524 if (nOrigRanges ==
0 ||
525 (nPlusRanges >
0 && origRanges->start > plusRanges->start)) {
526 SWAPval(Range *, &origRanges, &plusRanges);
527 SWAPval(
int, &nOrigRanges, &nPlusRanges);
528 isOld = !isOld;
529 }
530
531 origSet->n_ranges++;
532
533 *newRanges = *origRanges++;
534 nOrigRanges--;
535 if (!isOld)
536 RangesetRefreshRange(origSet, newRanges->start, newRanges->end);
537
538
539 while (nPlusRanges >
0 && newRanges->end >= plusRanges->start) {
540 do {
541 if (newRanges->end < plusRanges->end) {
542 if (isOld)
543 RangesetRefreshRange(origSet, newRanges->end, plusRanges->end);
544 newRanges->end = plusRanges->end;
545 }
546 plusRanges++;
547 nPlusRanges--;
548 }
while (nPlusRanges >
0 && newRanges->end >= plusRanges->start);
549
550
551
552 SWAPval(Range *, &origRanges, &plusRanges);
553 SWAPval(
int, &nOrigRanges, &nPlusRanges);
554 isOld = !isOld;
555 }
556
557
558
559
560 newRanges++;
561 }
562
563
564 RangesFree(oldRanges);
565 origSet->ranges = RangesRealloc(origSet->ranges, origSet->n_ranges);
566
567 return origSet->n_ranges;
568 }
569
570
571
572
573
574
575
576
577 int RangesetRemove(Rangeset *origSet, Rangeset *minusSet)
578 {
579 Range *origRanges, *minusRanges, *newRanges, *oldRanges;
580 int nOrigRanges, nMinusRanges;
581
582 origRanges = origSet->ranges;
583 nOrigRanges = origSet->n_ranges;
584
585 minusRanges = minusSet->ranges;
586 nMinusRanges = minusSet->n_ranges;
587
588 if (nOrigRanges ==
0 || nMinusRanges ==
0)
589 return 0;
590
591
592 newRanges = RangesNew(origSet->n_ranges + minusSet->n_ranges);
593
594 oldRanges = origRanges;
595 origSet->ranges = newRanges;
596 origSet->n_ranges =
0;
597
598
599
600
601 while (nOrigRanges >
0) {
602 do {
603
604 while (nMinusRanges >
0
605 && minusRanges->end <= origRanges->start) {
606 minusRanges++;
607 nMinusRanges--;
608 }
609
610 if (nMinusRanges >
0) {
611
612 while (nOrigRanges >
0
613 && origRanges->end <= minusRanges->start) {
614 *newRanges++ = *origRanges++;
615 nOrigRanges--;
616 origSet->n_ranges++;
617 }
618 }
else {
619
620 while (nOrigRanges >
0) {
621 *newRanges++ = *origRanges++;
622 nOrigRanges--;
623 origSet->n_ranges++;
624 }
625 }
626 }
while (nMinusRanges >
0 && minusRanges->end <= origRanges->start);
627
628
629 if (nOrigRanges >
0) {
630 if (minusRanges->start <= origRanges->start) {
631
632 if (minusRanges->end < origRanges->end) {
633 RangesetRefreshRange(origSet, origRanges->start,
634 minusRanges->end);
635 origRanges->start = minusRanges->end;
636 minusRanges++;
637 nMinusRanges--;
638 }
else {
639
640 RangesetRefreshRange(origSet, origRanges->start,
641 origRanges->end);
642 origRanges++;
643 nOrigRanges--;
644 }
645 }
else {
646
647 newRanges->start = origRanges->start;
648 newRanges->end = minusRanges->start;
649 newRanges++;
650 origSet->n_ranges++;
651
652 if (minusRanges->end < origRanges->end) {
653
654 RangesetRefreshRange(origSet, minusRanges->start,
655 minusRanges->end);
656 origRanges->start = minusRanges->end;
657 minusRanges++;
658 nMinusRanges--;
659 }
else {
660
661 RangesetRefreshRange(origSet, minusRanges->start,
662 origRanges->end);
663 origRanges++;
664 nOrigRanges--;
665 }
666 }
667 }
668 }
669
670
671 RangesFree(oldRanges);
672 origSet->ranges = RangesRealloc(origSet->ranges, origSet->n_ranges);
673
674 return origSet->n_ranges;
675 }
676
677
678
679
680
681
682
683
684 int RangesetGetNRanges(Rangeset *rangeset)
685 {
686 return rangeset ? rangeset->n_ranges :
0;
687 }
688
689
690
691
692
693 void RangesetGetInfo(Rangeset *rangeset,
int *defined,
int *label,
694 int *count,
char **color,
char **name,
char **mode)
695 {
696 if (rangeset ==
NULL) {
697 *defined = False;
698 *label =
0;
699 *count =
0;
700 *color =
"";
701 *name =
"";
702 *mode =
"";
703 }
704 else {
705 *defined = True;
706 *label = (
int)rangeset->label;
707 *count = rangeset->n_ranges;
708 *color = rangeset->color_name ? rangeset->color_name :
"";
709 *name = rangeset->name ? rangeset->name :
"";
710 *mode = rangeset->update_name;
711 }
712 }
713
714
715
716 static Rangeset *rangesetFixMaxpos(Rangeset *rangeset,
int ins,
int del)
717 {
718 rangeset->maxpos += ins - del;
719 return rangeset;
720 }
721
722
723
724
725
726
727
728 RangesetTable *RangesetTableAlloc(textBuffer *buffer)
729 {
730 int i;
731 RangesetTable *table = (RangesetTable *)NEditMalloc(
sizeof (RangesetTable));
732
733 if (!table)
734 return table;
735
736 table->buf = buffer;
737
738 for (i =
0; i <
N_RANGESETS; i++) {
739 RangesetInit(&table->set[i], rangeset_labels[i], buffer);
740 table->order[i] = (
unsigned char)i;
741 table->active[i] =
0;
742 table->depth[i] = (
unsigned char)i;
743 }
744
745 table->n_set =
0;
746 table->list[
0] =
'\0';
747
748
749 BufAddHighPriorityModifyCB(buffer, RangesetBufModifiedCB, table);
750 return table;
751 }
752
753 RangesetTable *RangesetTableFree(RangesetTable *table)
754 {
755 int i;
756
757 if (table) {
758 BufRemoveModifyCB(table->buf, RangesetBufModifiedCB, table);
759 for (i =
0; i <
N_RANGESETS; i++)
760 RangesetEmpty(&table->set[i]);
761 NEditFree(table);
762 }
763 return (RangesetTable *)
0;
764 }
765
766
767
768
769 static void rangesetClone(Rangeset *destRangeset, Rangeset *srcRangeset)
770 {
771 destRangeset->update_fn = srcRangeset->update_fn;
772 destRangeset->update_name = srcRangeset->update_name;
773 destRangeset->maxpos = srcRangeset->maxpos;
774 destRangeset->last_index = srcRangeset->last_index;
775 destRangeset->n_ranges = srcRangeset->n_ranges;
776 destRangeset->color_set = srcRangeset->color_set;
777 destRangeset->color = srcRangeset->color;
778
779 if (srcRangeset->color_name) {
780 destRangeset->color_name = (
char*)NEditMalloc(strlen(srcRangeset->color_name) +
1);
781 strcpy(destRangeset->color_name, srcRangeset->color_name);
782 }
783
784 if (srcRangeset->name) {
785 destRangeset->name = (
char*)NEditMalloc(strlen(srcRangeset->name) +
1);
786 strcpy(destRangeset->name, srcRangeset->name);
787 }
788
789 if (srcRangeset->ranges) {
790 destRangeset->ranges = RangesNew(srcRangeset->n_ranges);
791 memcpy(destRangeset->ranges, srcRangeset->ranges,
792 srcRangeset->n_ranges *
sizeof(Range));
793 }
794 }
795
796
797
798
799
800
801
802 RangesetTable *RangesetTableClone(RangesetTable *srcTable,
803 textBuffer *destBuffer)
804 {
805 RangesetTable *newTable =
NULL;
806 int i;
807
808 if (srcTable ==
NULL)
809 return NULL;
810
811 newTable = RangesetTableAlloc(destBuffer);
812
813 newTable->n_set = srcTable->n_set;
814 memcpy(newTable->order, srcTable->order,
815 sizeof(
unsigned char) *
N_RANGESETS);
816 memcpy(newTable->active, srcTable->active,
817 sizeof(
unsigned char) *
N_RANGESETS);
818 memcpy(newTable->depth, srcTable->depth,
819 sizeof(
unsigned char) *
N_RANGESETS);
820 memcpy(newTable->list, srcTable->list,
821 sizeof(
unsigned char) *(
N_RANGESETS +
1));
822
823 for (i =
0; i <
N_RANGESETS; i++)
824 rangesetClone(&newTable->set[i], &srcTable->set[i]);
825
826 return newTable;
827 }
828
829
830
831
832
833 int RangesetFindIndex(RangesetTable *table,
int label,
int must_be_active)
834 {
835 int i;
836 unsigned char *p_label;
837
838 if(label ==
0) {
839 return -
1;
840 }
841
842 if (table !=
NULL) {
843 p_label = (
unsigned char*)strchr((
char*)rangeset_labels, label);
844 if (p_label) {
845 i = p_label - rangeset_labels;
846 if (!must_be_active || table->active[i])
847 return i;
848 }
849 }
850
851 return -
1;
852 }
853
854
855
856
857
858
859 static void RangesetTableListSet(RangesetTable *table)
860 {
861 int i;
862
863 for (i =
0; i < table->n_set; i++)
864 table->list[i] = rangeset_labels[(
int)table->order[i]];
865 table->list[table->n_set] =
'\0';
866 }
867
868
869
870
871
872 int RangesetLabelOK(
int label)
873 {
874 return strchr((
char*)rangeset_labels, label) !=
NULL;
875 }
876
877
878
879
880
881 static int activateRangeset(RangesetTable *table,
int active)
882 {
883 int depth, i, j;
884
885 if (table->active[active])
886 return 0;
887
888 depth = table->depth[active];
889
890
891
892
893 for (i = depth; i >
0; i--) {
894 j = table->order[i] = table->order[i -
1];
895 table->depth[j] = i;
896 }
897
898 table->order[
0] = active;
899 table->depth[active] =
0;
900
901
902 table->active[active] =
1;
903 table->n_set++;
904
905 RangesetTableListSet(table);
906
907 return 1;
908 }
909
910 static int deactivateRangeset(RangesetTable *table,
int active)
911 {
912 int depth, n, i, j;
913
914 if (!table->active[active])
915 return 0;
916
917
918
919
920 depth = table->depth[active];
921 n = table->n_set -
1;
922
923 for (i = depth; i < n; i++) {
924 j = table->order[i] = table->order[i +
1];
925 table->depth[j] = i;
926 }
927
928 table->order[n] = active;
929 table->depth[active] = n;
930
931
932 table->active[active] =
0;
933 table->n_set--;
934
935 RangesetTableListSet(table);
936
937 return 1;
938 }
939
940
941
942
943
944
945 int nRangesetsAvailable(RangesetTable *table)
946 {
947 return(
N_RANGESETS - table->n_set);
948 }
949
950
951
952
953
954
955 int RangesetCreate(RangesetTable *table)
956 {
957 int label;
958 int setIndex;
959
960 size_t firstAvailableIndex = strspn((
char*)rangeset_labels, (
char*)table->list);
961
962 if(firstAvailableIndex >=
sizeof(rangeset_labels))
963 return 0;
964
965 label = rangeset_labels[firstAvailableIndex];
966
967 setIndex = RangesetFindIndex(table, label,
0);
968
969 if (setIndex <
0)
970 return 0;
971
972 if (table->active[setIndex])
973 return label;
974
975 if (activateRangeset(table, setIndex))
976 RangesetInit(&table->set[setIndex],
977 rangeset_labels[setIndex], table->buf);
978
979 return label;
980 }
981
982
983
984
985
986 Rangeset *RangesetForget(RangesetTable *table,
int label)
987 {
988 int set_ind = RangesetFindIndex(table, label,
1);
989
990 if (set_ind <
0)
991 return (Rangeset *)
0;
992
993 if (deactivateRangeset(table, set_ind))
994 RangesetEmpty(&table->set[set_ind]);
995
996 return &table->set[set_ind];
997 }
998
999
1000
1001
1002
1003
1004
1005
1006 Rangeset *RangesetFetch(RangesetTable *table,
int label)
1007 {
1008 int rangesetIndex = RangesetFindIndex(table, label,
0);
1009
1010 if (rangesetIndex <
0)
1011 return (Rangeset *)
NULL;
1012
1013 if (table->active[rangesetIndex])
1014 return &table->set[rangesetIndex];
1015 else
1016 return (Rangeset *)
NULL;
1017 }
1018
1019
1020 unsigned char *RangesetGetList(RangesetTable *table)
1021 {
1022 return table ? table->list : (
unsigned char *)
"";
1023 }
1024
1025
1026
1027
1028 void RangesetTableUpdatePos(RangesetTable *table,
int pos,
int ins,
int del)
1029 {
1030 int i;
1031 Rangeset *p;
1032
1033 if (!table || (ins ==
0 && del ==
0))
1034 return;
1035
1036 for (i =
0; i < table->n_set; i++) {
1037 p = &table->set[(
int)table->order[i]];
1038 p->update_fn(p, pos, ins, del);
1039 }
1040 }
1041
1042 void RangesetBufModifiedCB(
int pos,
int nInserted,
int nDeleted,
int nRestyled,
1043 const char *deletedText,
void *cbArg)
1044 {
1045 RangesetTable *table = (RangesetTable *)cbArg;
1046 if ((nInserted != nDeleted) || BufCmp(table->buf, pos, nInserted, deletedText) !=
0) {
1047 RangesetTableUpdatePos(table, pos, nInserted, nDeleted);
1048 }
1049 }
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060 int RangesetIndex1ofPos(RangesetTable *table,
int pos,
int needs_color)
1061 {
1062 int i;
1063 Rangeset *rangeset;
1064
1065 if (!table)
1066 return 0;
1067
1068 for (i =
0; i < table->n_set; i++) {
1069 rangeset = &table->set[(
int)table->order[i]];
1070 if (RangesetCheckRangeOfPos(rangeset, pos) >=
0) {
1071 if (needs_color && rangeset->color_set >=
0 && rangeset->color_name)
1072 return table->order[i] +
1;
1073 }
1074 }
1075 return 0;
1076 }
1077
1078
1079
1080
1081
1082
1083
1084 int RangesetAssignColorName(Rangeset *rangeset,
char *color_name)
1085 {
1086 char *cp;
1087
1088 if (color_name && color_name[
0] ==
'\0')
1089 color_name = (
char *)
0;
1090
1091
1092 if (color_name) {
1093 cp = (
char*)NEditMalloc(strlen(color_name) +
1);
1094 strcpy(cp, color_name);
1095 }
1096 else
1097 cp = color_name;
1098
1099
1100 NEditFree(rangeset->color_name);
1101
1102 rangeset->color_name = cp;
1103 rangeset->color_set =
0;
1104
1105 rangesetRefreshAllRanges(rangeset);
1106 return 1;
1107 }
1108
1109
1110
1111
1112
1113 int RangesetAssignName(Rangeset *rangeset,
char *name)
1114 {
1115 char *cp;
1116
1117 if (name && name[
0] ==
'\0')
1118 name = (
char *)
0;
1119
1120
1121 if (name) {
1122 cp = (
char*)NEditMalloc(strlen(name) +
1);
1123 strcpy(cp, name);
1124 }
1125 else {
1126 cp = name;
1127 }
1128
1129
1130 NEditFree(rangeset->name);
1131
1132 rangeset->name = cp;
1133
1134 return 1;
1135 }
1136
1137
1138
1139
1140
1141
1142 int RangesetAssignColorPixel(Rangeset *rangeset, XftColor color,
int ok)
1143 {
1144 rangeset->color_set = ok ?
1 : -
1;
1145 rangeset->color = color;
1146 return 1;
1147 }
1148
1149
1150
1151
1152
1153
1154 char *RangesetGetName(Rangeset *rangeset)
1155 {
1156 return rangeset->name;
1157 }
1158
1159
1160
1161
1162
1163 int RangesetGetColorValid(Rangeset *rangeset, XftColor *color)
1164 {
1165 *color = rangeset->color;
1166 return rangeset->color_set;
1167 }
1168
1169
1170
1171
1172
1173 char *RangesetTableGetColorName(RangesetTable *table,
int index)
1174 {
1175 Rangeset *rangeset = &table->set[index];
1176 return rangeset->color_name;
1177 }
1178
1179
1180
1181
1182
1183 int RangesetTableGetColorValid(RangesetTable *table,
int index, XftColor *color, Rangeset **rs)
1184 {
1185 Rangeset *rangeset = &table->set[index];
1186 *color = rangeset->color;
1187 *rs = rangeset;
1188 return rangeset->color_set;
1189 }
1190
1191
1192
1193
1194
1195
1196 Rangeset* RangesetTableAssignColorPixel(RangesetTable *table,
int index, XftColor color,
1197 int ok)
1198 {
1199 Rangeset *rangeset = &table->set[index];
1200 rangeset->color_set = ok ?
1 : -
1;
1201 rangeset->color = color;
1202 return rangeset;
1203 }
1204
1205 XftColor* RangesetGetColor(Rangeset *rangeset)
1206 {
1207 return &rangeset->color;
1208 }
1209
1210
1211
1212 #define is_start(i) !((i) &
1)
1213 #define is_end(i) ((i) &
1)
1214
1215
1216
1217
1218
1219
1220
1221
1222 static int rangesetWeightedAtOrBefore(Rangeset *rangeset,
int pos)
1223 {
1224 int i, last, n, *rangeTable = (
int *)rangeset->ranges;
1225
1226 n = rangeset->n_ranges;
1227 if (n ==
0)
1228 return 0;
1229
1230 last = rangeset->last_index;
1231
1232 if (last >= n || last <
0)
1233 last =
0;
1234
1235 n *=
2;
1236 last *=
2;
1237
1238 if (pos >= rangeTable[last])
1239 i = weighted_at_or_before(rangeTable, last, n, pos);
1240 else
1241 i = weighted_at_or_before(rangeTable,
0, last, pos);
1242
1243 rangeset->last_index = i /
2;
1244
1245 return i;
1246 }
1247
1248
1249
1250
1251
1252 static int rangesetShuffleToFrom(
int *rangeTable,
int to,
int from,
int n,
int delta)
1253 {
1254 int end, diff = from - to;
1255
1256 if (n <=
0)
1257 return 0;
1258
1259 if (delta !=
0) {
1260 if (diff >
0) {
1261 for (end = to + n; to < end; to++)
1262 rangeTable[to] = rangeTable[to + diff] + delta;
1263 }
1264 else if (diff <
0) {
1265 for (end = to, to += n; --to >= end;)
1266 rangeTable[to] = rangeTable[to + diff] + delta;
1267 }
1268 else {
1269 for (end = n; end--;)
1270 rangeTable[to++] += delta;
1271 }
1272 }
1273 else {
1274 if (diff >
0) {
1275 for (end = to + n; to < end; to++)
1276 rangeTable[to] = rangeTable[to + diff];
1277 }
1278 else if (diff <
0) {
1279 for (end = to, to += n; --to >= end;)
1280 rangeTable[to] = rangeTable[to + diff];
1281 }
1282
1283 }
1284
1285 return n;
1286 }
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298 static Rangeset *rangesetInsDelMaintain(Rangeset *rangeset,
int pos,
int ins,
int del)
1299 {
1300 int i, j, n, *rangeTable = (
int *)rangeset->ranges;
1301 int end_del, movement;
1302
1303 n =
2 * rangeset->n_ranges;
1304
1305 i = rangesetWeightedAtOrBefore(rangeset, pos);
1306
1307 if (i == n)
1308 return rangesetFixMaxpos(rangeset, ins, del);
1309
1310 end_del = pos + del;
1311 movement = ins - del;
1312
1313
1314
1315
1316
1317 j = i;
1318 while (j < n && rangeTable[j] <= end_del)
1319 j++;
1320
1321
1322
1323 if (j > i)
1324 rangeTable[i] = pos + ins;
1325
1326
1327
1328
1329
1330 if (is_start(i) != is_start(j))
1331 i++;
1332
1333 rangesetShuffleToFrom(rangeTable, i, j, n - j, movement);
1334
1335 n -= j - i;
1336 rangeset->n_ranges = n /
2;
1337 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1338
1339
1340 return rangesetFixMaxpos(rangeset, ins, del);
1341 }
1342
1343
1344
1345
1346
1347
1348
1349 static Rangeset *rangesetInclMaintain(Rangeset *rangeset,
int pos,
int ins,
int del)
1350 {
1351 int i, j, n, *rangeTable = (
int *)rangeset->ranges;
1352 int end_del, movement;
1353
1354 n =
2 * rangeset->n_ranges;
1355
1356 i = rangesetWeightedAtOrBefore(rangeset, pos);
1357
1358 if (i == n)
1359 return rangesetFixMaxpos(rangeset, ins, del);
1360
1361
1362
1363
1364 if (is_start(i) && rangeTable[i] == pos && ins >
0)
1365 i++;
1366
1367 end_del = pos + del;
1368 movement = ins - del;
1369
1370
1371
1372
1373
1374 j = i;
1375 while (j < n && rangeTable[j] <= end_del)
1376 j++;
1377
1378
1379
1380 if (j > i)
1381 rangeTable[i] = pos + ins;
1382
1383
1384
1385
1386
1387 if (is_start(i) != is_start(j))
1388 i++;
1389
1390 rangesetShuffleToFrom(rangeTable, i, j, n - j, movement);
1391
1392 n -= j - i;
1393 rangeset->n_ranges = n /
2;
1394 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1395
1396
1397 return rangesetFixMaxpos(rangeset, ins, del);
1398 }
1399
1400
1401
1402
1403
1404
1405
1406
1407 static Rangeset *rangesetDelInsMaintain(Rangeset *rangeset,
int pos,
int ins,
int del)
1408 {
1409 int i, j, n, *rangeTable = (
int *)rangeset->ranges;
1410 int end_del, movement;
1411
1412 n =
2 * rangeset->n_ranges;
1413
1414 i = rangesetWeightedAtOrBefore(rangeset, pos);
1415
1416 if (i == n)
1417 return rangesetFixMaxpos(rangeset, ins, del);
1418
1419 end_del = pos + del;
1420 movement = ins - del;
1421
1422
1423
1424
1425
1426 j = i;
1427 while (j < n && rangeTable[j] <= end_del)
1428 j++;
1429
1430
1431
1432
1433
1434 if (j > i)
1435 rangeTable[i] = (is_end(j)) ? pos + ins : pos;
1436
1437
1438
1439
1440
1441 if (is_start(i) != is_start(j))
1442 i++;
1443
1444 rangesetShuffleToFrom(rangeTable, i, j, n - j, movement);
1445
1446 n -= j - i;
1447 rangeset->n_ranges = n /
2;
1448 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1449
1450
1451 return rangesetFixMaxpos(rangeset, ins, del);
1452 }
1453
1454
1455
1456
1457
1458
1459
1460
1461 static Rangeset *rangesetExclMaintain(Rangeset *rangeset,
int pos,
int ins,
int del)
1462 {
1463 int i, j, n, *rangeTable = (
int *)rangeset->ranges;
1464 int end_del, movement;
1465
1466 n =
2 * rangeset->n_ranges;
1467
1468 i = rangesetWeightedAtOrBefore(rangeset, pos);
1469
1470 if (i == n)
1471 return rangesetFixMaxpos(rangeset, ins, del);
1472
1473
1474
1475
1476 if (is_end(i) && rangeTable[i] == pos && ins >
0)
1477 i++;
1478
1479 end_del = pos + del;
1480 movement = ins - del;
1481
1482
1483
1484
1485
1486 j = i;
1487 while (j < n && rangeTable[j] <= end_del)
1488 j++;
1489
1490
1491
1492
1493
1494 if (j > i)
1495 rangeTable[i] = (is_end(j)) ? pos + ins : pos;
1496
1497
1498
1499
1500
1501 if (is_start(i) != is_start(j))
1502 i++;
1503
1504 rangesetShuffleToFrom(rangeTable, i, j, n - j, movement);
1505
1506 n -= j - i;
1507 rangeset->n_ranges = n /
2;
1508 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1509
1510
1511 return rangesetFixMaxpos(rangeset, ins, del);
1512 }
1513
1514
1515
1516
1517
1518
1519 static Rangeset *rangesetBreakMaintain(Rangeset *rangeset,
int pos,
int ins,
int del)
1520 {
1521 int i, j, n, *rangeTable = (
int *)rangeset->ranges;
1522 int end_del, movement, need_gap;
1523
1524 n =
2 * rangeset->n_ranges;
1525
1526 i = rangesetWeightedAtOrBefore(rangeset, pos);
1527
1528 if (i == n)
1529 return rangesetFixMaxpos(rangeset, ins, del);
1530
1531
1532
1533
1534 if (is_end(i) && rangeTable[i] == pos && ins >
0)
1535 i++;
1536
1537 end_del = pos + del;
1538 movement = ins - del;
1539
1540
1541
1542
1543
1544 j = i;
1545 while (j < n && rangeTable[j] <= end_del)
1546 j++;
1547
1548 if (j > i)
1549 rangeTable[i] = pos;
1550
1551
1552
1553
1554
1555
1556
1557
1558 need_gap = (is_end(i) && is_end(j) && ins >
0);
1559
1560
1561 if (is_start(i) != is_start(j)) {
1562 if (is_start(i)) {
1563 if (rangeTable[i] == pos)
1564 rangeTable[i] = pos + ins;
1565 }
1566 i++;
1567 }
1568
1569
1570
1571
1572 if (need_gap)
1573 i +=
2;
1574
1575
1576 rangesetShuffleToFrom(rangeTable, i, j, n - j, movement);
1577
1578 if (need_gap) {
1579 rangeTable[i -
2] = pos;
1580 rangeTable[i -
1] = pos + ins;
1581 }
1582
1583 n -= j - i;
1584 rangeset->n_ranges = n /
2;
1585 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1586
1587
1588 return rangesetFixMaxpos(rangeset, ins, del);
1589 }
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599 int RangesetInverse(Rangeset *rangeset)
1600 {
1601 int *rangeTable;
1602 int n, has_zero, has_end;
1603
1604 if (!rangeset)
1605 return -
1;
1606
1607 rangeTable = (
int *)rangeset->ranges;
1608
1609 if (rangeset->n_ranges ==
0) {
1610 if (!rangeTable) {
1611 rangeset->ranges = RangesNew(
1);
1612 rangeTable = (
int *)rangeset->ranges;
1613 }
1614 rangeTable[
0] =
0;
1615 rangeTable[
1] = rangeset->maxpos;
1616 n =
2;
1617 }
1618 else {
1619 n = rangeset->n_ranges *
2;
1620
1621
1622 has_zero = (rangeTable[
0] ==
0);
1623 has_end = (rangeTable[n -
1] == rangeset->maxpos);
1624
1625
1626 rangeTable[n +
1] = rangeTable[n] = rangeset->maxpos;
1627
1628 if (has_zero) {
1629
1630 rangesetShuffleToFrom(rangeTable,
0,
1, n,
0);
1631 n -=
1;
1632 }
1633 else {
1634
1635 rangesetShuffleToFrom(rangeTable,
1,
0, n,
0);
1636 rangeTable[
0] =
0;
1637 n +=
1;
1638 }
1639 if (has_end)
1640 n -=
1;
1641 else
1642 n +=
1;
1643 }
1644
1645 rangeset->n_ranges = n /
2;
1646 rangeset->ranges = RangesRealloc((Range *)rangeTable, rangeset->n_ranges);
1647
1648 RangesetRefreshRange(rangeset,
0, rangeset->maxpos);
1649 return rangeset->n_ranges;
1650 }
1651
1652
1653
1654
1655
1656
1657
1658
1659 int RangesetAddBetween(Rangeset *rangeset,
int start,
int end)
1660 {
1661 int i, j, n, *rangeTable = (
int *)rangeset->ranges;
1662
1663 if (start > end) {
1664 i = start;
1665 start = end;
1666 end = i;
1667 }
1668 else if (start == end) {
1669 return rangeset->n_ranges;
1670 }
1671
1672 n =
2 * rangeset->n_ranges;
1673
1674 if (n ==
0) {
1675 rangeset->ranges = RangesNew(
1);
1676 rangeTable = (
int *)rangeset->ranges;
1677 i =
0;
1678 }
1679 else
1680 i = rangesetWeightedAtOrBefore(rangeset, start);
1681
1682 if (i == n) {
1683 rangeTable[n] = start;
1684 rangeTable[n +
1] = end;
1685 rangeset->n_ranges++;
1686 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1687
1688 RangesetRefreshRange(rangeset, start, end);
1689 return rangeset->n_ranges;
1690 }
1691
1692 j = i;
1693 while (j < n && rangeTable[j] <= end)
1694 j++;
1695
1696 if (i == j) {
1697 if (is_start(i)) {
1698
1699 rangesetShuffleToFrom(rangeTable, i +
2, i, n - i,
0);
1700 rangeTable[i] = start;
1701 rangeTable[i +
1] = end;
1702 rangeset->n_ranges++;
1703 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1704 }
1705 else {
1706 return rangeset->n_ranges;
1707 }
1708 }
1709 else {
1710
1711 if (is_start(i))
1712 rangeTable[i++] = start;
1713 if (is_start(j))
1714 rangeTable[--j] = end;
1715 if (i < j)
1716 rangesetShuffleToFrom(rangeTable, i, j, n - j,
0);
1717 n -= j - i;
1718 rangeset->n_ranges = n /
2;
1719 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1720 }
1721
1722 RangesetRefreshRange(rangeset, start, end);
1723 return rangeset->n_ranges;
1724 }
1725
1726
1727
1728
1729
1730
1731
1732 int RangesetRemoveBetween(Rangeset *rangeset,
int start,
int end)
1733 {
1734 int i, j, n, *rangeTable = (
int *)rangeset->ranges;
1735
1736 if (start > end) {
1737 i = start;
1738 start = end;
1739 end = i;
1740 }
1741 else if (start == end) {
1742 return rangeset->n_ranges;
1743 }
1744
1745 n =
2 * rangeset->n_ranges;
1746
1747 i = rangesetWeightedAtOrBefore(rangeset, start);
1748
1749 if (i == n)
1750 return rangeset->n_ranges;
1751
1752 j = i;
1753 while (j < n && rangeTable[j] <= end)
1754 j++;
1755
1756 if (i == j) {
1757
1758 if (is_start(i))
1759 return rangeset->n_ranges;
1760 else {
1761
1762 i--;
1763 rangesetShuffleToFrom(rangeTable, i +
2, i, n - i,
0);
1764 rangeTable[i +
1] = start;
1765 rangeTable[i +
2] = end;
1766 rangeset->n_ranges++;
1767 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1768 }
1769 }
1770 else {
1771
1772 if (is_end(i))
1773 rangeTable[i++] = start;
1774 if (is_end(j))
1775 rangeTable[--j] = end;
1776 if (i < j)
1777 rangesetShuffleToFrom(rangeTable, i, j, n - j,
0);
1778 n -= j - i;
1779 rangeset->n_ranges = n /
2;
1780 rangeset->ranges = RangesRealloc(rangeset->ranges, rangeset->n_ranges);
1781 }
1782
1783 RangesetRefreshRange(rangeset, start, end);
1784 return rangeset->n_ranges;
1785 }
1786
1787
1788
1789