1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 86, 93, 94, 95, 97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "intervals.h"
28 #include "blockinput.h"
34 #define min(x, y) ((x) < (y) ? (x) : (y))
36 static void insert_from_string_1
P_ ((Lisp_Object
, int, int, int, int, int, int));
37 static void insert_from_buffer_1 ();
38 static void gap_left
P_ ((int, int, int));
39 static void gap_right
P_ ((int, int));
40 static void adjust_markers_gap_motion
P_ ((int, int, int));
41 static void adjust_markers_for_insert
P_ ((int, int, int, int, int, int, int));
42 static void adjust_markers_for_delete
P_ ((int, int, int, int));
43 static void adjust_markers_for_record_delete
P_ ((int, int, int, int));
44 static void adjust_point
P_ ((int, int));
46 Lisp_Object
Fcombine_after_change_execute ();
48 /* Non-nil means don't call the after-change-functions right away,
49 just record an element in Vcombine_after_change_calls_list. */
50 Lisp_Object Vcombine_after_change_calls
;
52 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
53 describing changes which happened while combine_after_change_calls
54 was nonzero. We use this to decide how to call them
55 once the deferral ends.
58 BEG-UNCHANGED is the number of chars before the changed range.
59 END-UNCHANGED is the number of chars after the changed range,
60 and CHANGE-AMOUNT is the number of characters inserted by the change
61 (negative for a deletion). */
62 Lisp_Object combine_after_change_list
;
64 /* Buffer which combine_after_change_list is about. */
65 Lisp_Object combine_after_change_buffer
;
67 /* Check all markers in the current buffer, looking for something invalid. */
69 static int check_markers_debug_flag
;
71 #define CHECK_MARKERS() \
72 if (check_markers_debug_flag) \
79 register Lisp_Object tail
, prev
, next
;
81 tail
= BUF_MARKERS (current_buffer
);
83 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
85 if (XMARKER (tail
)->buffer
->text
!= current_buffer
->text
)
87 if (XMARKER (tail
)->charpos
> Z
)
89 if (XMARKER (tail
)->bytepos
> Z_BYTE
)
92 tail
= XMARKER (tail
)->chain
;
96 /* Move gap to position CHARPOS.
97 Note that this can quit! */
103 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
106 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
107 Note that this can quit! */
110 move_gap_both (charpos
, bytepos
)
111 int charpos
, bytepos
;
113 if (bytepos
< GPT_BYTE
)
114 gap_left (charpos
, bytepos
, 0);
115 else if (bytepos
> GPT_BYTE
)
116 gap_right (charpos
, bytepos
);
119 /* Move the gap to a position less than the current GPT.
120 BYTEPOS describes the new position as a byte position,
121 and CHARPOS is the corresponding char position.
122 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
125 gap_left (charpos
, bytepos
, newgap
)
126 register int charpos
, bytepos
;
129 register unsigned char *to
, *from
;
135 if (unchanged_modified
== MODIFF
136 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
138 beg_unchanged
= charpos
- BEG
;
139 end_unchanged
= Z
- charpos
;
143 if (Z
- GPT
< end_unchanged
)
144 end_unchanged
= Z
- GPT
;
145 if (charpos
< beg_unchanged
)
146 beg_unchanged
= charpos
- BEG
;
155 /* Now copy the characters. To move the gap down,
156 copy characters up. */
160 /* I gets number of characters left to copy. */
161 i
= new_s1
- bytepos
;
164 /* If a quit is requested, stop copying now.
165 Change BYTEPOS to be where we have actually moved the gap to. */
169 charpos
= BYTE_TO_CHAR (bytepos
);
172 /* Move at most 32000 chars before checking again for a quit. */
177 /* bcopy is safe if the two areas of memory do not overlap
178 or on systems where bcopy is always safe for moving upward. */
179 && (BCOPY_UPWARD_SAFE
180 || to
- from
>= 128))
182 /* If overlap is not safe, avoid it by not moving too many
183 characters at once. */
184 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
199 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
200 BYTEPOS is where the loop above stopped, which may be what was specified
201 or may be where a quit was detected. */
202 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
205 if (bytepos
< charpos
)
207 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
211 /* Move the gap to a position greater than than the current GPT.
212 BYTEPOS describes the new position as a byte position,
213 and CHARPOS is the corresponding char position. */
216 gap_right (charpos
, bytepos
)
217 register int charpos
, bytepos
;
219 register unsigned char *to
, *from
;
223 if (unchanged_modified
== MODIFF
224 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
226 beg_unchanged
= charpos
- BEG
;
227 end_unchanged
= Z
- charpos
;
231 if (Z
- charpos
- 1 < end_unchanged
)
232 end_unchanged
= Z
- charpos
;
233 if (GPT
- BEG
< beg_unchanged
)
234 beg_unchanged
= GPT
- BEG
;
242 /* Now copy the characters. To move the gap up,
243 copy characters down. */
247 /* I gets number of characters left to copy. */
248 i
= bytepos
- new_s1
;
251 /* If a quit is requested, stop copying now.
252 Change BYTEPOS to be where we have actually moved the gap to. */
256 charpos
= BYTE_TO_CHAR (bytepos
);
259 /* Move at most 32000 chars before checking again for a quit. */
264 /* bcopy is safe if the two areas of memory do not overlap
265 or on systems where bcopy is always safe for moving downward. */
266 && (BCOPY_DOWNWARD_SAFE
267 || from
- to
>= 128))
269 /* If overlap is not safe, avoid it by not moving too many
270 characters at once. */
271 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
286 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
290 if (bytepos
< charpos
)
292 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
296 /* Add AMOUNT to the byte position of every marker in the current buffer
297 whose current byte position is between FROM (exclusive) and TO (inclusive).
299 Also, any markers past the outside of that interval, in the direction
300 of adjustment, are first moved back to the near end of the interval
301 and then adjusted by AMOUNT.
303 When the latter adjustment is done, if AMOUNT is negative,
304 we record the adjustment for undo. (This case happens only for
307 The markers' character positions are not altered,
308 because gap motion does not affect character positions. */
310 int adjust_markers_test
;
313 adjust_markers_gap_motion (from
, to
, amount
)
314 register int from
, to
, amount
;
316 /* Now that a marker has a bytepos, not counting the gap,
317 nothing needs to be done here. */
320 register struct Lisp_Marker
*m
;
323 marker
= BUF_MARKERS (current_buffer
);
325 while (!NILP (marker
))
327 m
= XMARKER (marker
);
331 if (mpos
> to
&& mpos
< to
+ amount
)
333 if (adjust_markers_test
)
340 /* Here's the case where a marker is inside text being deleted.
341 AMOUNT can be negative for gap motion, too,
342 but then this range contains no markers. */
343 if (mpos
> from
+ amount
&& mpos
<= from
)
345 if (adjust_markers_test
)
347 mpos
= from
+ amount
;
350 if (mpos
> from
&& mpos
<= to
)
358 /* Adjust all markers for a deletion
359 whose range in bytes is FROM_BYTE to TO_BYTE.
360 The range in charpos is FROM to TO.
362 This function assumes that the gap is adjacent to
363 or inside of the range being deleted. */
366 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
)
367 register int from
, from_byte
, to
, to_byte
;
370 register struct Lisp_Marker
*m
;
371 register int charpos
;
373 marker
= BUF_MARKERS (current_buffer
);
375 while (!NILP (marker
))
377 m
= XMARKER (marker
);
378 charpos
= m
->charpos
;
383 /* If the marker is after the deletion,
384 relocate by number of chars / bytes deleted. */
387 m
->charpos
-= to
- from
;
388 m
->bytepos
-= to_byte
- from_byte
;
391 /* Here's the case where a marker is inside text being deleted. */
392 else if (charpos
> from
)
394 record_marker_adjustment (marker
, from
- charpos
);
396 m
->bytepos
= from_byte
;
403 /* Adjust all markers for calling record_delete for combining bytes.
404 whose range in bytes is FROM_BYTE to TO_BYTE.
405 The range in charpos is FROM to TO. */
408 adjust_markers_for_record_delete (from
, from_byte
, to
, to_byte
)
409 register int from
, from_byte
, to
, to_byte
;
412 register struct Lisp_Marker
*m
;
413 register int charpos
;
415 marker
= BUF_MARKERS (current_buffer
);
417 while (!NILP (marker
))
419 m
= XMARKER (marker
);
420 charpos
= m
->charpos
;
422 /* If the marker is after the deletion,
423 relocate by number of chars / bytes deleted. */
426 /* Here's the case where a marker is inside text being deleted. */
427 else if (charpos
> from
)
428 record_marker_adjustment (marker
, from
- charpos
);
434 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
435 to TO / TO_BYTE. We have to relocate the charpos of every marker
436 that points after the insertion (but not their bytepos).
438 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
439 that combine into one character with the text before the insertion.
440 COMBINED_AFTER_BYTES is the number of bytes after the insertion
441 that combine into one character with the last inserted bytes.
443 When a marker points at the insertion point,
444 we advance it if either its insertion-type is t
445 or BEFORE_MARKERS is true. */
448 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
,
449 combined_before_bytes
, combined_after_bytes
,
451 register int from
, from_byte
, to
, to_byte
;
452 int combined_before_bytes
, combined_after_bytes
, before_markers
;
456 int nchars
= to
- from
;
457 int nbytes
= to_byte
- from_byte
;
459 marker
= BUF_MARKERS (current_buffer
);
461 while (!NILP (marker
))
463 register struct Lisp_Marker
*m
= XMARKER (marker
);
465 /* In a single-byte buffer, a marker's two positions must be equal.
466 (If this insertion is going to combine characters, Z will
467 become different from Z_BYTE, but they might be the same now.
468 If so, the two OLD positions of the marker should be equal.) */
471 if (m
->charpos
!= m
->bytepos
)
475 if (m
->bytepos
== from_byte
)
477 if (m
->insertion_type
|| before_markers
)
479 m
->bytepos
+= nbytes
+ combined_after_bytes
;
480 m
->charpos
+= nchars
+ !!combined_after_bytes
;
481 /* Point the marker before the combined character,
482 so that undoing the insertion puts it back where it was. */
483 if (combined_after_bytes
)
484 DEC_BOTH (m
->charpos
, m
->bytepos
);
485 if (m
->insertion_type
)
488 else if (combined_before_bytes
)
490 /* This marker doesn't "need relocation",
491 but don't leave it pointing in the middle of a character.
492 Point the marker after the combined character,
493 so that undoing the insertion puts it back where it was. */
495 /* Here we depend on the fact that the gap is after
496 all of the combining bytes that we are going to skip over. */
497 DEC_BOTH (m
->charpos
, m
->bytepos
);
498 INC_BOTH (m
->charpos
, m
->bytepos
);
501 /* If a marker was pointing into the combining bytes
502 after the insertion, don't leave it there
503 in the middle of a character. */
504 else if (combined_after_bytes
&& m
->bytepos
>= from_byte
505 && m
->bytepos
< from_byte
+ combined_after_bytes
)
507 /* Put it after the combining bytes. */
508 m
->bytepos
= to_byte
+ combined_after_bytes
;
510 /* Now move it back before the combined character,
511 so that undoing the insertion will put it where it was. */
512 DEC_BOTH (m
->charpos
, m
->bytepos
);
514 else if (m
->bytepos
> from_byte
)
516 m
->bytepos
+= nbytes
;
517 m
->charpos
+= nchars
;
523 /* Adjusting only markers whose insertion-type is t may result in
524 disordered overlays in the slot `overlays_before'. */
526 fix_overlays_before (current_buffer
, from
, to
);
529 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
531 This is used only when the value of point changes due to an insert
532 or delete; it does not represent a conceptual change in point as a
533 marker. In particular, point is not crossing any interval
534 boundaries, so there's no need to use the usual SET_PT macro. In
535 fact it would be incorrect to do so, because either the old or the
536 new value of point is out of sync with the current set of
540 adjust_point (nchars
, nbytes
)
543 BUF_PT (current_buffer
) += nchars
;
544 BUF_PT_BYTE (current_buffer
) += nbytes
;
546 /* In a single-byte buffer, the two positions must be equal. */
552 /* Make the gap NBYTES_ADDED bytes longer. */
555 make_gap (nbytes_added
)
558 unsigned char *result
;
561 int real_gap_loc_byte
;
564 /* If we have to get more space, get enough to last a while. */
565 nbytes_added
+= 2000;
567 /* Don't allow a buffer size that won't fit in an int
568 even if it will fit in a Lisp integer.
569 That won't work because so many places use `int'. */
571 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
572 >= ((unsigned) 1 << (min (BITS_PER_INT
, VALBITS
) - 1)))
573 error ("Buffer exceeds maximum size");
576 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
577 result
= BUFFER_REALLOC (BEG_ADDR
, (Z_BYTE
- BEG_BYTE
578 + GAP_SIZE
+ nbytes_added
+ 1));
586 /* We can't unblock until the new address is properly stored. */
590 /* Prevent quitting in move_gap. */
595 real_gap_loc_byte
= GPT_BYTE
;
596 old_gap_size
= GAP_SIZE
;
598 /* Call the newly allocated space a gap at the end of the whole space. */
600 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
601 GAP_SIZE
= nbytes_added
;
603 /* Move the new gap down to be consecutive with the end of the old one.
604 This adjusts the markers properly too. */
605 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
607 /* Now combine the two into one large gap. */
608 GAP_SIZE
+= old_gap_size
;
610 GPT_BYTE
= real_gap_loc_byte
;
618 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
619 FROM_MULTIBYTE says whether the incoming text is multibyte.
620 TO_MULTIBYTE says whether to store the text as multibyte.
621 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
623 Return the number of bytes stored at TO_ADDR. */
626 copy_text (from_addr
, to_addr
, nbytes
,
627 from_multibyte
, to_multibyte
)
628 unsigned char *from_addr
;
629 unsigned char *to_addr
;
631 int from_multibyte
, to_multibyte
;
633 if (from_multibyte
== to_multibyte
)
635 bcopy (from_addr
, to_addr
, nbytes
);
638 else if (from_multibyte
)
641 int bytes_left
= nbytes
;
643 /* Convert multibyte to single byte. */
644 while (bytes_left
> 0)
647 c
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
648 *to_addr
++ = SINGLE_BYTE_CHAR_P (c
) ? c
: (c
& 0177) + 0200;
649 from_addr
+= thislen
;
657 unsigned char *initial_to_addr
= to_addr
;
659 /* Convert single-byte to multibyte. */
662 int c
= *from_addr
++;
663 unsigned char workbuf
[4], *str
;
666 if (c
>= 0240 && c
< 0400)
668 c
= unibyte_char_to_multibyte (c
);
669 len
= CHAR_STRING (c
, workbuf
, str
);
670 bcopy (str
, to_addr
, len
);
675 /* Special case for speed. */
676 *to_addr
++ = c
, nbytes
--;
678 return to_addr
- initial_to_addr
;
682 /* Return the number of bytes it would take
683 to convert some single-byte text to multibyte.
684 The single-byte text consists of NBYTES bytes at PTR. */
687 count_size_as_multibyte (ptr
, nbytes
)
692 int outgoing_nbytes
= 0;
694 for (i
= 0; i
< nbytes
; i
++)
696 unsigned int c
= *ptr
++;
702 c
= unibyte_char_to_multibyte (c
);
703 outgoing_nbytes
+= XINT (Fchar_bytes (make_number (c
)));
707 return outgoing_nbytes
;
710 /* Insert a string of specified length before point.
711 This function judges multibyteness based on
712 enable_multibyte_characters in the current buffer;
713 it never converts between single-byte and multibyte.
715 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
716 prepare_to_modify_buffer could relocate the text. */
719 insert (string
, nbytes
)
720 register unsigned char *string
;
726 insert_1 (string
, nbytes
, 0, 1, 0);
727 signal_after_change (opoint
, 0, PT
- opoint
);
731 /* Likewise, but inherit text properties from neighboring characters. */
734 insert_and_inherit (string
, nbytes
)
735 register unsigned char *string
;
741 insert_1 (string
, nbytes
, 1, 1, 0);
742 signal_after_change (opoint
, 0, PT
- opoint
);
746 /* Insert the character C before point. Do not inherit text properties. */
752 unsigned char workbuf
[4], *str
;
755 if (! NILP (current_buffer
->enable_multibyte_characters
))
756 len
= CHAR_STRING (c
, workbuf
, str
);
767 /* Insert the null-terminated string S before point. */
773 insert (s
, strlen (s
));
776 /* Like `insert' except that all markers pointing at the place where
777 the insertion happens are adjusted to point after it.
778 Don't use this function to insert part of a Lisp string,
779 since gc could happen and relocate it. */
782 insert_before_markers (string
, nbytes
)
783 unsigned char *string
;
790 insert_1 (string
, nbytes
, 0, 1, 1);
791 signal_after_change (opoint
, 0, PT
- opoint
);
795 /* Likewise, but inherit text properties from neighboring characters. */
798 insert_before_markers_and_inherit (string
, nbytes
)
799 unsigned char *string
;
806 insert_1 (string
, nbytes
, 1, 1, 1);
807 signal_after_change (opoint
, 0, PT
- opoint
);
811 /* Subroutine used by the insert functions above. */
814 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
815 register unsigned char *string
;
817 int inherit
, prepare
, before_markers
;
819 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
820 inherit
, prepare
, before_markers
);
823 /* See if the bytes before POS/POS_BYTE combine with bytes
824 at the start of STRING to form a single character.
825 If so, return the number of bytes at the start of STRING
826 which combine in this way. Otherwise, return 0. */
829 count_combining_before (string
, length
, pos
, pos_byte
)
830 unsigned char *string
;
834 int opos
= pos
, opos_byte
= pos_byte
;
836 unsigned char *p
= string
;
838 if (NILP (current_buffer
->enable_multibyte_characters
))
840 if (length
== 0 || CHAR_HEAD_P (*string
))
844 c
= FETCH_BYTE (pos_byte
- 1);
845 if (ASCII_BYTE_P (c
))
847 DEC_BOTH (pos
, pos_byte
);
848 c
= FETCH_BYTE (pos_byte
);
849 if (! BASE_LEADING_CODE_P (c
))
852 /* We have a combination situation.
853 Count the bytes at STRING that will combine. */
854 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
860 /* See if the bytes after POS/POS_BYTE combine with bytes
861 at the end of STRING to form a single character.
862 If so, return the number of bytes after POS/POS_BYTE
863 which combine in this way. Otherwise, return 0. */
866 count_combining_after (string
, length
, pos
, pos_byte
)
867 unsigned char *string
;
871 int opos
= pos
, opos_byte
= pos_byte
;
875 if (NILP (current_buffer
->enable_multibyte_characters
))
877 if (length
== 0 || ASCII_BYTE_P (string
[length
- 1]))
880 while (i
> 0 && ! CHAR_HEAD_P (string
[i
]))
884 if (! BASE_LEADING_CODE_P (string
[i
]))
889 c
= FETCH_BYTE (pos_byte
);
892 while (pos_byte
< ZV_BYTE
)
894 c
= FETCH_BYTE (pos_byte
);
900 return pos_byte
- opos_byte
;
903 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
904 following the position POS/POS_BYTE to the character preceding POS.
905 If TARGET is after POS+NBYTES, we only have to adjust the character
906 position TARGET, else, if TARGET is after POS, we have to adjust
907 both the character position TARGET and the byte position
908 TARGET_BYTE, else we don't have to do any adjustment. */
910 #define ADJUST_CHAR_POS(target, target_byte) \
912 if (target > pos + nbytes) \
914 else if (target >= pos) \
917 target_byte = pos_byte + nbytes; \
921 /* Combine NBYTES stray trailing-codes, which were formerly separate
922 characters, with the preceding character. These bytes
923 are located after position POS / POS_BYTE, and the preceding character
924 is located just before that position. */
927 combine_bytes (pos
, pos_byte
, nbytes
)
928 int pos
, pos_byte
, nbytes
;
930 /* Adjust all markers. */
931 adjust_markers_for_delete (pos
, pos_byte
, pos
+ nbytes
, pos_byte
);
933 adjust_overlays_for_delete (pos
, nbytes
);
935 ADJUST_CHAR_POS (BUF_PT (current_buffer
), BUF_PT_BYTE (current_buffer
));
936 ADJUST_CHAR_POS (GPT
, GPT_BYTE
);
937 ADJUST_CHAR_POS (Z
, Z_BYTE
);
938 ADJUST_CHAR_POS (ZV
, ZV_BYTE
);
940 if (BUF_INTERVALS (current_buffer
) != 0)
941 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
942 offset_intervals (current_buffer
, pos
, - nbytes
);
947 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
948 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
949 are the same as in insert_1. */
952 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
953 register unsigned char *string
;
954 register int nchars
, nbytes
;
955 int inherit
, prepare
, before_markers
;
957 register Lisp_Object temp
, deletion
;
958 int combined_before_bytes
, combined_after_bytes
;
960 if (NILP (current_buffer
->enable_multibyte_characters
))
964 move_gap_both (PT
, PT_BYTE
);
965 if (GAP_SIZE
< nbytes
)
966 make_gap (nbytes
- GAP_SIZE
);
969 prepare_to_modify_buffer (PT
, PT
, NULL
);
971 combined_before_bytes
972 = count_combining_before (string
, nbytes
, PT
, PT_BYTE
);
974 = count_combining_after (string
, nbytes
, PT
, PT_BYTE
);
976 /* Record deletion of the surrounding text that combines with
977 the insertion. This, together with recording the insertion,
978 will add up to the right stuff in the undo list.
980 But there is no need to actually delete the combining bytes
981 from the buffer and reinsert them. */
983 if (combined_after_bytes
)
985 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
986 PT
+ combined_after_bytes
,
987 PT_BYTE
+ combined_after_bytes
, 1);
989 adjust_markers_for_record_delete (PT
, PT_BYTE
,
990 PT
+ combined_after_bytes
,
991 PT_BYTE
+ combined_after_bytes
);
992 record_delete (PT
, deletion
);
995 if (combined_before_bytes
)
997 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
999 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1001 record_delete (PT
- 1, deletion
);
1004 record_insert (PT
- !!combined_before_bytes
,
1005 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1008 bcopy (string
, GPT_ADDR
, nbytes
);
1011 /* When we have combining at the end of the insertion,
1012 this is the character position before the combined character. */
1019 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1021 if (combined_after_bytes
)
1022 move_gap_both (GPT
+ combined_after_bytes
,
1023 GPT_BYTE
+ combined_after_bytes
);
1028 adjust_overlays_for_insert (PT
, nchars
);
1029 adjust_markers_for_insert (PT
, PT_BYTE
,
1030 PT
+ nchars
, PT_BYTE
+ nbytes
,
1031 combined_before_bytes
, combined_after_bytes
,
1034 #ifdef USE_TEXT_PROPERTIES
1035 if (BUF_INTERVALS (current_buffer
) != 0)
1036 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1037 offset_intervals (current_buffer
, PT
, nchars
);
1039 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
1040 Fset_text_properties (make_number (PT
), make_number (PT
+ nchars
),
1045 int pos
= PT
, pos_byte
= PT_BYTE
;
1047 adjust_point (nchars
+ combined_after_bytes
,
1048 nbytes
+ combined_after_bytes
);
1050 if (combined_after_bytes
)
1051 combine_bytes (pos
+ nchars
, pos_byte
+ nbytes
, combined_after_bytes
);
1053 if (combined_before_bytes
)
1054 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1058 /* Insert the part of the text of STRING, a Lisp object assumed to be
1059 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1060 starting at position POS / POS_BYTE. If the text of STRING has properties,
1061 copy them into the buffer.
1063 It does not work to use `insert' for this, because a GC could happen
1064 before we bcopy the stuff into the buffer, and relocate the string
1065 without insert noticing. */
1068 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
1070 register int pos
, pos_byte
, length
, length_byte
;
1076 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1078 signal_after_change (opoint
, 0, PT
- opoint
);
1082 /* Like `insert_from_string' except that all markers pointing
1083 at the place where the insertion happens are adjusted to point after it. */
1086 insert_from_string_before_markers (string
, pos
, pos_byte
,
1087 length
, length_byte
, inherit
)
1089 register int pos
, pos_byte
, length
, length_byte
;
1095 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1097 signal_after_change (opoint
, 0, PT
- opoint
);
1101 /* Subroutine of the insertion functions above. */
1104 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1105 inherit
, before_markers
)
1107 register int pos
, pos_byte
, nchars
, nbytes
;
1108 int inherit
, before_markers
;
1110 register Lisp_Object temp
;
1111 struct gcpro gcpro1
;
1112 int outgoing_nbytes
= nbytes
;
1113 int combined_before_bytes
, combined_after_bytes
;
1114 int adjusted_nchars
;
1116 Lisp_Object deletion
;
1118 /* Make OUTGOING_NBYTES describe the text
1119 as it will be inserted in this buffer. */
1121 if (NILP (current_buffer
->enable_multibyte_characters
))
1122 outgoing_nbytes
= nchars
;
1123 else if (! STRING_MULTIBYTE (string
))
1125 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
1128 /* Make sure point-max won't overflow after this insertion. */
1129 XSETINT (temp
, outgoing_nbytes
+ Z
);
1130 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1131 error ("Maximum buffer size exceeded");
1134 prepare_to_modify_buffer (PT
, PT
, NULL
);
1137 move_gap_both (PT
, PT_BYTE
);
1138 if (GAP_SIZE
< nbytes
)
1139 make_gap (outgoing_nbytes
- GAP_SIZE
);
1142 /* Copy the string text into the buffer, perhaps converting
1143 between single-byte and multibyte. */
1144 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1145 STRING_MULTIBYTE (string
),
1146 ! NILP (current_buffer
->enable_multibyte_characters
));
1148 /* We have copied text into the gap, but we have not altered
1149 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1150 to these functions and get the same results as we would
1151 have got earlier on. Meanwhile, PT_ADDR does point to
1152 the text that has been stored by copy_text. */
1154 combined_before_bytes
1155 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1156 combined_after_bytes
1157 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1159 /* Record deletion of the surrounding text that combines with
1160 the insertion. This, together with recording the insertion,
1161 will add up to the right stuff in the undo list.
1163 But there is no need to actually delete the combining bytes
1164 from the buffer and reinsert them. */
1166 if (combined_after_bytes
)
1168 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1169 PT
+ combined_after_bytes
,
1170 PT_BYTE
+ combined_after_bytes
, 1);
1172 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1173 PT
+ combined_after_bytes
,
1174 PT_BYTE
+ combined_after_bytes
);
1175 record_delete (PT
, deletion
);
1178 if (combined_before_bytes
)
1180 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1182 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1184 record_delete (PT
- 1, deletion
);
1187 record_insert (PT
- !!combined_before_bytes
,
1188 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1191 GAP_SIZE
-= outgoing_nbytes
;
1195 GPT_BYTE
+= outgoing_nbytes
;
1196 ZV_BYTE
+= outgoing_nbytes
;
1197 Z_BYTE
+= outgoing_nbytes
;
1198 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1200 if (combined_after_bytes
)
1201 move_gap_both (GPT
+ combined_after_bytes
,
1202 GPT_BYTE
+ combined_after_bytes
);
1207 adjust_overlays_for_insert (PT
, nchars
);
1208 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1209 PT_BYTE
+ outgoing_nbytes
,
1210 combined_before_bytes
, combined_after_bytes
,
1213 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1214 offset_intervals (current_buffer
, PT
, nchars
);
1216 intervals
= XSTRING (string
)->intervals
;
1217 /* Get the intervals for the part of the string we are inserting--
1218 not including the combined-before bytes. */
1219 if (nbytes
< STRING_BYTES (XSTRING (string
)))
1220 intervals
= copy_intervals (intervals
, pos
, nchars
);
1222 /* Insert those intervals. */
1223 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1224 current_buffer
, inherit
);
1227 int pos
= PT
, pos_byte
= PT_BYTE
;
1229 adjust_point (nchars
+ combined_after_bytes
,
1230 outgoing_nbytes
+ combined_after_bytes
);
1232 if (combined_after_bytes
)
1233 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1234 combined_after_bytes
);
1236 if (combined_before_bytes
)
1237 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1241 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1242 current buffer. If the text in BUF has properties, they are absorbed
1243 into the current buffer.
1245 It does not work to use `insert' for this, because a malloc could happen
1246 and relocate BUF's text before the bcopy happens. */
1249 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1251 int charpos
, nchars
;
1258 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1259 signal_after_change (opoint
, 0, PT
- opoint
);
1264 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1269 register Lisp_Object temp
, deletion
;
1271 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1272 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1273 int incoming_nbytes
= to_byte
- from_byte
;
1274 int outgoing_nbytes
= incoming_nbytes
;
1275 int combined_before_bytes
, combined_after_bytes
;
1276 int adjusted_nchars
;
1279 /* Make OUTGOING_NBYTES describe the text
1280 as it will be inserted in this buffer. */
1282 if (NILP (current_buffer
->enable_multibyte_characters
))
1283 outgoing_nbytes
= nchars
;
1284 else if (NILP (buf
->enable_multibyte_characters
))
1286 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1289 /* Make sure point-max won't overflow after this insertion. */
1290 XSETINT (temp
, outgoing_nbytes
+ Z
);
1291 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1292 error ("Maximum buffer size exceeded");
1294 prepare_to_modify_buffer (PT
, PT
, NULL
);
1297 move_gap_both (PT
, PT_BYTE
);
1298 if (GAP_SIZE
< outgoing_nbytes
)
1299 make_gap (outgoing_nbytes
- GAP_SIZE
);
1301 if (from
< BUF_GPT (buf
))
1303 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1304 if (chunk
> incoming_nbytes
)
1305 chunk
= incoming_nbytes
;
1306 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1308 ! NILP (buf
->enable_multibyte_characters
),
1309 ! NILP (current_buffer
->enable_multibyte_characters
));
1313 if (chunk
< incoming_nbytes
)
1314 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1315 GPT_ADDR
+ chunk
, incoming_nbytes
- chunk
,
1316 ! NILP (buf
->enable_multibyte_characters
),
1317 ! NILP (current_buffer
->enable_multibyte_characters
));
1319 /* We have copied text into the gap, but we have not altered
1320 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1321 to these functions and get the same results as we would
1322 have got earlier on. Meanwhile, GPT_ADDR does point to
1323 the text that has been stored by copy_text. */
1324 combined_before_bytes
1325 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1326 combined_after_bytes
1327 = count_combining_after (GPT_ADDR
, outgoing_nbytes
,
1330 /* Record deletion of the surrounding text that combines with
1331 the insertion. This, together with recording the insertion,
1332 will add up to the right stuff in the undo list.
1334 But there is no need to actually delete the combining bytes
1335 from the buffer and reinsert them. */
1337 if (combined_after_bytes
)
1339 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1340 PT
+ combined_after_bytes
,
1341 PT_BYTE
+ combined_after_bytes
, 1);
1343 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1344 PT
+ combined_after_bytes
,
1345 PT_BYTE
+ combined_after_bytes
);
1346 record_delete (PT
, deletion
);
1349 if (combined_before_bytes
)
1351 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1353 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1355 record_delete (PT
- 1, deletion
);
1358 record_insert (PT
- !!combined_before_bytes
,
1359 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1362 GAP_SIZE
-= outgoing_nbytes
;
1366 GPT_BYTE
+= outgoing_nbytes
;
1367 ZV_BYTE
+= outgoing_nbytes
;
1368 Z_BYTE
+= outgoing_nbytes
;
1369 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1371 if (combined_after_bytes
)
1372 move_gap_both (GPT
+ combined_after_bytes
,
1373 GPT_BYTE
+ combined_after_bytes
);
1378 adjust_overlays_for_insert (PT
, nchars
);
1379 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1380 PT_BYTE
+ outgoing_nbytes
,
1381 combined_before_bytes
, combined_after_bytes
, 0);
1383 #ifdef USE_TEXT_PROPERTIES
1384 if (BUF_INTERVALS (current_buffer
) != 0)
1385 offset_intervals (current_buffer
, PT
, nchars
);
1388 /* Get the intervals for the part of the string we are inserting--
1389 not including the combined-before bytes. */
1390 intervals
= BUF_INTERVALS (buf
);
1391 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1392 intervals
= copy_intervals (intervals
, from
, nchars
);
1394 /* Insert those intervals. */
1395 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1398 int pos
= PT
, pos_byte
= PT_BYTE
;
1400 adjust_point (nchars
+ combined_after_bytes
,
1401 outgoing_nbytes
+ combined_after_bytes
);
1403 if (combined_after_bytes
)
1404 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1405 combined_after_bytes
);
1407 if (combined_before_bytes
)
1408 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1412 /* This function should be called after moving gap to FROM and before
1413 altering text between FROM and TO. This adjusts various position
1414 keepers and markers as if the text is deleted. Don't forget to
1415 call adjust_after_replace after you actually alter the text. */
1418 adjust_before_replace (from
, from_byte
, to
, to_byte
)
1419 int from
, from_byte
, to
, to_byte
;
1421 Lisp_Object deletion
;
1422 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1426 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1427 record_delete (from
, deletion
);
1428 adjust_overlays_for_delete (from
, to
- from
);
1431 /* This function should be called after altering the text between FROM
1432 and TO to a new text of LEN chars (LEN_BYTE bytes), but before
1433 making the text into buffer contents.
1434 The new text exists just after GPT_ADDR. */
1437 adjust_after_replace (from
, from_byte
, to
, to_byte
, len
, len_byte
, replace
)
1438 int from
, from_byte
, to
, to_byte
, len
, len_byte
, replace
;
1440 int combined_before_bytes
1441 = count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
);
1442 int combined_after_bytes
1443 = count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
);
1444 Lisp_Object deletion
;
1446 if (combined_after_bytes
)
1448 deletion
= make_buffer_string_both (from
, from_byte
,
1449 from
+ combined_after_bytes
,
1450 from_byte
+ combined_after_bytes
, 1);
1452 adjust_markers_for_record_delete (from
, from_byte
,
1453 from
+ combined_after_bytes
,
1454 from_byte
+ combined_after_bytes
);
1455 record_delete (from
, deletion
);
1458 if (combined_before_bytes
)
1460 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1461 from
, from_byte
, 1);
1462 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1464 record_delete (from
- 1, deletion
);
1467 /* Update various buffer positions for the new text. */
1468 GAP_SIZE
-= len_byte
;
1470 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1471 GPT
+= len
; GPT_BYTE
+= len_byte
;
1472 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1474 if (combined_after_bytes
)
1475 move_gap_both (GPT
+ combined_after_bytes
,
1476 GPT_BYTE
+ combined_after_bytes
);
1478 record_insert (from
- !!combined_before_bytes
,
1479 len
- combined_before_bytes
+ !!combined_before_bytes
);
1480 adjust_overlays_for_insert (from
, len
);
1481 adjust_markers_for_insert (from
, from_byte
,
1482 from
+ len
, from_byte
+ len_byte
,
1483 combined_before_bytes
, combined_after_bytes
, 0);
1484 #ifdef USE_TEXT_PROPERTIES
1485 if (BUF_INTERVALS (current_buffer
) != 0)
1486 /* REPLACE zero means that we have not yet adjusted the interval
1487 tree for the text between FROM and TO, thus, we must treat the
1488 new text as a newly inserted text, not as a replacement of
1490 offset_intervals (current_buffer
, from
, len
- (replace
? to
- from
: 0));
1494 int pos
= PT
, pos_byte
= PT_BYTE
;
1497 adjust_point (len
- (to
- from
) + combined_after_bytes
,
1498 len_byte
- (to_byte
- from_byte
) + combined_after_bytes
);
1499 else if (from
== PT
&& combined_before_bytes
)
1500 adjust_point (0, combined_before_bytes
);
1502 if (combined_after_bytes
)
1503 combine_bytes (from
+ len
, from_byte
+ len_byte
, combined_after_bytes
);
1505 if (combined_before_bytes
)
1506 combine_bytes (from
, from_byte
, combined_before_bytes
);
1512 evaporate_overlays (from
);
1516 /* Replace the text from character positions FROM to TO with NEW,
1517 If PREPARE is nonzero, call prepare_to_modify_buffer.
1518 If INHERIT, the newly inserted text should inherit text properties
1519 from the surrounding non-deleted text. */
1521 /* Note that this does not yet handle markers quite right.
1522 Also it needs to record a single undo-entry that does a replacement
1523 rather than a separate delete and insert.
1524 That way, undo will also handle markers properly. */
1527 replace_range (from
, to
, new, prepare
, inherit
, nomarkers
)
1529 int from
, to
, prepare
, inherit
, nomarkers
;
1531 int inschars
= XSTRING (new)->size
;
1532 int insbytes
= STRING_BYTES (XSTRING (new));
1533 int from_byte
, to_byte
;
1534 int nbytes_del
, nchars_del
;
1535 register Lisp_Object temp
;
1536 struct gcpro gcpro1
;
1537 int combined_before_bytes
, combined_after_bytes
;
1538 int adjusted_inschars
;
1540 int outgoing_insbytes
= insbytes
;
1541 Lisp_Object deletion
;
1549 int range_length
= to
- from
;
1550 prepare_to_modify_buffer (from
, to
, &from
);
1551 to
= from
+ range_length
;
1556 /* Make args be valid */
1562 from_byte
= CHAR_TO_BYTE (from
);
1563 to_byte
= CHAR_TO_BYTE (to
);
1565 nchars_del
= to
- from
;
1566 nbytes_del
= to_byte
- from_byte
;
1568 if (nbytes_del
<= 0 && insbytes
== 0)
1571 /* Make OUTGOING_INSBYTES describe the text
1572 as it will be inserted in this buffer. */
1574 if (NILP (current_buffer
->enable_multibyte_characters
))
1575 outgoing_insbytes
= inschars
;
1576 else if (! STRING_MULTIBYTE (new))
1578 = count_size_as_multibyte (XSTRING (new)->data
, insbytes
);
1580 /* Make sure point-max won't overflow after this insertion. */
1581 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1582 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1583 error ("Maximum buffer size exceeded");
1587 /* Make sure the gap is somewhere in or next to what we are deleting. */
1589 gap_right (from
, from_byte
);
1591 gap_left (to
, to_byte
, 0);
1593 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1596 /* Relocate all markers pointing into the new, larger gap
1597 to point at the end of the text before the gap.
1598 Do this before recording the deletion,
1599 so that undo handles this after reinserting the text. */
1600 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1602 record_delete (from
, deletion
);
1604 GAP_SIZE
+= nbytes_del
;
1607 ZV_BYTE
-= nbytes_del
;
1608 Z_BYTE
-= nbytes_del
;
1610 GPT_BYTE
= from_byte
;
1611 *(GPT_ADDR
) = 0; /* Put an anchor. */
1616 if (GPT
- BEG
< beg_unchanged
)
1617 beg_unchanged
= GPT
- BEG
;
1618 if (Z
- GPT
< end_unchanged
)
1619 end_unchanged
= Z
- GPT
;
1621 if (GAP_SIZE
< insbytes
)
1622 make_gap (insbytes
- GAP_SIZE
);
1624 /* Copy the string text into the buffer, perhaps converting
1625 between single-byte and multibyte. */
1626 copy_text (XSTRING (new)->data
, GPT_ADDR
, insbytes
,
1627 STRING_MULTIBYTE (new),
1628 ! NILP (current_buffer
->enable_multibyte_characters
));
1630 /* We have copied text into the gap, but we have not altered
1631 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1632 to these functions and get the same results as we would
1633 have got earlier on. Meanwhile, GPT_ADDR does point to
1634 the text that has been stored by copy_text. */
1636 combined_before_bytes
1637 = count_combining_before (GPT_ADDR
, outgoing_insbytes
, PT
, PT_BYTE
);
1638 combined_after_bytes
1639 = count_combining_after (GPT_ADDR
, outgoing_insbytes
, PT
, PT_BYTE
);
1641 /* Record deletion of the surrounding text that combines with
1642 the insertion. This, together with recording the insertion,
1643 will add up to the right stuff in the undo list.
1645 But there is no need to actually delete the combining bytes
1646 from the buffer and reinsert them. */
1648 if (combined_after_bytes
)
1650 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1651 PT
+ combined_after_bytes
,
1652 PT_BYTE
+ combined_after_bytes
, 1);
1654 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1655 PT
+ combined_after_bytes
,
1656 PT_BYTE
+ combined_after_bytes
);
1657 record_delete (PT
, deletion
);
1660 if (combined_before_bytes
)
1662 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1664 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1666 record_delete (PT
- 1, deletion
);
1669 record_insert (PT
- !!combined_before_bytes
,
1670 inschars
- combined_before_bytes
+ !!combined_before_bytes
);
1672 GAP_SIZE
-= outgoing_insbytes
;
1676 GPT_BYTE
+= outgoing_insbytes
;
1677 ZV_BYTE
+= outgoing_insbytes
;
1678 Z_BYTE
+= outgoing_insbytes
;
1679 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1681 if (combined_after_bytes
)
1682 move_gap_both (GPT
+ combined_after_bytes
,
1683 GPT_BYTE
+ combined_after_bytes
);
1688 /* Adjust the overlay center as needed. This must be done after
1689 adjusting the markers that bound the overlays. */
1690 adjust_overlays_for_delete (from
, nchars_del
);
1691 adjust_overlays_for_insert (from
, inschars
);
1693 adjust_markers_for_insert (from
, from_byte
,
1694 from
+ inschars
, from_byte
+ outgoing_insbytes
,
1695 combined_before_bytes
, combined_after_bytes
, 0);
1697 #ifdef USE_TEXT_PROPERTIES
1698 offset_intervals (current_buffer
, PT
, inschars
- nchars_del
);
1700 /* Get the intervals for the part of the string we are inserting--
1701 not including the combined-before bytes. */
1702 intervals
= XSTRING (new)->intervals
;
1703 /* Insert those intervals. */
1704 graft_intervals_into_buffer (intervals
, from
, inschars
,
1705 current_buffer
, inherit
);
1708 /* Relocate point as if it were a marker. */
1710 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)
1711 + combined_after_bytes
),
1712 (from_byte
+ outgoing_insbytes
1713 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)
1714 + combined_after_bytes
));
1716 if (combined_after_bytes
)
1717 combine_bytes (from
+ inschars
, from_byte
+ outgoing_insbytes
,
1718 combined_after_bytes
);
1720 if (combined_before_bytes
)
1721 combine_bytes (from
, from_byte
, combined_before_bytes
);
1723 if (outgoing_insbytes
== 0)
1724 evaporate_overlays (from
);
1731 signal_after_change (from
, nchars_del
, PT
- from
);
1734 /* Delete characters in current buffer
1735 from FROM up to (but not including) TO.
1736 If TO comes before FROM, we delete nothing. */
1739 del_range (from
, to
)
1740 register int from
, to
;
1742 del_range_1 (from
, to
, 1);
1745 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
1748 del_range_1 (from
, to
, prepare
)
1749 int from
, to
, prepare
;
1751 int from_byte
, to_byte
;
1753 /* Make args be valid */
1764 int range_length
= to
- from
;
1765 prepare_to_modify_buffer (from
, to
, &from
);
1766 to
= from
+ range_length
;
1769 from_byte
= CHAR_TO_BYTE (from
);
1770 to_byte
= CHAR_TO_BYTE (to
);
1772 del_range_2 (from
, from_byte
, to
, to_byte
);
1775 /* Like del_range_1 but args are byte positions, not char positions. */
1778 del_range_byte (from_byte
, to_byte
, prepare
)
1779 int from_byte
, to_byte
, prepare
;
1783 /* Make args be valid */
1784 if (from_byte
< BEGV_BYTE
)
1785 from_byte
= BEGV_BYTE
;
1786 if (to_byte
> ZV_BYTE
)
1789 if (to_byte
<= from_byte
)
1792 from
= BYTE_TO_CHAR (from_byte
);
1793 to
= BYTE_TO_CHAR (to_byte
);
1797 int old_from
= from
, old_to
= Z
- to
;
1798 int range_length
= to
- from
;
1799 prepare_to_modify_buffer (from
, to
, &from
);
1800 to
= from
+ range_length
;
1802 if (old_from
!= from
)
1803 from_byte
= CHAR_TO_BYTE (from
);
1804 if (old_to
== Z
- to
)
1805 to_byte
= CHAR_TO_BYTE (to
);
1808 del_range_2 (from
, from_byte
, to
, to_byte
);
1811 /* Like del_range_1, but positions are specified both as charpos
1815 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
1816 int from
, from_byte
, to
, to_byte
, prepare
;
1818 /* Make args be valid */
1819 if (from_byte
< BEGV_BYTE
)
1820 from_byte
= BEGV_BYTE
;
1821 if (to_byte
> ZV_BYTE
)
1824 if (to_byte
<= from_byte
)
1834 int old_from
= from
, old_to
= Z
- to
;
1835 int range_length
= to
- from
;
1836 prepare_to_modify_buffer (from
, to
, &from
);
1837 to
= from
+ range_length
;
1839 if (old_from
!= from
)
1840 from_byte
= CHAR_TO_BYTE (from
);
1841 if (old_to
== Z
- to
)
1842 to_byte
= CHAR_TO_BYTE (to
);
1845 del_range_2 (from
, from_byte
, to
, to_byte
);
1848 /* Delete a range of text, specified both as character positions
1849 and byte positions. FROM and TO are character positions,
1850 while FROM_BYTE and TO_BYTE are byte positions. */
1853 del_range_2 (from
, from_byte
, to
, to_byte
)
1854 int from
, from_byte
, to
, to_byte
;
1856 register int nbytes_del
, nchars_del
;
1857 int combined_after_bytes
;
1858 Lisp_Object deletion
;
1863 nchars_del
= to
- from
;
1864 nbytes_del
= to_byte
- from_byte
;
1866 /* Make sure the gap is somewhere in or next to what we are deleting. */
1868 gap_right (from
, from_byte
);
1870 gap_left (to
, to_byte
, 0);
1872 combined_after_bytes
1873 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1874 ZV_BYTE
- to_byte
, from
, from_byte
);
1875 if (combined_after_bytes
)
1877 from_byte_1
= from_byte
;
1878 DEC_POS (from_byte_1
);
1881 from_byte_1
= from_byte
;
1884 = make_buffer_string_both (from
- !!combined_after_bytes
,
1886 to
+ combined_after_bytes
,
1887 to_byte
+ combined_after_bytes
, 1);
1888 if (combined_after_bytes
)
1889 /* COMBINED_AFTER_BYTES nonzero means that the above code moved
1890 the gap. We must move the gap again to a proper place. */
1891 move_gap_both (from
, from_byte
);
1893 /* Relocate all markers pointing into the new, larger gap
1894 to point at the end of the text before the gap.
1895 Do this before recording the deletion,
1896 so that undo handles this after reinserting the text. */
1897 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1898 if (combined_after_bytes
)
1900 /* Adjust markers for the phony deletion
1901 that we are about to call record_undo for. */
1903 /* Here we delete the markers that formerly
1904 pointed at TO ... TO + COMBINED_AFTER_BYTES.
1905 But because of the call to adjust_markers_for_delete, above,
1906 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
1907 adjust_markers_for_record_delete (from
, from_byte
,
1908 from
+ combined_after_bytes
,
1909 from_byte
+ combined_after_bytes
);
1911 adjust_markers_for_record_delete (from
- 1, from_byte_1
,
1914 record_delete (from
- !!combined_after_bytes
, deletion
);
1917 /* Relocate point as if it were a marker. */
1919 adjust_point (from
- (PT
< to
? PT
: to
),
1920 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1922 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1923 offset_intervals (current_buffer
, from
, - nchars_del
);
1925 /* Adjust the overlay center as needed. This must be done after
1926 adjusting the markers that bound the overlays. */
1927 adjust_overlays_for_delete (from
, nchars_del
);
1929 GAP_SIZE
+= nbytes_del
;
1930 ZV_BYTE
-= nbytes_del
;
1931 Z_BYTE
-= nbytes_del
;
1935 GPT_BYTE
= from_byte
;
1937 if (combined_after_bytes
)
1938 move_gap_both (GPT
+ combined_after_bytes
,
1939 GPT_BYTE
+ combined_after_bytes
);
1941 *(GPT_ADDR
) = 0; /* Put an anchor. */
1946 if (GPT
- BEG
< beg_unchanged
)
1947 beg_unchanged
= GPT
- BEG
;
1948 if (Z
- GPT
< end_unchanged
)
1949 end_unchanged
= Z
- GPT
;
1951 if (combined_after_bytes
)
1953 combine_bytes (from
, from_byte
, combined_after_bytes
);
1955 record_insert (GPT
- 1, 1);
1960 evaporate_overlays (from
);
1961 signal_after_change (from
, nchars_del
, 0);
1964 /* Call this if you're about to change the region of BUFFER from
1965 character positions START to END. This checks the read-only
1966 properties of the region, calls the necessary modification hooks,
1967 and warns the next redisplay that it should pay attention to that
1971 modify_region (buffer
, start
, end
)
1972 struct buffer
*buffer
;
1975 struct buffer
*old_buffer
= current_buffer
;
1977 if (buffer
!= old_buffer
)
1978 set_buffer_internal (buffer
);
1980 prepare_to_modify_buffer (start
, end
, NULL
);
1982 if (start
- 1 < beg_unchanged
1983 || (unchanged_modified
== MODIFF
1984 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
1985 beg_unchanged
= start
- 1;
1986 if (Z
- end
< end_unchanged
1987 || (unchanged_modified
== MODIFF
1988 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
1989 end_unchanged
= Z
- end
;
1991 if (MODIFF
<= SAVE_MODIFF
)
1992 record_first_change ();
1995 buffer
->point_before_scroll
= Qnil
;
1997 if (buffer
!= old_buffer
)
1998 set_buffer_internal (old_buffer
);
2001 /* Check that it is okay to modify the buffer between START and END,
2002 which are char positions.
2004 Run the before-change-function, if any. If intervals are in use,
2005 verify that the text to be modified is not read-only, and call
2006 any modification properties the text may have.
2008 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2009 by holding its value temporarily in a marker. */
2012 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
2016 if (!NILP (current_buffer
->read_only
))
2017 Fbarf_if_buffer_read_only ();
2019 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2020 if (BUF_INTERVALS (current_buffer
) != 0)
2024 Lisp_Object preserve_marker
;
2025 struct gcpro gcpro1
;
2026 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2027 GCPRO1 (preserve_marker
);
2028 verify_interval_modification (current_buffer
, start
, end
);
2029 *preserve_ptr
= marker_position (preserve_marker
);
2030 unchain_marker (preserve_marker
);
2034 verify_interval_modification (current_buffer
, start
, end
);
2037 #ifdef CLASH_DETECTION
2038 if (!NILP (current_buffer
->file_truename
)
2039 /* Make binding buffer-file-name to nil effective. */
2040 && !NILP (current_buffer
->filename
)
2041 && SAVE_MODIFF
>= MODIFF
)
2042 lock_file (current_buffer
->file_truename
);
2044 /* At least warn if this file has changed on disk since it was visited. */
2045 if (!NILP (current_buffer
->filename
)
2046 && SAVE_MODIFF
>= MODIFF
2047 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2048 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
2049 call1 (intern ("ask-user-about-supersession-threat"),
2050 current_buffer
->filename
);
2051 #endif /* not CLASH_DETECTION */
2053 signal_before_change (start
, end
, preserve_ptr
);
2055 if (current_buffer
->newline_cache
)
2056 invalidate_region_cache (current_buffer
,
2057 current_buffer
->newline_cache
,
2058 start
- BEG
, Z
- end
);
2059 if (current_buffer
->width_run_cache
)
2060 invalidate_region_cache (current_buffer
,
2061 current_buffer
->width_run_cache
,
2062 start
- BEG
, Z
- end
);
2064 Vdeactivate_mark
= Qt
;
2067 /* These macros work with an argument named `preserve_ptr'
2068 and a local variable named `preserve_marker'. */
2070 #define PRESERVE_VALUE \
2071 if (preserve_ptr && NILP (preserve_marker)) \
2072 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2074 #define RESTORE_VALUE \
2075 if (! NILP (preserve_marker)) \
2077 *preserve_ptr = marker_position (preserve_marker); \
2078 unchain_marker (preserve_marker); \
2081 #define PRESERVE_START_END \
2082 if (NILP (start_marker)) \
2083 start_marker = Fcopy_marker (start, Qnil); \
2084 if (NILP (end_marker)) \
2085 end_marker = Fcopy_marker (end, Qnil);
2087 #define FETCH_START \
2088 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2091 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2093 /* Signal a change to the buffer immediately before it happens.
2094 START_INT and END_INT are the bounds of the text to be changed.
2096 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2097 by holding its value temporarily in a marker. */
2100 signal_before_change (start_int
, end_int
, preserve_ptr
)
2101 int start_int
, end_int
;
2104 Lisp_Object start
, end
;
2105 Lisp_Object start_marker
, end_marker
;
2106 Lisp_Object preserve_marker
;
2107 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2109 start
= make_number (start_int
);
2110 end
= make_number (end_int
);
2111 preserve_marker
= Qnil
;
2112 start_marker
= Qnil
;
2114 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2116 /* If buffer is unmodified, run a special hook for that case. */
2117 if (SAVE_MODIFF
>= MODIFF
2118 && !NILP (Vfirst_change_hook
)
2119 && !NILP (Vrun_hooks
))
2123 call1 (Vrun_hooks
, Qfirst_change_hook
);
2126 /* Run the before-change-function if any.
2127 We don't bother "binding" this variable to nil
2128 because it is obsolete anyway and new code should not use it. */
2129 if (!NILP (Vbefore_change_function
))
2133 call2 (Vbefore_change_function
, FETCH_START
, FETCH_END
);
2136 /* Now run the before-change-functions if any. */
2137 if (!NILP (Vbefore_change_functions
))
2139 Lisp_Object args
[3];
2140 Lisp_Object before_change_functions
;
2141 Lisp_Object after_change_functions
;
2142 struct gcpro gcpro1
, gcpro2
;
2147 /* "Bind" before-change-functions and after-change-functions
2148 to nil--but in a way that errors don't know about.
2149 That way, if there's an error in them, they will stay nil. */
2150 before_change_functions
= Vbefore_change_functions
;
2151 after_change_functions
= Vafter_change_functions
;
2152 Vbefore_change_functions
= Qnil
;
2153 Vafter_change_functions
= Qnil
;
2154 GCPRO2 (before_change_functions
, after_change_functions
);
2156 /* Actually run the hook functions. */
2157 args
[0] = Qbefore_change_functions
;
2158 args
[1] = FETCH_START
;
2159 args
[2] = FETCH_END
;
2160 run_hook_list_with_args (before_change_functions
, 3, args
);
2162 /* "Unbind" the variables we "bound" to nil. */
2163 Vbefore_change_functions
= before_change_functions
;
2164 Vafter_change_functions
= after_change_functions
;
2168 if (!NILP (current_buffer
->overlays_before
)
2169 || !NILP (current_buffer
->overlays_after
))
2172 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2173 FETCH_START
, FETCH_END
, Qnil
);
2176 if (! NILP (start_marker
))
2177 free_marker (start_marker
);
2178 if (! NILP (end_marker
))
2179 free_marker (end_marker
);
2184 /* Signal a change immediately after it happens.
2185 CHARPOS is the character position of the start of the changed text.
2186 LENDEL is the number of characters of the text before the change.
2187 (Not the whole buffer; just the part that was changed.)
2188 LENINS is the number of characters in that part of the text
2189 after the change. */
2192 signal_after_change (charpos
, lendel
, lenins
)
2193 int charpos
, lendel
, lenins
;
2195 /* If we are deferring calls to the after-change functions
2196 and there are no before-change functions,
2197 just record the args that we were going to use. */
2198 if (! NILP (Vcombine_after_change_calls
)
2199 && NILP (Vbefore_change_function
) && NILP (Vbefore_change_functions
)
2200 && NILP (current_buffer
->overlays_before
)
2201 && NILP (current_buffer
->overlays_after
))
2205 if (!NILP (combine_after_change_list
)
2206 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2207 Fcombine_after_change_execute ();
2209 elt
= Fcons (make_number (charpos
- BEG
),
2210 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2211 Fcons (make_number (lenins
- lendel
), Qnil
)));
2212 combine_after_change_list
2213 = Fcons (elt
, combine_after_change_list
);
2214 combine_after_change_buffer
= Fcurrent_buffer ();
2219 if (!NILP (combine_after_change_list
))
2220 Fcombine_after_change_execute ();
2222 /* Run the after-change-function if any.
2223 We don't bother "binding" this variable to nil
2224 because it is obsolete anyway and new code should not use it. */
2225 if (!NILP (Vafter_change_function
))
2226 call3 (Vafter_change_function
,
2227 make_number (charpos
), make_number (charpos
+ lenins
),
2228 make_number (lendel
));
2230 if (!NILP (Vafter_change_functions
))
2232 Lisp_Object args
[4];
2233 Lisp_Object before_change_functions
;
2234 Lisp_Object after_change_functions
;
2235 struct gcpro gcpro1
, gcpro2
;
2237 /* "Bind" before-change-functions and after-change-functions
2238 to nil--but in a way that errors don't know about.
2239 That way, if there's an error in them, they will stay nil. */
2240 before_change_functions
= Vbefore_change_functions
;
2241 after_change_functions
= Vafter_change_functions
;
2242 Vbefore_change_functions
= Qnil
;
2243 Vafter_change_functions
= Qnil
;
2244 GCPRO2 (before_change_functions
, after_change_functions
);
2246 /* Actually run the hook functions. */
2247 args
[0] = Qafter_change_functions
;
2248 XSETFASTINT (args
[1], charpos
);
2249 XSETFASTINT (args
[2], charpos
+ lenins
);
2250 XSETFASTINT (args
[3], lendel
);
2251 run_hook_list_with_args (after_change_functions
,
2254 /* "Unbind" the variables we "bound" to nil. */
2255 Vbefore_change_functions
= before_change_functions
;
2256 Vafter_change_functions
= after_change_functions
;
2260 if (!NILP (current_buffer
->overlays_before
)
2261 || !NILP (current_buffer
->overlays_after
))
2262 report_overlay_modification (make_number (charpos
),
2263 make_number (charpos
+ lenins
),
2265 make_number (charpos
),
2266 make_number (charpos
+ lenins
),
2267 make_number (lendel
));
2269 /* After an insertion, call the text properties
2270 insert-behind-hooks or insert-in-front-hooks. */
2272 report_interval_modification (charpos
, charpos
+ lenins
);
2276 Fcombine_after_change_execute_1 (val
)
2279 Vcombine_after_change_calls
= val
;
2283 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2284 Scombine_after_change_execute
, 0, 0, 0,
2285 "This function is for use internally in `combine-after-change-calls'.")
2288 register Lisp_Object val
;
2289 int count
= specpdl_ptr
- specpdl
;
2290 int beg
, end
, change
;
2294 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2296 Fset_buffer (combine_after_change_buffer
);
2298 /* # chars unchanged at beginning of buffer. */
2300 /* # chars unchanged at end of buffer. */
2302 /* Total amount of insertion (negative for deletion). */
2305 /* Scan the various individual changes,
2306 accumulating the range info in BEG, END and CHANGE. */
2307 for (tail
= combine_after_change_list
; CONSP (tail
);
2308 tail
= XCONS (tail
)->cdr
)
2311 int thisbeg
, thisend
, thischange
;
2313 /* Extract the info from the next element. */
2314 elt
= XCONS (tail
)->car
;
2317 thisbeg
= XINT (XCONS (elt
)->car
);
2319 elt
= XCONS (elt
)->cdr
;
2322 thisend
= XINT (XCONS (elt
)->car
);
2324 elt
= XCONS (elt
)->cdr
;
2327 thischange
= XINT (XCONS (elt
)->car
);
2329 /* Merge this range into the accumulated range. */
2330 change
+= thischange
;
2337 /* Get the current start and end positions of the range
2338 that was changed. */
2342 /* We are about to handle these, so discard them. */
2343 combine_after_change_list
= Qnil
;
2345 /* Now run the after-change functions for real.
2346 Turn off the flag that defers them. */
2347 record_unwind_protect (Fcombine_after_change_execute_1
,
2348 Vcombine_after_change_calls
);
2349 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2351 return unbind_to (count
, val
);
2356 staticpro (&combine_after_change_list
);
2357 combine_after_change_list
= Qnil
;
2359 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2360 "Non-nil means enable debugging checks for invalid marker positions.");
2361 check_markers_debug_flag
= 0;
2362 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2363 "Used internally by the `combine-after-change-calls' macro.");
2364 Vcombine_after_change_calls
= Qnil
;
2366 defsubr (&Scombine_after_change_execute
);