diff options
| author | Richard M. Stallman | 1997-07-17 06:51:28 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1997-07-17 06:51:28 +0000 |
| commit | c5ca4d3a1b9bb3165401212fa39621562c5c0e1f (patch) | |
| tree | 79b9f5d9238d33c424230780dabe97b9b10f11a0 /src | |
| parent | 1fca3fae800a27dbe292463d9d9df5dcee30c6ae (diff) | |
| download | emacs-c5ca4d3a1b9bb3165401212fa39621562c5c0e1f.tar.gz emacs-c5ca4d3a1b9bb3165401212fa39621562c5c0e1f.zip | |
(replace_range): New function.
Diffstat (limited to 'src')
| -rw-r--r-- | src/insdel.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/insdel.c b/src/insdel.c index 098b1670a4f..73f177c02d2 100644 --- a/src/insdel.c +++ b/src/insdel.c | |||
| @@ -695,6 +695,122 @@ insert_from_string_before_markers (string, pos, length, inherit) | |||
| 695 | } | 695 | } |
| 696 | } | 696 | } |
| 697 | 697 | ||
| 698 | /* Replace the text from FROM to TO with NEW, | ||
| 699 | If PREPARE is nonzero, call prepare_to_modify_buffer. | ||
| 700 | If INHERIT, the newly inserted text should inherit text properties | ||
| 701 | from the surrounding non-deleted text. */ | ||
| 702 | |||
| 703 | /* Note that this does not yet handle markers quite right. | ||
| 704 | Also it needs to record a single undo-entry that does a replacement | ||
| 705 | rather than a separate delete and insert. | ||
| 706 | That way, undo will also handle markers properly. */ | ||
| 707 | |||
| 708 | void | ||
| 709 | replace_range (from, to, new, prepare, inherit) | ||
| 710 | Lisp_Object new; | ||
| 711 | int from, to, prepare, inherit; | ||
| 712 | { | ||
| 713 | int numdel; | ||
| 714 | int inslen = XSTRING (new)->size; | ||
| 715 | register Lisp_Object temp; | ||
| 716 | struct gcpro gcpro1; | ||
| 717 | |||
| 718 | GCPRO1 (new); | ||
| 719 | |||
| 720 | if (prepare) | ||
| 721 | { | ||
| 722 | int range_length = to - from; | ||
| 723 | prepare_to_modify_buffer (from, to, &from); | ||
| 724 | to = from + range_length; | ||
| 725 | } | ||
| 726 | |||
| 727 | /* Make args be valid */ | ||
| 728 | if (from < BEGV) | ||
| 729 | from = BEGV; | ||
| 730 | if (to > ZV) | ||
| 731 | to = ZV; | ||
| 732 | |||
| 733 | UNGCPRO; | ||
| 734 | |||
| 735 | numdel = to - from; | ||
| 736 | |||
| 737 | /* Make sure point-max won't overflow after this insertion. */ | ||
| 738 | XSETINT (temp, Z - numdel + inslen); | ||
| 739 | if (Z - numdel + inslen != XINT (temp)) | ||
| 740 | error ("maximum buffer size exceeded"); | ||
| 741 | |||
| 742 | if (numdel <= 0 && inslen == 0) | ||
| 743 | return; | ||
| 744 | |||
| 745 | GCPRO1 (new); | ||
| 746 | |||
| 747 | /* Make sure the gap is somewhere in or next to what we are deleting. */ | ||
| 748 | if (from > GPT) | ||
| 749 | gap_right (from); | ||
| 750 | if (to < GPT) | ||
| 751 | gap_left (to, 0); | ||
| 752 | |||
| 753 | /* Relocate all markers pointing into the new, larger gap | ||
| 754 | to point at the end of the text before the gap. | ||
| 755 | This has to be done before recording the deletion, | ||
| 756 | so undo handles this after reinserting the text. */ | ||
| 757 | adjust_markers (to + GAP_SIZE, to + GAP_SIZE, - numdel - GAP_SIZE); | ||
| 758 | |||
| 759 | record_delete (from, numdel); | ||
| 760 | |||
| 761 | GAP_SIZE += numdel; | ||
| 762 | ZV -= numdel; | ||
| 763 | Z -= numdel; | ||
| 764 | GPT = from; | ||
| 765 | *(GPT_ADDR) = 0; /* Put an anchor. */ | ||
| 766 | |||
| 767 | if (GPT - BEG < beg_unchanged) | ||
| 768 | beg_unchanged = GPT - BEG; | ||
| 769 | if (Z - GPT < end_unchanged) | ||
| 770 | end_unchanged = Z - GPT; | ||
| 771 | |||
| 772 | if (GAP_SIZE < inslen) | ||
| 773 | make_gap (inslen - GAP_SIZE); | ||
| 774 | |||
| 775 | record_insert (from, inslen); | ||
| 776 | |||
| 777 | bcopy (XSTRING (new)->data, GPT_ADDR, inslen); | ||
| 778 | |||
| 779 | /* Relocate point as if it were a marker. */ | ||
| 780 | if (from < PT) | ||
| 781 | adjust_point (from + inslen - (PT < to ? PT : to)); | ||
| 782 | |||
| 783 | #ifdef USE_TEXT_PROPERTIES | ||
| 784 | offset_intervals (current_buffer, PT, inslen - numdel); | ||
| 785 | #endif | ||
| 786 | |||
| 787 | GAP_SIZE -= inslen; | ||
| 788 | GPT += inslen; | ||
| 789 | ZV += inslen; | ||
| 790 | Z += inslen; | ||
| 791 | if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */ | ||
| 792 | |||
| 793 | /* Adjust the overlay center as needed. This must be done after | ||
| 794 | adjusting the markers that bound the overlays. */ | ||
| 795 | adjust_overlays_for_delete (from, numdel); | ||
| 796 | adjust_overlays_for_insert (from, inslen); | ||
| 797 | adjust_markers_for_insert (from, inslen); | ||
| 798 | |||
| 799 | #ifdef USE_TEXT_PROPERTIES | ||
| 800 | /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */ | ||
| 801 | graft_intervals_into_buffer (XSTRING (new)->intervals, from, inslen, | ||
| 802 | current_buffer, inherit); | ||
| 803 | #endif | ||
| 804 | |||
| 805 | if (inslen == 0) | ||
| 806 | evaporate_overlays (from); | ||
| 807 | |||
| 808 | MODIFF++; | ||
| 809 | UNGCPRO; | ||
| 810 | |||
| 811 | signal_after_change (from, numdel, inslen); | ||
| 812 | } | ||
| 813 | |||
| 698 | /* Delete characters in current buffer | 814 | /* Delete characters in current buffer |
| 699 | from FROM up to (but not including) TO. */ | 815 | from FROM up to (but not including) TO. */ |
| 700 | 816 | ||