Changes in editwin.c [b2c1bd4:af1920fd]
Legend:
- Unmodified
- Added
- Removed
-
editwin.c
rb2c1bd4 raf1920fd 36 36 e->echochar='\0'; 37 37 38 /* We get initialized multiple times, but we need to hold on to 39 the callbacks, so we can't NULL them here. */ 38 40 /* 39 // We get initialized multiple times, but we need to hold on to40 // the callbacks, so we can't NULL them here.41 41 e->command = NULL; 42 42 e->callback = NULL; … … 116 116 owl_function_error("Internal error: No editwin callback!"); 117 117 } else { 118 / / owl_function_error("text: |%s|", owl_editwin_get_text(e));118 /* owl_function_error("text: |%s|", owl_editwin_get_text(e)); */ 119 119 cb(e); 120 120 } … … 143 143 e->buffy=0; 144 144 owl_editwin_overwrite_string(e, text); 145 owl_editwin_overwrite_char(e, '\0');146 145 e->lock=strlen(text); 147 146 /* if (text[e->lock-1]=='\n') e->lock--; */ 148 /* e->buffx=x; */149 /* e->buffy=y; */150 _owl_editwin_set_xy_by_index(e, e->lock);147 e->buffx=x; 148 e->buffy=y; 149 owl_editwin_adjust_for_locktext(e); 151 150 owl_editwin_redisplay(e, 0); 152 151 } … … 256 255 257 256 /* start at topline */ 258 ptr1 =e->buff;259 for (i = 0; i <e->topline; i++) {260 ptr2 =strchr(ptr1, '\n');257 ptr1=e->buff; 258 for (i=0; i<e->topline; i++) { 259 ptr2=strchr(ptr1, '\n'); 261 260 if (!ptr2) { 262 261 /* we're already on the last line */ 263 262 break; 264 263 } 265 ptr1 = ptr2 +1;264 ptr1=ptr2+1; 266 265 } 267 266 /* ptr1 now stores the starting point */ 268 267 269 268 /* find the ending point and store it in ptr3 */ 270 ptr2 =ptr1;271 ptr3 =ptr1;272 for (i = 0; i <e->winlines; i++) {273 ptr3 =strchr(ptr2, '\n');269 ptr2=ptr1; 270 ptr3=ptr1; 271 for (i=0; i<e->winlines; i++) { 272 ptr3=strchr(ptr2, '\n'); 274 273 if (!ptr3) { 275 274 /* we've hit the last line */ 276 275 /* print everything to the end */ 277 ptr3 = e->buff + e->bufflen -1;276 ptr3=e->buff+e->bufflen-1; 278 277 ptr3--; 279 278 break; 280 279 } 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') {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') { 289 288 waddstr(e->curswin, buff); 290 289 } else { 291 290 /* translate to echochar, *except* for the locktext */ 292 291 int len; 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++) { 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++) { 305 299 waddch(e->curswin, e->echochar); 306 300 } 307 301 } 308 wmove(e->curswin, e->buffy-e->topline, e->buffx + _owl_editwin_cursor_adjustment(e));302 wmove(e->curswin, e->buffy-e->topline, e->buffx); 309 303 wnoutrefresh(e->curswin); 310 if (update ==1) {304 if (update==1) { 311 305 doupdate(); 312 306 } 313 307 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 }344 308 } 345 309 … … 351 315 int _owl_editwin_linewrap_word(owl_editwin *e) 352 316 { 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; 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 } 382 337 } 383 338 … … 385 340 * characters over) 386 341 */ 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); 342 void owl_editwin_insert_char(owl_editwin *e, char c) 343 { 344 345 int z, i, ret; 392 346 393 347 /* \r is \n */ 394 if (c =='\r') {395 c ='\n';396 } 397 398 if (c == '\n' && e->style ==OWL_EDITWIN_STYLE_ONELINE) {348 if (c=='\r') { 349 c='\n'; 350 } 351 352 if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) { 399 353 /* perhaps later this will change some state that allows the string 400 354 to be read */ … … 402 356 } 403 357 404 g_unichar_to_utf8(c, tmp);405 len = strlen(tmp);406 407 358 /* make sure there is enough memory for the new text */ 408 if ((e->bufflen + len) > (e->allocated -5)) {359 if ((e->bufflen+1) > (e->allocated-5)) { 409 360 _owl_editwin_addspace(e); 410 361 } 411 362 412 363 /* get the insertion point */ 413 z =_owl_editwin_get_index_from_xy(e);364 z=_owl_editwin_get_index_from_xy(e); 414 365 415 366 /* If we're going to insert at the last column do word wrapping, unless it's a \n */ 416 if ((e->buffx + 1 == e->wrapcol) && (c !='\n')) {417 ret =_owl_editwin_linewrap_word(e);418 if (ret ==-1) {367 if ((e->buffx+1==e->wrapcol) && (c!='\n')) { 368 ret=_owl_editwin_linewrap_word(e); 369 if (ret==-1) { 419 370 /* we couldn't wrap, insert a hard newline instead */ 420 371 owl_editwin_insert_char(e, '\n'); … … 422 373 } 423 374 375 z=_owl_editwin_get_index_from_xy(e); 424 376 /* shift all the other characters right */ 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 } 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; 433 383 434 384 /* housekeeping */ 435 e->bufflen += len; 436 e->buff[e->bufflen] = '\0'; 385 e->bufflen++; 386 e->buff[e->bufflen]='\0'; 387 388 /* advance the cursor */ 389 if (c=='\n') { 390 e->buffx=0; 391 e->buffy++; 392 } else { 393 e->buffx++; 394 } 395 } 396 397 /* overwrite the character at the current point with 'c' */ 398 void owl_editwin_overwrite_char(owl_editwin *e, char c) 399 { 400 int z; 437 401 438 /* advance the cursor */439 z += len;440 _owl_editwin_set_xy_by_index(e, z);441 }442 443 /* overwrite the character at the current point with 'c' */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 450 402 /* \r is \n */ 451 if (c =='\r') {452 c ='\n';453 } 454 455 if (c == '\n' && e->style ==OWL_EDITWIN_STYLE_ONELINE) {403 if (c=='\r') { 404 c='\n'; 405 } 406 407 if (c=='\n' && e->style==OWL_EDITWIN_STYLE_ONELINE) { 456 408 /* perhaps later this will change some state that allows the string 457 409 to be read */ … … 459 411 } 460 412 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)) { 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)) { 473 418 _owl_editwin_addspace(e); 474 419 } 475 420 } 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 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 494 430 /* advance the cursor */ 495 z += newlen; 496 _owl_editwin_set_xy_by_index(e, z); 431 if (c=='\n') { 432 e->buffx=0; 433 e->buffy++; 434 } else { 435 e->buffx++; 436 } 437 497 438 } 498 439 … … 502 443 void owl_editwin_delete_char(owl_editwin *e) 503 444 { 504 int z; 505 char *p1, *p2; 506 gunichar c; 507 508 if (e->bufflen == 0) return; 445 int z, i; 446 447 if (e->bufflen==0) return; 509 448 510 449 /* get the deletion point */ 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); 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'; 523 459 } 524 460 … … 531 467 { 532 468 int z; 533 char *p1, *p2, *p3, *tmp;534 535 if (e->bufflen ==0) return;469 char tmp; 470 471 if (e->bufflen==0) return; 536 472 537 473 /* get the cursor point */ 538 z =_owl_editwin_get_index_from_xy(e);539 540 if (z ==e->bufflen) {474 z=_owl_editwin_get_index_from_xy(e); 475 476 if (z==e->bufflen) { 541 477 /* point is after last character */ 542 478 z--; 543 479 } 544 480 545 if (z -1 < e->lock) {481 if (z-1 < e->lock) { 546 482 /* point is at beginning of buffer, do nothing */ 547 483 return; 548 484 } 549 485 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); 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); 572 490 } 573 491 … … 577 495 void owl_editwin_insert_string(owl_editwin *e, char *string) 578 496 { 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); 497 int i, j; 498 499 j=strlen(string); 500 for (i=0; i<j; i++) { 501 owl_editwin_insert_char(e, string[i]); 591 502 } 592 503 } … … 598 509 void owl_editwin_overwrite_string(owl_editwin *e, char *string) 599 510 { 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); 511 int i, j; 512 513 j=strlen(string); 514 for (i=0; i<j; i++) { 515 owl_editwin_overwrite_char(e, string[i]); 613 516 } 614 517 } … … 621 524 int i; 622 525 char *ptr1, *ptr2; 623 gunichar c; 624 625 if (e->bufflen == 0) return(0); 526 527 if (e->bufflen==0) return(0); 626 528 627 529 /* first go to the yth line */ 628 ptr1 =e->buff;629 for (i = 0; i <e->buffy; i++) {630 ptr2= 530 ptr1=e->buff; 531 for (i=0; i<e->buffy; i++) { 532 ptr2=strchr(ptr1, '\n'); 631 533 if (!ptr2) { 632 534 /* we're already on the last line */ 633 535 break; 634 536 } 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 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 } 659 555 660 556 void _owl_editwin_set_xy_by_index(owl_editwin *e, int index) 661 557 { 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; 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 } 725 570 } 726 571 … … 729 574 /* if we happen to have the cursor over locked text 730 575 * move it to be out of the locktext region */ 731 if (_owl_editwin_get_index_from_xy(e) <e->lock) {576 if (_owl_editwin_get_index_from_xy(e)<e->lock) { 732 577 _owl_editwin_set_xy_by_index(e, e->lock); 733 578 } … … 749 594 { 750 595 if (e->buffy > 0) e->buffy--; 751 if (e->buffx >= owl_editwin_get_numc ells_on_line(e, e->buffy)) {752 e->buffx=owl_editwin_get_numc ells_on_line(e, e->buffy);596 if (e->buffx >= owl_editwin_get_numchars_on_line(e, e->buffy)) { 597 e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); 753 598 } 754 599 … … 767 612 768 613 /* if we're past the last character move back */ 769 if (e->buffx >= owl_editwin_get_numc ells_on_line(e, e->buffy)) {770 e->buffx=owl_editwin_get_numc ells_on_line(e, e->buffy);614 if (e->buffx >= owl_editwin_get_numchars_on_line(e, e->buffy)) { 615 e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); 771 616 } 772 617 … … 782 627 void owl_editwin_key_left(owl_editwin *e) 783 628 { 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; 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; 797 640 } 798 641 … … 804 647 { 805 648 int i; 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); 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 } 818 660 } 819 661 820 662 /* do we need to scroll down? */ 821 if (e->buffy -e->topline >= e->winlines) {822 e->topline += e->winlines /2;663 if (e->buffy-e->topline >= e->winlines) { 664 e->topline+=e->winlines/2; 823 665 } 824 666 } … … 827 669 { 828 670 int i, x; 829 gunichar c = '\0';830 671 831 672 /* if we're starting on a space, find the first non-space */ … … 840 681 } 841 682 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); 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); 846 686 if (e->buffx < i) { 847 687 /* move right till end of line */ 848 688 while (e->buffx < i) { 849 owl_editwin_key_right(e); 850 c = _owl_editwin_get_char_at_xy(e); 851 if (c == ' ') return; 689 e->buffx++; 690 if (e->buff[_owl_editwin_get_index_from_xy(e)]==' ') return; 852 691 if (e->buffx == i) return; 853 692 } … … 855 694 /* try to move down */ 856 695 if (e->style==OWL_EDITWIN_STYLE_MULTILINE) { 857 if (e->buffy+1 < owl_editwin_get_numlines(e)) {696 if (e->buffy+1 < owl_editwin_get_numlines(e)) { 858 697 e->buffx=0; 859 698 e->buffy++; … … 868 707 void owl_editwin_move_to_previousword(owl_editwin *e) 869 708 { 870 int i; 871 gunichar c; 872 char *ptr1, *ptr2; 709 int i, x; 873 710 874 711 /* are we already at the beginning of the word? */ 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 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') ) { 715 owl_editwin_key_left(e); 716 } 717 886 718 /* are we starting on a space character? */ 887 i =_owl_editwin_get_index_from_xy(e);888 while (e->buff[i] == ' ' || e->buff[i] == '\n' || e->buff[i] =='\0') {719 i=_owl_editwin_get_index_from_xy(e); 720 if (e->buff[i]==' ' || e->buff[i]=='\n' || e->buff[i]=='\0') { 889 721 /* find the first non-space */ 890 owl_editwin_key_left(e); 891 i = _owl_editwin_get_index_from_xy(e); 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 } 892 728 } 893 729 894 730 /* find the last non-space */ 895 owl_editwin_key_left(e); 896 ptr1 = e->buff + _owl_editwin_get_index_from_xy(e); 897 while (ptr1 >= e->buff + e->lock) { 898 ptr2 = g_utf8_find_prev_char(e->buff, ptr1); 899 if (!ptr2) break; 900 901 c = g_utf8_get_char(ptr2); 902 if (c == ' ' || c == '\n'){ 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); 903 735 break; 904 736 } 905 owl_editwin_key_left(e); 906 ptr1 = e->buff + _owl_editwin_get_index_from_xy(e); 907 } 737 } 738 _owl_editwin_set_xy_by_index(e, x); 908 739 } 909 740 … … 911 742 void owl_editwin_delete_nextword(owl_editwin *e) 912 743 { 913 char *ptr1, *start; 914 gunichar c; 744 int z; 915 745 916 746 if (e->bufflen==0) return; 917 747 918 start = ptr1 = e->buff + _owl_editwin_get_index_from_xy(e); 919 /* if we start out on a space character then jump past all the 920 spaces up first */ 921 while (*ptr1 == ' ' || *ptr1 == '\n') { 922 ++ptr1; 923 } 924 925 /* then jump past the next word */ 926 927 while (ptr1 && ptr1 - e->buff < e->bufflen) { 928 c = g_utf8_get_char(ptr1); 929 if (c == ' ' || c == '\n' || c == '\0') break; 930 ptr1 = g_utf8_find_next_char(ptr1, NULL); 931 } 932 933 if (ptr1) { /* We broke on a space, */ 934 ptr1 = g_utf8_find_next_char(ptr1, NULL); 935 if (ptr1) { /* and there's a character after it, */ 936 /* nuke everything back to our starting point. */ 937 _owl_editwin_remove_bytes(e, ptr1 - start); 938 return; 939 } 940 } 941 942 /* If we get here, we ran out of string, drop what's left. */ 943 *start = '\0'; 944 e->bufflen = start - e->buff; 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); 945 770 } 946 771 … … 948 773 { 949 774 /* go backwards to the last non-space character, then delete chars */ 950 int startpos, endpos;775 int i, startpos, endpos; 951 776 952 777 startpos = _owl_editwin_get_index_from_xy(e); 953 778 owl_editwin_move_to_previousword(e); 954 779 endpos = _owl_editwin_get_index_from_xy(e); 955 _owl_editwin_remove_bytes(e, startpos-endpos); 780 for (i=0; i<startpos-endpos; i++) { 781 owl_editwin_delete_char(e); 782 } 956 783 } 957 784 … … 960 787 int i; 961 788 962 if (owl_editwin_get_numchars_on_line(e, e->buffy) >e->buffx) {789 if (owl_editwin_get_numchars_on_line(e, e->buffy)>e->buffx) { 963 790 /* normal line */ 964 791 i=_owl_editwin_get_index_from_xy(e); … … 981 808 void owl_editwin_move_to_line_end(owl_editwin *e) 982 809 { 983 e->buffx=owl_editwin_get_numc ells_on_line(e, e->buffy);810 e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); 984 811 } 985 812 … … 994 821 /* go to last char */ 995 822 e->buffy=owl_editwin_get_numlines(e)-1; 996 e->buffx=owl_editwin_get_numc ells_on_line(e, e->buffy);823 e->buffx=owl_editwin_get_numchars_on_line(e, e->buffy); 997 824 owl_editwin_key_right(e); 998 825 … … 1034 861 /* main loop */ 1035 862 while (1) { 1036 i =_owl_editwin_get_index_from_xy(e);863 i=_owl_editwin_get_index_from_xy(e); 1037 864 1038 865 /* bail if we hit the end of the buffer */ 1039 if (i >= e->bufflen || e->buff[i] == '\0') break;866 if (i>=e->bufflen) break; 1040 867 1041 868 /* bail if we hit the end of the paragraph */ 1042 if (e->buff[i] == '\n' && e->buff[i+1] =='\n') break;869 if (e->buff[i]=='\n' && e->buff[i+1]=='\n') break; 1043 870 1044 871 /* if we've travelled too far, linewrap */ 1045 872 if ((e->buffx) >= e->fillcol) { 1046 int len = e->bufflen;1047 873 _owl_editwin_linewrap_word(e); 1048 /* we may have added a character. */1049 if (i < save) save += e->bufflen - len;1050 874 } 1051 875 1052 876 /* did we hit the end of a line too soon? */ 1053 /* asedeno: Here we replace a newline with a space. We may want to 1054 consider removing the space if the characters to either side 1055 are CJK ideograms.*/ 1056 i = _owl_editwin_get_index_from_xy(e); 1057 if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) { 877 i=_owl_editwin_get_index_from_xy(e); 878 if (e->buff[i]=='\n' && e->buffx<e->fillcol-1) { 1058 879 /* ********* we need to make sure we don't pull in a word that's too long ***********/ 1059 880 e->buff[i]=' '; 1060 881 } 1061 882 1062 883 /* fix spacing */ 1063 i =_owl_editwin_get_index_from_xy(e);1064 if (e->buff[i] == ' ' && e->buff[i+1] ==' ') {1065 if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] =='?') {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]=='?') { 1066 887 owl_editwin_key_right(e); 1067 888 } else { 1068 889 owl_editwin_delete_char(e); 1069 /* if we did this ahead of the save point, adjust it. Changing 1070 by one is fine here because we're only removing an ASCII 1071 space. */ 1072 if (i < save) save--; 890 /* if we did this ahead of the save point, adjust it */ 891 if (i<save) save--; 1073 892 } 1074 893 } else { 1075 894 owl_editwin_key_right(e); 1076 895 } 896 1077 897 } 1078 898 … … 1095 915 int owl_editwin_check_dotsend(owl_editwin *e) 1096 916 { 1097 char *p, *p_n, *p_p; 1098 gunichar c; 917 int i; 1099 918 1100 919 if (!e->dotsend) return(0); 1101 1102 p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen); 1103 p_n = g_utf8_find_next_char(p, NULL); 1104 p_p = g_utf8_find_prev_char(e->buff, p); 1105 c = g_utf8_get_char(p); 1106 while (p != NULL) { 1107 if (*p == '.' 1108 && p_p != NULL && (*p_p == '\n' || *p_p == '\r') 1109 && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) { 1110 e->bufflen = p - e->buff; 1111 e->buff[e->bufflen] = '\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'; 1112 926 return(1); 1113 927 } 1114 if (c != '\0' && !g_unichar_isspace(c)) return(0); 1115 p_n = p; 1116 p = p_p; 1117 c = g_utf8_get_char(p); 1118 p_p = g_utf8_find_prev_char(e->buff, p); 928 if (!isspace((int) e->buff[i])) { 929 return(0); 930 } 1119 931 } 1120 932 return(0); 1121 933 } 1122 934 1123 void owl_editwin_post_process_char(owl_editwin *e, owl_input j)935 void owl_editwin_post_process_char(owl_editwin *e, int j) 1124 936 { 1125 937 /* check if we need to scroll down */ … … 1127 939 e->topline+=e->winlines/2; 1128 940 } 1129 if ((j .ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {941 if ((j==13 || j==10) && owl_editwin_check_dotsend(e)) { 1130 942 owl_command_editmulti_done(e); 1131 943 return; … … 1134 946 } 1135 947 1136 void _owl_editwin_process_char(owl_editwin *e, gunichar j) 1137 { 1138 if (!(g_unichar_iscntrl(j) && (j != 10) && (j != 13))) { 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 { 1139 954 owl_editwin_insert_char(e, j); 1140 }1141 }1142 1143 1144 void owl_editwin_process_char(owl_editwin *e, owl_input j)1145 {1146 if (j.ch == ERR) return;1147 /* Ignore ncurses control characters. */1148 if (j.ch < 0x100) {1149 _owl_editwin_process_char(e, j.uch);1150 955 } 1151 956 } … … 1174 979 } 1175 980 1176 /* now count characters */ 1177 i = 0; 1178 ptr2 = ptr1; 1179 while (ptr2 - e->buff < e->bufflen 1180 && *ptr2 != '\n') { 1181 ++i; 1182 ptr2 = g_utf8_next_char(ptr2); 1183 } 1184 return i; 1185 } 1186 1187 int owl_editwin_get_numcells_on_line(owl_editwin *e, int line) 1188 { 1189 int i; 1190 char *ptr1, *ptr2; 1191 gunichar c; 1192 1193 if (e->bufflen==0) return(0); 1194 1195 /* first go to the yth line */ 1196 ptr1=e->buff; 1197 for (i=0; i<line; i++) { 1198 ptr2=strchr(ptr1, '\n'); 1199 if (!ptr2) { 1200 /* we're already on the last line */ 1201 return(0); 1202 } 1203 ptr1=ptr2+1; 1204 } 1205 1206 /* now count cells */ 1207 i = 0; 1208 ptr2 = ptr1; 1209 while (ptr2 - e->buff < e->bufflen 1210 && *ptr2 != '\n') { 1211 c = g_utf8_get_char(ptr2); 1212 i += mk_wcwidth(c); 1213 ptr2 = g_utf8_next_char(ptr2); 1214 } 1215 return i; 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 */ 1216 987 } 1217 988
Note: See TracChangeset
for help on using the changeset viewer.