1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 86,93,94,95,97,98, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "intervals.h"
29 #include "blockinput.h"
30 #include "region-cache.h"
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));
42 void adjust_markers_for_delete
P_ ((int, int, int, int));
43 static void adjust_markers_for_replace
P_ ((int, int, 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 Lisp_Object Qinhibit_modification_hooks
;
70 /* Check all markers in the current buffer, looking for something invalid. */
72 static int check_markers_debug_flag
;
74 #define CHECK_MARKERS() \
75 if (check_markers_debug_flag) \
82 register Lisp_Object tail
;
83 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
85 tail
= BUF_MARKERS (current_buffer
);
89 if (XMARKER (tail
)->buffer
->text
!= current_buffer
->text
)
91 if (XMARKER (tail
)->charpos
> Z
)
93 if (XMARKER (tail
)->bytepos
> Z_BYTE
)
95 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (XMARKER (tail
)->bytepos
)))
98 tail
= XMARKER (tail
)->chain
;
102 /* Move gap to position CHARPOS.
103 Note that this can quit! */
109 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
112 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
113 Note that this can quit! */
116 move_gap_both (charpos
, bytepos
)
117 int charpos
, bytepos
;
119 if (bytepos
< GPT_BYTE
)
120 gap_left (charpos
, bytepos
, 0);
121 else if (bytepos
> GPT_BYTE
)
122 gap_right (charpos
, bytepos
);
125 /* Move the gap to a position less than the current GPT.
126 BYTEPOS describes the new position as a byte position,
127 and CHARPOS is the corresponding char position.
128 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
131 gap_left (charpos
, bytepos
, newgap
)
132 register int charpos
, bytepos
;
135 register unsigned char *to
, *from
;
140 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
147 /* Now copy the characters. To move the gap down,
148 copy characters up. */
152 /* I gets number of characters left to copy. */
153 i
= new_s1
- bytepos
;
156 /* If a quit is requested, stop copying now.
157 Change BYTEPOS to be where we have actually moved the gap to. */
161 charpos
= BYTE_TO_CHAR (bytepos
);
164 /* Move at most 32000 chars before checking again for a quit. */
169 /* bcopy is safe if the two areas of memory do not overlap
170 or on systems where bcopy is always safe for moving upward. */
171 && (BCOPY_UPWARD_SAFE
172 || to
- from
>= 128))
174 /* If overlap is not safe, avoid it by not moving too many
175 characters at once. */
176 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
191 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
192 BYTEPOS is where the loop above stopped, which may be what was specified
193 or may be where a quit was detected. */
194 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
197 if (bytepos
< charpos
)
199 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
203 /* Move the gap to a position greater than than the current GPT.
204 BYTEPOS describes the new position as a byte position,
205 and CHARPOS is the corresponding char position. */
208 gap_right (charpos
, bytepos
)
209 register int charpos
, bytepos
;
211 register unsigned char *to
, *from
;
215 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
222 /* Now copy the characters. To move the gap up,
223 copy characters down. */
227 /* I gets number of characters left to copy. */
228 i
= bytepos
- new_s1
;
231 /* If a quit is requested, stop copying now.
232 Change BYTEPOS to be where we have actually moved the gap to. */
236 charpos
= BYTE_TO_CHAR (bytepos
);
239 /* Move at most 32000 chars before checking again for a quit. */
244 /* bcopy is safe if the two areas of memory do not overlap
245 or on systems where bcopy is always safe for moving downward. */
246 && (BCOPY_DOWNWARD_SAFE
247 || from
- to
>= 128))
249 /* If overlap is not safe, avoid it by not moving too many
250 characters at once. */
251 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
266 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
270 if (bytepos
< charpos
)
272 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
276 /* Add AMOUNT to the byte position of every marker in the current buffer
277 whose current byte position is between FROM (exclusive) and TO (inclusive).
279 Also, any markers past the outside of that interval, in the direction
280 of adjustment, are first moved back to the near end of the interval
281 and then adjusted by AMOUNT.
283 When the latter adjustment is done, if AMOUNT is negative,
284 we record the adjustment for undo. (This case happens only for
287 The markers' character positions are not altered,
288 because gap motion does not affect character positions. */
290 int adjust_markers_test
;
293 adjust_markers_gap_motion (from
, to
, amount
)
294 register int from
, to
, amount
;
296 /* Now that a marker has a bytepos, not counting the gap,
297 nothing needs to be done here. */
300 register struct Lisp_Marker
*m
;
303 marker
= BUF_MARKERS (current_buffer
);
305 while (!NILP (marker
))
307 m
= XMARKER (marker
);
311 if (mpos
> to
&& mpos
< to
+ amount
)
313 if (adjust_markers_test
)
320 /* Here's the case where a marker is inside text being deleted.
321 AMOUNT can be negative for gap motion, too,
322 but then this range contains no markers. */
323 if (mpos
> from
+ amount
&& mpos
<= from
)
325 if (adjust_markers_test
)
327 mpos
= from
+ amount
;
330 if (mpos
> from
&& mpos
<= to
)
338 /* Adjust all markers for a deletion
339 whose range in bytes is FROM_BYTE to TO_BYTE.
340 The range in charpos is FROM to TO.
342 This function assumes that the gap is adjacent to
343 or inside of the range being deleted. */
346 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
)
347 register int from
, from_byte
, to
, to_byte
;
350 register struct Lisp_Marker
*m
;
351 register int charpos
;
353 marker
= BUF_MARKERS (current_buffer
);
355 while (!NILP (marker
))
357 m
= XMARKER (marker
);
358 charpos
= m
->charpos
;
363 /* If the marker is after the deletion,
364 relocate by number of chars / bytes deleted. */
367 m
->charpos
-= to
- from
;
368 m
->bytepos
-= to_byte
- from_byte
;
370 /* Here's the case where a marker is inside text being deleted. */
371 else if (charpos
> from
)
373 if (! m
->insertion_type
)
374 /* Normal markers will end up at the beginning of the
375 re-inserted text after undoing a deletion, and must be
376 adjusted to move them to the correct place. */
377 record_marker_adjustment (marker
, from
- charpos
);
378 else if (charpos
< to
)
379 /* Before-insertion markers will automatically move forward
380 upon re-inserting the deleted text, so we have to arrange
381 for them to move backward to the correct position. */
382 record_marker_adjustment (marker
, charpos
- to
);
385 m
->bytepos
= from_byte
;
387 /* Here's the case where a before-insertion marker is immediately
388 before the deleted region. */
389 else if (charpos
== from
&& m
->insertion_type
)
391 /* Undoing the change uses normal insertion, which will
392 incorrectly make MARKER move forward, so we arrange for it
393 to then move backward to the correct place at the beginning
394 of the deleted region. */
395 record_marker_adjustment (marker
, to
- from
);
403 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
404 to TO / TO_BYTE. We have to relocate the charpos of every marker
405 that points after the insertion (but not their bytepos).
407 When a marker points at the insertion point,
408 we advance it if either its insertion-type is t
409 or BEFORE_MARKERS is true. */
412 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
, before_markers
)
413 register int from
, from_byte
, to
, to_byte
;
418 int nchars
= to
- from
;
419 int nbytes
= to_byte
- from_byte
;
421 marker
= BUF_MARKERS (current_buffer
);
423 while (!NILP (marker
))
425 register struct Lisp_Marker
*m
= XMARKER (marker
);
427 /* In a single-byte buffer, a marker's two positions must be
431 if (m
->charpos
!= m
->bytepos
)
435 if (m
->bytepos
== from_byte
)
437 if (m
->insertion_type
|| before_markers
)
439 m
->bytepos
= to_byte
;
441 if (m
->insertion_type
)
445 else if (m
->bytepos
> from_byte
)
447 m
->bytepos
+= nbytes
;
448 m
->charpos
+= nchars
;
454 /* Adjusting only markers whose insertion-type is t may result in
455 disordered overlays in the slot `overlays_before'. */
457 fix_overlays_before (current_buffer
, from
, to
);
460 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
462 This is used only when the value of point changes due to an insert
463 or delete; it does not represent a conceptual change in point as a
464 marker. In particular, point is not crossing any interval
465 boundaries, so there's no need to use the usual SET_PT macro. In
466 fact it would be incorrect to do so, because either the old or the
467 new value of point is out of sync with the current set of
471 adjust_point (nchars
, nbytes
)
474 BUF_PT (current_buffer
) += nchars
;
475 BUF_PT_BYTE (current_buffer
) += nbytes
;
477 /* In a single-byte buffer, the two positions must be equal. */
483 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
484 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
485 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
489 adjust_markers_for_replace (from
, from_byte
, old_chars
, old_bytes
,
490 new_chars
, new_bytes
)
491 int from
, from_byte
, old_chars
, old_bytes
, new_chars
, new_bytes
;
493 Lisp_Object marker
= BUF_MARKERS (current_buffer
);
494 int prev_to_byte
= from_byte
+ old_bytes
;
495 int diff_chars
= new_chars
- old_chars
;
496 int diff_bytes
= new_bytes
- old_bytes
;
498 while (!NILP (marker
))
500 register struct Lisp_Marker
*m
= XMARKER (marker
);
502 if (m
->bytepos
>= prev_to_byte
)
504 m
->charpos
+= diff_chars
;
505 m
->bytepos
+= diff_bytes
;
507 else if (m
->bytepos
> from_byte
)
510 m
->bytepos
= from_byte
;
520 /* Make the gap NBYTES_ADDED bytes longer. */
523 make_gap_larger (nbytes_added
)
528 int real_gap_loc_byte
;
531 /* If we have to get more space, get enough to last a while. */
532 nbytes_added
+= 2000;
534 /* Don't allow a buffer size that won't fit in an int
535 even if it will fit in a Lisp integer.
536 That won't work because so many places use `int'.
538 Make sure we don't introduce overflows in the calculation. */
540 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
541 >= (((EMACS_INT
) 1 << (min (VALBITS
, BITS_PER_INT
) - 1)) - 1
543 error ("Buffer exceeds maximum size");
545 enlarge_buffer_text (current_buffer
, nbytes_added
);
547 /* Prevent quitting in move_gap. */
552 real_gap_loc_byte
= GPT_BYTE
;
553 old_gap_size
= GAP_SIZE
;
555 /* Call the newly allocated space a gap at the end of the whole space. */
557 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
558 GAP_SIZE
= nbytes_added
;
560 /* Move the new gap down to be consecutive with the end of the old one.
561 This adjusts the markers properly too. */
562 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
564 /* Now combine the two into one large gap. */
565 GAP_SIZE
+= old_gap_size
;
567 GPT_BYTE
= real_gap_loc_byte
;
576 /* Make the gap NBYTES_REMOVED bytes shorted. */
579 make_gap_smaller (nbytes_removed
)
584 int real_gap_loc_byte
;
587 int real_beg_unchanged
;
590 /* Make sure the gap is at least 20 bytes. */
591 if (GAP_SIZE
- nbytes_removed
< 20)
592 nbytes_removed
= GAP_SIZE
- 20;
594 /* Prevent quitting in move_gap. */
599 real_gap_loc_byte
= GPT_BYTE
;
600 new_gap_size
= GAP_SIZE
- nbytes_removed
;
602 real_Z_byte
= Z_BYTE
;
603 real_beg_unchanged
= BEG_UNCHANGED
;
605 /* Pretend that the last unwanted part of the gap is the entire gap,
606 and that the first desired part of the gap is part of the buffer
608 bzero (GPT_ADDR
, new_gap_size
);
610 GPT_BYTE
+= new_gap_size
;
612 Z_BYTE
+= new_gap_size
;
613 GAP_SIZE
= nbytes_removed
;
615 /* Move the unwanted pretend gap to the end of the buffer. This
616 adjusts the markers properly too. */
617 gap_right (Z
, Z_BYTE
);
619 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
621 /* Now restore the desired gap. */
622 GAP_SIZE
= new_gap_size
;
624 GPT_BYTE
= real_gap_loc_byte
;
626 Z_BYTE
= real_Z_byte
;
627 BEG_UNCHANGED
= real_beg_unchanged
;
636 make_gap (nbytes_added
)
639 if (nbytes_added
>= 0)
640 make_gap_larger (nbytes_added
);
641 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
643 make_gap_smaller (-nbytes_added
);
647 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
648 FROM_MULTIBYTE says whether the incoming text is multibyte.
649 TO_MULTIBYTE says whether to store the text as multibyte.
650 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
652 Return the number of bytes stored at TO_ADDR. */
655 copy_text (from_addr
, to_addr
, nbytes
,
656 from_multibyte
, to_multibyte
)
657 unsigned char *from_addr
;
658 unsigned char *to_addr
;
660 int from_multibyte
, to_multibyte
;
662 if (from_multibyte
== to_multibyte
)
664 bcopy (from_addr
, to_addr
, nbytes
);
667 else if (from_multibyte
)
670 int bytes_left
= nbytes
;
671 Lisp_Object tbl
= Qnil
;
673 /* We set the variable tbl to the reverse table of
674 Vnonascii_translation_table in advance. */
675 if (CHAR_TABLE_P (Vnonascii_translation_table
))
677 tbl
= Fchar_table_extra_slot (Vnonascii_translation_table
,
679 if (!CHAR_TABLE_P (tbl
))
683 /* Convert multibyte to single byte. */
684 while (bytes_left
> 0)
687 c
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
688 if (!SINGLE_BYTE_CHAR_P (c
))
689 c
= multibyte_char_to_unibyte (c
, tbl
);
691 from_addr
+= thislen
;
692 bytes_left
-= thislen
;
699 unsigned char *initial_to_addr
= to_addr
;
701 /* Convert single-byte to multibyte. */
704 int c
= *from_addr
++;
708 c
= unibyte_char_to_multibyte (c
);
709 to_addr
+= CHAR_STRING (c
, to_addr
);
713 /* Special case for speed. */
714 *to_addr
++ = c
, nbytes
--;
716 return to_addr
- initial_to_addr
;
720 /* Return the number of bytes it would take
721 to convert some single-byte text to multibyte.
722 The single-byte text consists of NBYTES bytes at PTR. */
725 count_size_as_multibyte (ptr
, nbytes
)
730 int outgoing_nbytes
= 0;
732 for (i
= 0; i
< nbytes
; i
++)
734 unsigned int c
= *ptr
++;
740 c
= unibyte_char_to_multibyte (c
);
741 outgoing_nbytes
+= CHAR_BYTES (c
);
745 return outgoing_nbytes
;
748 /* Insert a string of specified length before point.
749 This function judges multibyteness based on
750 enable_multibyte_characters in the current buffer;
751 it never converts between single-byte and multibyte.
753 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
754 prepare_to_modify_buffer could relocate the text. */
757 insert (string
, nbytes
)
758 register unsigned char *string
;
764 insert_1 (string
, nbytes
, 0, 1, 0);
765 signal_after_change (opoint
, 0, PT
- opoint
);
766 update_compositions (opoint
, PT
, CHECK_BORDER
);
770 /* Likewise, but inherit text properties from neighboring characters. */
773 insert_and_inherit (string
, nbytes
)
774 register unsigned char *string
;
780 insert_1 (string
, nbytes
, 1, 1, 0);
781 signal_after_change (opoint
, 0, PT
- opoint
);
782 update_compositions (opoint
, PT
, CHECK_BORDER
);
786 /* Insert the character C before point. Do not inherit text properties. */
792 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
795 if (! NILP (current_buffer
->enable_multibyte_characters
))
796 len
= CHAR_STRING (c
, str
);
806 /* Insert the null-terminated string S before point. */
812 insert (s
, strlen (s
));
815 /* Like `insert' except that all markers pointing at the place where
816 the insertion happens are adjusted to point after it.
817 Don't use this function to insert part of a Lisp string,
818 since gc could happen and relocate it. */
821 insert_before_markers (string
, nbytes
)
822 unsigned char *string
;
829 insert_1 (string
, nbytes
, 0, 1, 1);
830 signal_after_change (opoint
, 0, PT
- opoint
);
831 update_compositions (opoint
, PT
, CHECK_BORDER
);
835 /* Likewise, but inherit text properties from neighboring characters. */
838 insert_before_markers_and_inherit (string
, nbytes
)
839 unsigned char *string
;
846 insert_1 (string
, nbytes
, 1, 1, 1);
847 signal_after_change (opoint
, 0, PT
- opoint
);
848 update_compositions (opoint
, PT
, CHECK_BORDER
);
852 /* Subroutine used by the insert functions above. */
855 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
856 register unsigned char *string
;
858 int inherit
, prepare
, before_markers
;
860 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
861 inherit
, prepare
, before_markers
);
865 #ifdef BYTE_COMBINING_DEBUG
867 /* See if the bytes before POS/POS_BYTE combine with bytes
868 at the start of STRING to form a single character.
869 If so, return the number of bytes at the start of STRING
870 which combine in this way. Otherwise, return 0. */
873 count_combining_before (string
, length
, pos
, pos_byte
)
874 unsigned char *string
;
878 int len
, combining_bytes
;
881 if (NILP (current_buffer
->enable_multibyte_characters
))
884 /* At first, we can exclude the following cases:
885 (1) STRING[0] can't be a following byte of multibyte sequence.
886 (2) POS is the start of the current buffer.
887 (3) A character before POS is not a multibyte character. */
888 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
890 if (pos_byte
== BEG_BYTE
) /* case (2) */
893 p
= BYTE_POS_ADDR (pos_byte
- 1);
894 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
895 if (! BASE_LEADING_CODE_P (*p
)) /* case (3) */
898 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
899 if (combining_bytes
<= 0)
900 /* The character preceding POS is, complete and no room for
901 combining bytes (combining_bytes == 0), or an independent 8-bit
902 character (combining_bytes < 0). */
905 /* We have a combination situation. Count the bytes at STRING that
908 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
911 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
914 /* See if the bytes after POS/POS_BYTE combine with bytes
915 at the end of STRING to form a single character.
916 If so, return the number of bytes after POS/POS_BYTE
917 which combine in this way. Otherwise, return 0. */
920 count_combining_after (string
, length
, pos
, pos_byte
)
921 unsigned char *string
;
925 int opos_byte
= pos_byte
;
930 if (NILP (current_buffer
->enable_multibyte_characters
))
933 /* At first, we can exclude the following cases:
934 (1) The last byte of STRING is an ASCII.
935 (2) POS is the last of the current buffer.
936 (3) A character at POS can't be a following byte of multibyte
938 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1])) /* case (1) */
940 if (pos_byte
== Z_BYTE
) /* case (2) */
942 bufp
= BYTE_POS_ADDR (pos_byte
);
943 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
947 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
953 /* All characters in STRING are not character head. We must
954 check also preceding bytes at POS. We are sure that the gap
956 unsigned char *p
= BEG_ADDR
;
958 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
960 if (i
< 0 || !BASE_LEADING_CODE_P (p
[i
]))
963 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
964 return (bytes
<= pos_byte
- 1 - i
+ length
966 : bytes
- (pos_byte
- 1 - i
+ length
));
968 if (!BASE_LEADING_CODE_P (string
[i
]))
971 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
973 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
975 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
981 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
982 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
983 are the same as in insert_1. */
986 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
987 register unsigned char *string
;
988 register int nchars
, nbytes
;
989 int inherit
, prepare
, before_markers
;
994 if (NILP (current_buffer
->enable_multibyte_characters
))
998 /* Do this before moving and increasing the gap,
999 because the before-change hooks might move the gap
1000 or make it smaller. */
1001 prepare_to_modify_buffer (PT
, PT
, NULL
);
1004 move_gap_both (PT
, PT_BYTE
);
1005 if (GAP_SIZE
< nbytes
)
1006 make_gap (nbytes
- GAP_SIZE
);
1008 #ifdef BYTE_COMBINING_DEBUG
1009 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
1010 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
1014 /* Record deletion of the surrounding text that combines with
1015 the insertion. This, together with recording the insertion,
1016 will add up to the right stuff in the undo list. */
1017 record_insert (PT
, nchars
);
1020 bcopy (string
, GPT_ADDR
, nbytes
);
1029 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1034 adjust_overlays_for_insert (PT
, nchars
);
1035 adjust_markers_for_insert (PT
, PT_BYTE
,
1036 PT
+ nchars
, PT_BYTE
+ nbytes
,
1039 if (BUF_INTERVALS (current_buffer
) != 0)
1040 offset_intervals (current_buffer
, PT
, nchars
);
1042 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
1043 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
1046 adjust_point (nchars
, nbytes
);
1051 /* Insert the part of the text of STRING, a Lisp object assumed to be
1052 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1053 starting at position POS / POS_BYTE. If the text of STRING has properties,
1054 copy them into the buffer.
1056 It does not work to use `insert' for this, because a GC could happen
1057 before we bcopy the stuff into the buffer, and relocate the string
1058 without insert noticing. */
1061 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
1063 register int pos
, pos_byte
, length
, length_byte
;
1067 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1069 signal_after_change (opoint
, 0, PT
- opoint
);
1070 update_compositions (opoint
, PT
, CHECK_BORDER
);
1073 /* Like `insert_from_string' except that all markers pointing
1074 at the place where the insertion happens are adjusted to point after it. */
1077 insert_from_string_before_markers (string
, pos
, pos_byte
,
1078 length
, length_byte
, inherit
)
1080 register int pos
, pos_byte
, length
, length_byte
;
1084 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1086 signal_after_change (opoint
, 0, PT
- opoint
);
1087 update_compositions (opoint
, PT
, CHECK_BORDER
);
1090 /* Subroutine of the insertion functions above. */
1093 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1094 inherit
, before_markers
)
1096 register int pos
, pos_byte
, nchars
, nbytes
;
1097 int inherit
, before_markers
;
1099 struct gcpro gcpro1
;
1100 int outgoing_nbytes
= nbytes
;
1103 /* Make OUTGOING_NBYTES describe the text
1104 as it will be inserted in this buffer. */
1106 if (NILP (current_buffer
->enable_multibyte_characters
))
1107 outgoing_nbytes
= nchars
;
1108 else if (! STRING_MULTIBYTE (string
))
1110 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
1114 /* Do this before moving and increasing the gap,
1115 because the before-change hooks might move the gap
1116 or make it smaller. */
1117 prepare_to_modify_buffer (PT
, PT
, NULL
);
1120 move_gap_both (PT
, PT_BYTE
);
1121 if (GAP_SIZE
< outgoing_nbytes
)
1122 make_gap (outgoing_nbytes
- GAP_SIZE
);
1125 /* Copy the string text into the buffer, perhaps converting
1126 between single-byte and multibyte. */
1127 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1128 STRING_MULTIBYTE (string
),
1129 ! NILP (current_buffer
->enable_multibyte_characters
));
1131 #ifdef BYTE_COMBINING_DEBUG
1132 /* We have copied text into the gap, but we have not altered
1133 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1134 to these functions and get the same results as we would
1135 have got earlier on. Meanwhile, PT_ADDR does point to
1136 the text that has been stored by copy_text. */
1137 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1138 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1142 record_insert (PT
, nchars
);
1145 GAP_SIZE
-= outgoing_nbytes
;
1149 GPT_BYTE
+= outgoing_nbytes
;
1150 ZV_BYTE
+= outgoing_nbytes
;
1151 Z_BYTE
+= outgoing_nbytes
;
1152 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1157 adjust_overlays_for_insert (PT
, nchars
);
1158 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1159 PT_BYTE
+ outgoing_nbytes
,
1162 offset_intervals (current_buffer
, PT
, nchars
);
1164 intervals
= XSTRING (string
)->intervals
;
1165 /* Get the intervals for the part of the string we are inserting. */
1166 if (nbytes
< STRING_BYTES (XSTRING (string
)))
1167 intervals
= copy_intervals (intervals
, pos
, nchars
);
1169 /* Insert those intervals. */
1170 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1171 current_buffer
, inherit
);
1173 adjust_point (nchars
, outgoing_nbytes
);
1176 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1177 current buffer. If the text in BUF has properties, they are absorbed
1178 into the current buffer.
1180 It does not work to use `insert' for this, because a malloc could happen
1181 and relocate BUF's text before the bcopy happens. */
1184 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1186 int charpos
, nchars
;
1191 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1192 signal_after_change (opoint
, 0, PT
- opoint
);
1193 update_compositions (opoint
, PT
, CHECK_BORDER
);
1197 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1202 register Lisp_Object temp
;
1203 int chunk
, chunk_expanded
;
1204 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1205 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1206 int incoming_nbytes
= to_byte
- from_byte
;
1207 int outgoing_nbytes
= incoming_nbytes
;
1210 /* Make OUTGOING_NBYTES describe the text
1211 as it will be inserted in this buffer. */
1213 if (NILP (current_buffer
->enable_multibyte_characters
))
1214 outgoing_nbytes
= nchars
;
1215 else if (NILP (buf
->enable_multibyte_characters
))
1217 int outgoing_before_gap
= 0;
1218 int outgoing_after_gap
= 0;
1220 if (from
< BUF_GPT (buf
))
1222 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1223 if (chunk
> incoming_nbytes
)
1224 chunk
= incoming_nbytes
;
1226 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1232 if (chunk
< incoming_nbytes
)
1234 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1236 incoming_nbytes
- chunk
);
1238 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1241 /* Make sure point-max won't overflow after this insertion. */
1242 XSETINT (temp
, outgoing_nbytes
+ Z
);
1243 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1244 error ("Maximum buffer size exceeded");
1246 /* Do this before moving and increasing the gap,
1247 because the before-change hooks might move the gap
1248 or make it smaller. */
1249 prepare_to_modify_buffer (PT
, PT
, NULL
);
1252 move_gap_both (PT
, PT_BYTE
);
1253 if (GAP_SIZE
< outgoing_nbytes
)
1254 make_gap (outgoing_nbytes
- GAP_SIZE
);
1256 if (from
< BUF_GPT (buf
))
1258 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1259 if (chunk
> incoming_nbytes
)
1260 chunk
= incoming_nbytes
;
1261 /* Record number of output bytes, so we know where
1262 to put the output from the second copy_text. */
1264 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1266 ! NILP (buf
->enable_multibyte_characters
),
1267 ! NILP (current_buffer
->enable_multibyte_characters
));
1270 chunk_expanded
= chunk
= 0;
1272 if (chunk
< incoming_nbytes
)
1273 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1274 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1275 ! NILP (buf
->enable_multibyte_characters
),
1276 ! NILP (current_buffer
->enable_multibyte_characters
));
1278 #ifdef BYTE_COMBINING_DEBUG
1279 /* We have copied text into the gap, but we have not altered
1280 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1281 to these functions and get the same results as we would
1282 have got earlier on. Meanwhile, GPT_ADDR does point to
1283 the text that has been stored by copy_text. */
1284 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1285 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1289 record_insert (PT
, nchars
);
1292 GAP_SIZE
-= outgoing_nbytes
;
1296 GPT_BYTE
+= outgoing_nbytes
;
1297 ZV_BYTE
+= outgoing_nbytes
;
1298 Z_BYTE
+= outgoing_nbytes
;
1299 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1304 adjust_overlays_for_insert (PT
, nchars
);
1305 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1306 PT_BYTE
+ outgoing_nbytes
,
1309 if (BUF_INTERVALS (current_buffer
) != 0)
1310 offset_intervals (current_buffer
, PT
, nchars
);
1312 /* Get the intervals for the part of the string we are inserting. */
1313 intervals
= BUF_INTERVALS (buf
);
1314 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1316 if (buf
== current_buffer
&& PT
<= from
)
1318 intervals
= copy_intervals (intervals
, from
, nchars
);
1321 /* Insert those intervals. */
1322 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1324 adjust_point (nchars
, outgoing_nbytes
);
1327 /* Record undo information and adjust markers and position keepers for
1328 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1329 chars (LEN_BYTE bytes) which resides in the gap just after
1332 PREV_TEXT nil means the new text was just inserted. */
1335 adjust_after_replace (from
, from_byte
, prev_text
, len
, len_byte
)
1336 int from
, from_byte
, len
, len_byte
;
1337 Lisp_Object prev_text
;
1339 int nchars_del
= 0, nbytes_del
= 0;
1341 #ifdef BYTE_COMBINING_DEBUG
1342 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1343 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1347 if (STRINGP (prev_text
))
1349 nchars_del
= XSTRING (prev_text
)->size
;
1350 nbytes_del
= STRING_BYTES (XSTRING (prev_text
));
1353 /* Update various buffer positions for the new text. */
1354 GAP_SIZE
-= len_byte
;
1356 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1357 GPT
+= len
; GPT_BYTE
+= len_byte
;
1358 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1361 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1364 adjust_markers_for_insert (from
, from_byte
,
1365 from
+ len
, from_byte
+ len_byte
, 0);
1367 if (! EQ (current_buffer
->undo_list
, Qt
))
1370 record_delete (from
, prev_text
);
1371 record_insert (from
, len
);
1374 if (len
> nchars_del
)
1375 adjust_overlays_for_insert (from
, len
- nchars_del
);
1376 else if (len
< nchars_del
)
1377 adjust_overlays_for_delete (from
, nchars_del
- len
);
1378 if (BUF_INTERVALS (current_buffer
) != 0)
1380 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1384 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1386 /* As byte combining will decrease Z, we must check this again. */
1387 if (Z
- GPT
< END_UNCHANGED
)
1388 END_UNCHANGED
= Z
- GPT
;
1393 evaporate_overlays (from
);
1397 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1398 This is for use when undo is not enabled in the current buffer. */
1401 adjust_after_replace_noundo (from
, from_byte
, nchars_del
, nbytes_del
, len
, len_byte
)
1402 int from
, from_byte
, nchars_del
, nbytes_del
, len
, len_byte
;
1404 #ifdef BYTE_COMBINING_DEBUG
1405 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1406 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1410 /* Update various buffer positions for the new text. */
1411 GAP_SIZE
-= len_byte
;
1413 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1414 GPT
+= len
; GPT_BYTE
+= len_byte
;
1415 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1418 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1421 adjust_markers_for_insert (from
, from_byte
,
1422 from
+ len
, from_byte
+ len_byte
, 0);
1424 if (len
> nchars_del
)
1425 adjust_overlays_for_insert (from
, len
- nchars_del
);
1426 else if (len
< nchars_del
)
1427 adjust_overlays_for_delete (from
, nchars_del
- len
);
1428 if (BUF_INTERVALS (current_buffer
) != 0)
1430 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1434 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1436 /* As byte combining will decrease Z, we must check this again. */
1437 if (Z
- GPT
< END_UNCHANGED
)
1438 END_UNCHANGED
= Z
- GPT
;
1443 evaporate_overlays (from
);
1447 /* Record undo information, adjust markers and position keepers for an
1448 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1449 text already exists in the current buffer but character length (TO
1450 - FROM) may be incorrect, the correct length is NEWLEN. */
1453 adjust_after_insert (from
, from_byte
, to
, to_byte
, newlen
)
1454 int from
, from_byte
, to
, to_byte
, newlen
;
1456 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
1459 move_gap_both (to
, to_byte
);
1460 GAP_SIZE
+= len_byte
;
1461 GPT
-= len
; GPT_BYTE
-= len_byte
;
1462 ZV
-= len
; ZV_BYTE
-= len_byte
;
1463 Z
-= len
; Z_BYTE
-= len_byte
;
1464 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1467 /* Replace the text from character positions FROM to TO with NEW,
1468 If PREPARE is nonzero, call prepare_to_modify_buffer.
1469 If INHERIT, the newly inserted text should inherit text properties
1470 from the surrounding non-deleted text. */
1472 /* Note that this does not yet handle markers quite right.
1473 Also it needs to record a single undo-entry that does a replacement
1474 rather than a separate delete and insert.
1475 That way, undo will also handle markers properly.
1477 But if MARKERS is 0, don't relocate markers. */
1480 replace_range (from
, to
, new, prepare
, inherit
, markers
)
1482 int from
, to
, prepare
, inherit
, markers
;
1484 int inschars
= XSTRING (new)->size
;
1485 int insbytes
= STRING_BYTES (XSTRING (new));
1486 int from_byte
, to_byte
;
1487 int nbytes_del
, nchars_del
;
1488 register Lisp_Object temp
;
1489 struct gcpro gcpro1
;
1491 int outgoing_insbytes
= insbytes
;
1492 Lisp_Object deletion
;
1501 int range_length
= to
- from
;
1502 prepare_to_modify_buffer (from
, to
, &from
);
1503 to
= from
+ range_length
;
1508 /* Make args be valid */
1514 from_byte
= CHAR_TO_BYTE (from
);
1515 to_byte
= CHAR_TO_BYTE (to
);
1517 nchars_del
= to
- from
;
1518 nbytes_del
= to_byte
- from_byte
;
1520 if (nbytes_del
<= 0 && insbytes
== 0)
1523 /* Make OUTGOING_INSBYTES describe the text
1524 as it will be inserted in this buffer. */
1526 if (NILP (current_buffer
->enable_multibyte_characters
))
1527 outgoing_insbytes
= inschars
;
1528 else if (! STRING_MULTIBYTE (new))
1530 = count_size_as_multibyte (XSTRING (new)->data
, insbytes
);
1532 /* Make sure point-max won't overflow after this insertion. */
1533 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1534 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1535 error ("Maximum buffer size exceeded");
1539 /* Make sure the gap is somewhere in or next to what we are deleting. */
1541 gap_right (from
, from_byte
);
1543 gap_left (to
, to_byte
, 0);
1545 /* Even if we don't record for undo, we must keep the original text
1546 because we may have to recover it because of inappropriate byte
1548 if (! EQ (current_buffer
->undo_list
, Qt
))
1549 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1551 GAP_SIZE
+= nbytes_del
;
1554 ZV_BYTE
-= nbytes_del
;
1555 Z_BYTE
-= nbytes_del
;
1557 GPT_BYTE
= from_byte
;
1558 *(GPT_ADDR
) = 0; /* Put an anchor. */
1563 if (GPT
- BEG
< BEG_UNCHANGED
)
1564 BEG_UNCHANGED
= GPT
- BEG
;
1565 if (Z
- GPT
< END_UNCHANGED
)
1566 END_UNCHANGED
= Z
- GPT
;
1568 if (GAP_SIZE
< insbytes
)
1569 make_gap (insbytes
- GAP_SIZE
);
1571 /* Copy the string text into the buffer, perhaps converting
1572 between single-byte and multibyte. */
1573 copy_text (XSTRING (new)->data
, GPT_ADDR
, insbytes
,
1574 STRING_MULTIBYTE (new),
1575 ! NILP (current_buffer
->enable_multibyte_characters
));
1577 #ifdef BYTE_COMBINING_DEBUG
1578 /* We have copied text into the gap, but we have not marked
1579 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1580 here, for both the previous text and the following text.
1581 Meanwhile, GPT_ADDR does point to
1582 the text that has been stored by copy_text. */
1583 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1584 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1588 if (! EQ (current_buffer
->undo_list
, Qt
))
1590 record_delete (from
, deletion
);
1591 record_insert (from
, inschars
);
1594 GAP_SIZE
-= outgoing_insbytes
;
1598 GPT_BYTE
+= outgoing_insbytes
;
1599 ZV_BYTE
+= outgoing_insbytes
;
1600 Z_BYTE
+= outgoing_insbytes
;
1601 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1606 /* Adjust the overlay center as needed. This must be done after
1607 adjusting the markers that bound the overlays. */
1608 adjust_overlays_for_delete (from
, nchars_del
);
1609 adjust_overlays_for_insert (from
, inschars
);
1611 /* Adjust markers for the deletion and the insertion. */
1613 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1614 inschars
, outgoing_insbytes
);
1616 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1618 /* Get the intervals for the part of the string we are inserting--
1619 not including the combined-before bytes. */
1620 intervals
= XSTRING (new)->intervals
;
1621 /* Insert those intervals. */
1622 graft_intervals_into_buffer (intervals
, from
, inschars
,
1623 current_buffer
, inherit
);
1625 /* Relocate point as if it were a marker. */
1627 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1628 (from_byte
+ outgoing_insbytes
1629 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1631 if (outgoing_insbytes
== 0)
1632 evaporate_overlays (from
);
1639 signal_after_change (from
, nchars_del
, GPT
- from
);
1640 update_compositions (from
, GPT
, CHECK_BORDER
);
1643 /* Delete characters in current buffer
1644 from FROM up to (but not including) TO.
1645 If TO comes before FROM, we delete nothing. */
1648 del_range (from
, to
)
1649 register int from
, to
;
1651 del_range_1 (from
, to
, 1, 0);
1654 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1655 RET_STRING says to return the deleted text. */
1658 del_range_1 (from
, to
, prepare
, ret_string
)
1659 int from
, to
, prepare
, ret_string
;
1661 int from_byte
, to_byte
;
1662 Lisp_Object deletion
;
1663 struct gcpro gcpro1
;
1665 /* Make args be valid */
1676 int range_length
= to
- from
;
1677 prepare_to_modify_buffer (from
, to
, &from
);
1678 to
= min (ZV
, from
+ range_length
);
1681 from_byte
= CHAR_TO_BYTE (from
);
1682 to_byte
= CHAR_TO_BYTE (to
);
1684 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1686 signal_after_change (from
, to
- from
, 0);
1687 update_compositions (from
, from
, CHECK_HEAD
);
1692 /* Like del_range_1 but args are byte positions, not char positions. */
1695 del_range_byte (from_byte
, to_byte
, prepare
)
1696 int from_byte
, to_byte
, prepare
;
1700 /* Make args be valid */
1701 if (from_byte
< BEGV_BYTE
)
1702 from_byte
= BEGV_BYTE
;
1703 if (to_byte
> ZV_BYTE
)
1706 if (to_byte
<= from_byte
)
1709 from
= BYTE_TO_CHAR (from_byte
);
1710 to
= BYTE_TO_CHAR (to_byte
);
1714 int old_from
= from
, old_to
= Z
- to
;
1715 int range_length
= to
- from
;
1716 prepare_to_modify_buffer (from
, to
, &from
);
1717 to
= from
+ range_length
;
1719 if (old_from
!= from
)
1720 from_byte
= CHAR_TO_BYTE (from
);
1726 else if (old_to
== Z
- to
)
1727 to_byte
= CHAR_TO_BYTE (to
);
1730 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1731 signal_after_change (from
, to
- from
, 0);
1732 update_compositions (from
, from
, CHECK_HEAD
);
1735 /* Like del_range_1, but positions are specified both as charpos
1739 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
1740 int from
, from_byte
, to
, to_byte
, prepare
;
1742 /* Make args be valid */
1743 if (from_byte
< BEGV_BYTE
)
1744 from_byte
= BEGV_BYTE
;
1745 if (to_byte
> ZV_BYTE
)
1748 if (to_byte
<= from_byte
)
1758 int old_from
= from
, old_to
= Z
- to
;
1759 int range_length
= to
- from
;
1760 prepare_to_modify_buffer (from
, to
, &from
);
1761 to
= from
+ range_length
;
1763 if (old_from
!= from
)
1764 from_byte
= CHAR_TO_BYTE (from
);
1770 else if (old_to
== Z
- to
)
1771 to_byte
= CHAR_TO_BYTE (to
);
1774 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1775 signal_after_change (from
, to
- from
, 0);
1776 update_compositions (from
, from
, CHECK_HEAD
);
1779 /* Delete a range of text, specified both as character positions
1780 and byte positions. FROM and TO are character positions,
1781 while FROM_BYTE and TO_BYTE are byte positions.
1782 If RET_STRING is true, the deleted area is returned as a string. */
1785 del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
)
1786 int from
, from_byte
, to
, to_byte
, ret_string
;
1788 register int nbytes_del
, nchars_del
;
1789 Lisp_Object deletion
;
1793 nchars_del
= to
- from
;
1794 nbytes_del
= to_byte
- from_byte
;
1796 /* Make sure the gap is somewhere in or next to what we are deleting. */
1798 gap_right (from
, from_byte
);
1800 gap_left (to
, to_byte
, 0);
1802 #ifdef BYTE_COMBINING_DEBUG
1803 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1804 Z_BYTE
- to_byte
, from
, from_byte
))
1808 if (ret_string
|| ! EQ (current_buffer
->undo_list
, Qt
))
1809 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1813 /* Relocate all markers pointing into the new, larger gap
1814 to point at the end of the text before the gap.
1815 Do this before recording the deletion,
1816 so that undo handles this after reinserting the text. */
1817 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1819 if (! EQ (current_buffer
->undo_list
, Qt
))
1820 record_delete (from
, deletion
);
1823 /* Relocate point as if it were a marker. */
1825 adjust_point (from
- (PT
< to
? PT
: to
),
1826 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1828 offset_intervals (current_buffer
, from
, - nchars_del
);
1830 /* Adjust the overlay center as needed. This must be done after
1831 adjusting the markers that bound the overlays. */
1832 adjust_overlays_for_delete (from
, nchars_del
);
1834 GAP_SIZE
+= nbytes_del
;
1835 ZV_BYTE
-= nbytes_del
;
1836 Z_BYTE
-= nbytes_del
;
1840 GPT_BYTE
= from_byte
;
1841 *(GPT_ADDR
) = 0; /* Put an anchor. */
1846 if (GPT
- BEG
< BEG_UNCHANGED
)
1847 BEG_UNCHANGED
= GPT
- BEG
;
1848 if (Z
- GPT
< END_UNCHANGED
)
1849 END_UNCHANGED
= Z
- GPT
;
1853 evaporate_overlays (from
);
1858 /* Call this if you're about to change the region of BUFFER from
1859 character positions START to END. This checks the read-only
1860 properties of the region, calls the necessary modification hooks,
1861 and warns the next redisplay that it should pay attention to that
1865 modify_region (buffer
, start
, end
)
1866 struct buffer
*buffer
;
1869 struct buffer
*old_buffer
= current_buffer
;
1871 if (buffer
!= old_buffer
)
1872 set_buffer_internal (buffer
);
1874 prepare_to_modify_buffer (start
, end
, NULL
);
1876 BUF_COMPUTE_UNCHANGED (buffer
, start
- 1, end
);
1878 if (MODIFF
<= SAVE_MODIFF
)
1879 record_first_change ();
1882 buffer
->point_before_scroll
= Qnil
;
1884 if (buffer
!= old_buffer
)
1885 set_buffer_internal (old_buffer
);
1888 /* Check that it is okay to modify the buffer between START and END,
1889 which are char positions.
1891 Run the before-change-function, if any. If intervals are in use,
1892 verify that the text to be modified is not read-only, and call
1893 any modification properties the text may have.
1895 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1896 by holding its value temporarily in a marker. */
1899 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
1903 if (!NILP (current_buffer
->read_only
))
1904 Fbarf_if_buffer_read_only ();
1906 /* Let redisplay consider other windows than selected_window
1907 if modifying another buffer. */
1908 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
1909 ++windows_or_buffers_changed
;
1911 if (BUF_INTERVALS (current_buffer
) != 0)
1915 Lisp_Object preserve_marker
;
1916 struct gcpro gcpro1
;
1917 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1918 GCPRO1 (preserve_marker
);
1919 verify_interval_modification (current_buffer
, start
, end
);
1920 *preserve_ptr
= marker_position (preserve_marker
);
1921 unchain_marker (preserve_marker
);
1925 verify_interval_modification (current_buffer
, start
, end
);
1928 #ifdef CLASH_DETECTION
1929 if (!NILP (current_buffer
->file_truename
)
1930 /* Make binding buffer-file-name to nil effective. */
1931 && !NILP (current_buffer
->filename
)
1932 && SAVE_MODIFF
>= MODIFF
)
1933 lock_file (current_buffer
->file_truename
);
1935 /* At least warn if this file has changed on disk since it was visited. */
1936 if (!NILP (current_buffer
->filename
)
1937 && SAVE_MODIFF
>= MODIFF
1938 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1939 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
1940 call1 (intern ("ask-user-about-supersession-threat"),
1941 current_buffer
->filename
);
1942 #endif /* not CLASH_DETECTION */
1944 signal_before_change (start
, end
, preserve_ptr
);
1946 if (current_buffer
->newline_cache
)
1947 invalidate_region_cache (current_buffer
,
1948 current_buffer
->newline_cache
,
1949 start
- BEG
, Z
- end
);
1950 if (current_buffer
->width_run_cache
)
1951 invalidate_region_cache (current_buffer
,
1952 current_buffer
->width_run_cache
,
1953 start
- BEG
, Z
- end
);
1955 Vdeactivate_mark
= Qt
;
1958 /* These macros work with an argument named `preserve_ptr'
1959 and a local variable named `preserve_marker'. */
1961 #define PRESERVE_VALUE \
1962 if (preserve_ptr && NILP (preserve_marker)) \
1963 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1965 #define RESTORE_VALUE \
1966 if (! NILP (preserve_marker)) \
1968 *preserve_ptr = marker_position (preserve_marker); \
1969 unchain_marker (preserve_marker); \
1972 #define PRESERVE_START_END \
1973 if (NILP (start_marker)) \
1974 start_marker = Fcopy_marker (start, Qnil); \
1975 if (NILP (end_marker)) \
1976 end_marker = Fcopy_marker (end, Qnil);
1978 #define FETCH_START \
1979 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1982 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1984 /* Signal a change to the buffer immediately before it happens.
1985 START_INT and END_INT are the bounds of the text to be changed.
1987 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1988 by holding its value temporarily in a marker. */
1991 signal_before_change (start_int
, end_int
, preserve_ptr
)
1992 int start_int
, end_int
;
1995 Lisp_Object start
, end
;
1996 Lisp_Object start_marker
, end_marker
;
1997 Lisp_Object preserve_marker
;
1998 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2000 if (inhibit_modification_hooks
)
2003 start
= make_number (start_int
);
2004 end
= make_number (end_int
);
2005 preserve_marker
= Qnil
;
2006 start_marker
= Qnil
;
2008 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2010 /* If buffer is unmodified, run a special hook for that case. */
2011 if (SAVE_MODIFF
>= MODIFF
2012 && !NILP (Vfirst_change_hook
)
2013 && !NILP (Vrun_hooks
))
2017 call1 (Vrun_hooks
, Qfirst_change_hook
);
2020 /* Now run the before-change-functions if any. */
2021 if (!NILP (Vbefore_change_functions
))
2023 Lisp_Object args
[3];
2024 Lisp_Object before_change_functions
;
2025 Lisp_Object after_change_functions
;
2026 struct gcpro gcpro1
, gcpro2
;
2027 struct buffer
*old
= current_buffer
;
2033 /* "Bind" before-change-functions and after-change-functions
2034 to nil--but in a way that errors don't know about.
2035 That way, if there's an error in them, they will stay nil. */
2036 before_change_functions
= Vbefore_change_functions
;
2037 after_change_functions
= Vafter_change_functions
;
2038 Vbefore_change_functions
= Qnil
;
2039 Vafter_change_functions
= Qnil
;
2040 GCPRO2 (before_change_functions
, after_change_functions
);
2042 /* Actually run the hook functions. */
2043 args
[0] = Qbefore_change_functions
;
2044 args
[1] = FETCH_START
;
2045 args
[2] = FETCH_END
;
2046 run_hook_list_with_args (before_change_functions
, 3, args
);
2048 /* "Unbind" the variables we "bound" to nil. Beware a
2049 buffer-local hook which changes the buffer when run (e.g. W3). */
2050 if (old
!= current_buffer
)
2052 new = current_buffer
;
2053 set_buffer_internal (old
);
2054 Vbefore_change_functions
= before_change_functions
;
2055 Vafter_change_functions
= after_change_functions
;
2056 set_buffer_internal (new);
2060 Vbefore_change_functions
= before_change_functions
;
2061 Vafter_change_functions
= after_change_functions
;
2066 if (!NILP (current_buffer
->overlays_before
)
2067 || !NILP (current_buffer
->overlays_after
))
2070 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2071 FETCH_START
, FETCH_END
, Qnil
);
2074 if (! NILP (start_marker
))
2075 free_marker (start_marker
);
2076 if (! NILP (end_marker
))
2077 free_marker (end_marker
);
2082 /* Signal a change immediately after it happens.
2083 CHARPOS is the character position of the start of the changed text.
2084 LENDEL is the number of characters of the text before the change.
2085 (Not the whole buffer; just the part that was changed.)
2086 LENINS is the number of characters in that part of the text
2087 after the change. */
2090 signal_after_change (charpos
, lendel
, lenins
)
2091 int charpos
, lendel
, lenins
;
2093 if (inhibit_modification_hooks
)
2096 /* If we are deferring calls to the after-change functions
2097 and there are no before-change functions,
2098 just record the args that we were going to use. */
2099 if (! NILP (Vcombine_after_change_calls
)
2100 && NILP (Vbefore_change_functions
)
2101 && NILP (current_buffer
->overlays_before
)
2102 && NILP (current_buffer
->overlays_after
))
2106 if (!NILP (combine_after_change_list
)
2107 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2108 Fcombine_after_change_execute ();
2110 elt
= Fcons (make_number (charpos
- BEG
),
2111 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2112 Fcons (make_number (lenins
- lendel
), Qnil
)));
2113 combine_after_change_list
2114 = Fcons (elt
, combine_after_change_list
);
2115 combine_after_change_buffer
= Fcurrent_buffer ();
2120 if (!NILP (combine_after_change_list
))
2121 Fcombine_after_change_execute ();
2123 if (!NILP (Vafter_change_functions
))
2125 Lisp_Object args
[4];
2126 Lisp_Object before_change_functions
;
2127 Lisp_Object after_change_functions
;
2128 struct buffer
*old
= current_buffer
;
2130 struct gcpro gcpro1
, gcpro2
;
2132 /* "Bind" before-change-functions and after-change-functions
2133 to nil--but in a way that errors don't know about.
2134 That way, if there's an error in them, they will stay nil. */
2135 before_change_functions
= Vbefore_change_functions
;
2136 after_change_functions
= Vafter_change_functions
;
2137 Vbefore_change_functions
= Qnil
;
2138 Vafter_change_functions
= Qnil
;
2139 GCPRO2 (before_change_functions
, after_change_functions
);
2141 /* Actually run the hook functions. */
2142 args
[0] = Qafter_change_functions
;
2143 XSETFASTINT (args
[1], charpos
);
2144 XSETFASTINT (args
[2], charpos
+ lenins
);
2145 XSETFASTINT (args
[3], lendel
);
2146 run_hook_list_with_args (after_change_functions
,
2149 /* "Unbind" the variables we "bound" to nil. Beware a
2150 buffer-local hook which changes the buffer when run (e.g. W3). */
2151 if (old
!= current_buffer
)
2153 new = current_buffer
;
2154 set_buffer_internal (old
);
2155 Vbefore_change_functions
= before_change_functions
;
2156 Vafter_change_functions
= after_change_functions
;
2157 set_buffer_internal (new);
2161 Vbefore_change_functions
= before_change_functions
;
2162 Vafter_change_functions
= after_change_functions
;
2167 if (!NILP (current_buffer
->overlays_before
)
2168 || !NILP (current_buffer
->overlays_after
))
2169 report_overlay_modification (make_number (charpos
),
2170 make_number (charpos
+ lenins
),
2172 make_number (charpos
),
2173 make_number (charpos
+ lenins
),
2174 make_number (lendel
));
2176 /* After an insertion, call the text properties
2177 insert-behind-hooks or insert-in-front-hooks. */
2179 report_interval_modification (make_number (charpos
),
2180 make_number (charpos
+ lenins
));
2184 Fcombine_after_change_execute_1 (val
)
2187 Vcombine_after_change_calls
= val
;
2191 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2192 Scombine_after_change_execute
, 0, 0, 0,
2193 doc
: /* This function is for use internally in `combine-after-change-calls'. */)
2196 int count
= specpdl_ptr
- specpdl
;
2197 int beg
, end
, change
;
2201 if (NILP (combine_after_change_list
))
2204 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2206 Fset_buffer (combine_after_change_buffer
);
2208 /* # chars unchanged at beginning of buffer. */
2210 /* # chars unchanged at end of buffer. */
2212 /* Total amount of insertion (negative for deletion). */
2215 /* Scan the various individual changes,
2216 accumulating the range info in BEG, END and CHANGE. */
2217 for (tail
= combine_after_change_list
; CONSP (tail
);
2221 int thisbeg
, thisend
, thischange
;
2223 /* Extract the info from the next element. */
2227 thisbeg
= XINT (XCAR (elt
));
2232 thisend
= XINT (XCAR (elt
));
2237 thischange
= XINT (XCAR (elt
));
2239 /* Merge this range into the accumulated range. */
2240 change
+= thischange
;
2247 /* Get the current start and end positions of the range
2248 that was changed. */
2252 /* We are about to handle these, so discard them. */
2253 combine_after_change_list
= Qnil
;
2255 /* Now run the after-change functions for real.
2256 Turn off the flag that defers them. */
2257 record_unwind_protect (Fcombine_after_change_execute_1
,
2258 Vcombine_after_change_calls
);
2259 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2260 update_compositions (begpos
, endpos
, CHECK_ALL
);
2262 return unbind_to (count
, Qnil
);
2268 staticpro (&combine_after_change_list
);
2269 combine_after_change_list
= Qnil
;
2270 combine_after_change_buffer
= Qnil
;
2272 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2273 doc
: /* Non-nil means enable debugging checks for invalid marker positions. */);
2274 check_markers_debug_flag
= 0;
2275 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2276 doc
: /* Used internally by the `combine-after-change-calls' macro. */);
2277 Vcombine_after_change_calls
= Qnil
;
2279 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks
,
2280 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2281 This affects `before-change-functions' and `after-change-functions',
2282 as well as hooks attached to text properties and overlays. */);
2283 inhibit_modification_hooks
= 0;
2284 Qinhibit_modification_hooks
= intern ("inhibit-modification-hooks");
2285 staticpro (&Qinhibit_modification_hooks
);
2287 defsubr (&Scombine_after_change_execute
);