| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "default.h" |
| 17 |
#include "tkPort.h" |
| 18 |
#include "tkInt.h" |
| 19 |
#include "tkText.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#define LAST_CHAR 1000000 |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
static CONST char * ForwBack _ANSI_ARGS_((CONST char *string, |
| 32 |
TkTextIndex *indexPtr)); |
| 33 |
static CONST char * StartEnd _ANSI_ARGS_((CONST char *string, |
| 34 |
TkTextIndex *indexPtr)); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
TkTextIndex * |
| 57 |
TkTextMakeByteIndex(tree, lineIndex, byteIndex, indexPtr) |
| 58 |
TkTextBTree tree; |
| 59 |
|
| 60 |
int lineIndex; |
| 61 |
|
| 62 |
int byteIndex; |
| 63 |
TkTextIndex *indexPtr; |
| 64 |
{ |
| 65 |
TkTextSegment *segPtr; |
| 66 |
int index; |
| 67 |
CONST char *p, *start; |
| 68 |
Tcl_UniChar ch; |
| 69 |
|
| 70 |
indexPtr->tree = tree; |
| 71 |
if (lineIndex < 0) { |
| 72 |
lineIndex = 0; |
| 73 |
byteIndex = 0; |
| 74 |
} |
| 75 |
if (byteIndex < 0) { |
| 76 |
byteIndex = 0; |
| 77 |
} |
| 78 |
indexPtr->linePtr = TkBTreeFindLine(tree, lineIndex); |
| 79 |
if (indexPtr->linePtr == NULL) { |
| 80 |
indexPtr->linePtr = TkBTreeFindLine(tree, TkBTreeNumLines(tree)); |
| 81 |
byteIndex = 0; |
| 82 |
} |
| 83 |
if (byteIndex == 0) { |
| 84 |
indexPtr->byteIndex = byteIndex; |
| 85 |
return indexPtr; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
index = 0; |
| 94 |
for (segPtr = indexPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr) { |
| 95 |
if (segPtr == NULL) { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
indexPtr->byteIndex = index - sizeof(char); |
| 103 |
break; |
| 104 |
} |
| 105 |
if (index + segPtr->size > byteIndex) { |
| 106 |
indexPtr->byteIndex = byteIndex; |
| 107 |
if ((byteIndex > index) && (segPtr->typePtr == &tkTextCharType)) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
start = segPtr->body.chars + (byteIndex - index); |
| 116 |
p = Tcl_UtfPrev(start, segPtr->body.chars); |
| 117 |
p += Tcl_UtfToUniChar(p, &ch); |
| 118 |
indexPtr->byteIndex += p - start; |
| 119 |
} |
| 120 |
break; |
| 121 |
} |
| 122 |
index += segPtr->size; |
| 123 |
} |
| 124 |
return indexPtr; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
TkTextIndex * |
| 148 |
TkTextMakeCharIndex(tree, lineIndex, charIndex, indexPtr) |
| 149 |
TkTextBTree tree; |
| 150 |
|
| 151 |
int lineIndex; |
| 152 |
|
| 153 |
int charIndex; |
| 154 |
TkTextIndex *indexPtr; |
| 155 |
{ |
| 156 |
register TkTextSegment *segPtr; |
| 157 |
char *p, *start, *end; |
| 158 |
int index, offset; |
| 159 |
Tcl_UniChar ch; |
| 160 |
|
| 161 |
indexPtr->tree = tree; |
| 162 |
if (lineIndex < 0) { |
| 163 |
lineIndex = 0; |
| 164 |
charIndex = 0; |
| 165 |
} |
| 166 |
if (charIndex < 0) { |
| 167 |
charIndex = 0; |
| 168 |
} |
| 169 |
indexPtr->linePtr = TkBTreeFindLine(tree, lineIndex); |
| 170 |
if (indexPtr->linePtr == NULL) { |
| 171 |
indexPtr->linePtr = TkBTreeFindLine(tree, TkBTreeNumLines(tree)); |
| 172 |
charIndex = 0; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
index = 0; |
| 181 |
for (segPtr = indexPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr) { |
| 182 |
if (segPtr == NULL) { |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
indexPtr->byteIndex = index - sizeof(char); |
| 190 |
break; |
| 191 |
} |
| 192 |
if (segPtr->typePtr == &tkTextCharType) { |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
start = segPtr->body.chars; |
| 198 |
end = start + segPtr->size; |
| 199 |
for (p = start; p < end; p += offset) { |
| 200 |
if (charIndex == 0) { |
| 201 |
indexPtr->byteIndex = index; |
| 202 |
return indexPtr; |
| 203 |
} |
| 204 |
charIndex--; |
| 205 |
offset = Tcl_UtfToUniChar(p, &ch); |
| 206 |
index += offset; |
| 207 |
} |
| 208 |
} else { |
| 209 |
if (charIndex < segPtr->size) { |
| 210 |
indexPtr->byteIndex = index; |
| 211 |
break; |
| 212 |
} |
| 213 |
charIndex -= segPtr->size; |
| 214 |
index += segPtr->size; |
| 215 |
} |
| 216 |
} |
| 217 |
return indexPtr; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
TkTextSegment * |
| 241 |
TkTextIndexToSeg(indexPtr, offsetPtr) |
| 242 |
CONST TkTextIndex *indexPtr; |
| 243 |
int *offsetPtr; |
| 244 |
|
| 245 |
{ |
| 246 |
TkTextSegment *segPtr; |
| 247 |
int offset; |
| 248 |
|
| 249 |
for (offset = indexPtr->byteIndex, segPtr = indexPtr->linePtr->segPtr; |
| 250 |
offset >= segPtr->size; |
| 251 |
offset -= segPtr->size, segPtr = segPtr->nextPtr) { |
| 252 |
|
| 253 |
} |
| 254 |
if (offsetPtr != NULL) { |
| 255 |
*offsetPtr = offset; |
| 256 |
} |
| 257 |
return segPtr; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
int |
| 279 |
TkTextSegToOffset(segPtr, linePtr) |
| 280 |
CONST TkTextSegment *segPtr; |
| 281 |
CONST TkTextLine *linePtr; |
| 282 |
{ |
| 283 |
CONST TkTextSegment *segPtr2; |
| 284 |
int offset; |
| 285 |
|
| 286 |
offset = 0; |
| 287 |
for (segPtr2 = linePtr->segPtr; segPtr2 != segPtr; |
| 288 |
segPtr2 = segPtr2->nextPtr) { |
| 289 |
offset += segPtr2->size; |
| 290 |
} |
| 291 |
return offset; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
int |
| 314 |
TkTextGetIndex(interp, textPtr, string, indexPtr) |
| 315 |
Tcl_Interp *interp; |
| 316 |
TkText *textPtr; |
| 317 |
CONST char *string; |
| 318 |
TkTextIndex *indexPtr; |
| 319 |
{ |
| 320 |
char *p, *end, *endOfBase; |
| 321 |
Tcl_HashEntry *hPtr; |
| 322 |
TkTextTag *tagPtr; |
| 323 |
TkTextSearch search; |
| 324 |
TkTextIndex first, last; |
| 325 |
int wantLast, result; |
| 326 |
char c; |
| 327 |
CONST char *cp; |
| 328 |
Tcl_DString copy; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
if (TkTextMarkNameToIndex(textPtr, string, indexPtr) == TCL_OK) { |
| 340 |
return TCL_OK; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
indexPtr->tree = textPtr->tree; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
Tcl_DStringInit(©); |
| 360 |
p = strrchr(Tcl_DStringAppend(©, string, -1), '.'); |
| 361 |
if (p != NULL) { |
| 362 |
if ((p[1] == 'f') && (strncmp(p+1, "first", 5) == 0)) { |
| 363 |
wantLast = 0; |
| 364 |
endOfBase = p+6; |
| 365 |
} else if ((p[1] == 'l') && (strncmp(p+1, "last", 4) == 0)) { |
| 366 |
wantLast = 1; |
| 367 |
endOfBase = p+5; |
| 368 |
} else { |
| 369 |
goto tryxy; |
| 370 |
} |
| 371 |
*p = 0; |
| 372 |
hPtr = Tcl_FindHashEntry(&textPtr->tagTable, Tcl_DStringValue(©)); |
| 373 |
*p = '.'; |
| 374 |
if (hPtr == NULL) { |
| 375 |
goto tryxy; |
| 376 |
} |
| 377 |
tagPtr = (TkTextTag *) Tcl_GetHashValue(hPtr); |
| 378 |
TkTextMakeByteIndex(textPtr->tree, 0, 0, &first); |
| 379 |
TkTextMakeByteIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree), 0, |
| 380 |
&last); |
| 381 |
TkBTreeStartSearch(&first, &last, tagPtr, &search); |
| 382 |
if (!TkBTreeCharTagged(&first, tagPtr) && !TkBTreeNextTag(&search)) { |
| 383 |
Tcl_ResetResult(interp); |
| 384 |
Tcl_AppendResult(interp, |
| 385 |
"text doesn't contain any characters tagged with \"", |
| 386 |
Tcl_GetHashKey(&textPtr->tagTable, hPtr), "\"", |
| 387 |
(char *) NULL); |
| 388 |
Tcl_DStringFree(©); |
| 389 |
return TCL_ERROR; |
| 390 |
} |
| 391 |
*indexPtr = search.curIndex; |
| 392 |
if (wantLast) { |
| 393 |
while (TkBTreeNextTag(&search)) { |
| 394 |
*indexPtr = search.curIndex; |
| 395 |
} |
| 396 |
} |
| 397 |
goto gotBase; |
| 398 |
} |
| 399 |
|
| 400 |
tryxy: |
| 401 |
if (string[0] == '@') { |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
int x, y; |
| 407 |
|
| 408 |
cp = string+1; |
| 409 |
x = strtol(cp, &end, 0); |
| 410 |
if ((end == cp) || (*end != ',')) { |
| 411 |
goto error; |
| 412 |
} |
| 413 |
cp = end+1; |
| 414 |
y = strtol(cp, &end, 0); |
| 415 |
if (end == cp) { |
| 416 |
goto error; |
| 417 |
} |
| 418 |
TkTextPixelIndex(textPtr, x, y, indexPtr); |
| 419 |
endOfBase = end; |
| 420 |
goto gotBase; |
| 421 |
} |
| 422 |
|
| 423 |
if (isdigit(UCHAR(string[0])) || (string[0] == '-')) { |
| 424 |
int lineIndex, charIndex; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
lineIndex = strtol(string, &end, 0) - 1; |
| 431 |
if ((end == string) || (*end != '.')) { |
| 432 |
goto error; |
| 433 |
} |
| 434 |
p = end+1; |
| 435 |
if ((*p == 'e') && (strncmp(p, "end", 3) == 0)) { |
| 436 |
charIndex = LAST_CHAR; |
| 437 |
endOfBase = p+3; |
| 438 |
} else { |
| 439 |
charIndex = strtol(p, &end, 0); |
| 440 |
if (end == p) { |
| 441 |
goto error; |
| 442 |
} |
| 443 |
endOfBase = end; |
| 444 |
} |
| 445 |
TkTextMakeCharIndex(textPtr->tree, lineIndex, charIndex, indexPtr); |
| 446 |
goto gotBase; |
| 447 |
} |
| 448 |
|
| 449 |
for (p = Tcl_DStringValue(©); *p != 0; p++) { |
| 450 |
if (isspace(UCHAR(*p)) || (*p == '+') || (*p == '-')) { |
| 451 |
break; |
| 452 |
} |
| 453 |
} |
| 454 |
endOfBase = p; |
| 455 |
if (string[0] == '.') { |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
c = *endOfBase; |
| 461 |
*endOfBase = 0; |
| 462 |
result = TkTextWindowIndex(textPtr, Tcl_DStringValue(©), indexPtr); |
| 463 |
*endOfBase = c; |
| 464 |
if (result != 0) { |
| 465 |
goto gotBase; |
| 466 |
} |
| 467 |
} |
| 468 |
if ((string[0] == 'e') |
| 469 |
&& (strncmp(string, "end", |
| 470 |
(size_t) (endOfBase-Tcl_DStringValue(©))) == 0)) { |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
TkTextMakeByteIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree), |
| 476 |
0, indexPtr); |
| 477 |
goto gotBase; |
| 478 |
} else { |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
c = *endOfBase; |
| 484 |
*endOfBase = 0; |
| 485 |
result = TkTextMarkNameToIndex(textPtr, Tcl_DStringValue(©), |
| 486 |
indexPtr); |
| 487 |
*endOfBase = c; |
| 488 |
if (result == TCL_OK) { |
| 489 |
goto gotBase; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
c = *endOfBase; |
| 497 |
*endOfBase = 0; |
| 498 |
result = TkTextImageIndex(textPtr, Tcl_DStringValue(©), indexPtr); |
| 499 |
*endOfBase = c; |
| 500 |
if (result != 0) { |
| 501 |
goto gotBase; |
| 502 |
} |
| 503 |
} |
| 504 |
goto error; |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
gotBase: |
| 516 |
cp = endOfBase; |
| 517 |
while (1) { |
| 518 |
while (isspace(UCHAR(*cp))) { |
| 519 |
cp++; |
| 520 |
} |
| 521 |
if (*cp == 0) { |
| 522 |
break; |
| 523 |
} |
| 524 |
|
| 525 |
if ((*cp == '+') || (*cp == '-')) { |
| 526 |
cp = ForwBack(cp, indexPtr); |
| 527 |
} else { |
| 528 |
cp = StartEnd(cp, indexPtr); |
| 529 |
} |
| 530 |
if (cp == NULL) { |
| 531 |
goto error; |
| 532 |
} |
| 533 |
} |
| 534 |
Tcl_DStringFree(©); |
| 535 |
return TCL_OK; |
| 536 |
|
| 537 |
error: |
| 538 |
Tcl_DStringFree(©); |
| 539 |
Tcl_ResetResult(interp); |
| 540 |
Tcl_AppendResult(interp, "bad text index \"", string, "\"", |
| 541 |
(char *) NULL); |
| 542 |
return TCL_ERROR; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
void |
| 563 |
TkTextPrintIndex(indexPtr, string) |
| 564 |
CONST TkTextIndex *indexPtr; |
| 565 |
char *string; |
| 566 |
|
| 567 |
{ |
| 568 |
TkTextSegment *segPtr; |
| 569 |
int numBytes, charIndex; |
| 570 |
|
| 571 |
numBytes = indexPtr->byteIndex; |
| 572 |
charIndex = 0; |
| 573 |
for (segPtr = indexPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr) { |
| 574 |
if (numBytes <= segPtr->size) { |
| 575 |
break; |
| 576 |
} |
| 577 |
if (segPtr->typePtr == &tkTextCharType) { |
| 578 |
charIndex += Tcl_NumUtfChars(segPtr->body.chars, segPtr->size); |
| 579 |
} else { |
| 580 |
charIndex += segPtr->size; |
| 581 |
} |
| 582 |
numBytes -= segPtr->size; |
| 583 |
} |
| 584 |
if (segPtr->typePtr == &tkTextCharType) { |
| 585 |
charIndex += Tcl_NumUtfChars(segPtr->body.chars, numBytes); |
| 586 |
} else { |
| 587 |
charIndex += numBytes; |
| 588 |
} |
| 589 |
sprintf(string, "%d.%d", TkBTreeLineIndex(indexPtr->linePtr) + 1, |
| 590 |
charIndex); |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
int |
| 612 |
TkTextIndexCmp(index1Ptr, index2Ptr) |
| 613 |
CONST TkTextIndex *index1Ptr; |
| 614 |
CONST TkTextIndex *index2Ptr; |
| 615 |
{ |
| 616 |
int line1, line2; |
| 617 |
|
| 618 |
if (index1Ptr->linePtr == index2Ptr->linePtr) { |
| 619 |
if (index1Ptr->byteIndex < index2Ptr->byteIndex) { |
| 620 |
return -1; |
| 621 |
} else if (index1Ptr->byteIndex > index2Ptr->byteIndex) { |
| 622 |
return 1; |
| 623 |
} else { |
| 624 |
return 0; |
| 625 |
} |
| 626 |
} |
| 627 |
line1 = TkBTreeLineIndex(index1Ptr->linePtr); |
| 628 |
line2 = TkBTreeLineIndex(index2Ptr->linePtr); |
| 629 |
if (line1 < line2) { |
| 630 |
return -1; |
| 631 |
} |
| 632 |
if (line1 > line2) { |
| 633 |
return 1; |
| 634 |
} |
| 635 |
return 0; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
static CONST char * |
| 659 |
ForwBack(string, indexPtr) |
| 660 |
CONST char *string; |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
TkTextIndex *indexPtr; |
| 665 |
{ |
| 666 |
register CONST char *p, *units; |
| 667 |
char *end; |
| 668 |
int count, lineIndex; |
| 669 |
size_t length; |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
p = string+1; |
| 676 |
while (isspace(UCHAR(*p))) { |
| 677 |
p++; |
| 678 |
} |
| 679 |
count = strtol(p, &end, 0); |
| 680 |
if (end == p) { |
| 681 |
return NULL; |
| 682 |
} |
| 683 |
p = end; |
| 684 |
while (isspace(UCHAR(*p))) { |
| 685 |
p++; |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
units = p; |
| 695 |
while ((*p != '\0') && !isspace(UCHAR(*p)) && (*p != '+') && (*p != '-')) { |
| 696 |
p++; |
| 697 |
} |
| 698 |
length = p - units; |
| 699 |
if ((*units == 'c') && (strncmp(units, "chars", length) == 0)) { |
| 700 |
if (*string == '+') { |
| 701 |
TkTextIndexForwChars(indexPtr, count, indexPtr); |
| 702 |
} else { |
| 703 |
TkTextIndexBackChars(indexPtr, count, indexPtr); |
| 704 |
} |
| 705 |
} else if ((*units == 'l') && (strncmp(units, "lines", length) == 0)) { |
| 706 |
lineIndex = TkBTreeLineIndex(indexPtr->linePtr); |
| 707 |
if (*string == '+') { |
| 708 |
lineIndex += count; |
| 709 |
} else { |
| 710 |
lineIndex -= count; |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
if (lineIndex < 0) { |
| 720 |
lineIndex = 0; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
TkTextMakeByteIndex(indexPtr->tree, lineIndex, indexPtr->byteIndex, |
| 735 |
indexPtr); |
| 736 |
} else { |
| 737 |
return NULL; |
| 738 |
} |
| 739 |
return p; |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
void |
| 762 |
TkTextIndexForwBytes(srcPtr, byteCount, dstPtr) |
| 763 |
CONST TkTextIndex *srcPtr; |
| 764 |
int byteCount; |
| 765 |
|
| 766 |
TkTextIndex *dstPtr; |
| 767 |
{ |
| 768 |
TkTextLine *linePtr; |
| 769 |
TkTextSegment *segPtr; |
| 770 |
int lineLength; |
| 771 |
|
| 772 |
if (byteCount < 0) { |
| 773 |
TkTextIndexBackBytes(srcPtr, -byteCount, dstPtr); |
| 774 |
return; |
| 775 |
} |
| 776 |
|
| 777 |
*dstPtr = *srcPtr; |
| 778 |
dstPtr->byteIndex += byteCount; |
| 779 |
while (1) { |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
lineLength = 0; |
| 785 |
for (segPtr = dstPtr->linePtr->segPtr; segPtr != NULL; |
| 786 |
segPtr = segPtr->nextPtr) { |
| 787 |
lineLength += segPtr->size; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
if (dstPtr->byteIndex < lineLength) { |
| 796 |
return; |
| 797 |
} |
| 798 |
dstPtr->byteIndex -= lineLength; |
| 799 |
linePtr = TkBTreeNextLine(dstPtr->linePtr); |
| 800 |
if (linePtr == NULL) { |
| 801 |
dstPtr->byteIndex = lineLength - 1; |
| 802 |
return; |
| 803 |
} |
| 804 |
dstPtr->linePtr = linePtr; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
void |
| 828 |
TkTextIndexForwChars(srcPtr, charCount, dstPtr) |
| 829 |
CONST TkTextIndex *srcPtr; |
| 830 |
int charCount; |
| 831 |
|
| 832 |
TkTextIndex *dstPtr; |
| 833 |
{ |
| 834 |
TkTextLine *linePtr; |
| 835 |
TkTextSegment *segPtr; |
| 836 |
int byteOffset; |
| 837 |
char *start, *end, *p; |
| 838 |
Tcl_UniChar ch; |
| 839 |
|
| 840 |
if (charCount < 0) { |
| 841 |
TkTextIndexBackChars(srcPtr, -charCount, dstPtr); |
| 842 |
return; |
| 843 |
} |
| 844 |
|
| 845 |
*dstPtr = *srcPtr; |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
segPtr = TkTextIndexToSeg(dstPtr, &byteOffset); |
| 853 |
while (1) { |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
for ( ; segPtr != NULL; segPtr = segPtr->nextPtr) { |
| 860 |
if (segPtr->typePtr == &tkTextCharType) { |
| 861 |
start = segPtr->body.chars + byteOffset; |
| 862 |
end = segPtr->body.chars + segPtr->size; |
| 863 |
for (p = start; p < end; p += Tcl_UtfToUniChar(p, &ch)) { |
| 864 |
if (charCount == 0) { |
| 865 |
dstPtr->byteIndex += (p - start); |
| 866 |
return; |
| 867 |
} |
| 868 |
charCount--; |
| 869 |
} |
| 870 |
} else { |
| 871 |
if (charCount < segPtr->size - byteOffset) { |
| 872 |
dstPtr->byteIndex += charCount; |
| 873 |
return; |
| 874 |
} |
| 875 |
charCount -= segPtr->size - byteOffset; |
| 876 |
} |
| 877 |
dstPtr->byteIndex += segPtr->size - byteOffset; |
| 878 |
byteOffset = 0; |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
linePtr = TkBTreeNextLine(dstPtr->linePtr); |
| 888 |
if (linePtr == NULL) { |
| 889 |
dstPtr->byteIndex -= sizeof(char); |
| 890 |
return; |
| 891 |
} |
| 892 |
dstPtr->linePtr = linePtr; |
| 893 |
dstPtr->byteIndex = 0; |
| 894 |
segPtr = dstPtr->linePtr->segPtr; |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
void |
| 918 |
TkTextIndexBackBytes(srcPtr, byteCount, dstPtr) |
| 919 |
CONST TkTextIndex *srcPtr; |
| 920 |
int byteCount; |
| 921 |
|
| 922 |
TkTextIndex *dstPtr; |
| 923 |
{ |
| 924 |
TkTextSegment *segPtr; |
| 925 |
int lineIndex; |
| 926 |
|
| 927 |
if (byteCount < 0) { |
| 928 |
TkTextIndexForwBytes(srcPtr, -byteCount, dstPtr); |
| 929 |
return; |
| 930 |
} |
| 931 |
|
| 932 |
*dstPtr = *srcPtr; |
| 933 |
dstPtr->byteIndex -= byteCount; |
| 934 |
lineIndex = -1; |
| 935 |
while (dstPtr->byteIndex < 0) { |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
if (lineIndex < 0) { |
| 942 |
lineIndex = TkBTreeLineIndex(dstPtr->linePtr); |
| 943 |
} |
| 944 |
if (lineIndex == 0) { |
| 945 |
dstPtr->byteIndex = 0; |
| 946 |
return; |
| 947 |
} |
| 948 |
lineIndex--; |
| 949 |
dstPtr->linePtr = TkBTreeFindLine(dstPtr->tree, lineIndex); |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
for (segPtr = dstPtr->linePtr->segPtr; segPtr != NULL; |
| 956 |
segPtr = segPtr->nextPtr) { |
| 957 |
dstPtr->byteIndex += segPtr->size; |
| 958 |
} |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
void |
| 982 |
TkTextIndexBackChars(srcPtr, charCount, dstPtr) |
| 983 |
CONST TkTextIndex *srcPtr; |
| 984 |
int charCount; |
| 985 |
|
| 986 |
TkTextIndex *dstPtr; |
| 987 |
{ |
| 988 |
TkTextSegment *segPtr, *oldPtr; |
| 989 |
int lineIndex, segSize; |
| 990 |
CONST char *p, *start, *end; |
| 991 |
|
| 992 |
if (charCount <= 0) { |
| 993 |
TkTextIndexForwChars(srcPtr, -charCount, dstPtr); |
| 994 |
return; |
| 995 |
} |
| 996 |
|
| 997 |
*dstPtr = *srcPtr; |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
lineIndex = -1; |
| 1005 |
|
| 1006 |
segSize = dstPtr->byteIndex; |
| 1007 |
for (segPtr = dstPtr->linePtr->segPtr; ; segPtr = segPtr->nextPtr) { |
| 1008 |
if (segSize <= segPtr->size) { |
| 1009 |
break; |
| 1010 |
} |
| 1011 |
segSize -= segPtr->size; |
| 1012 |
} |
| 1013 |
while (1) { |
| 1014 |
if (segPtr->typePtr == &tkTextCharType) { |
| 1015 |
start = segPtr->body.chars; |
| 1016 |
end = segPtr->body.chars + segSize; |
| 1017 |
for (p = end; ; p = Tcl_UtfPrev(p, start)) { |
| 1018 |
if (charCount == 0) { |
| 1019 |
dstPtr->byteIndex -= (end - p); |
| 1020 |
return; |
| 1021 |
} |
| 1022 |
if (p == start) { |
| 1023 |
break; |
| 1024 |
} |
| 1025 |
charCount--; |
| 1026 |
} |
| 1027 |
} else { |
| 1028 |
if (charCount <= segSize) { |
| 1029 |
dstPtr->byteIndex -= charCount; |
| 1030 |
return; |
| 1031 |
} |
| 1032 |
charCount -= segSize; |
| 1033 |
} |
| 1034 |
dstPtr->byteIndex -= segSize; |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
oldPtr = segPtr; |
| 1041 |
segPtr = dstPtr->linePtr->segPtr; |
| 1042 |
if (segPtr != oldPtr) { |
| 1043 |
for ( ; segPtr->nextPtr != oldPtr; segPtr = segPtr->nextPtr) { |
| 1044 |
|
| 1045 |
} |
| 1046 |
segSize = segPtr->size; |
| 1047 |
continue; |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
if (lineIndex < 0) { |
| 1055 |
lineIndex = TkBTreeLineIndex(dstPtr->linePtr); |
| 1056 |
} |
| 1057 |
if (lineIndex == 0) { |
| 1058 |
dstPtr->byteIndex = 0; |
| 1059 |
return; |
| 1060 |
} |
| 1061 |
lineIndex--; |
| 1062 |
dstPtr->linePtr = TkBTreeFindLine(dstPtr->tree, lineIndex); |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
oldPtr = dstPtr->linePtr->segPtr; |
| 1069 |
for (segPtr = oldPtr; segPtr != NULL; segPtr = segPtr->nextPtr) { |
| 1070 |
dstPtr->byteIndex += segPtr->size; |
| 1071 |
oldPtr = segPtr; |
| 1072 |
} |
| 1073 |
segPtr = oldPtr; |
| 1074 |
segSize = segPtr->size; |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
static CONST char * |
| 1099 |
StartEnd(string, indexPtr) |
| 1100 |
CONST char *string; |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
TkTextIndex *indexPtr; |
| 1105 |
{ |
| 1106 |
CONST char *p; |
| 1107 |
int c, offset; |
| 1108 |
size_t length; |
| 1109 |
register TkTextSegment *segPtr; |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
for (p = string; isalnum(UCHAR(*p)); p++) { |
| 1116 |
|
| 1117 |
} |
| 1118 |
length = p-string; |
| 1119 |
if ((*string == 'l') && (strncmp(string, "lineend", length) == 0) |
| 1120 |
&& (length >= 5)) { |
| 1121 |
indexPtr->byteIndex = 0; |
| 1122 |
for (segPtr = indexPtr->linePtr->segPtr; segPtr != NULL; |
| 1123 |
segPtr = segPtr->nextPtr) { |
| 1124 |
indexPtr->byteIndex += segPtr->size; |
| 1125 |
} |
| 1126 |
indexPtr->byteIndex -= sizeof(char); |
| 1127 |
} else if ((*string == 'l') && (strncmp(string, "linestart", length) == 0) |
| 1128 |
&& (length >= 5)) { |
| 1129 |
indexPtr->byteIndex = 0; |
| 1130 |
} else if ((*string == 'w') && (strncmp(string, "wordend", length) == 0) |
| 1131 |
&& (length >= 5)) { |
| 1132 |
int firstChar = 1; |
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
segPtr = TkTextIndexToSeg(indexPtr, &offset); |
| 1141 |
while (1) { |
| 1142 |
int chSize = 1; |
| 1143 |
|
| 1144 |
if (segPtr->typePtr == &tkTextCharType) { |
| 1145 |
Tcl_UniChar ch; |
| 1146 |
|
| 1147 |
chSize = Tcl_UtfToUniChar(segPtr->body.chars + offset, &ch); |
| 1148 |
if (!Tcl_UniCharIsWordChar(ch)) { |
| 1149 |
break; |
| 1150 |
} |
| 1151 |
firstChar = 0; |
| 1152 |
} |
| 1153 |
offset += chSize; |
| 1154 |
indexPtr->byteIndex += chSize; |
| 1155 |
if (offset >= segPtr->size) { |
| 1156 |
segPtr = TkTextIndexToSeg(indexPtr, &offset); |
| 1157 |
} |
| 1158 |
} |
| 1159 |
if (firstChar) { |
| 1160 |
TkTextIndexForwChars(indexPtr, 1, indexPtr); |
| 1161 |
} |
| 1162 |
} else if ((*string == 'w') && (strncmp(string, "wordstart", length) == 0) |
| 1163 |
&& (length >= 5)) { |
| 1164 |
int firstChar = 1; |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
segPtr = TkTextIndexToSeg(indexPtr, &offset); |
| 1174 |
while (1) { |
| 1175 |
int chSize = 1; |
| 1176 |
|
| 1177 |
if (segPtr->typePtr == &tkTextCharType) { |
| 1178 |
Tcl_UniChar ch; |
| 1179 |
|
| 1180 |
Tcl_UtfToUniChar(segPtr->body.chars + offset, &ch); |
| 1181 |
if (!Tcl_UniCharIsWordChar(ch)) { |
| 1182 |
break; |
| 1183 |
} |
| 1184 |
if (offset > 0) { |
| 1185 |
chSize = (segPtr->body.chars + offset |
| 1186 |
- Tcl_UtfPrev(segPtr->body.chars + offset, |
| 1187 |
segPtr->body.chars)); |
| 1188 |
} |
| 1189 |
firstChar = 0; |
| 1190 |
} else { |
| 1191 |
} |
| 1192 |
offset -= chSize; |
| 1193 |
indexPtr->byteIndex -= chSize; |
| 1194 |
if (offset < 0) { |
| 1195 |
if (indexPtr->byteIndex < 0) { |
| 1196 |
indexPtr->byteIndex = 0; |
| 1197 |
goto done; |
| 1198 |
} |
| 1199 |
segPtr = TkTextIndexToSeg(indexPtr, &offset); |
| 1200 |
} |
| 1201 |
} |
| 1202 |
if (!firstChar) { |
| 1203 |
TkTextIndexForwChars(indexPtr, 1, indexPtr); |
| 1204 |
} |
| 1205 |
} else { |
| 1206 |
return NULL; |
| 1207 |
} |
| 1208 |
done: |
| 1209 |
return p; |
| 1210 |
} |