diff options
| author | Richard M. Stallman | 1998-01-01 02:52:20 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1998-01-01 02:52:20 +0000 |
| commit | fa8ed3e084fa8cda88229219e49622e0f897c2ac (patch) | |
| tree | 928368fbd4f5c1f8edf365857455184f4554323f /src | |
| parent | 4bbd5bc3149ecf8e5d50d88a9730d07fdb32d776 (diff) | |
| download | emacs-fa8ed3e084fa8cda88229219e49622e0f897c2ac.tar.gz emacs-fa8ed3e084fa8cda88229219e49622e0f897c2ac.zip | |
(looking_at_1): Use bytepos to call re_search_2.
Convert search regs to charpos, if search succeeded.
(search_buffer): Likewise. Also convert charpos <-> bytepos
in non-regexp case.
(scan_buffer): Convert charpos <-> bytepos.
(scan_newline): New function.
(find_next_newline): Function deleted.
(set_search_regs): Args now in bytes; convert to charpos.
(Freplace_match): Convert charpos <-> bytepos when scanning case.
Diffstat (limited to 'src')
| -rw-r--r-- | src/search.c | 338 |
1 files changed, 236 insertions, 102 deletions
diff --git a/src/search.c b/src/search.c index df73f4a5369..82a64e900a9 100644 --- a/src/search.c +++ b/src/search.c | |||
| @@ -236,36 +236,39 @@ looking_at_1 (string, posix) | |||
| 236 | that make up the visible portion of the buffer. */ | 236 | that make up the visible portion of the buffer. */ |
| 237 | 237 | ||
| 238 | p1 = BEGV_ADDR; | 238 | p1 = BEGV_ADDR; |
| 239 | s1 = GPT - BEGV; | 239 | s1 = GPT_BYTE - BEGV_BYTE; |
| 240 | p2 = GAP_END_ADDR; | 240 | p2 = GAP_END_ADDR; |
| 241 | s2 = ZV - GPT; | 241 | s2 = ZV_BYTE - GPT_BYTE; |
| 242 | if (s1 < 0) | 242 | if (s1 < 0) |
| 243 | { | 243 | { |
| 244 | p2 = p1; | 244 | p2 = p1; |
| 245 | s2 = ZV - BEGV; | 245 | s2 = ZV_BYTE - BEGV_BYTE; |
| 246 | s1 = 0; | 246 | s1 = 0; |
| 247 | } | 247 | } |
| 248 | if (s2 < 0) | 248 | if (s2 < 0) |
| 249 | { | 249 | { |
| 250 | s1 = ZV - BEGV; | 250 | s1 = ZV_BYTE - BEGV_BYTE; |
| 251 | s2 = 0; | 251 | s2 = 0; |
| 252 | } | 252 | } |
| 253 | 253 | ||
| 254 | re_match_object = Qnil; | 254 | re_match_object = Qnil; |
| 255 | 255 | ||
| 256 | i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2, | 256 | i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2, |
| 257 | PT - BEGV, &search_regs, | 257 | PT_BYTE - BEGV_BYTE, &search_regs, |
| 258 | ZV - BEGV); | 258 | ZV_BYTE - BEGV_BYTE); |
| 259 | if (i == -2) | 259 | if (i == -2) |
| 260 | matcher_overflow (); | 260 | matcher_overflow (); |
| 261 | 261 | ||
| 262 | val = (0 <= i ? Qt : Qnil); | 262 | val = (0 <= i ? Qt : Qnil); |
| 263 | for (i = 0; i < search_regs.num_regs; i++) | 263 | if (i >= 0) |
| 264 | if (search_regs.start[i] >= 0) | 264 | for (i = 0; i < search_regs.num_regs; i++) |
| 265 | { | 265 | if (search_regs.start[i] >= 0) |
| 266 | search_regs.start[i] += BEGV; | 266 | { |
| 267 | search_regs.end[i] += BEGV; | 267 | search_regs.start[i] |
| 268 | } | 268 | = BYTE_TO_CHAR (search_regs.start[i] + BEGV_BYTE); |
| 269 | search_regs.end[i] | ||
| 270 | = BYTE_TO_CHAR (search_regs.end[i] + BEGV_BYTE); | ||
| 271 | } | ||
| 269 | XSETBUFFER (last_thing_searched, current_buffer); | 272 | XSETBUFFER (last_thing_searched, current_buffer); |
| 270 | immediate_quit = 0; | 273 | immediate_quit = 0; |
| 271 | return val; | 274 | return val; |
| @@ -514,7 +517,8 @@ scan_buffer (target, start, end, count, shortage, allow_quit) | |||
| 514 | the position of the last character before the next such | 517 | the position of the last character before the next such |
| 515 | obstacle --- the last character the dumb search loop should | 518 | obstacle --- the last character the dumb search loop should |
| 516 | examine. */ | 519 | examine. */ |
| 517 | register int ceiling = end - 1; | 520 | int ceiling_byte = CHAR_TO_BYTE (end) - 1; |
| 521 | int start_byte = CHAR_TO_BYTE (start); | ||
| 518 | 522 | ||
| 519 | /* If we're looking for a newline, consult the newline cache | 523 | /* If we're looking for a newline, consult the newline cache |
| 520 | to see where we can avoid some scanning. */ | 524 | to see where we can avoid some scanning. */ |
| @@ -523,29 +527,31 @@ scan_buffer (target, start, end, count, shortage, allow_quit) | |||
| 523 | int next_change; | 527 | int next_change; |
| 524 | immediate_quit = 0; | 528 | immediate_quit = 0; |
| 525 | while (region_cache_forward | 529 | while (region_cache_forward |
| 526 | (current_buffer, newline_cache, start, &next_change)) | 530 | (current_buffer, newline_cache, start_byte, &next_change)) |
| 527 | start = next_change; | 531 | start_byte = next_change; |
| 528 | immediate_quit = allow_quit; | 532 | immediate_quit = allow_quit; |
| 529 | 533 | ||
| 530 | /* start should never be after end. */ | 534 | /* START should never be after END. */ |
| 531 | if (start >= end) | 535 | if (start_byte > ceiling_byte) |
| 532 | start = end - 1; | 536 | start_byte = ceiling_byte; |
| 533 | 537 | ||
| 534 | /* Now the text after start is an unknown region, and | 538 | /* Now the text after start is an unknown region, and |
| 535 | next_change is the position of the next known region. */ | 539 | next_change is the position of the next known region. */ |
| 536 | ceiling = min (next_change - 1, ceiling); | 540 | ceiling_byte = min (next_change - 1, ceiling_byte); |
| 537 | } | 541 | } |
| 538 | 542 | ||
| 539 | /* The dumb loop can only scan text stored in contiguous | 543 | /* The dumb loop can only scan text stored in contiguous |
| 540 | bytes. BUFFER_CEILING_OF returns the last character | 544 | bytes. BUFFER_CEILING_OF returns the last character |
| 541 | position that is contiguous, so the ceiling is the | 545 | position that is contiguous, so the ceiling is the |
| 542 | position after that. */ | 546 | position after that. */ |
| 543 | ceiling = min (BUFFER_CEILING_OF (start), ceiling); | 547 | ceiling_byte = min (BUFFER_CEILING_OF (start_byte), ceiling_byte); |
| 544 | 548 | ||
| 545 | { | 549 | { |
| 546 | /* The termination address of the dumb loop. */ | 550 | /* The termination address of the dumb loop. */ |
| 547 | register unsigned char *ceiling_addr = POS_ADDR (ceiling) + 1; | 551 | register unsigned char *ceiling_addr |
| 548 | register unsigned char *cursor = POS_ADDR (start); | 552 | = BYTE_POS_ADDR (ceiling_byte) + 1; |
| 553 | register unsigned char *cursor | ||
| 554 | = BYTE_POS_ADDR (start_byte); | ||
| 549 | unsigned char *base = cursor; | 555 | unsigned char *base = cursor; |
| 550 | 556 | ||
| 551 | while (cursor < ceiling_addr) | 557 | while (cursor < ceiling_addr) |
| @@ -560,8 +566,8 @@ scan_buffer (target, start, end, count, shortage, allow_quit) | |||
| 560 | the region from start to cursor is free of them. */ | 566 | the region from start to cursor is free of them. */ |
| 561 | if (target == '\n' && newline_cache) | 567 | if (target == '\n' && newline_cache) |
| 562 | know_region_cache (current_buffer, newline_cache, | 568 | know_region_cache (current_buffer, newline_cache, |
| 563 | start + scan_start - base, | 569 | start_byte + scan_start - base, |
| 564 | start + cursor - base); | 570 | start_byte + cursor - base); |
| 565 | 571 | ||
| 566 | /* Did we find the target character? */ | 572 | /* Did we find the target character? */ |
| 567 | if (cursor < ceiling_addr) | 573 | if (cursor < ceiling_addr) |
| @@ -569,20 +575,21 @@ scan_buffer (target, start, end, count, shortage, allow_quit) | |||
| 569 | if (--count == 0) | 575 | if (--count == 0) |
| 570 | { | 576 | { |
| 571 | immediate_quit = 0; | 577 | immediate_quit = 0; |
| 572 | return (start + cursor - base + 1); | 578 | return BYTE_TO_CHAR (start_byte + cursor - base + 1); |
| 573 | } | 579 | } |
| 574 | cursor++; | 580 | cursor++; |
| 575 | } | 581 | } |
| 576 | } | 582 | } |
| 577 | 583 | ||
| 578 | start += cursor - base; | 584 | start = BYTE_TO_CHAR (start_byte + cursor - base); |
| 579 | } | 585 | } |
| 580 | } | 586 | } |
| 581 | else | 587 | else |
| 582 | while (start > end) | 588 | while (start > end) |
| 583 | { | 589 | { |
| 584 | /* The last character to check before the next obstacle. */ | 590 | /* The last character to check before the next obstacle. */ |
| 585 | register int ceiling = end; | 591 | int ceiling_byte = CHAR_TO_BYTE (end); |
| 592 | int start_byte = CHAR_TO_BYTE (start); | ||
| 586 | 593 | ||
| 587 | /* Consult the newline cache, if appropriate. */ | 594 | /* Consult the newline cache, if appropriate. */ |
| 588 | if (target == '\n' && newline_cache) | 595 | if (target == '\n' && newline_cache) |
| @@ -590,26 +597,26 @@ scan_buffer (target, start, end, count, shortage, allow_quit) | |||
| 590 | int next_change; | 597 | int next_change; |
| 591 | immediate_quit = 0; | 598 | immediate_quit = 0; |
| 592 | while (region_cache_backward | 599 | while (region_cache_backward |
| 593 | (current_buffer, newline_cache, start, &next_change)) | 600 | (current_buffer, newline_cache, start_byte, &next_change)) |
| 594 | start = next_change; | 601 | start_byte = next_change; |
| 595 | immediate_quit = allow_quit; | 602 | immediate_quit = allow_quit; |
| 596 | 603 | ||
| 597 | /* Start should never be at or before end. */ | 604 | /* Start should never be at or before end. */ |
| 598 | if (start <= end) | 605 | if (start_byte <= ceiling_byte) |
| 599 | start = end + 1; | 606 | start_byte = ceiling_byte + 1; |
| 600 | 607 | ||
| 601 | /* Now the text before start is an unknown region, and | 608 | /* Now the text before start is an unknown region, and |
| 602 | next_change is the position of the next known region. */ | 609 | next_change is the position of the next known region. */ |
| 603 | ceiling = max (next_change, ceiling); | 610 | ceiling_byte = max (next_change, ceiling_byte); |
| 604 | } | 611 | } |
| 605 | 612 | ||
| 606 | /* Stop scanning before the gap. */ | 613 | /* Stop scanning before the gap. */ |
| 607 | ceiling = max (BUFFER_FLOOR_OF (start - 1), ceiling); | 614 | ceiling_byte = max (BUFFER_FLOOR_OF (start_byte - 1), ceiling_byte); |
| 608 | 615 | ||
| 609 | { | 616 | { |
| 610 | /* The termination address of the dumb loop. */ | 617 | /* The termination address of the dumb loop. */ |
| 611 | register unsigned char *ceiling_addr = POS_ADDR (ceiling); | 618 | register unsigned char *ceiling_addr = BYTE_POS_ADDR (ceiling_byte); |
| 612 | register unsigned char *cursor = POS_ADDR (start - 1); | 619 | register unsigned char *cursor = BYTE_POS_ADDR (start_byte - 1); |
| 613 | unsigned char *base = cursor; | 620 | unsigned char *base = cursor; |
| 614 | 621 | ||
| 615 | while (cursor >= ceiling_addr) | 622 | while (cursor >= ceiling_addr) |
| @@ -623,8 +630,8 @@ scan_buffer (target, start, end, count, shortage, allow_quit) | |||
| 623 | the region from after the cursor to start is free of them. */ | 630 | the region from after the cursor to start is free of them. */ |
| 624 | if (target == '\n' && newline_cache) | 631 | if (target == '\n' && newline_cache) |
| 625 | know_region_cache (current_buffer, newline_cache, | 632 | know_region_cache (current_buffer, newline_cache, |
| 626 | start + cursor - base, | 633 | start_byte + cursor - base, |
| 627 | start + scan_start - base); | 634 | start_byte + scan_start - base); |
| 628 | 635 | ||
| 629 | /* Did we find the target character? */ | 636 | /* Did we find the target character? */ |
| 630 | if (cursor >= ceiling_addr) | 637 | if (cursor >= ceiling_addr) |
| @@ -632,13 +639,13 @@ scan_buffer (target, start, end, count, shortage, allow_quit) | |||
| 632 | if (++count >= 0) | 639 | if (++count >= 0) |
| 633 | { | 640 | { |
| 634 | immediate_quit = 0; | 641 | immediate_quit = 0; |
| 635 | return (start + cursor - base); | 642 | return BYTE_TO_CHAR (start_byte + cursor - base); |
| 636 | } | 643 | } |
| 637 | cursor--; | 644 | cursor--; |
| 638 | } | 645 | } |
| 639 | } | 646 | } |
| 640 | 647 | ||
| 641 | start += cursor - base; | 648 | start = BYTE_TO_CHAR (start_byte + cursor - base); |
| 642 | } | 649 | } |
| 643 | } | 650 | } |
| 644 | 651 | ||
| @@ -647,25 +654,135 @@ scan_buffer (target, start, end, count, shortage, allow_quit) | |||
| 647 | *shortage = count * direction; | 654 | *shortage = count * direction; |
| 648 | return start; | 655 | return start; |
| 649 | } | 656 | } |
| 657 | |||
| 658 | /* Search for COUNT instances of a line boundary, which means either a | ||
| 659 | newline or (if selective display enabled) a carriage return. | ||
| 660 | Start at START. If COUNT is negative, search backwards. | ||
| 661 | |||
| 662 | We report the resulting position by calling TEMP_SET_PT_BOTH. | ||
| 663 | |||
| 664 | If we find COUNT instances. we position after (always after, | ||
| 665 | even if scanning backwards) the COUNTth match, and return 0. | ||
| 666 | |||
| 667 | If we don't find COUNT instances before reaching the end of the | ||
| 668 | buffer (or the beginning, if scanning backwards), we return | ||
| 669 | the number of line boundaries left unfound, and position at | ||
| 670 | the limit we bumped up against. | ||
| 671 | |||
| 672 | If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do | ||
| 673 | except when inside redisplay. */ | ||
| 650 | 674 | ||
| 651 | int | 675 | int |
| 652 | find_next_newline_no_quit (from, cnt) | 676 | scan_newline (start, start_byte, limit, limit_byte, count, allow_quit) |
| 653 | register int from, cnt; | 677 | int start, start_byte; |
| 678 | int limit, limit_byte; | ||
| 679 | register int count; | ||
| 680 | int allow_quit; | ||
| 654 | { | 681 | { |
| 655 | return scan_buffer ('\n', from, 0, cnt, (int *) 0, 0); | 682 | int direction = ((count > 0) ? 1 : -1); |
| 683 | |||
| 684 | register unsigned char *cursor; | ||
| 685 | unsigned char *base; | ||
| 686 | |||
| 687 | register int ceiling; | ||
| 688 | register unsigned char *ceiling_addr; | ||
| 689 | |||
| 690 | /* If we are not in selective display mode, | ||
| 691 | check only for newlines. */ | ||
| 692 | int selective_display = (!NILP (current_buffer->selective_display) | ||
| 693 | && !INTEGERP (current_buffer->selective_display)); | ||
| 694 | |||
| 695 | /* The code that follows is like scan_buffer | ||
| 696 | but checks for either newline or carriage return. */ | ||
| 697 | |||
| 698 | immediate_quit = allow_quit; | ||
| 699 | |||
| 700 | start_byte = CHAR_TO_BYTE (start); | ||
| 701 | |||
| 702 | if (count > 0) | ||
| 703 | { | ||
| 704 | while (start_byte < limit_byte) | ||
| 705 | { | ||
| 706 | ceiling = BUFFER_CEILING_OF (start_byte); | ||
| 707 | ceiling = min (limit_byte - 1, ceiling); | ||
| 708 | ceiling_addr = BYTE_POS_ADDR (ceiling) + 1; | ||
| 709 | base = (cursor = BYTE_POS_ADDR (start_byte)); | ||
| 710 | while (1) | ||
| 711 | { | ||
| 712 | while (*cursor != '\n' && ++cursor != ceiling_addr) | ||
| 713 | ; | ||
| 714 | |||
| 715 | if (cursor != ceiling_addr) | ||
| 716 | { | ||
| 717 | if (--count == 0) | ||
| 718 | { | ||
| 719 | immediate_quit = 0; | ||
| 720 | start_byte = start_byte + cursor - base + 1; | ||
| 721 | start = BYTE_TO_CHAR (start_byte); | ||
| 722 | TEMP_SET_PT_BOTH (start, start_byte); | ||
| 723 | return 0; | ||
| 724 | } | ||
| 725 | else | ||
| 726 | if (++cursor == ceiling_addr) | ||
| 727 | break; | ||
| 728 | } | ||
| 729 | else | ||
| 730 | break; | ||
| 731 | } | ||
| 732 | start_byte += cursor - base; | ||
| 733 | } | ||
| 734 | } | ||
| 735 | else | ||
| 736 | { | ||
| 737 | int start_byte = CHAR_TO_BYTE (start); | ||
| 738 | while (start_byte > limit_byte) | ||
| 739 | { | ||
| 740 | ceiling = BUFFER_FLOOR_OF (start_byte - 1); | ||
| 741 | ceiling = max (limit_byte, ceiling); | ||
| 742 | ceiling_addr = BYTE_POS_ADDR (ceiling) - 1; | ||
| 743 | base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1); | ||
| 744 | while (1) | ||
| 745 | { | ||
| 746 | while (--cursor != ceiling_addr && *cursor != '\n') | ||
| 747 | ; | ||
| 748 | |||
| 749 | if (cursor != ceiling_addr) | ||
| 750 | { | ||
| 751 | if (++count == 0) | ||
| 752 | { | ||
| 753 | immediate_quit = 0; | ||
| 754 | /* Return the position AFTER the match we found. */ | ||
| 755 | start_byte = start_byte + cursor - base + 1; | ||
| 756 | start = BYTE_TO_CHAR (start_byte); | ||
| 757 | TEMP_SET_PT_BOTH (start, start_byte); | ||
| 758 | return 0; | ||
| 759 | } | ||
| 760 | } | ||
| 761 | else | ||
| 762 | break; | ||
| 763 | } | ||
| 764 | /* Here we add 1 to compensate for the last decrement | ||
| 765 | of CURSOR, which took it past the valid range. */ | ||
| 766 | start_byte += cursor - base + 1; | ||
| 767 | } | ||
| 768 | } | ||
| 769 | |||
| 770 | TEMP_SET_PT_BOTH (limit, limit_byte); | ||
| 771 | |||
| 772 | return count * direction; | ||
| 656 | } | 773 | } |
| 657 | 774 | ||
| 658 | int | 775 | int |
| 659 | find_next_newline (from, cnt) | 776 | find_next_newline_no_quit (from, cnt) |
| 660 | register int from, cnt; | 777 | register int from, cnt; |
| 661 | { | 778 | { |
| 662 | return scan_buffer ('\n', from, 0, cnt, (int *) 0, 1); | 779 | return scan_buffer ('\n', from, 0, cnt, (int *) 0, 0); |
| 663 | } | 780 | } |
| 664 | 781 | ||
| 665 | |||
| 666 | /* Like find_next_newline, but returns position before the newline, | 782 | /* Like find_next_newline, but returns position before the newline, |
| 667 | not after, and only search up to TO. This isn't just | 783 | not after, and only search up to TO. This isn't just |
| 668 | find_next_newline (...)-1, because you might hit TO. */ | 784 | find_next_newline (...)-1, because you might hit TO. */ |
| 785 | |||
| 669 | int | 786 | int |
| 670 | find_before_next_newline (from, to, cnt) | 787 | find_before_next_newline (from, to, cnt) |
| 671 | int from, to, cnt; | 788 | int from, to, cnt; |
| @@ -748,6 +865,8 @@ search_command (string, bound, noerror, count, direction, RE, posix) | |||
| 748 | return make_number (np); | 865 | return make_number (np); |
| 749 | } | 866 | } |
| 750 | 867 | ||
| 868 | /* Return 1 if REGEXP it matches just one constant string. */ | ||
| 869 | |||
| 751 | static int | 870 | static int |
| 752 | trivial_regexp_p (regexp) | 871 | trivial_regexp_p (regexp) |
| 753 | Lisp_Object regexp; | 872 | Lisp_Object regexp; |
| @@ -846,18 +965,18 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix) | |||
| 846 | that make up the visible portion of the buffer. */ | 965 | that make up the visible portion of the buffer. */ |
| 847 | 966 | ||
| 848 | p1 = BEGV_ADDR; | 967 | p1 = BEGV_ADDR; |
| 849 | s1 = GPT - BEGV; | 968 | s1 = GPT_BYTE - BEGV_BYTE; |
| 850 | p2 = GAP_END_ADDR; | 969 | p2 = GAP_END_ADDR; |
| 851 | s2 = ZV - GPT; | 970 | s2 = ZV_BYTE - GPT_BYTE; |
| 852 | if (s1 < 0) | 971 | if (s1 < 0) |
| 853 | { | 972 | { |
| 854 | p2 = p1; | 973 | p2 = p1; |
| 855 | s2 = ZV - BEGV; | 974 | s2 = ZV_BYTE - BEGV_BYTE; |
| 856 | s1 = 0; | 975 | s1 = 0; |
| 857 | } | 976 | } |
| 858 | if (s2 < 0) | 977 | if (s2 < 0) |
| 859 | { | 978 | { |
| 860 | s1 = ZV - BEGV; | 979 | s1 = ZV_BYTE - BEGV_BYTE; |
| 861 | s2 = 0; | 980 | s2 = 0; |
| 862 | } | 981 | } |
| 863 | re_match_object = Qnil; | 982 | re_match_object = Qnil; |
| @@ -875,12 +994,13 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix) | |||
| 875 | } | 994 | } |
| 876 | if (val >= 0) | 995 | if (val >= 0) |
| 877 | { | 996 | { |
| 878 | j = BEGV; | ||
| 879 | for (i = 0; i < search_regs.num_regs; i++) | 997 | for (i = 0; i < search_regs.num_regs; i++) |
| 880 | if (search_regs.start[i] >= 0) | 998 | if (search_regs.start[i] >= 0) |
| 881 | { | 999 | { |
| 882 | search_regs.start[i] += j; | 1000 | search_regs.start[i] |
| 883 | search_regs.end[i] += j; | 1001 | = BYTE_TO_CHAR (search_regs.start[i] + BEGV_BYTE); |
| 1002 | search_regs.end[i] | ||
| 1003 | = BYTE_TO_CHAR (search_regs.end[i] + BEGV_BYTE); | ||
| 884 | } | 1004 | } |
| 885 | XSETBUFFER (last_thing_searched, current_buffer); | 1005 | XSETBUFFER (last_thing_searched, current_buffer); |
| 886 | /* Set pos to the new position. */ | 1006 | /* Set pos to the new position. */ |
| @@ -905,12 +1025,13 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix) | |||
| 905 | } | 1025 | } |
| 906 | if (val >= 0) | 1026 | if (val >= 0) |
| 907 | { | 1027 | { |
| 908 | j = BEGV; | ||
| 909 | for (i = 0; i < search_regs.num_regs; i++) | 1028 | for (i = 0; i < search_regs.num_regs; i++) |
| 910 | if (search_regs.start[i] >= 0) | 1029 | if (search_regs.start[i] >= 0) |
| 911 | { | 1030 | { |
| 912 | search_regs.start[i] += j; | 1031 | search_regs.start[i] |
| 913 | search_regs.end[i] += j; | 1032 | = BYTE_TO_CHAR (search_regs.start[i] + BEGV_BYTE); |
| 1033 | search_regs.end[i] | ||
| 1034 | = BYTE_TO_CHAR (search_regs.end[i] + BEGV_BYTE); | ||
| 914 | } | 1035 | } |
| 915 | XSETBUFFER (last_thing_searched, current_buffer); | 1036 | XSETBUFFER (last_thing_searched, current_buffer); |
| 916 | pos = search_regs.end[0]; | 1037 | pos = search_regs.end[0]; |
| @@ -927,6 +1048,8 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix) | |||
| 927 | } | 1048 | } |
| 928 | else /* non-RE case */ | 1049 | else /* non-RE case */ |
| 929 | { | 1050 | { |
| 1051 | int pos_byte = CHAR_TO_BYTE (pos); | ||
| 1052 | int lim_byte = CHAR_TO_BYTE (lim); | ||
| 930 | #ifdef C_ALLOCA | 1053 | #ifdef C_ALLOCA |
| 931 | int BM_tab_space[0400]; | 1054 | int BM_tab_space[0400]; |
| 932 | BM_tab = &BM_tab_space[0]; | 1055 | BM_tab = &BM_tab_space[0]; |
| @@ -979,7 +1102,7 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix) | |||
| 979 | /* it entirely if there is none. */ | 1102 | /* it entirely if there is none. */ |
| 980 | 1103 | ||
| 981 | dirlen = len * direction; | 1104 | dirlen = len * direction; |
| 982 | infinity = dirlen - (lim + pos + len + len) * direction; | 1105 | infinity = dirlen - (lim_byte + pos_byte + len + len) * direction; |
| 983 | if (direction < 0) | 1106 | if (direction < 0) |
| 984 | pat = (base_pat += len - 1); | 1107 | pat = (base_pat += len - 1); |
| 985 | BM_tab_base = BM_tab; | 1108 | BM_tab_base = BM_tab; |
| @@ -1023,32 +1146,33 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix) | |||
| 1023 | /* different. */ | 1146 | /* different. */ |
| 1024 | } | 1147 | } |
| 1025 | infinity = dirlen - infinity; | 1148 | infinity = dirlen - infinity; |
| 1026 | pos += dirlen - ((direction > 0) ? direction : 0); | 1149 | pos_byte += dirlen - ((direction > 0) ? direction : 0); |
| 1027 | /* loop invariant - pos points at where last char (first char if reverse) | 1150 | /* loop invariant - POS_BYTE points at where last char (first |
| 1028 | of pattern would align in a possible match. */ | 1151 | char if reverse) of pattern would align in a possible match. */ |
| 1029 | while (n != 0) | 1152 | while (n != 0) |
| 1030 | { | 1153 | { |
| 1031 | /* It's been reported that some (broken) compiler thinks that | 1154 | /* It's been reported that some (broken) compiler thinks that |
| 1032 | Boolean expressions in an arithmetic context are unsigned. | 1155 | Boolean expressions in an arithmetic context are unsigned. |
| 1033 | Using an explicit ?1:0 prevents this. */ | 1156 | Using an explicit ?1:0 prevents this. */ |
| 1034 | if ((lim - pos - ((direction > 0) ? 1 : 0)) * direction < 0) | 1157 | if ((lim_byte - pos_byte - ((direction > 0) ? 1 : 0)) * direction |
| 1158 | < 0) | ||
| 1035 | return (n * (0 - direction)); | 1159 | return (n * (0 - direction)); |
| 1036 | /* First we do the part we can by pointers (maybe nothing) */ | 1160 | /* First we do the part we can by pointers (maybe nothing) */ |
| 1037 | QUIT; | 1161 | QUIT; |
| 1038 | pat = base_pat; | 1162 | pat = base_pat; |
| 1039 | limit = pos - dirlen + direction; | 1163 | limit = pos_byte - dirlen + direction; |
| 1040 | limit = ((direction > 0) | 1164 | limit = ((direction > 0) |
| 1041 | ? BUFFER_CEILING_OF (limit) | 1165 | ? BUFFER_CEILING_OF (limit) |
| 1042 | : BUFFER_FLOOR_OF (limit)); | 1166 | : BUFFER_FLOOR_OF (limit)); |
| 1043 | /* LIMIT is now the last (not beyond-last!) value | 1167 | /* LIMIT is now the last (not beyond-last!) value POS_BYTE |
| 1044 | POS can take on without hitting edge of buffer or the gap. */ | 1168 | can take on without hitting edge of buffer or the gap. */ |
| 1045 | limit = ((direction > 0) | 1169 | limit = ((direction > 0) |
| 1046 | ? min (lim - 1, min (limit, pos + 20000)) | 1170 | ? min (lim_byte - 1, min (limit, pos_byte + 20000)) |
| 1047 | : max (lim, max (limit, pos - 20000))); | 1171 | : max (lim_byte, max (limit, pos_byte - 20000))); |
| 1048 | if ((limit - pos) * direction > 20) | 1172 | if ((limit - pos_byte) * direction > 20) |
| 1049 | { | 1173 | { |
| 1050 | p_limit = POS_ADDR (limit); | 1174 | p_limit = BYTE_POS_ADDR (limit); |
| 1051 | p2 = (cursor = POS_ADDR (pos)); | 1175 | p2 = (cursor = BYTE_POS_ADDR (pos_byte)); |
| 1052 | /* In this loop, pos + cursor - p2 is the surrogate for pos */ | 1176 | /* In this loop, pos + cursor - p2 is the surrogate for pos */ |
| 1053 | while (1) /* use one cursor setting as long as i can */ | 1177 | while (1) /* use one cursor setting as long as i can */ |
| 1054 | { | 1178 | { |
| @@ -1104,7 +1228,7 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix) | |||
| 1104 | { | 1228 | { |
| 1105 | cursor -= direction; | 1229 | cursor -= direction; |
| 1106 | 1230 | ||
| 1107 | set_search_regs (pos + cursor - p2 + ((direction > 0) | 1231 | set_search_regs (pos_byte + cursor - p2 + ((direction > 0) |
| 1108 | ? 1 - len : 0), | 1232 | ? 1 - len : 0), |
| 1109 | len); | 1233 | len); |
| 1110 | 1234 | ||
| @@ -1117,19 +1241,19 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix) | |||
| 1117 | else | 1241 | else |
| 1118 | cursor += stride_for_teases; /* <sigh> we lose - */ | 1242 | cursor += stride_for_teases; /* <sigh> we lose - */ |
| 1119 | } | 1243 | } |
| 1120 | pos += cursor - p2; | 1244 | pos_byte += cursor - p2; |
| 1121 | } | 1245 | } |
| 1122 | else | 1246 | else |
| 1123 | /* Now we'll pick up a clump that has to be done the hard */ | 1247 | /* Now we'll pick up a clump that has to be done the hard */ |
| 1124 | /* way because it covers a discontinuity */ | 1248 | /* way because it covers a discontinuity */ |
| 1125 | { | 1249 | { |
| 1126 | limit = ((direction > 0) | 1250 | limit = ((direction > 0) |
| 1127 | ? BUFFER_CEILING_OF (pos - dirlen + 1) | 1251 | ? BUFFER_CEILING_OF (pos_byte - dirlen + 1) |
| 1128 | : BUFFER_FLOOR_OF (pos - dirlen - 1)); | 1252 | : BUFFER_FLOOR_OF (pos_byte - dirlen - 1)); |
| 1129 | limit = ((direction > 0) | 1253 | limit = ((direction > 0) |
| 1130 | ? min (limit + len, lim - 1) | 1254 | ? min (limit + len, lim_byte - 1) |
| 1131 | : max (limit - len, lim)); | 1255 | : max (limit - len, lim_byte)); |
| 1132 | /* LIMIT is now the last value POS can have | 1256 | /* LIMIT is now the last value POS_BYTE can have |
| 1133 | and still be valid for a possible match. */ | 1257 | and still be valid for a possible match. */ |
| 1134 | while (1) | 1258 | while (1) |
| 1135 | { | 1259 | { |
| @@ -1137,59 +1261,59 @@ search_buffer (string, pos, lim, n, RE, trt, inverse_trt, posix) | |||
| 1137 | /* speed because it will usually run only once. */ | 1261 | /* speed because it will usually run only once. */ |
| 1138 | /* (the reach is at most len + 21, and typically */ | 1262 | /* (the reach is at most len + 21, and typically */ |
| 1139 | /* does not exceed len) */ | 1263 | /* does not exceed len) */ |
| 1140 | while ((limit - pos) * direction >= 0) | 1264 | while ((limit - pos_byte) * direction >= 0) |
| 1141 | pos += BM_tab[FETCH_BYTE (pos)]; | 1265 | pos_byte += BM_tab[FETCH_BYTE (pos_byte)]; |
| 1142 | /* now run the same tests to distinguish going off the */ | 1266 | /* now run the same tests to distinguish going off the */ |
| 1143 | /* end, a match or a phony match. */ | 1267 | /* end, a match or a phony match. */ |
| 1144 | if ((pos - limit) * direction <= len) | 1268 | if ((pos_byte - limit) * direction <= len) |
| 1145 | break; /* ran off the end */ | 1269 | break; /* ran off the end */ |
| 1146 | /* Found what might be a match. | 1270 | /* Found what might be a match. |
| 1147 | Set POS back to last (first if reverse) char pos. */ | 1271 | Set POS_BYTE back to last (first if reverse) pos. */ |
| 1148 | pos -= infinity; | 1272 | pos_byte -= infinity; |
| 1149 | i = dirlen - direction; | 1273 | i = dirlen - direction; |
| 1150 | while ((i -= direction) + direction != 0) | 1274 | while ((i -= direction) + direction != 0) |
| 1151 | { | 1275 | { |
| 1152 | pos -= direction; | 1276 | pos_byte -= direction; |
| 1153 | if (pat[i] != (trt != 0 | 1277 | if (pat[i] != (trt != 0 |
| 1154 | ? XINT (trt[FETCH_BYTE (pos)]) | 1278 | ? XINT (trt[FETCH_BYTE (pos_byte)]) |
| 1155 | : FETCH_BYTE (pos))) | 1279 | : FETCH_BYTE (pos_byte))) |
| 1156 | break; | 1280 | break; |
| 1157 | } | 1281 | } |
| 1158 | /* Above loop has moved POS part or all the way | 1282 | /* Above loop has moved POS_BYTE part or all the way |
| 1159 | back to the first char pos (last char pos if reverse). | 1283 | back to the first pos (last pos if reverse). |
| 1160 | Set it once again at the last (first if reverse) char. */ | 1284 | Set it once again at the last (first if reverse) char. */ |
| 1161 | pos += dirlen - i- direction; | 1285 | pos_byte += dirlen - i- direction; |
| 1162 | if (i + direction == 0) | 1286 | if (i + direction == 0) |
| 1163 | { | 1287 | { |
| 1164 | pos -= direction; | 1288 | pos_byte -= direction; |
| 1165 | 1289 | ||
| 1166 | set_search_regs (pos + ((direction > 0) ? 1 - len : 0), | 1290 | set_search_regs (pos_byte + ((direction > 0) ? 1 - len : 0), |
| 1167 | len); | 1291 | len); |
| 1168 | 1292 | ||
| 1169 | if ((n -= direction) != 0) | 1293 | if ((n -= direction) != 0) |
| 1170 | pos += dirlen; /* to resume search */ | 1294 | pos_byte += dirlen; /* to resume search */ |
| 1171 | else | 1295 | else |
| 1172 | return ((direction > 0) | 1296 | return ((direction > 0) |
| 1173 | ? search_regs.end[0] : search_regs.start[0]); | 1297 | ? search_regs.end[0] : search_regs.start[0]); |
| 1174 | } | 1298 | } |
| 1175 | else | 1299 | else |
| 1176 | pos += stride_for_teases; | 1300 | pos_byte += stride_for_teases; |
| 1177 | } | 1301 | } |
| 1178 | } | 1302 | } |
| 1179 | /* We have done one clump. Can we continue? */ | 1303 | /* We have done one clump. Can we continue? */ |
| 1180 | if ((lim - pos) * direction < 0) | 1304 | if ((lim_byte - pos_byte) * direction < 0) |
| 1181 | return ((0 - n) * direction); | 1305 | return ((0 - n) * direction); |
| 1182 | } | 1306 | } |
| 1183 | return pos; | 1307 | return BYTE_TO_CHAR (pos_byte); |
| 1184 | } | 1308 | } |
| 1185 | } | 1309 | } |
| 1186 | 1310 | ||
| 1187 | /* Record beginning BEG and end BEG + LEN | 1311 | /* Record beginning BEG_BYTE and end BEG_BYTE + NBYTES |
| 1188 | for a match just found in the current buffer. */ | 1312 | for a match just found in the current buffer. */ |
| 1189 | 1313 | ||
| 1190 | static void | 1314 | static void |
| 1191 | set_search_regs (beg, len) | 1315 | set_search_regs (beg_byte, nbytes) |
| 1192 | int beg, len; | 1316 | int beg_byte, nbytes; |
| 1193 | { | 1317 | { |
| 1194 | /* Make sure we have registers in which to store | 1318 | /* Make sure we have registers in which to store |
| 1195 | the match position. */ | 1319 | the match position. */ |
| @@ -1200,8 +1324,8 @@ set_search_regs (beg, len) | |||
| 1200 | search_regs.num_regs = 2; | 1324 | search_regs.num_regs = 2; |
| 1201 | } | 1325 | } |
| 1202 | 1326 | ||
| 1203 | search_regs.start[0] = beg; | 1327 | search_regs.start[0] = BYTE_TO_CHAR (beg_byte); |
| 1204 | search_regs.end[0] = beg + len; | 1328 | search_regs.end[0] = BYTE_TO_CHAR (beg_byte + nbytes); |
| 1205 | XSETBUFFER (last_thing_searched, current_buffer); | 1329 | XSETBUFFER (last_thing_searched, current_buffer); |
| 1206 | } | 1330 | } |
| 1207 | 1331 | ||
| @@ -1466,9 +1590,19 @@ since only regular expressions have distinguished subexpressions.") | |||
| 1466 | 1590 | ||
| 1467 | if (NILP (fixedcase)) | 1591 | if (NILP (fixedcase)) |
| 1468 | { | 1592 | { |
| 1593 | int beg; | ||
| 1469 | /* Decide how to casify by examining the matched text. */ | 1594 | /* Decide how to casify by examining the matched text. */ |
| 1470 | 1595 | ||
| 1471 | last = search_regs.end[sub]; | 1596 | if (NILP (string)) |
| 1597 | last = CHAR_TO_BYTE (search_regs.end[sub]); | ||
| 1598 | else | ||
| 1599 | last = search_regs.end[sub]; | ||
| 1600 | |||
| 1601 | if (NILP (string)) | ||
| 1602 | beg = CHAR_TO_BYTE (search_regs.start[sub]); | ||
| 1603 | else | ||
| 1604 | beg = search_regs.start[sub]; | ||
| 1605 | |||
| 1472 | prevc = '\n'; | 1606 | prevc = '\n'; |
| 1473 | case_action = all_caps; | 1607 | case_action = all_caps; |
| 1474 | 1608 | ||
| @@ -1479,7 +1613,7 @@ since only regular expressions have distinguished subexpressions.") | |||
| 1479 | some_nonuppercase_initial = 0; | 1613 | some_nonuppercase_initial = 0; |
| 1480 | some_uppercase = 0; | 1614 | some_uppercase = 0; |
| 1481 | 1615 | ||
| 1482 | for (pos = search_regs.start[sub]; pos < last; pos++) | 1616 | for (pos = beg; pos < last; pos++) |
| 1483 | { | 1617 | { |
| 1484 | if (NILP (string)) | 1618 | if (NILP (string)) |
| 1485 | c = FETCH_BYTE (pos); | 1619 | c = FETCH_BYTE (pos); |
| @@ -1624,7 +1758,7 @@ since only regular expressions have distinguished subexpressions.") | |||
| 1624 | else | 1758 | else |
| 1625 | opoint = PT; | 1759 | opoint = PT; |
| 1626 | 1760 | ||
| 1627 | temp_set_point (search_regs.start[sub], current_buffer); | 1761 | TEMP_SET_PT (search_regs.start[sub]); |
| 1628 | 1762 | ||
| 1629 | /* We insert the replacement text before the old text, and then | 1763 | /* We insert the replacement text before the old text, and then |
| 1630 | delete the original text. This means that markers at the | 1764 | delete the original text. This means that markers at the |
| @@ -1681,9 +1815,9 @@ since only regular expressions have distinguished subexpressions.") | |||
| 1681 | 1815 | ||
| 1682 | /* Put point back where it was in the text. */ | 1816 | /* Put point back where it was in the text. */ |
| 1683 | if (opoint <= 0) | 1817 | if (opoint <= 0) |
| 1684 | temp_set_point (opoint + ZV, current_buffer); | 1818 | TEMP_SET_PT (opoint + ZV); |
| 1685 | else | 1819 | else |
| 1686 | temp_set_point (opoint, current_buffer); | 1820 | TEMP_SET_PT (opoint); |
| 1687 | 1821 | ||
| 1688 | /* Now move point "officially" to the start of the inserted replacement. */ | 1822 | /* Now move point "officially" to the start of the inserted replacement. */ |
| 1689 | move_if_not_intangible (newpoint); | 1823 | move_if_not_intangible (newpoint); |