-
Notifications
You must be signed in to change notification settings - Fork 1
/
ckTextTag.c
922 lines (862 loc) · 25.9 KB
/
ckTextTag.c
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
/*
* ckTextTag.c --
*
* This module implements the "tag" subcommand of the widget command
* for text widgets, plus most of the other high-level functions
* related to tags.
*
* Copyright (c) 1992-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
* Copyright (c) 1995 Christian Werner
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "ckPort.h"
#include "ck.h"
#include "ckText.h"
#include "default.h"
/*
* Information used for parsing tag configuration information:
*/
static Ck_ConfigSpec tagConfigSpecs[] = {
{CK_CONFIG_ATTR, "-attributes", (char *) NULL, (char *) NULL,
(char *) NULL, Ck_Offset(CkTextTag, attr), 0},
{CK_CONFIG_COLOR, "-background", (char *) NULL, (char *) NULL,
(char *) NULL, Ck_Offset(CkTextTag, bg), 0},
{CK_CONFIG_COLOR, "-foreground", (char *) NULL, (char *) NULL,
(char *) NULL, Ck_Offset(CkTextTag, fg), 0},
{CK_CONFIG_STRING, "-justify", (char *) NULL, (char *) NULL,
(char *) NULL, Ck_Offset(CkTextTag, justifyString), CK_CONFIG_NULL_OK},
{CK_CONFIG_STRING, "-lmargin1", (char *) NULL, (char *) NULL,
(char *) NULL, Ck_Offset(CkTextTag, lMargin1String), CK_CONFIG_NULL_OK},
{CK_CONFIG_STRING, "-lmargin2", (char *) NULL, (char *) NULL,
(char *) NULL, Ck_Offset(CkTextTag, lMargin2String), CK_CONFIG_NULL_OK},
{CK_CONFIG_STRING, "-rmargin", (char *) NULL, (char *) NULL,
(char *) NULL, Ck_Offset(CkTextTag, rMarginString), CK_CONFIG_NULL_OK},
{CK_CONFIG_STRING, "-tabs", (char *) NULL, (char *) NULL,
(char *) NULL, Ck_Offset(CkTextTag, tabString), CK_CONFIG_NULL_OK},
{CK_CONFIG_UID, "-wrap", (char *) NULL, (char *) NULL,
(char *) NULL, Ck_Offset(CkTextTag, wrapMode),
CK_CONFIG_NULL_OK},
{CK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
(char *) NULL, 0, 0}
};
/*
* Forward declarations for procedures defined later in this file:
*/
static void ChangeTagPriority _ANSI_ARGS_((CkText *textPtr,
CkTextTag *tagPtr, int prio));
static CkTextTag * FindTag _ANSI_ARGS_((Tcl_Interp *interp,
CkText *textPtr, char *tagName));
static void SortTags _ANSI_ARGS_((int numTags,
CkTextTag **tagArrayPtr));
static int TagSortProc _ANSI_ARGS_((CONST VOID *first,
CONST VOID *second));
/*
*--------------------------------------------------------------
*
* CkTextTagCmd --
*
* This procedure is invoked to process the "tag" options of
* the widget command for text widgets. See the user documentation
* for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
CkTextTagCmd(textPtr, interp, argc, argv)
register CkText *textPtr; /* Information about text widget. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. Someone else has already
* parsed this command enough to know that
* argv[1] is "tag". */
{
int c, i, addTag;
size_t length;
char *fullOption;
register CkTextTag *tagPtr;
CkTextIndex first, last, index1, index2;
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag option ?arg arg ...?\"", (char *) NULL);
return TCL_ERROR;
}
c = argv[2][0];
length = strlen(argv[2]);
if ((c == 'a') && (strncmp(argv[2], "add", length) == 0)) {
fullOption = "add";
addTag = 1;
addAndRemove:
if (argc < 5) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag ", fullOption,
" tagName index1 ?index2 index1 index2 ...?\"",
(char *) NULL);
return TCL_ERROR;
}
tagPtr = CkTextCreateTag(textPtr, argv[3]);
for (i = 4; i < argc; i += 2) {
if (CkTextGetIndex(interp, textPtr, argv[i], &index1) != TCL_OK) {
return TCL_ERROR;
}
if (argc > (i+1)) {
if (CkTextGetIndex(interp, textPtr, argv[i+1], &index2)
!= TCL_OK) {
return TCL_ERROR;
}
if (CkTextIndexCmp(&index1, &index2) >= 0) {
return TCL_OK;
}
} else {
index2 = index1;
CkTextIndexForwChars(&index2, 1, &index2);
}
if (tagPtr->affectsDisplay) {
CkTextRedrawTag(textPtr, &index1, &index2, tagPtr, !addTag);
} else {
/*
* Still need to trigger enter/leave events on tags that
* have changed.
*/
CkTextEventuallyRepick(textPtr);
}
CkBTreeTag(&index1, &index2, tagPtr, addTag);
/*
* If the tag is "sel" then grab the selection if we're supposed
* to export it and don't already have it. Also, invalidate
* partially-completed selection retrievals.
*/
if (tagPtr == textPtr->selTagPtr) {
textPtr->abortSelections = 1;
}
}
} else if ((c == 'b') && (strncmp(argv[2], "bind", length) == 0)) {
if ((argc < 4) || (argc > 6)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag bind tagName ?sequence? ?command?\"",
(char *) NULL);
return TCL_ERROR;
}
tagPtr = CkTextCreateTag(textPtr, argv[3]);
/*
* Make a binding table if the widget doesn't already have
* one.
*/
if (textPtr->bindingTable == NULL) {
textPtr->bindingTable = Ck_CreateBindingTable(interp);
}
if (argc == 6) {
int append = 0;
unsigned long mask;
if (argv[5][0] == 0) {
return Ck_DeleteBinding(interp, textPtr->bindingTable,
(ClientData) tagPtr, argv[4]);
}
if (argv[5][0] == '+') {
argv[5]++;
append = 1;
}
mask = Ck_CreateBinding(interp, textPtr->bindingTable,
(ClientData) tagPtr, argv[4], argv[5], append);
if (mask != TCL_OK) {
return TCL_ERROR;
}
} else if (argc == 5) {
char *command;
command = Ck_GetBinding(interp, textPtr->bindingTable,
(ClientData) tagPtr, argv[4]);
if (command == NULL) {
return TCL_ERROR;
}
Tcl_SetResult(interp, command, TCL_VOLATILE);
} else {
Ck_GetAllBindings(interp, textPtr->bindingTable,
(ClientData) tagPtr);
}
} else if ((c == 'c') && (strncmp(argv[2], "cget", length) == 0)
&& (length >= 2)) {
if (argc != 5) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag cget tagName option\"",
(char *) NULL);
return TCL_ERROR;
}
tagPtr = FindTag(interp, textPtr, argv[3]);
if (tagPtr == NULL) {
return TCL_ERROR;
}
return Ck_ConfigureValue(interp, textPtr->winPtr, tagConfigSpecs,
(char *) tagPtr, argv[4], 0);
} else if ((c == 'c') && (strncmp(argv[2], "configure", length) == 0)
&& (length >= 2)) {
if (argc < 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag configure tagName ?option? ?value? ",
"?option value ...?\"", (char *) NULL);
return TCL_ERROR;
}
tagPtr = CkTextCreateTag(textPtr, argv[3]);
if (argc == 4) {
return Ck_ConfigureInfo(interp, textPtr->winPtr, tagConfigSpecs,
(char *) tagPtr, (char *) NULL, 0);
} else if (argc == 5) {
return Ck_ConfigureInfo(interp, textPtr->winPtr, tagConfigSpecs,
(char *) tagPtr, argv[4], 0);
} else {
int result;
result = Ck_ConfigureWidget(interp, textPtr->winPtr,
tagConfigSpecs, argc-4, argv+4, (char *) tagPtr, 0);
/*
* Some of the configuration options, like -underline
* and -justify, require additional translation (this is
* needed because we need to distinguish a particular value
* of an option from "unspecified").
*/
if (tagPtr->justifyString != NULL) {
if (Ck_GetJustify(interp, tagPtr->justifyString,
&tagPtr->justify) != TCL_OK) {
return TCL_ERROR;
}
}
if (tagPtr->lMargin1String != NULL) {
if (Ck_GetCoord(interp, textPtr->winPtr,
tagPtr->lMargin1String, &tagPtr->lMargin1) != TCL_OK) {
return TCL_ERROR;
}
}
if (tagPtr->lMargin2String != NULL) {
if (Ck_GetCoord(interp, textPtr->winPtr,
tagPtr->lMargin2String, &tagPtr->lMargin2) != TCL_OK) {
return TCL_ERROR;
}
}
if (tagPtr->rMarginString != NULL) {
if (Ck_GetCoord(interp, textPtr->winPtr,
tagPtr->rMarginString, &tagPtr->rMargin) != TCL_OK) {
return TCL_ERROR;
}
}
if (tagPtr->tabArrayPtr != NULL) {
ckfree((char *) tagPtr->tabArrayPtr);
tagPtr->tabArrayPtr = NULL;
}
if (tagPtr->tabString != NULL) {
tagPtr->tabArrayPtr = CkTextGetTabs(interp, textPtr->winPtr,
tagPtr->tabString);
if (tagPtr->tabArrayPtr == NULL) {
return TCL_ERROR;
}
}
if ((tagPtr->wrapMode != NULL)
&& (tagPtr->wrapMode != ckTextCharUid)
&& (tagPtr->wrapMode != ckTextNoneUid)
&& (tagPtr->wrapMode != ckTextWordUid)) {
Tcl_AppendResult(interp, "bad wrap mode \"", tagPtr->wrapMode,
"\": must be char, none, or word", (char *) NULL);
tagPtr->wrapMode = NULL;
return TCL_ERROR;
}
/*
* If the "sel" tag was changed, be sure to mirror information
* from the tag back into the text widget record. NOTE: we
* don't have to free up information in the widget record
* before overwriting it, because it was mirrored in the tag
* and hence freed when the tag field was overwritten.
*/
if (tagPtr == textPtr->selTagPtr) {
textPtr->selBg = tagPtr->bg;
textPtr->selFg = tagPtr->fg;
textPtr->selAttr = tagPtr->attr;
}
tagPtr->affectsDisplay = 1;
CkTextRedrawTag(textPtr, (CkTextIndex *) NULL,
(CkTextIndex *) NULL, tagPtr, 1);
return result;
}
} else if ((c == 'd') && (strncmp(argv[2], "delete", length) == 0)) {
Tcl_HashEntry *hPtr;
if (argc < 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag delete tagName tagName ...\"",
(char *) NULL);
return TCL_ERROR;
}
for (i = 3; i < argc; i++) {
hPtr = Tcl_FindHashEntry(&textPtr->tagTable, argv[i]);
if (hPtr == NULL) {
continue;
}
tagPtr = (CkTextTag *) Tcl_GetHashValue(hPtr);
if (tagPtr == textPtr->selTagPtr) {
continue;
}
if (tagPtr->affectsDisplay) {
CkTextRedrawTag(textPtr, (CkTextIndex *) NULL,
(CkTextIndex *) NULL, tagPtr, 1);
}
#if CK_USE_UTF
CkBTreeTag(CkTextMakeByteIndex(textPtr->tree, 0, 0, &first),
CkTextMakeByteIndex(textPtr->tree,
CkBTreeNumLines(textPtr->tree), 0, &last),
tagPtr, 0);
#else
CkBTreeTag(CkTextMakeIndex(textPtr->tree, 0, 0, &first),
CkTextMakeIndex(textPtr->tree,
CkBTreeNumLines(textPtr->tree), 0, &last),
tagPtr, 0);
#endif
Tcl_DeleteHashEntry(hPtr);
if (textPtr->bindingTable != NULL) {
Ck_DeleteAllBindings(textPtr->bindingTable,
(ClientData) tagPtr);
}
/*
* Update the tag priorities to reflect the deletion of this tag.
*/
ChangeTagPriority(textPtr, tagPtr, textPtr->numTags-1);
textPtr->numTags -= 1;
CkTextFreeTag(textPtr, tagPtr);
}
} else if ((c == 'l') && (strncmp(argv[2], "lower", length) == 0)) {
CkTextTag *tagPtr2;
int prio;
if ((argc != 4) && (argc != 5)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag lower tagName ?belowThis?\"",
(char *) NULL);
return TCL_ERROR;
}
tagPtr = FindTag(interp, textPtr, argv[3]);
if (tagPtr == NULL) {
return TCL_ERROR;
}
if (argc == 5) {
tagPtr2 = FindTag(interp, textPtr, argv[4]);
if (tagPtr2 == NULL) {
return TCL_ERROR;
}
if (tagPtr->priority < tagPtr2->priority) {
prio = tagPtr2->priority - 1;
} else {
prio = tagPtr2->priority;
}
} else {
prio = 0;
}
ChangeTagPriority(textPtr, tagPtr, prio);
CkTextRedrawTag(textPtr, (CkTextIndex *) NULL, (CkTextIndex *) NULL,
tagPtr, 1);
} else if ((c == 'n') && (strncmp(argv[2], "names", length) == 0)
&& (length >= 2)) {
CkTextTag **arrayPtr;
int arraySize;
if ((argc != 3) && (argc != 4)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag names ?index?\"",
(char *) NULL);
return TCL_ERROR;
}
if (argc == 3) {
Tcl_HashSearch search;
Tcl_HashEntry *hPtr;
arrayPtr = (CkTextTag **) ckalloc((unsigned)
(textPtr->numTags * sizeof(CkTextTag *)));
for (i = 0, hPtr = Tcl_FirstHashEntry(&textPtr->tagTable, &search);
hPtr != NULL; i++, hPtr = Tcl_NextHashEntry(&search)) {
arrayPtr[i] = (CkTextTag *) Tcl_GetHashValue(hPtr);
}
arraySize = textPtr->numTags;
} else {
if (CkTextGetIndex(interp, textPtr, argv[3], &index1)
!= TCL_OK) {
return TCL_ERROR;
}
arrayPtr = CkBTreeGetTags(&index1, &arraySize);
if (arrayPtr == NULL) {
return TCL_OK;
}
}
SortTags(arraySize, arrayPtr);
for (i = 0; i < arraySize; i++) {
tagPtr = arrayPtr[i];
Tcl_AppendElement(interp, tagPtr->name);
}
ckfree((char *) arrayPtr);
} else if ((c == 'n') && (strncmp(argv[2], "nextrange", length) == 0)
&& (length >= 2)) {
CkTextSearch tSearch;
char position[TK_POS_CHARS];
if ((argc != 5) && (argc != 6)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag nextrange tagName index1 ?index2?\"",
(char *) NULL);
return TCL_ERROR;
}
tagPtr = FindTag((Tcl_Interp *) NULL, textPtr, argv[3]);
if (tagPtr == NULL) {
return TCL_OK;
}
if (CkTextGetIndex(interp, textPtr, argv[4], &index1) != TCL_OK) {
return TCL_ERROR;
}
#if CK_USE_UTF
CkTextMakeByteIndex(textPtr->tree, CkBTreeNumLines(textPtr->tree),
0, &last);
#else
CkTextMakeIndex(textPtr->tree, CkBTreeNumLines(textPtr->tree),
0, &last);
#endif
if (argc == 5) {
index2 = last;
} else if (CkTextGetIndex(interp, textPtr, argv[5], &index2)
!= TCL_OK) {
return TCL_ERROR;
}
/*
* The search below is a bit tricky. Rather than use the B-tree
* facilities to stop the search at index2, let it search up
* until the end of the file but check for a position past index2
* ourselves. The reason for doing it this way is that we only
* care whether the *start* of the range is before index2; once
* we find the start, we don't want CkBTreeNextTag to abort the
* search because the end of the range is after index2.
*/
CkBTreeStartSearch(&index1, &last, tagPtr, &tSearch);
if (CkBTreeCharTagged(&index1, tagPtr)) {
CkTextSegment *segPtr;
int offset;
/*
* The first character is tagged. See if there is an
* on-toggle just before the character. If not, then
* skip to the end of this tagged range.
*/
for (segPtr = index1.linePtr->segPtr, offset = index1.charIndex;
offset >= 0;
offset -= segPtr->size, segPtr = segPtr->nextPtr) {
if ((offset == 0) && (segPtr->typePtr == &ckTextToggleOnType)
&& (segPtr->body.toggle.tagPtr == tagPtr)) {
goto gotStart;
}
}
if (!CkBTreeNextTag(&tSearch)) {
return TCL_OK;
}
}
/*
* Find the start of the tagged range.
*/
if (!CkBTreeNextTag(&tSearch)) {
return TCL_OK;
}
gotStart:
if (CkTextIndexCmp(&tSearch.curIndex, &index2) >= 0) {
return TCL_OK;
}
CkTextPrintIndex(&tSearch.curIndex, position);
Tcl_AppendElement(interp, position);
CkBTreeNextTag(&tSearch);
CkTextPrintIndex(&tSearch.curIndex, position);
Tcl_AppendElement(interp, position);
} else if ((c == 'r') && (strncmp(argv[2], "raise", length) == 0)
&& (length >= 3)) {
CkTextTag *tagPtr2;
int prio;
if ((argc != 4) && (argc != 5)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag raise tagName ?aboveThis?\"",
(char *) NULL);
return TCL_ERROR;
}
tagPtr = FindTag(interp, textPtr, argv[3]);
if (tagPtr == NULL) {
return TCL_ERROR;
}
if (argc == 5) {
tagPtr2 = FindTag(interp, textPtr, argv[4]);
if (tagPtr2 == NULL) {
return TCL_ERROR;
}
if (tagPtr->priority <= tagPtr2->priority) {
prio = tagPtr2->priority;
} else {
prio = tagPtr2->priority + 1;
}
} else {
prio = textPtr->numTags-1;
}
ChangeTagPriority(textPtr, tagPtr, prio);
CkTextRedrawTag(textPtr, (CkTextIndex *) NULL, (CkTextIndex *) NULL,
tagPtr, 1);
} else if ((c == 'r') && (strncmp(argv[2], "ranges", length) == 0)
&& (length >= 3)) {
CkTextSearch tSearch;
char position[TK_POS_CHARS];
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " tag ranges tagName\"", (char *) NULL);
return TCL_ERROR;
}
tagPtr = FindTag((Tcl_Interp *) NULL, textPtr, argv[3]);
if (tagPtr == NULL) {
return TCL_OK;
}
#if CK_USE_UTF
CkTextMakeByteIndex(textPtr->tree, 0, 0, &first);
CkTextMakeByteIndex(textPtr->tree, CkBTreeNumLines(textPtr->tree),
0, &last);
#else
CkTextMakeIndex(textPtr->tree, 0, 0, &first);
CkTextMakeIndex(textPtr->tree, CkBTreeNumLines(textPtr->tree),
0, &last);
#endif
CkBTreeStartSearch(&first, &last, tagPtr, &tSearch);
if (CkBTreeCharTagged(&first, tagPtr)) {
CkTextPrintIndex(&first, position);
Tcl_AppendElement(interp, position);
}
while (CkBTreeNextTag(&tSearch)) {
CkTextPrintIndex(&tSearch.curIndex, position);
Tcl_AppendElement(interp, position);
}
} else if ((c == 'r') && (strncmp(argv[2], "remove", length) == 0)
&& (length >= 2)) {
fullOption = "remove";
addTag = 0;
goto addAndRemove;
} else {
Tcl_AppendResult(interp, "bad tag option \"", argv[2],
"\": must be add, bind, cget, configure, delete, lower, ",
"names, nextrange, raise, ranges, or remove",
(char *) NULL);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* CkTextCreateTag --
*
* Find the record describing a tag within a given text widget,
* creating a new record if one doesn't already exist.
*
* Results:
* The return value is a pointer to the CkTextTag record for tagName.
*
* Side effects:
* A new tag record is created if there isn't one already defined
* for tagName.
*
*----------------------------------------------------------------------
*/
CkTextTag *
CkTextCreateTag(textPtr, tagName)
CkText *textPtr; /* Widget in which tag is being used. */
char *tagName; /* Name of desired tag. */
{
register CkTextTag *tagPtr;
Tcl_HashEntry *hPtr;
int new;
hPtr = Tcl_CreateHashEntry(&textPtr->tagTable, tagName, &new);
if (!new) {
return (CkTextTag *) Tcl_GetHashValue(hPtr);
}
/*
* No existing entry. Create a new one, initialize it, and add a
* pointer to it to the hash table entry.
*/
tagPtr = (CkTextTag *) ckalloc(sizeof(CkTextTag));
tagPtr->name = Tcl_GetHashKey(&textPtr->tagTable, hPtr);
tagPtr->priority = textPtr->numTags;
tagPtr->bg = -1;
tagPtr->fg = -1;
tagPtr->attr = -1;
tagPtr->justifyString = NULL;
tagPtr->justify = CK_JUSTIFY_LEFT;
tagPtr->lMargin1String = NULL;
tagPtr->lMargin1 = 0;
tagPtr->lMargin2String = NULL;
tagPtr->lMargin2 = 0;
tagPtr->rMarginString = NULL;
tagPtr->rMargin = 0;
tagPtr->tabString = NULL;
tagPtr->tabArrayPtr = NULL;
tagPtr->wrapMode = NULL;
tagPtr->affectsDisplay = 0;
textPtr->numTags++;
Tcl_SetHashValue(hPtr, tagPtr);
return tagPtr;
}
/*
*----------------------------------------------------------------------
*
* FindTag --
*
* See if tag is defined for a given widget.
*
* Results:
* If tagName is defined in textPtr, a pointer to its CkTextTag
* structure is returned. Otherwise NULL is returned and an
* error message is recorded in interp->result unless interp
* is NULL.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static CkTextTag *
FindTag(interp, textPtr, tagName)
Tcl_Interp *interp; /* Interpreter to use for error message;
* if NULL, then don't record an error
* message. */
CkText *textPtr; /* Widget in which tag is being used. */
char *tagName; /* Name of desired tag. */
{
Tcl_HashEntry *hPtr;
hPtr = Tcl_FindHashEntry(&textPtr->tagTable, tagName);
if (hPtr != NULL) {
return (CkTextTag *) Tcl_GetHashValue(hPtr);
}
if (interp != NULL) {
Tcl_AppendResult(interp, "tag \"", tagName,
"\" isn't defined in text widget", (char *) NULL);
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
* CkTextFreeTag --
*
* This procedure is called when a tag is deleted to free up the
* memory and other resources associated with the tag.
*
* Results:
* None.
*
* Side effects:
* Memory and other resources are freed.
*
*----------------------------------------------------------------------
*/
void
CkTextFreeTag(textPtr, tagPtr)
CkText *textPtr; /* Info about overall widget. */
register CkTextTag *tagPtr; /* Tag being deleted. */
{
if (tagPtr->justifyString != NULL) {
ckfree(tagPtr->justifyString);
}
if (tagPtr->lMargin1String != NULL) {
ckfree(tagPtr->lMargin1String);
}
if (tagPtr->lMargin2String != NULL) {
ckfree(tagPtr->lMargin2String);
}
if (tagPtr->rMarginString != NULL) {
ckfree(tagPtr->rMarginString);
}
if (tagPtr->tabString != NULL) {
ckfree(tagPtr->tabString);
}
if (tagPtr->tabArrayPtr != NULL) {
ckfree((char *) tagPtr->tabArrayPtr);
}
ckfree((char *) tagPtr);
}
/*
*----------------------------------------------------------------------
*
* SortTags --
*
* This procedure sorts an array of tag pointers in increasing
* order of priority, optimizing for the common case where the
* array is small.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
SortTags(numTags, tagArrayPtr)
int numTags; /* Number of tag pointers at *tagArrayPtr. */
CkTextTag **tagArrayPtr; /* Pointer to array of pointers. */
{
int i, j, prio;
register CkTextTag **tagPtrPtr;
CkTextTag **maxPtrPtr, *tmp;
if (numTags < 2) {
return;
}
if (numTags < 20) {
for (i = numTags-1; i > 0; i--, tagArrayPtr++) {
maxPtrPtr = tagPtrPtr = tagArrayPtr;
prio = tagPtrPtr[0]->priority;
for (j = i, tagPtrPtr++; j > 0; j--, tagPtrPtr++) {
if (tagPtrPtr[0]->priority < prio) {
prio = tagPtrPtr[0]->priority;
maxPtrPtr = tagPtrPtr;
}
}
tmp = *maxPtrPtr;
*maxPtrPtr = *tagArrayPtr;
*tagArrayPtr = tmp;
}
} else {
qsort((VOID *) tagArrayPtr, (unsigned) numTags, sizeof (CkTextTag *),
TagSortProc);
}
}
/*
*----------------------------------------------------------------------
*
* TagSortProc --
*
* This procedure is called by qsort when sorting an array of
* tags in priority order.
*
* Results:
* The return value is -1 if the first argument should be before
* the second element (i.e. it has lower priority), 0 if it's
* equivalent (this should never happen!), and 1 if it should be
* after the second element.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
TagSortProc(first, second)
CONST VOID *first, *second; /* Elements to be compared. */
{
CkTextTag *tagPtr1, *tagPtr2;
tagPtr1 = * (CkTextTag **) first;
tagPtr2 = * (CkTextTag **) second;
return tagPtr1->priority - tagPtr2->priority;
}
/*
*----------------------------------------------------------------------
*
* ChangeTagPriority --
*
* This procedure changes the priority of a tag by modifying
* its priority and the priorities of other tags that are affected
* by the change.
*
* Results:
* None.
*
* Side effects:
* Priorities may be changed for some or all of the tags in
* textPtr. The tags will be arranged so that there is exactly
* one tag at each priority level between 0 and textPtr->numTags-1,
* with tagPtr at priority "prio".
*
*----------------------------------------------------------------------
*/
static void
ChangeTagPriority(textPtr, tagPtr, prio)
CkText *textPtr; /* Information about text widget. */
CkTextTag *tagPtr; /* Tag whose priority is to be
* changed. */
int prio; /* New priority for tag. */
{
int low, high, delta;
register CkTextTag *tagPtr2;
Tcl_HashEntry *hPtr;
Tcl_HashSearch search;
if (prio < 0) {
prio = 0;
}
if (prio >= textPtr->numTags) {
prio = textPtr->numTags-1;
}
if (prio == tagPtr->priority) {
return;
} else if (prio < tagPtr->priority) {
low = prio;
high = tagPtr->priority-1;
delta = 1;
} else {
low = tagPtr->priority+1;
high = prio;
delta = -1;
}
for (hPtr = Tcl_FirstHashEntry(&textPtr->tagTable, &search);
hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
tagPtr2 = (CkTextTag *) Tcl_GetHashValue(hPtr);
if ((tagPtr2->priority >= low) && (tagPtr2->priority <= high)) {
tagPtr2->priority += delta;
}
}
tagPtr->priority = prio;
}
/*
*--------------------------------------------------------------
*
* CkTextBindProc --
*
* This procedure is invoked by the Ck dispatcher to handle
* events associated with bindings on items.
*
* Results:
* None.
*
* Side effects:
* Depends on the command invoked as part of the binding
* (if there was any).
*
*--------------------------------------------------------------
*/
void
CkTextBindProc(clientData, eventPtr)
ClientData clientData; /* Pointer to text structure. */
CkEvent *eventPtr; /* Pointer to X event that just
* happened. */
{
}
/*
*--------------------------------------------------------------
*
* CkTextPickCurrent --
*
* Find the character containing the coordinates in an event
* and place the "current" mark on that character. If the
* "current" mark has moved then generate a fake leave event
* on the old current character and a fake enter event on the new
* current character.
*
* Results:
* None.
*
* Side effects:
* The current mark for textPtr may change. If it does,
* then the commands associated with character entry and leave
* could do just about anything.
*
*--------------------------------------------------------------
*/
void
CkTextPickCurrent(textPtr, eventPtr)
register CkText *textPtr; /* Text widget in which to select
* current character. */
CkEvent *eventPtr; /* Event describing location of
* mouse cursor. Must be EnterWindow,
* LeaveWindow, ButtonRelease, or
* MotionNotify. */
{
}