Changes in editwin.c [af1920fd:5435ab2]
Legend:
- Unmodified
- Added
- Removed
-
editwin.c
raf1920fd r5435ab2 143 143 e->buffy=0; 144 144 owl_editwin_overwrite_string(e, text); 145 owl_editwin_overwrite_char(e, '\0'); 145 146 e->lock=strlen(text); 146 147 /* if (text[e->lock-1]=='\n') e->lock--; */ 147 e->buffx=x;148 e->buffy=y;149 owl_editwin_adjust_for_locktext(e);148 /* e->buffx=x; */ 149 /* e->buffy=y; */ 150 _owl_editwin_set_xy_by_index(e, e->lock); 150 151 owl_editwin_redisplay(e, 0); 151 152 } … … 255 256 256 257 /* start at topline */ 257 ptr1 =e->buff;258 for (i =0; i<e->topline; i++) {259 ptr2 =strchr(ptr1, '\n');258 ptr1 = e->buff; 259 for (i = 0; i < e->topline; i++) { 260 ptr2 = strchr(ptr1, '\n'); 260 261 if (!ptr2) { 261 262 /* we're already on the last line */ 262 263 break; 263 264 } 264 ptr1 =ptr2+1;265 ptr1 = ptr2 + 1; 265 266 } 266 267 /* ptr1 now stores the starting point */ 267 268 268 269 /* find the ending point and store it in ptr3 */ 269 ptr2 =ptr1;270 ptr3 =ptr1;271 for (i =0; i<e->winlines; i++) {272 ptr3 =strchr(ptr2, '\n');270 ptr2 = ptr1; 271 ptr3 = ptr1; 272 for (i = 0; i < e->winlines; i++) { 273 ptr3 = strchr(ptr2, '\n'); 273 274 if (!ptr3) { 274 275 /* we've hit the last line */ 275 276 /* print everything to the end */ 276 ptr3 =e->buff+e->bufflen-1;277 ptr3 = e->buff + e->bufflen - 1; 277 278 ptr3--; 278 279 break; 279 280 } 280 ptr2 =ptr3+1;281 } 282 ptr3 +=2;283 284 buff =owl_malloc(ptr3-ptr1+50);285 strncpy(buff, ptr1, ptr3 -ptr1);286 buff[ptr3 -ptr1]='\0';287 if (e->echochar =='\0') {281 ptr2 = ptr3 + 1; 282 } 283 ptr3 += 2; 284 285 buff = owl_malloc(ptr3 - ptr1 + 50); 286 strncpy(buff, ptr1, ptr3 - ptr1); 287 buff[ptr3 - ptr1] = '\0'; 288 if (e->echochar == '\0') { 288 289 waddstr(e->curswin, buff); 289 290 } else { 290 291 /* translate to echochar, *except* for the locktext */ 291 292 int len; 292 int dolocklen=e->lock-(ptr1-e->buff); 293 294 for (i=0; i<dolocklen; i++) { 295 waddch(e->curswin, buff[i]); 296 } 297 len=strlen(buff); 298 for (i=0; i<len-dolocklen; i++) { 293 int dolocklen = e->lock - (ptr1 - e->buff); 294 char *locktext; 295 char tmp = e->buff[dolocklen]; 296 297 e->buff[dolocklen] = '\0'; 298 locktext = owl_strdup(e->buff); 299 e->buff[dolocklen] = tmp; 300 301 waddstr(e->curswin, locktext); 302 303 len = strlen(buff); 304 for (i = 0; i < len-dolocklen; i++) { 299 305 waddch(e->curswin, e->echochar); 300 306 } 301 307 } 302 wmove(e->curswin, e->buffy-e->topline, e->buffx );308 wmove(e->curswin, e->buffy-e->topline, e->buffx + _owl_editwin_cursor_adjustment(e)); 303 309 wnoutrefresh(e->curswin); 304 if (update ==1) {310 if (update == 1) { 305 311 doupdate(); 306 312 } 307 313 owl_free(buff); 314 } 315 316 /* Remove n bytes at cursor. */ 317 void _owl_editwin_remove_bytes(owl_editwin *e, int n) /*noproto*/ 318 { 319 int i = _owl_editwin_get_index_from_xy(e) + n; 320 for (; i < e->bufflen; i++) { 321 e->buff[i-n] = e->buff[i]; 322 } 323 324 e->bufflen -= n; 325 e->buff[e->bufflen] = '\0'; 326 } 327 328 /* Insert n bytes at cursor.*/ 329 void _owl_editwin_insert_bytes(owl_editwin *e, int n) /*noproto*/ 330 { 331 int i, z; 332 333 if ((e->bufflen + n) > (e->allocated - 5)) { 334 _owl_editwin_addspace(e); 335 } 336 337 e->bufflen += n; 338 e->buff[e->bufflen] = '\0'; 339 340 z = _owl_editwin_get_index_from_xy(e); 341 for (i = e->bufflen - 1; i > z; i--) { 342 e->buff[i] = e->buff[i - n]; 343 } 308 344 } 309 345 … … 315 351 int _owl_editwin_linewrap_word(owl_editwin *e) 316 352 { 317 int i, z; 318 319 z=_owl_editwin_get_index_from_xy(e); 320 /* move back and line wrap the previous word */ 321 for (i=z-1; ; i--) { 322 /* move back until you find a space or hit the beginning of the line */ 323 if (e->buff[i]==' ') { 324 /* replace the space with a newline */ 325 e->buff[i]='\n'; 326 e->buffy++; 327 e->buffx=z-i-1; 328 /* were we on the last line */ 329 return(0); 330 } else if (e->buff[i]=='\n' || i<=e->lock) { 331 /* we hit the begginning of the line or the buffer, we cannot 332 * wrap. 333 */ 334 return(-1); 335 } 336 } 353 int x, y; 354 int i; 355 char *ptr1, *start; 356 gunichar c; 357 358 /* saving values */ 359 x = e->buffx; 360 y = e->buffy; 361 start = e->buff + e->lock; 362 363 ptr1 = e->buff + _owl_editwin_get_index_from_xy(e); 364 ptr1 = g_utf8_find_prev_char(start, ptr1); 365 366 while (ptr1) { 367 c = g_utf8_get_char(ptr1); 368 if (owl_util_can_break_after(c)) { 369 if (c != ' ') { 370 i = ptr1 - e->buff; 371 _owl_editwin_set_xy_by_index(e, i); 372 _owl_editwin_insert_bytes(e, 1); 373 /* _owl_editwin_insert_bytes may move e->buff. */ 374 ptr1 = e->buff + i; 375 } 376 *ptr1 = '\n'; 377 return 0; 378 } 379 ptr1 = g_utf8_find_prev_char(start, ptr1); 380 } 381 return -1; 337 382 } 338 383 … … 340 385 * characters over) 341 386 */ 342 void owl_editwin_insert_char(owl_editwin *e, char c) 343 { 344 345 int z, i, ret; 387 void owl_editwin_insert_char(owl_editwin *e, gunichar c) 388 { 389 int z, i, ret, len; 390 char tmp[6]; 391 memset(tmp, '\0', 6); 346 392 347 393 /* \r is \n */ 348 if (c =='\r') {349 c ='\n';350 } 351 352 if (c =='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {394 if (c == '\r') { 395 c = '\n'; 396 } 397 398 if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) { 353 399 /* perhaps later this will change some state that allows the string 354 400 to be read */ … … 356 402 } 357 403 404 g_unichar_to_utf8(c, tmp); 405 len = strlen(tmp); 406 358 407 /* make sure there is enough memory for the new text */ 359 if ((e->bufflen +1) > (e->allocated-5)) {408 if ((e->bufflen + len) > (e->allocated - 5)) { 360 409 _owl_editwin_addspace(e); 361 410 } 362 411 363 412 /* get the insertion point */ 364 z =_owl_editwin_get_index_from_xy(e);413 z = _owl_editwin_get_index_from_xy(e); 365 414 366 415 /* If we're going to insert at the last column do word wrapping, unless it's a \n */ 367 if ((e->buffx +1==e->wrapcol) && (c!='\n')) {368 ret =_owl_editwin_linewrap_word(e);369 if (ret ==-1) {416 if ((e->buffx + 1 == e->wrapcol) && (c != '\n')) { 417 ret = _owl_editwin_linewrap_word(e); 418 if (ret == -1) { 370 419 /* we couldn't wrap, insert a hard newline instead */ 371 420 owl_editwin_insert_char(e, '\n'); … … 373 422 } 374 423 375 z=_owl_editwin_get_index_from_xy(e);376 424 /* shift all the other characters right */ 377 for (i=e->bufflen; i>z; i--) { 378 e->buff[i]=e->buff[i-1]; 379 } 380 381 /* insert the new one */ 382 e->buff[z]=c; 425 if (z != e->bufflen) { 426 _owl_editwin_insert_bytes(e, len); 427 } 428 429 /* insert the new character */ 430 for(i = 0; i < len; i++) { 431 e->buff[z + i] = tmp[i]; 432 } 383 433 384 434 /* housekeeping */ 385 e->bufflen ++;386 e->buff[e->bufflen] ='\0';387 435 e->bufflen += len; 436 e->buff[e->bufflen] = '\0'; 437 388 438 /* advance the cursor */ 389 if (c=='\n') { 390 e->buffx=0; 391 e->buffy++; 392 } else { 393 e->buffx++; 394 } 439 z += len; 440 _owl_editwin_set_xy_by_index(e, z); 395 441 } 396 442 397 443 /* overwrite the character at the current point with 'c' */ 398 void owl_editwin_overwrite_char(owl_editwin *e, char c) 399 { 400 int z; 401 444 void owl_editwin_overwrite_char(owl_editwin *e, gunichar c) 445 { 446 int z, oldlen, newlen, i; 447 char tmp[6]; 448 memset(tmp, '\0', 6); 449 402 450 /* \r is \n */ 403 if (c =='\r') {404 c ='\n';405 } 406 407 if (c =='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) {451 if (c == '\r') { 452 c = '\n'; 453 } 454 455 if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) { 408 456 /* perhaps later this will change some state that allows the string 409 457 to be read */ … … 411 459 } 412 460 413 z=_owl_editwin_get_index_from_xy(e); 414 415 /* only if we are at the end of the buffer do we create new space */ 416 if (z==e->bufflen) { 417 if ((e->bufflen+1) > (e->allocated-5)) { 461 g_unichar_to_utf8(c, tmp); 462 newlen = strlen(tmp); 463 464 z = _owl_editwin_get_index_from_xy(e); 465 { 466 char *t = g_utf8_find_next_char(e->buff + z, NULL); 467 oldlen = (t ? (t - (e->buff + z)) : 0); 468 } 469 470 /* only if we are at the end of the buffer do we create new space here */ 471 if (z == e->bufflen) { 472 if ((e->bufflen+newlen) > (e->allocated-5)) { 418 473 _owl_editwin_addspace(e); 419 474 } 420 475 } 421 422 e->buff[z]=c; 423 424 /* housekeeping if we are at the end of the buffer */ 425 if (z==e->bufflen) { 426 e->bufflen++; 427 e->buff[e->bufflen]='\0'; 428 } 429 476 /* if not at the end of the buffer, adjust based in char size difference. */ 477 else if (oldlen > newlen) { 478 _owl_editwin_remove_bytes(e, oldlen-newlen); 479 } 480 else /* oldlen < newlen */ { 481 _owl_editwin_insert_bytes(e, newlen-oldlen); 482 } 483 /* Overwrite the old char*/ 484 for (i = 0; i < newlen; i++) { 485 e->buff[z+i] = tmp[i]; 486 } 487 488 /* housekeeping */ 489 if (z == e->bufflen) { 490 e->bufflen += newlen; 491 e->buff[e->bufflen] = '\0'; 492 } 493 430 494 /* advance the cursor */ 431 if (c=='\n') { 432 e->buffx=0; 433 e->buffy++; 434 } else { 435 e->buffx++; 436 } 437 495 z += newlen; 496 _owl_editwin_set_xy_by_index(e, z); 438 497 } 439 498 … … 443 502 void owl_editwin_delete_char(owl_editwin *e) 444 503 { 445 int z, i; 446 447 if (e->bufflen==0) return; 504 int z; 505 char *p1, *p2; 506 gunichar c; 507 508 if (e->bufflen == 0) return; 448 509 449 510 /* get the deletion point */ 450 z=_owl_editwin_get_index_from_xy(e); 451 452 if (z==e->bufflen) return; 453 454 for (i=z; i<e->bufflen; i++) { 455 e->buff[i]=e->buff[i+1]; 456 } 457 e->bufflen--; 458 e->buff[e->bufflen]='\0'; 511 z = _owl_editwin_get_index_from_xy(e); 512 513 if (z == e->bufflen) return; 514 515 p1 = e->buff + z; 516 p2 = g_utf8_next_char(p1); 517 c = g_utf8_get_char(p2); 518 while (g_unichar_ismark(c)) { 519 p2 = g_utf8_next_char(p2); 520 c = g_utf8_get_char(p2); 521 } 522 _owl_editwin_remove_bytes(e, p2-p1); 459 523 } 460 524 … … 467 531 { 468 532 int z; 469 char tmp;470 471 if (e->bufflen ==0) return;533 char *p1, *p2, *p3, *tmp; 534 535 if (e->bufflen == 0) return; 472 536 473 537 /* get the cursor point */ 474 z =_owl_editwin_get_index_from_xy(e);475 476 if (z ==e->bufflen) {538 z = _owl_editwin_get_index_from_xy(e); 539 540 if (z == e->bufflen) { 477 541 /* point is after last character */ 478 542 z--; 479 543 } 480 544 481 if (z -1 < e->lock) {545 if (z - 1 < e->lock) { 482 546 /* point is at beginning of buffer, do nothing */ 483 547 return; 484 548 } 485 549 486 tmp=e->buff[z]; 487 e->buff[z]=e->buff[z-1]; 488 e->buff[z-1]=tmp; 489 owl_editwin_key_right(e); 550 /* Transpose two utf-8 unicode glyphs. */ 551 p1 = e->buff + z; 552 553 p2 = g_utf8_find_next_char(p1, NULL); 554 while (p2 != NULL && g_unichar_ismark(g_utf8_get_char(p2))) { 555 p2 = g_utf8_find_next_char(p2, NULL); 556 } 557 if (p2 == NULL) return; 558 559 p3 = g_utf8_find_prev_char(e->buff, p1); 560 while (p3 != NULL && g_unichar_ismark(g_utf8_get_char(p3))) { 561 p3 = g_utf8_find_prev_char(p3, NULL); 562 } 563 if (p3 == NULL) return; 564 565 tmp = owl_malloc(p2 - p3 + 5); 566 *tmp = '\0'; 567 strncat(tmp, p1, p2 - p1); 568 strncat(tmp, p3, p1 - p3); 569 strncpy(p3, tmp, p2 - p3); 570 owl_free(tmp); 571 _owl_editwin_set_xy_by_index(e, p3 - e->buff); 490 572 } 491 573 … … 495 577 void owl_editwin_insert_string(owl_editwin *e, char *string) 496 578 { 497 int i, j; 498 499 j=strlen(string); 500 for (i=0; i<j; i++) { 501 owl_editwin_insert_char(e, string[i]); 579 char *p; 580 gunichar c; 581 if (!g_utf8_validate(string, -1, NULL)) { 582 owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string."); 583 return; 584 } 585 p = string; 586 c = g_utf8_get_char(p); 587 while (c) { 588 _owl_editwin_process_char(e, c); 589 p = g_utf8_next_char(p); 590 c = g_utf8_get_char(p); 502 591 } 503 592 } … … 509 598 void owl_editwin_overwrite_string(owl_editwin *e, char *string) 510 599 { 511 int i, j; 512 513 j=strlen(string); 514 for (i=0; i<j; i++) { 515 owl_editwin_overwrite_char(e, string[i]); 600 char *p; 601 gunichar c; 602 603 if (!g_utf8_validate(string, -1, NULL)) { 604 owl_function_debugmsg("owl_editwin_overwrite_string: received non-utf-8 string."); 605 return; 606 } 607 p = string; 608 c = g_utf8_get_char(p); 609 while (c) { 610 owl_editwin_overwrite_char(e, c); 611 p = g_utf8_next_char(p); 612 c = g_utf8_get_char(p); 516 613 } 517 614 } … … 524 621 int i; 525 622 char *ptr1, *ptr2; 526 527 if (e->bufflen==0) return(0); 623 gunichar c; 624 625 if (e->bufflen == 0) return(0); 528 626 529 627 /* first go to the yth line */ 530 ptr1 =e->buff;531 for (i =0; i<e->buffy; i++) {532 ptr2= strchr(ptr1, '\n');628 ptr1 = e->buff; 629 for (i = 0; i < e->buffy; i++) { 630 ptr2= strchr(ptr1, '\n'); 533 631 if (!ptr2) { 534 632 /* we're already on the last line */ 535 633 break; 536 634 } 537 ptr1=ptr2+1; 538 } 539 540 /* now go to the xth character */ 541 ptr2=strchr(ptr1, '\n'); 542 if (!ptr2) { 543 ptr2=e->buff+e->bufflen; 544 } 545 546 if ((ptr2-ptr1) < e->buffx) { 547 ptr1=ptr2-1; 548 } else { 549 ptr1+=e->buffx; 550 } 551 552 /* printf("DEBUG: index is %i\r\n", ptr1-e->buff); */ 553 return(ptr1-e->buff); 554 } 635 ptr1 = ptr2 + 1; 636 } 637 638 /* now go to the xth cell */ 639 ptr2 = ptr1; 640 i = 0; 641 while (ptr2 != NULL && i < e->buffx && (ptr2 - e->buff) < e->bufflen) { 642 c = g_utf8_get_char(ptr2); 643 i += (c == '\n' ? 1 : mk_wcwidth(c)); 644 ptr2 = g_utf8_next_char(ptr2); 645 } 646 while(ptr2 != NULL && g_unichar_ismark(g_utf8_get_char(ptr2))) { 647 ptr2 = g_utf8_next_char(ptr2); 648 } 649 if (ptr2 == NULL) return e->bufflen; 650 return(ptr2 - e->buff); 651 } 652 653 /* We assume x,y are not set to point to a mid-char */ 654 gunichar _owl_editwin_get_char_at_xy(owl_editwin *e) 655 { 656 return g_utf8_get_char(e->buff + _owl_editwin_get_index_from_xy(e)); 657 } 658 555 659 556 660 void _owl_editwin_set_xy_by_index(owl_editwin *e, int index) 557 661 { 558 int z, i; 559 560 z=_owl_editwin_get_index_from_xy(e); 561 if (index>z) { 562 for (i=0; i<index-z; i++) { 563 owl_editwin_key_right(e); 564 } 565 } else if (index<z) { 566 for (i=0; i<z-index; i++) { 567 owl_editwin_key_left(e); 568 } 569 } 662 char *ptr1, *ptr2, *target; 663 gunichar c; 664 665 e->buffx = 0; 666 e->buffy = 0; 667 668 ptr1 = e->buff; 669 target = ptr1 + index; 670 /* target sanitizing */ 671 if ((target[0] & 0x80) && (~target[0] & 0x40)) { 672 /* middle of a utf-8 character, back up to previous character. */ 673 target = g_utf8_find_prev_char(e->buff, target); 674 } 675 c = g_utf8_get_char(target); 676 while (g_unichar_ismark(c) && target > e->buff) { 677 /* Adjust the target off of combining characters and the like. */ 678 target = g_utf8_find_prev_char(e->buff, target); 679 c = g_utf8_get_char(target); 680 } 681 /* If we start with a mark, something is wrong.*/ 682 if (g_unichar_ismark(c)) return; 683 684 /* Now our target should be acceptable. */ 685 ptr2 = strchr(ptr1, '\n'); 686 while (ptr2 != NULL && ptr2 < target) { 687 e->buffy++; 688 ptr1 = ptr2 + 1; 689 ptr2 = strchr(ptr1, '\n'); 690 } 691 ptr2 = ptr1; 692 while (ptr2 != NULL && ptr2 < target) { 693 c = g_utf8_get_char(ptr2); 694 e->buffx += mk_wcwidth(c); 695 ptr2 = g_utf8_next_char(ptr2); 696 } 697 } 698 699 int _owl_editwin_cursor_adjustment(owl_editwin *e) 700 { 701 char *ptr1, *ptr2; 702 gunichar c; 703 int x, i; 704 705 /* Find line */ 706 ptr1 = e->buff; 707 ptr2 = strchr(ptr1, '\n'); 708 for (i = 0; ptr2 != NULL && i < e->buffy; i++) { 709 ptr1 = ptr2 + 1; 710 ptr2 = strchr(ptr1, '\n'); 711 } 712 ptr2 = ptr1; 713 714 /* Find char */ 715 x = 0; 716 while (ptr2 != NULL && x < e->buffx) { 717 if (*ptr2 == '\n') return 0; 718 c = g_utf8_get_char(ptr2); 719 x += mk_wcwidth(c); 720 ptr2 = g_utf8_next_char(ptr2); 721 } 722 723 /* calculate x offset */ 724 return x - e->buffx; 570 725 } 571 726 … … 574 729 /* if we happen to have the cursor over locked text 575 730 * move it to be out of the locktext region */ 576 if (_owl_editwin_get_index_from_xy(e) <e->lock) {731 if (_owl_editwin_get_index_from_xy(e) < e->lock) { 577 732 _owl_editwin_set_xy_by_index(e, e->lock); 578 733 } … … 594 749 { 595 750 if (e->buffy > 0) e->buffy--; 596 if (e->buffx >= owl_editwin_get_numc hars_on_line(e, e->buffy)) {597 e->buffx=owl_editwin_get_numc hars_on_line(e, e->buffy);751 if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) { 752 e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy); 598 753 } 599 754 … … 612 767 613 768 /* if we're past the last character move back */ 614 if (e->buffx >= owl_editwin_get_numc hars_on_line(e, e->buffy)) {615 e->buffx=owl_editwin_get_numc hars_on_line(e, e->buffy);769 if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) { 770 e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy); 616 771 } 617 772 … … 627 782 void owl_editwin_key_left(owl_editwin *e) 628 783 { 629 /* move left if we can, and maybe up a line */ 630 if (e->buffx>0) { 631 e->buffx--; 632 } else if (e->buffy>0) { 633 e->buffy--; 634 e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); 635 } 636 637 /* do we need to scroll up? */ 638 if (e->buffy-e->topline < 0) { 639 e->topline-=e->winlines/2; 784 int i; 785 char * p; 786 i = _owl_editwin_get_index_from_xy(e); 787 p = e->buff + i; 788 p = g_utf8_find_prev_char(e->buff, p); 789 while (p && g_unichar_ismark(g_utf8_get_char(p))) { 790 p = g_utf8_find_prev_char(e->buff, p); 791 } 792 if (p == NULL) p = e->buff; 793 _owl_editwin_set_xy_by_index(e, p - e->buff); 794 795 if (e->buffy - e->topline < 0) { 796 e->topline -= e->winlines / 2; 640 797 } 641 798 … … 647 804 { 648 805 int i; 649 650 /* move right if we can, and skip down a line if needed */ 651 i=owl_editwin_get_numchars_on_line(e, e->buffy); 652 if (e->buffx < i) { 653 e->buffx++; 654 /* } else if (e->buffy+1 < owl_editwin_get_numlines(e)) { */ 655 } else if (_owl_editwin_get_index_from_xy(e) < e->bufflen) { 656 if (e->style==OWL_EDITWIN_STYLE_MULTILINE) { 657 e->buffx=0; 658 e->buffy++; 659 } 806 char * p; 807 i = _owl_editwin_get_index_from_xy(e); 808 p = e->buff + i; 809 p = g_utf8_find_next_char(p, NULL); 810 while (p && g_unichar_ismark(g_utf8_get_char(p))) { 811 p = g_utf8_find_next_char(p, NULL); 812 } 813 if (p == NULL) { 814 _owl_editwin_set_xy_by_index(e, e->bufflen); 815 } 816 else { 817 _owl_editwin_set_xy_by_index(e, p - e->buff); 660 818 } 661 819 662 820 /* do we need to scroll down? */ 663 if (e->buffy -e->topline >= e->winlines) {664 e->topline +=e->winlines/2;821 if (e->buffy - e->topline >= e->winlines) { 822 e->topline += e->winlines / 2; 665 823 } 666 824 } … … 669 827 { 670 828 int i, x; 829 gunichar c = '\0'; 671 830 672 831 /* if we're starting on a space, find the first non-space */ … … 681 840 } 682 841 683 /* find the next space, newline or end of line and go there, if 684 already at the end of the line, continue on to the next */ 685 i=owl_editwin_get_numchars_on_line(e, e->buffy); 842 /* find the next space, newline or end of line and go 843 there, if already at the end of the line, continue on to the next */ 844 i=owl_editwin_get_numcells_on_line(e, e->buffy); 845 c = _owl_editwin_get_char_at_xy(e); 686 846 if (e->buffx < i) { 687 847 /* move right till end of line */ 688 848 while (e->buffx < i) { 689 e->buffx++; 690 if (e->buff[_owl_editwin_get_index_from_xy(e)]==' ') return; 849 owl_editwin_key_right(e); 850 c = _owl_editwin_get_char_at_xy(e); 851 if (c == ' ') return; 691 852 if (e->buffx == i) return; 692 853 } … … 694 855 /* try to move down */ 695 856 if (e->style==OWL_EDITWIN_STYLE_MULTILINE) { 696 if (e->buffy+1 < 857 if (e->buffy+1 < owl_editwin_get_numlines(e)) { 697 858 e->buffx=0; 698 859 e->buffy++; … … 707 868 void owl_editwin_move_to_previousword(owl_editwin *e) 708 869 { 709 int i, x; 870 int i; 871 gunichar c; 872 char *ptr1, *ptr2; 710 873 711 874 /* are we already at the beginning of the word? */ 712 i=_owl_editwin_get_index_from_xy(e); 713 if ( (e->buff[i]!=' ' && e->buff[i]!='\n' && e->buff[i]!='\0') && 714 (e->buff[i-1]==' ' || e->buff[i-1]=='\n') ) { 875 c = _owl_editwin_get_char_at_xy(e); 876 i = _owl_editwin_get_index_from_xy(e); 877 ptr1 = e->buff + i; 878 if (*ptr1 != ' ' && *ptr1 != '\n' && *ptr1 != '\0' ) { 879 ptr1 = g_utf8_find_prev_char(e->buff, ptr1); 880 c = g_utf8_get_char(ptr1); 881 if (c == ' ' || c == '\n') { 882 owl_editwin_key_left(e); 883 } 884 } 885 886 /* are we starting on a space character? */ 887 i = _owl_editwin_get_index_from_xy(e); 888 while (i > e->lock && (e->buff[i] == ' ' || e->buff[i] == '\n' || e->buff[i] == '\0')) { 889 /* find the first non-space */ 890 owl_editwin_key_left(e); 891 i = _owl_editwin_get_index_from_xy(e); 892 } 893 894 /* find the last non-space */ 895 ptr1 = e->buff + _owl_editwin_get_index_from_xy(e); 896 while (ptr1 >= e->buff + e->lock) { 897 ptr2 = g_utf8_find_prev_char(e->buff, ptr1); 898 if (!ptr2) break; 899 900 c = g_utf8_get_char(ptr2); 901 if (c == ' ' || c == '\n'){ 902 break; 903 } 715 904 owl_editwin_key_left(e); 716 } 717 718 /* are we starting on a space character? */ 719 i=_owl_editwin_get_index_from_xy(e); 720 if (e->buff[i]==' ' || e->buff[i]=='\n' || e->buff[i]=='\0') { 721 /* find the first non-space */ 722 for (x=i; x>=e->lock; x--) { 723 if (e->buff[x]!=' ' && e->buff[x]!='\n' && e->buff[x]!='\0') { 724 _owl_editwin_set_xy_by_index(e, x); 725 break; 726 } 727 } 728 } 729 730 /* find the last non-space */ 731 i=_owl_editwin_get_index_from_xy(e); 732 for (x=i; x>=e->lock; x--) { 733 if (e->buff[x-1]==' ' || e->buff[x-1]=='\n') { 734 _owl_editwin_set_xy_by_index(e, x); 735 break; 736 } 737 } 738 _owl_editwin_set_xy_by_index(e, x); 905 ptr1 = e->buff + _owl_editwin_get_index_from_xy(e); 906 } 739 907 } 740 908 … … 742 910 void owl_editwin_delete_nextword(owl_editwin *e) 743 911 { 744 int z; 912 char *ptr1, *start; 913 gunichar c; 745 914 746 915 if (e->bufflen==0) return; 747 916 748 /* if we start out on a space character then gobble all the spaces 749 up first */ 750 while (1) { 751 z=_owl_editwin_get_index_from_xy(e); 752 if (e->buff[z]==' ' || e->buff[z]=='\n') { 753 owl_editwin_delete_char(e); 754 } else { 755 break; 756 } 757 } 758 759 /* then nuke the next word */ 760 while (1) { 761 z=_owl_editwin_get_index_from_xy(e); 762 /* z == e->bufflen check added to prevent a hang I (nelhage) have 763 seen repeatedly while using owl. I'm not sure precisely what 764 conditions lead to it. */ 765 if (z == e->bufflen 766 || e->buff[z+1]==' ' || e->buff[z+1]=='\n' || e->buff[z+1]=='\0') break; 767 owl_editwin_delete_char(e); 768 } 769 owl_editwin_delete_char(e); 917 start = ptr1 = e->buff + _owl_editwin_get_index_from_xy(e); 918 /* if we start out on a space character then jump past all the 919 spaces up first */ 920 while (*ptr1 == ' ' || *ptr1 == '\n') { 921 ++ptr1; 922 } 923 924 /* then jump past the next word */ 925 926 while (ptr1 && ptr1 - e->buff < e->bufflen) { 927 c = g_utf8_get_char(ptr1); 928 if (c == ' ' || c == '\n' || c == '\0') break; 929 ptr1 = g_utf8_find_next_char(ptr1, NULL); 930 } 931 932 if (ptr1) { /* We broke on a space, */ 933 ptr1 = g_utf8_find_next_char(ptr1, NULL); 934 if (ptr1) { /* and there's a character after it, */ 935 /* nuke everything back to our starting point. */ 936 _owl_editwin_remove_bytes(e, ptr1 - start); 937 return; 938 } 939 } 940 941 /* If we get here, we ran out of string, drop what's left. */ 942 *start = '\0'; 943 e->bufflen = start - e->buff; 770 944 } 771 945 … … 773 947 { 774 948 /* go backwards to the last non-space character, then delete chars */ 775 int i,startpos, endpos;949 int startpos, endpos; 776 950 777 951 startpos = _owl_editwin_get_index_from_xy(e); 778 952 owl_editwin_move_to_previousword(e); 779 953 endpos = _owl_editwin_get_index_from_xy(e); 780 for (i=0; i<startpos-endpos; i++) { 781 owl_editwin_delete_char(e); 782 } 954 _owl_editwin_remove_bytes(e, startpos-endpos); 783 955 } 784 956 … … 787 959 int i; 788 960 789 if (owl_editwin_get_numchars_on_line(e, e->buffy) >e->buffx) {961 if (owl_editwin_get_numchars_on_line(e, e->buffy) > e->buffx) { 790 962 /* normal line */ 791 963 i=_owl_editwin_get_index_from_xy(e); … … 808 980 void owl_editwin_move_to_line_end(owl_editwin *e) 809 981 { 810 e->buffx=owl_editwin_get_numc hars_on_line(e, e->buffy);982 e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy); 811 983 } 812 984 … … 821 993 /* go to last char */ 822 994 e->buffy=owl_editwin_get_numlines(e)-1; 823 e->buffx=owl_editwin_get_numc hars_on_line(e, e->buffy);995 e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy); 824 996 owl_editwin_key_right(e); 825 997 … … 861 1033 /* main loop */ 862 1034 while (1) { 863 i =_owl_editwin_get_index_from_xy(e);1035 i = _owl_editwin_get_index_from_xy(e); 864 1036 865 1037 /* bail if we hit the end of the buffer */ 866 if (i >=e->bufflen) break;1038 if (i >= e->bufflen || e->buff[i] == '\0') break; 867 1039 868 1040 /* bail if we hit the end of the paragraph */ 869 if (e->buff[i] =='\n' && e->buff[i+1]=='\n') break;1041 if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break; 870 1042 871 1043 /* if we've travelled too far, linewrap */ 872 1044 if ((e->buffx) >= e->fillcol) { 1045 int len = e->bufflen; 873 1046 _owl_editwin_linewrap_word(e); 1047 /* we may have added a character. */ 1048 if (i < save) save += e->bufflen - len; 874 1049 } 875 1050 876 1051 /* did we hit the end of a line too soon? */ 877 i=_owl_editwin_get_index_from_xy(e); 878 if (e->buff[i]=='\n' && e->buffx<e->fillcol-1) { 1052 /* asedeno: Here we replace a newline with a space. We may want to 1053 consider removing the space if the characters to either side 1054 are CJK ideograms.*/ 1055 i = _owl_editwin_get_index_from_xy(e); 1056 if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) { 879 1057 /* ********* we need to make sure we don't pull in a word that's too long ***********/ 880 1058 e->buff[i]=' '; 881 1059 } 882 1060 883 1061 /* fix spacing */ 884 i =_owl_editwin_get_index_from_xy(e);885 if (e->buff[i] ==' ' && e->buff[i+1]==' ') {886 if (e->buff[i-1] =='.' || e->buff[i-1]=='!' || e->buff[i-1]=='?') {1062 i = _owl_editwin_get_index_from_xy(e); 1063 if (e->buff[i] == ' ' && e->buff[i+1] == ' ') { 1064 if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] == '?') { 887 1065 owl_editwin_key_right(e); 888 1066 } else { 889 1067 owl_editwin_delete_char(e); 890 /* if we did this ahead of the save point, adjust it */ 891 if (i<save) save--; 1068 /* if we did this ahead of the save point, adjust it. Changing 1069 by one is fine here because we're only removing an ASCII 1070 space. */ 1071 if (i < save) save--; 892 1072 } 893 1073 } else { 894 1074 owl_editwin_key_right(e); 895 1075 } 896 897 1076 } 898 1077 … … 915 1094 int owl_editwin_check_dotsend(owl_editwin *e) 916 1095 { 917 int i; 1096 char *p, *p_n, *p_p; 1097 gunichar c; 918 1098 919 1099 if (!e->dotsend) return(0); 920 for (i=e->bufflen-1; i>0; i--) { 921 if (e->buff[i] == '.' 922 && (e->buff[i-1] == '\n' || e->buff[i-1] == '\r') 923 && (e->buff[i+1] == '\n' || e->buff[i+1] == '\r')) { 924 e->bufflen = i; 925 e->buff[i] = '\0'; 1100 1101 p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen); 1102 p_n = g_utf8_find_next_char(p, NULL); 1103 p_p = g_utf8_find_prev_char(e->buff, p); 1104 c = g_utf8_get_char(p); 1105 while (p != NULL) { 1106 if (*p == '.' 1107 && p_p != NULL && (*p_p == '\n' || *p_p == '\r') 1108 && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) { 1109 e->bufflen = p - e->buff; 1110 e->buff[e->bufflen] = '\0'; 926 1111 return(1); 927 1112 } 928 if (!isspace((int) e->buff[i])) { 929 return(0); 930 } 1113 if (c != '\0' && !g_unichar_isspace(c)) return(0); 1114 p_n = p; 1115 p = p_p; 1116 c = g_utf8_get_char(p); 1117 p_p = g_utf8_find_prev_char(e->buff, p); 931 1118 } 932 1119 return(0); 933 1120 } 934 1121 935 void owl_editwin_post_process_char(owl_editwin *e, int j)1122 void owl_editwin_post_process_char(owl_editwin *e, owl_input j) 936 1123 { 937 1124 /* check if we need to scroll down */ … … 939 1126 e->topline+=e->winlines/2; 940 1127 } 941 if ((j ==13 || j==10) && owl_editwin_check_dotsend(e)) {1128 if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) { 942 1129 owl_command_editmulti_done(e); 943 1130 return; … … 946 1133 } 947 1134 948 void owl_editwin_process_char(owl_editwin *e, int j) 949 { 950 if (j == ERR) return; 951 if (j>127 || ((j<32) && (j!=10) && (j!=13))) { 952 return; 953 } else { 1135 void _owl_editwin_process_char(owl_editwin *e, gunichar j) 1136 { 1137 if (!(g_unichar_iscntrl(j) && (j != 10) && (j != 13))) { 954 1138 owl_editwin_insert_char(e, j); 1139 } 1140 } 1141 1142 1143 void owl_editwin_process_char(owl_editwin *e, owl_input j) 1144 { 1145 if (j.ch == ERR) return; 1146 /* Ignore ncurses control characters. */ 1147 if (j.ch < 0x100) { 1148 _owl_editwin_process_char(e, j.uch); 955 1149 } 956 1150 } … … 979 1173 } 980 1174 981 /* now go to the xth character */ 982 ptr2=strchr(ptr1, '\n'); 983 if (!ptr2) { 984 return(e->buff + e->bufflen - ptr1); 985 } 986 return(ptr2-ptr1); /* don't count the newline for now */ 1175 /* now count characters */ 1176 i = 0; 1177 ptr2 = ptr1; 1178 while (ptr2 - e->buff < e->bufflen 1179 && *ptr2 != '\n') { 1180 ++i; 1181 ptr2 = g_utf8_next_char(ptr2); 1182 } 1183 return i; 1184 } 1185 1186 int owl_editwin_get_numcells_on_line(owl_editwin *e, int line) 1187 { 1188 int i; 1189 char *ptr1, *ptr2; 1190 gunichar c; 1191 1192 if (e->bufflen==0) return(0); 1193 1194 /* first go to the yth line */ 1195 ptr1=e->buff; 1196 for (i=0; i<line; i++) { 1197 ptr2=strchr(ptr1, '\n'); 1198 if (!ptr2) { 1199 /* we're already on the last line */ 1200 return(0); 1201 } 1202 ptr1=ptr2+1; 1203 } 1204 1205 /* now count cells */ 1206 i = 0; 1207 ptr2 = ptr1; 1208 while (ptr2 - e->buff < e->bufflen 1209 && *ptr2 != '\n') { 1210 c = g_utf8_get_char(ptr2); 1211 i += mk_wcwidth(c); 1212 ptr2 = g_utf8_next_char(ptr2); 1213 } 1214 return i; 987 1215 } 988 1216
Note: See TracChangeset
for help on using the changeset viewer.