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. */
27 #include "intervals.h"
31 #include "blockinput.h"
32 #include "region-cache.h"
38 #define min(x, y) ((x) < (y) ? (x) : (y))
39 #define max(x, y) ((x) > (y) ? (x) : (y))
41 static void insert_from_string_1
P_ ((Lisp_Object
, int, int, int, int, int, int));
42 static void insert_from_buffer_1 ();
43 static void gap_left
P_ ((int, int, int));
44 static void gap_right
P_ ((int, int));
45 static void adjust_markers_gap_motion
P_ ((int, int, int));
46 static void adjust_markers_for_insert
P_ ((int, int, int, int, int, int, int));
47 static void adjust_markers_for_delete
P_ ((int, int, int, int));
48 static void adjust_markers_for_record_delete
P_ ((int, int, int, int));
49 static void adjust_point
P_ ((int, int));
51 Lisp_Object
Fcombine_after_change_execute ();
53 /* Non-nil means don't call the after-change-functions right away,
54 just record an element in Vcombine_after_change_calls_list. */
55 Lisp_Object Vcombine_after_change_calls
;
57 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
58 describing changes which happened while combine_after_change_calls
59 was nonzero. We use this to decide how to call them
60 once the deferral ends.
63 BEG-UNCHANGED is the number of chars before the changed range.
64 END-UNCHANGED is the number of chars after the changed range,
65 and CHANGE-AMOUNT is the number of characters inserted by the change
66 (negative for a deletion). */
67 Lisp_Object combine_after_change_list
;
69 /* Buffer which combine_after_change_list is about. */
70 Lisp_Object combine_after_change_buffer
;
72 /* Check all markers in the current buffer, looking for something invalid. */
74 static int check_markers_debug_flag
;
76 #define CHECK_MARKERS() \
77 if (check_markers_debug_flag) \
84 register Lisp_Object tail
, prev
, next
;
85 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
87 tail
= BUF_MARKERS (current_buffer
);
89 while (XSYMBOL (tail
) != XSYMBOL (Qnil
))
91 if (XMARKER (tail
)->buffer
->text
!= current_buffer
->text
)
93 if (XMARKER (tail
)->charpos
> Z
)
95 if (XMARKER (tail
)->bytepos
> Z_BYTE
)
97 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (XMARKER (tail
)->bytepos
)))
100 tail
= XMARKER (tail
)->chain
;
104 /* Move gap to position CHARPOS.
105 Note that this can quit! */
111 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
114 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
115 Note that this can quit! */
118 move_gap_both (charpos
, bytepos
)
119 int charpos
, bytepos
;
121 if (bytepos
< GPT_BYTE
)
122 gap_left (charpos
, bytepos
, 0);
123 else if (bytepos
> GPT_BYTE
)
124 gap_right (charpos
, bytepos
);
127 /* Move the gap to a position less than the current GPT.
128 BYTEPOS describes the new position as a byte position,
129 and CHARPOS is the corresponding char position.
130 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
133 gap_left (charpos
, bytepos
, newgap
)
134 register int charpos
, bytepos
;
137 register unsigned char *to
, *from
;
143 if (unchanged_modified
== MODIFF
144 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
146 beg_unchanged
= charpos
- BEG
;
147 end_unchanged
= Z
- charpos
;
151 if (Z
- GPT
< end_unchanged
)
152 end_unchanged
= Z
- GPT
;
153 if (charpos
< beg_unchanged
)
154 beg_unchanged
= charpos
- BEG
;
163 /* Now copy the characters. To move the gap down,
164 copy characters up. */
168 /* I gets number of characters left to copy. */
169 i
= new_s1
- bytepos
;
172 /* If a quit is requested, stop copying now.
173 Change BYTEPOS to be where we have actually moved the gap to. */
177 charpos
= BYTE_TO_CHAR (bytepos
);
180 /* Move at most 32000 chars before checking again for a quit. */
185 /* bcopy is safe if the two areas of memory do not overlap
186 or on systems where bcopy is always safe for moving upward. */
187 && (BCOPY_UPWARD_SAFE
188 || to
- from
>= 128))
190 /* If overlap is not safe, avoid it by not moving too many
191 characters at once. */
192 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
207 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
208 BYTEPOS is where the loop above stopped, which may be what was specified
209 or may be where a quit was detected. */
210 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
213 if (bytepos
< charpos
)
215 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
219 /* Move the gap to a position greater than than the current GPT.
220 BYTEPOS describes the new position as a byte position,
221 and CHARPOS is the corresponding char position. */
224 gap_right (charpos
, bytepos
)
225 register int charpos
, bytepos
;
227 register unsigned char *to
, *from
;
231 if (unchanged_modified
== MODIFF
232 && overlay_unchanged_modified
== OVERLAY_MODIFF
)
234 beg_unchanged
= charpos
- BEG
;
235 end_unchanged
= Z
- charpos
;
239 if (Z
- charpos
- 1 < end_unchanged
)
240 end_unchanged
= Z
- charpos
;
241 if (GPT
- BEG
< beg_unchanged
)
242 beg_unchanged
= GPT
- BEG
;
250 /* Now copy the characters. To move the gap up,
251 copy characters down. */
255 /* I gets number of characters left to copy. */
256 i
= bytepos
- new_s1
;
259 /* If a quit is requested, stop copying now.
260 Change BYTEPOS to be where we have actually moved the gap to. */
264 charpos
= BYTE_TO_CHAR (bytepos
);
267 /* Move at most 32000 chars before checking again for a quit. */
272 /* bcopy is safe if the two areas of memory do not overlap
273 or on systems where bcopy is always safe for moving downward. */
274 && (BCOPY_DOWNWARD_SAFE
275 || from
- to
>= 128))
277 /* If overlap is not safe, avoid it by not moving too many
278 characters at once. */
279 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
294 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
298 if (bytepos
< charpos
)
300 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
304 /* Add AMOUNT to the byte position of every marker in the current buffer
305 whose current byte position is between FROM (exclusive) and TO (inclusive).
307 Also, any markers past the outside of that interval, in the direction
308 of adjustment, are first moved back to the near end of the interval
309 and then adjusted by AMOUNT.
311 When the latter adjustment is done, if AMOUNT is negative,
312 we record the adjustment for undo. (This case happens only for
315 The markers' character positions are not altered,
316 because gap motion does not affect character positions. */
318 int adjust_markers_test
;
321 adjust_markers_gap_motion (from
, to
, amount
)
322 register int from
, to
, amount
;
324 /* Now that a marker has a bytepos, not counting the gap,
325 nothing needs to be done here. */
328 register struct Lisp_Marker
*m
;
331 marker
= BUF_MARKERS (current_buffer
);
333 while (!NILP (marker
))
335 m
= XMARKER (marker
);
339 if (mpos
> to
&& mpos
< to
+ amount
)
341 if (adjust_markers_test
)
348 /* Here's the case where a marker is inside text being deleted.
349 AMOUNT can be negative for gap motion, too,
350 but then this range contains no markers. */
351 if (mpos
> from
+ amount
&& mpos
<= from
)
353 if (adjust_markers_test
)
355 mpos
= from
+ amount
;
358 if (mpos
> from
&& mpos
<= to
)
366 /* Adjust all markers for a deletion
367 whose range in bytes is FROM_BYTE to TO_BYTE.
368 The range in charpos is FROM to TO.
370 This function assumes that the gap is adjacent to
371 or inside of the range being deleted. */
374 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
)
375 register int from
, from_byte
, to
, to_byte
;
378 register struct Lisp_Marker
*m
;
379 register int charpos
;
381 marker
= BUF_MARKERS (current_buffer
);
383 while (!NILP (marker
))
385 m
= XMARKER (marker
);
386 charpos
= m
->charpos
;
391 /* If the marker is after the deletion,
392 relocate by number of chars / bytes deleted. */
395 m
->charpos
-= to
- from
;
396 m
->bytepos
-= to_byte
- from_byte
;
399 /* Here's the case where a marker is inside text being deleted. */
400 else if (charpos
> from
)
402 record_marker_adjustment (marker
, from
- charpos
);
404 m
->bytepos
= from_byte
;
412 /* Adjust all markers for calling record_delete for combining bytes.
413 whose range in bytes is FROM_BYTE to TO_BYTE.
414 The range in charpos is FROM to TO. */
417 adjust_markers_for_record_delete (from
, from_byte
, to
, to_byte
)
418 register int from
, from_byte
, to
, to_byte
;
421 register struct Lisp_Marker
*m
;
422 register int charpos
;
424 marker
= BUF_MARKERS (current_buffer
);
426 while (!NILP (marker
))
428 m
= XMARKER (marker
);
429 charpos
= m
->charpos
;
431 /* If the marker is after the deletion,
432 relocate by number of chars / bytes deleted. */
435 /* Here's the case where a marker is inside text being deleted. */
436 else if (charpos
> from
)
437 record_marker_adjustment (marker
, from
- charpos
);
443 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
444 to TO / TO_BYTE. We have to relocate the charpos of every marker
445 that points after the insertion (but not their bytepos).
447 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
448 that combine into one character with the text before the insertion.
449 COMBINED_AFTER_BYTES is the number of bytes after the insertion
450 that combine into one character with the last inserted bytes.
452 When a marker points at the insertion point,
453 we advance it if either its insertion-type is t
454 or BEFORE_MARKERS is true. */
457 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
,
458 combined_before_bytes
, combined_after_bytes
,
460 register int from
, from_byte
, to
, to_byte
;
461 int combined_before_bytes
, combined_after_bytes
, before_markers
;
465 int nchars
= to
- from
;
466 int nbytes
= to_byte
- from_byte
;
468 marker
= BUF_MARKERS (current_buffer
);
470 while (!NILP (marker
))
472 register struct Lisp_Marker
*m
= XMARKER (marker
);
474 /* In a single-byte buffer, a marker's two positions must be equal.
475 (If this insertion is going to combine characters, Z will
476 become different from Z_BYTE, but they might be the same now.
477 If so, the two OLD positions of the marker should be equal.) */
480 if (m
->charpos
!= m
->bytepos
)
484 if (m
->bytepos
== from_byte
)
486 if (m
->insertion_type
|| before_markers
)
488 m
->bytepos
= to_byte
+ combined_after_bytes
;
489 m
->charpos
= to
- combined_before_bytes
;
490 /* Point the marker before the combined character,
491 so that undoing the insertion puts it back where it was. */
492 if (combined_after_bytes
)
493 DEC_BOTH (m
->charpos
, m
->bytepos
);
494 if (m
->insertion_type
)
497 else if (combined_before_bytes
)
499 /* This marker doesn't "need relocation",
500 but don't leave it pointing in the middle of a character.
501 Point the marker after the combined character,
502 so that undoing the insertion puts it back where it was. */
503 m
->bytepos
+= combined_before_bytes
;
504 if (combined_before_bytes
== nbytes
)
505 /* All new bytes plus combined_after_bytes (if any)
507 m
->bytepos
+= combined_after_bytes
;
510 /* If a marker was pointing into the combining bytes
511 after the insertion, don't leave it there
512 in the middle of a character. */
513 else if (combined_after_bytes
&& m
->bytepos
>= from_byte
514 && m
->bytepos
< from_byte
+ combined_after_bytes
)
516 /* Put it after the combining bytes. */
517 m
->bytepos
= to_byte
+ combined_after_bytes
;
518 m
->charpos
= to
- combined_before_bytes
;
519 /* Now move it back before the combined character,
520 so that undoing the insertion will put it where it was. */
521 DEC_BOTH (m
->charpos
, m
->bytepos
);
523 else if (m
->bytepos
> from_byte
)
525 m
->bytepos
+= nbytes
;
526 m
->charpos
+= nchars
- combined_after_bytes
- combined_before_bytes
;
532 /* Adjusting only markers whose insertion-type is t may result in
533 disordered overlays in the slot `overlays_before'. */
535 fix_overlays_before (current_buffer
, from
, to
);
538 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
540 This is used only when the value of point changes due to an insert
541 or delete; it does not represent a conceptual change in point as a
542 marker. In particular, point is not crossing any interval
543 boundaries, so there's no need to use the usual SET_PT macro. In
544 fact it would be incorrect to do so, because either the old or the
545 new value of point is out of sync with the current set of
549 adjust_point (nchars
, nbytes
)
552 BUF_PT (current_buffer
) += nchars
;
553 BUF_PT_BYTE (current_buffer
) += nbytes
;
555 /* In a single-byte buffer, the two positions must be equal. */
561 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
562 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
565 See the comment of adjust_markers_for_insert for the args
566 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES. */
569 adjust_markers_for_replace (from
, from_byte
, old_chars
, old_bytes
,
570 new_chars
, new_bytes
,
571 combined_before_bytes
, combined_after_bytes
)
572 int from
, from_byte
, old_chars
, old_bytes
, new_chars
, new_bytes
;
573 int combined_before_bytes
, combined_after_bytes
;
575 Lisp_Object marker
= BUF_MARKERS (current_buffer
);
576 int prev_to_byte
= from_byte
+ old_bytes
;
578 = (new_chars
- combined_before_bytes
) - (old_chars
+ combined_after_bytes
);
579 int diff_bytes
= new_bytes
- old_bytes
;
581 while (!NILP (marker
))
583 register struct Lisp_Marker
*m
= XMARKER (marker
);
585 if (m
->bytepos
>= prev_to_byte
587 /* If this is an insertion (replacing 0 chars),
588 reject the case of a marker that is at the
589 insertion point and should stay before the insertion. */
590 || m
->bytepos
> from_byte
|| m
->insertion_type
))
592 if (m
->bytepos
< prev_to_byte
+ combined_after_bytes
)
594 /* Put it after the combining bytes. */
595 m
->bytepos
= from_byte
+ new_bytes
+ combined_after_bytes
;
596 m
->charpos
= from
+ new_chars
- combined_before_bytes
;
600 m
->charpos
+= diff_chars
;
601 m
->bytepos
+= diff_bytes
;
604 else if (m
->bytepos
>= from_byte
)
607 m
->bytepos
= from_byte
+ combined_before_bytes
;
608 /* If all new bytes are combined in addition to that there
609 are after combining bytes, we must set byte position of
610 the marker after the after combining bytes. */
611 if (combined_before_bytes
== new_bytes
)
612 m
->bytepos
+= combined_after_bytes
;
622 /* Make the gap NBYTES_ADDED bytes longer. */
625 make_gap (nbytes_added
)
628 unsigned char *result
;
631 int real_gap_loc_byte
;
634 /* If we have to get more space, get enough to last a while. */
635 nbytes_added
+= 2000;
637 /* Don't allow a buffer size that won't fit in an int
638 even if it will fit in a Lisp integer.
639 That won't work because so many places use `int'. */
641 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
642 >= ((unsigned) 1 << (min (BITS_PER_INT
, VALBITS
) - 1)))
643 error ("Buffer exceeds maximum size");
646 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
647 result
= BUFFER_REALLOC (BEG_ADDR
, (Z_BYTE
- BEG_BYTE
648 + GAP_SIZE
+ nbytes_added
+ 1));
656 /* We can't unblock until the new address is properly stored. */
660 /* Prevent quitting in move_gap. */
665 real_gap_loc_byte
= GPT_BYTE
;
666 old_gap_size
= GAP_SIZE
;
668 /* Call the newly allocated space a gap at the end of the whole space. */
670 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
671 GAP_SIZE
= nbytes_added
;
673 /* Move the new gap down to be consecutive with the end of the old one.
674 This adjusts the markers properly too. */
675 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
677 /* Now combine the two into one large gap. */
678 GAP_SIZE
+= old_gap_size
;
680 GPT_BYTE
= real_gap_loc_byte
;
688 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
689 FROM_MULTIBYTE says whether the incoming text is multibyte.
690 TO_MULTIBYTE says whether to store the text as multibyte.
691 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
693 Return the number of bytes stored at TO_ADDR. */
696 copy_text (from_addr
, to_addr
, nbytes
,
697 from_multibyte
, to_multibyte
)
698 unsigned char *from_addr
;
699 unsigned char *to_addr
;
701 int from_multibyte
, to_multibyte
;
703 if (from_multibyte
== to_multibyte
)
705 bcopy (from_addr
, to_addr
, nbytes
);
708 else if (from_multibyte
)
711 int bytes_left
= nbytes
;
712 Lisp_Object tbl
= Qnil
, temp
;
714 /* We set the variable tbl to the reverse table of
715 Vnonascii_translation_table in advance. */
716 if (CHAR_TABLE_P (Vnonascii_translation_table
))
718 tbl
= Fchar_table_extra_slot (Vnonascii_translation_table
,
720 if (!CHAR_TABLE_P (tbl
))
724 /* Convert multibyte to single byte. */
725 while (bytes_left
> 0)
727 int thislen
, c
, c_save
;
728 c
= c_save
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
729 if (!SINGLE_BYTE_CHAR_P (c
))
730 c
= multibyte_char_to_unibyte (c
, tbl
);
732 from_addr
+= thislen
;
733 bytes_left
-= thislen
;
740 unsigned char *initial_to_addr
= to_addr
;
742 /* Convert single-byte to multibyte. */
745 int c
= *from_addr
++;
746 unsigned char workbuf
[4], *str
;
751 || (c
>= 0200 && !NILP (Vnonascii_translation_table
))))
753 c
= unibyte_char_to_multibyte (c
);
754 len
= CHAR_STRING (c
, workbuf
, str
);
755 bcopy (str
, to_addr
, len
);
760 /* Special case for speed. */
761 *to_addr
++ = c
, nbytes
--;
763 return to_addr
- initial_to_addr
;
767 /* Return the number of bytes it would take
768 to convert some single-byte text to multibyte.
769 The single-byte text consists of NBYTES bytes at PTR. */
772 count_size_as_multibyte (ptr
, nbytes
)
777 int outgoing_nbytes
= 0;
779 for (i
= 0; i
< nbytes
; i
++)
781 unsigned int c
= *ptr
++;
783 if (c
< 0200 || (c
< 0240 && NILP (Vnonascii_translation_table
)))
787 c
= unibyte_char_to_multibyte (c
);
788 outgoing_nbytes
+= CHAR_BYTES (c
);
792 return outgoing_nbytes
;
795 /* Insert a string of specified length before point.
796 This function judges multibyteness based on
797 enable_multibyte_characters in the current buffer;
798 it never converts between single-byte and multibyte.
800 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
801 prepare_to_modify_buffer could relocate the text. */
804 insert (string
, nbytes
)
805 register unsigned char *string
;
811 insert_1 (string
, nbytes
, 0, 1, 0);
812 signal_after_change (opoint
, 0, PT
- opoint
);
816 /* Likewise, but inherit text properties from neighboring characters. */
819 insert_and_inherit (string
, nbytes
)
820 register unsigned char *string
;
826 insert_1 (string
, nbytes
, 1, 1, 0);
827 signal_after_change (opoint
, 0, PT
- opoint
);
831 /* Insert the character C before point. Do not inherit text properties. */
837 unsigned char workbuf
[4], *str
;
840 if (! NILP (current_buffer
->enable_multibyte_characters
))
841 len
= CHAR_STRING (c
, workbuf
, str
);
852 /* Insert the null-terminated string S before point. */
858 insert (s
, strlen (s
));
861 /* Like `insert' except that all markers pointing at the place where
862 the insertion happens are adjusted to point after it.
863 Don't use this function to insert part of a Lisp string,
864 since gc could happen and relocate it. */
867 insert_before_markers (string
, nbytes
)
868 unsigned char *string
;
875 insert_1 (string
, nbytes
, 0, 1, 1);
876 signal_after_change (opoint
, 0, PT
- opoint
);
880 /* Likewise, but inherit text properties from neighboring characters. */
883 insert_before_markers_and_inherit (string
, nbytes
)
884 unsigned char *string
;
891 insert_1 (string
, nbytes
, 1, 1, 1);
892 signal_after_change (opoint
, 0, PT
- opoint
);
896 /* Subroutine used by the insert functions above. */
899 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
900 register unsigned char *string
;
902 int inherit
, prepare
, before_markers
;
904 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
905 inherit
, prepare
, before_markers
);
908 /* See if the bytes before POS/POS_BYTE combine with bytes
909 at the start of STRING to form a single character.
910 If so, return the number of bytes at the start of STRING
911 which combine in this way. Otherwise, return 0. */
914 count_combining_before (string
, length
, pos
, pos_byte
)
915 unsigned char *string
;
919 int opos
= pos
, opos_byte
= pos_byte
;
921 unsigned char *p
= string
;
923 if (NILP (current_buffer
->enable_multibyte_characters
))
925 if (length
== 0 || CHAR_HEAD_P (*string
))
929 c
= FETCH_BYTE (pos_byte
- 1);
930 if (ASCII_BYTE_P (c
))
932 DEC_BOTH (pos
, pos_byte
);
933 c
= FETCH_BYTE (pos_byte
);
934 if (! BASE_LEADING_CODE_P (c
))
937 /* We have a combination situation.
938 Count the bytes at STRING that will combine. */
939 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
945 /* See if the bytes after POS/POS_BYTE combine with bytes
946 at the end of STRING to form a single character.
947 If so, return the number of bytes after POS/POS_BYTE
948 which combine in this way. Otherwise, return 0. */
951 count_combining_after (string
, length
, pos
, pos_byte
)
952 unsigned char *string
;
956 int opos
= pos
, opos_byte
= pos_byte
;
960 if (NILP (current_buffer
->enable_multibyte_characters
))
962 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1]))
965 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
971 /* All characters in `string' are not character head.
972 We must check also preceding bytes at POS.
973 We are sure that the gap is at POS. */
976 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
978 if (i
< 0 || !BASE_LEADING_CODE_P (string
[i
]))
981 else if (!BASE_LEADING_CODE_P (string
[i
]))
986 c
= FETCH_BYTE (pos_byte
);
989 while (pos_byte
< Z_BYTE
)
991 c
= FETCH_BYTE (pos_byte
);
997 return pos_byte
- opos_byte
;
1000 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
1001 following the position POS/POS_BYTE to the character preceding POS.
1002 If TARGET is after POS+NBYTES, we only have to adjust the character
1003 position TARGET, else, if TARGET is after POS, we have to adjust
1004 both the character position TARGET and the byte position
1005 TARGET_BYTE, else we don't have to do any adjustment. */
1007 #define ADJUST_CHAR_POS(target, target_byte) \
1009 if (target > pos + nbytes) \
1011 else if (target >= pos) \
1014 target_byte = pos_byte + nbytes; \
1018 /* Combine NBYTES stray trailing-codes, which were formerly separate
1019 characters, with the preceding character. These bytes
1020 are located after position POS / POS_BYTE, and the preceding character
1021 is located just before that position.
1023 This function does not adjust markers for byte combining. That
1024 should be done in advance by the functions
1025 adjust_markers_for_insert or adjust_markers_for_replace. */
1028 combine_bytes (pos
, pos_byte
, nbytes
)
1029 int pos
, pos_byte
, nbytes
;
1031 adjust_overlays_for_delete (pos
, nbytes
);
1033 ADJUST_CHAR_POS (BUF_PT (current_buffer
), BUF_PT_BYTE (current_buffer
));
1034 ADJUST_CHAR_POS (GPT
, GPT_BYTE
);
1035 ADJUST_CHAR_POS (Z
, Z_BYTE
);
1036 ADJUST_CHAR_POS (ZV
, ZV_BYTE
);
1038 if (BUF_INTERVALS (current_buffer
) != 0)
1039 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1040 offset_intervals (current_buffer
, pos
, - nbytes
);
1044 byte_combining_error ()
1046 error ("Byte combining across boundary of accessible buffer text inhibitted");
1049 /* If we are going to combine bytes at POS which is at a narrowed
1050 region boundary, signal an error. */
1051 #define CHECK_BYTE_COMBINING_FOR_INSERT(pos) \
1053 if (combined_before_bytes && pos == BEGV \
1054 || combined_after_bytes && pos == ZV) \
1055 byte_combining_error (); \
1059 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1060 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
1061 are the same as in insert_1. */
1064 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
1065 register unsigned char *string
;
1066 register int nchars
, nbytes
;
1067 int inherit
, prepare
, before_markers
;
1069 register Lisp_Object temp
;
1070 int combined_before_bytes
, combined_after_bytes
;
1072 if (NILP (current_buffer
->enable_multibyte_characters
))
1076 /* Do this before moving and increasing the gap,
1077 because the before-change hooks might move the gap
1078 or make it smaller. */
1079 prepare_to_modify_buffer (PT
, PT
, NULL
);
1082 move_gap_both (PT
, PT_BYTE
);
1083 if (GAP_SIZE
< nbytes
)
1084 make_gap (nbytes
- GAP_SIZE
);
1086 combined_before_bytes
1087 = count_combining_before (string
, nbytes
, PT
, PT_BYTE
);
1088 combined_after_bytes
1089 = count_combining_after (string
, nbytes
, PT
, PT_BYTE
);
1090 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1092 /* Record deletion of the surrounding text that combines with
1093 the insertion. This, together with recording the insertion,
1094 will add up to the right stuff in the undo list.
1096 But there is no need to actually delete the combining bytes
1097 from the buffer and reinsert them. */
1099 if (combined_after_bytes
)
1101 Lisp_Object deletion
;
1104 if (! EQ (current_buffer
->undo_list
, Qt
))
1105 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1106 PT
+ combined_after_bytes
,
1107 PT_BYTE
+ combined_after_bytes
, 1);
1109 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1110 PT
+ combined_after_bytes
,
1111 PT_BYTE
+ combined_after_bytes
);
1112 if (! EQ (current_buffer
->undo_list
, Qt
))
1113 record_delete (PT
, deletion
);
1116 if (combined_before_bytes
)
1118 Lisp_Object deletion
;
1121 if (! EQ (current_buffer
->undo_list
, Qt
))
1122 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1124 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1126 if (! EQ (current_buffer
->undo_list
, Qt
))
1127 record_delete (PT
- 1, deletion
);
1130 record_insert (PT
- !!combined_before_bytes
,
1131 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1134 bcopy (string
, GPT_ADDR
, nbytes
);
1137 /* When we have combining at the end of the insertion,
1138 this is the character position before the combined character. */
1145 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1147 if (combined_after_bytes
)
1148 move_gap_both (GPT
+ combined_after_bytes
,
1149 GPT_BYTE
+ combined_after_bytes
);
1154 adjust_overlays_for_insert (PT
, nchars
);
1155 adjust_markers_for_insert (PT
, PT_BYTE
,
1156 PT
+ nchars
, PT_BYTE
+ nbytes
,
1157 combined_before_bytes
, combined_after_bytes
,
1160 #ifdef USE_TEXT_PROPERTIES
1161 if (BUF_INTERVALS (current_buffer
) != 0)
1162 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1163 offset_intervals (current_buffer
, PT
, nchars
);
1165 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
1166 Fset_text_properties (make_number (PT
), make_number (PT
+ nchars
),
1171 int pos
= PT
, pos_byte
= PT_BYTE
;
1173 adjust_point (nchars
+ combined_after_bytes
,
1174 nbytes
+ combined_after_bytes
);
1176 if (combined_after_bytes
)
1177 combine_bytes (pos
+ nchars
, pos_byte
+ nbytes
, combined_after_bytes
);
1179 if (combined_before_bytes
)
1180 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1186 /* Insert the part of the text of STRING, a Lisp object assumed to be
1187 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1188 starting at position POS / POS_BYTE. If the text of STRING has properties,
1189 copy them into the buffer.
1191 It does not work to use `insert' for this, because a GC could happen
1192 before we bcopy the stuff into the buffer, and relocate the string
1193 without insert noticing. */
1196 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
1198 register int pos
, pos_byte
, length
, length_byte
;
1202 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1204 signal_after_change (opoint
, 0, PT
- opoint
);
1207 /* Like `insert_from_string' except that all markers pointing
1208 at the place where the insertion happens are adjusted to point after it. */
1211 insert_from_string_before_markers (string
, pos
, pos_byte
,
1212 length
, length_byte
, inherit
)
1214 register int pos
, pos_byte
, length
, length_byte
;
1218 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1220 signal_after_change (opoint
, 0, PT
- opoint
);
1223 /* Subroutine of the insertion functions above. */
1226 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1227 inherit
, before_markers
)
1229 register int pos
, pos_byte
, nchars
, nbytes
;
1230 int inherit
, before_markers
;
1232 register Lisp_Object temp
;
1233 struct gcpro gcpro1
;
1234 int outgoing_nbytes
= nbytes
;
1235 int combined_before_bytes
, combined_after_bytes
;
1236 int adjusted_nchars
;
1239 /* Make OUTGOING_NBYTES describe the text
1240 as it will be inserted in this buffer. */
1242 if (NILP (current_buffer
->enable_multibyte_characters
))
1243 outgoing_nbytes
= nchars
;
1244 else if (! STRING_MULTIBYTE (string
))
1246 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
1250 /* Do this before moving and increasing the gap,
1251 because the before-change hooks might move the gap
1252 or make it smaller. */
1253 prepare_to_modify_buffer (PT
, PT
, NULL
);
1256 move_gap_both (PT
, PT_BYTE
);
1257 if (GAP_SIZE
< outgoing_nbytes
)
1258 make_gap (outgoing_nbytes
- GAP_SIZE
);
1261 /* Copy the string text into the buffer, perhaps converting
1262 between single-byte and multibyte. */
1263 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1264 STRING_MULTIBYTE (string
),
1265 ! NILP (current_buffer
->enable_multibyte_characters
));
1267 /* We have copied text into the gap, but we have not altered
1268 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1269 to these functions and get the same results as we would
1270 have got earlier on. Meanwhile, PT_ADDR does point to
1271 the text that has been stored by copy_text. */
1273 combined_before_bytes
1274 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1275 combined_after_bytes
1276 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1278 unsigned char save
= *(GPT_ADDR
);
1280 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1284 /* Record deletion of the surrounding text that combines with
1285 the insertion. This, together with recording the insertion,
1286 will add up to the right stuff in the undo list.
1288 But there is no need to actually delete the combining bytes
1289 from the buffer and reinsert them. */
1291 if (combined_after_bytes
)
1293 Lisp_Object deletion
;
1296 if (! EQ (current_buffer
->undo_list
, Qt
))
1297 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1298 PT
+ combined_after_bytes
,
1299 PT_BYTE
+ combined_after_bytes
, 1);
1301 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1302 PT
+ combined_after_bytes
,
1303 PT_BYTE
+ combined_after_bytes
);
1304 if (! EQ (current_buffer
->undo_list
, Qt
))
1305 record_delete (PT
, deletion
);
1308 if (combined_before_bytes
)
1310 Lisp_Object deletion
;
1313 if (! EQ (current_buffer
->undo_list
, Qt
))
1314 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1316 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1318 if (! EQ (current_buffer
->undo_list
, Qt
))
1319 record_delete (PT
- 1, deletion
);
1322 record_insert (PT
- !!combined_before_bytes
,
1323 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1326 GAP_SIZE
-= outgoing_nbytes
;
1330 GPT_BYTE
+= outgoing_nbytes
;
1331 ZV_BYTE
+= outgoing_nbytes
;
1332 Z_BYTE
+= outgoing_nbytes
;
1333 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1335 if (combined_after_bytes
)
1336 move_gap_both (GPT
+ combined_after_bytes
,
1337 GPT_BYTE
+ combined_after_bytes
);
1342 adjust_overlays_for_insert (PT
, nchars
);
1343 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1344 PT_BYTE
+ outgoing_nbytes
,
1345 combined_before_bytes
, combined_after_bytes
,
1348 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1349 offset_intervals (current_buffer
, PT
, nchars
);
1351 intervals
= XSTRING (string
)->intervals
;
1352 /* Get the intervals for the part of the string we are inserting--
1353 not including the combined-before bytes. */
1354 if (nbytes
< STRING_BYTES (XSTRING (string
)))
1355 intervals
= copy_intervals (intervals
, pos
, nchars
);
1357 /* Insert those intervals. */
1358 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1359 current_buffer
, inherit
);
1362 int pos
= PT
, pos_byte
= PT_BYTE
;
1364 adjust_point (nchars
+ combined_after_bytes
,
1365 outgoing_nbytes
+ combined_after_bytes
);
1367 if (combined_after_bytes
)
1368 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1369 combined_after_bytes
);
1371 if (combined_before_bytes
)
1372 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1376 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1377 current buffer. If the text in BUF has properties, they are absorbed
1378 into the current buffer.
1380 It does not work to use `insert' for this, because a malloc could happen
1381 and relocate BUF's text before the bcopy happens. */
1384 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1386 int charpos
, nchars
;
1391 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1392 signal_after_change (opoint
, 0, PT
- opoint
);
1396 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1401 register Lisp_Object temp
;
1402 int chunk
, chunk_expanded
;
1403 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1404 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1405 int incoming_nbytes
= to_byte
- from_byte
;
1406 int outgoing_nbytes
= incoming_nbytes
;
1407 int combined_before_bytes
, combined_after_bytes
;
1408 int adjusted_nchars
;
1411 /* Make OUTGOING_NBYTES describe the text
1412 as it will be inserted in this buffer. */
1414 if (NILP (current_buffer
->enable_multibyte_characters
))
1415 outgoing_nbytes
= nchars
;
1416 else if (NILP (buf
->enable_multibyte_characters
))
1418 int outgoing_before_gap
= 0;
1419 int outgoing_after_gap
= 0;
1421 if (from
< BUF_GPT (buf
))
1423 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1424 if (chunk
> incoming_nbytes
)
1425 chunk
= incoming_nbytes
;
1427 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1433 if (chunk
< incoming_nbytes
)
1435 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1437 incoming_nbytes
- chunk
);
1439 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1442 /* Make sure point-max won't overflow after this insertion. */
1443 XSETINT (temp
, outgoing_nbytes
+ Z
);
1444 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1445 error ("Maximum buffer size exceeded");
1447 /* Do this before moving and increasing the gap,
1448 because the before-change hooks might move the gap
1449 or make it smaller. */
1450 prepare_to_modify_buffer (PT
, PT
, NULL
);
1453 move_gap_both (PT
, PT_BYTE
);
1454 if (GAP_SIZE
< outgoing_nbytes
)
1455 make_gap (outgoing_nbytes
- GAP_SIZE
);
1457 if (from
< BUF_GPT (buf
))
1459 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1460 if (chunk
> incoming_nbytes
)
1461 chunk
= incoming_nbytes
;
1462 /* Record number of output bytes, so we know where
1463 to put the output from the second copy_text. */
1465 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1467 ! NILP (buf
->enable_multibyte_characters
),
1468 ! NILP (current_buffer
->enable_multibyte_characters
));
1471 chunk_expanded
= chunk
= 0;
1473 if (chunk
< incoming_nbytes
)
1474 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1475 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1476 ! NILP (buf
->enable_multibyte_characters
),
1477 ! NILP (current_buffer
->enable_multibyte_characters
));
1479 /* We have copied text into the gap, but we have not altered
1480 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1481 to these functions and get the same results as we would
1482 have got earlier on. Meanwhile, GPT_ADDR does point to
1483 the text that has been stored by copy_text. */
1484 combined_before_bytes
1485 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1486 combined_after_bytes
1487 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1489 unsigned char save
= *(GPT_ADDR
);
1491 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1495 /* Record deletion of the surrounding text that combines with
1496 the insertion. This, together with recording the insertion,
1497 will add up to the right stuff in the undo list.
1499 But there is no need to actually delete the combining bytes
1500 from the buffer and reinsert them. */
1502 if (combined_after_bytes
)
1504 Lisp_Object deletion
;
1507 if (! EQ (current_buffer
->undo_list
, Qt
))
1508 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1509 PT
+ combined_after_bytes
,
1510 PT_BYTE
+ combined_after_bytes
, 1);
1512 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1513 PT
+ combined_after_bytes
,
1514 PT_BYTE
+ combined_after_bytes
);
1515 if (! EQ (current_buffer
->undo_list
, Qt
))
1516 record_delete (PT
, deletion
);
1519 if (combined_before_bytes
)
1521 Lisp_Object deletion
;
1524 if (! EQ (current_buffer
->undo_list
, Qt
))
1525 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1527 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1529 if (! EQ (current_buffer
->undo_list
, Qt
))
1530 record_delete (PT
- 1, deletion
);
1533 record_insert (PT
- !!combined_before_bytes
,
1534 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1537 GAP_SIZE
-= outgoing_nbytes
;
1541 GPT_BYTE
+= outgoing_nbytes
;
1542 ZV_BYTE
+= outgoing_nbytes
;
1543 Z_BYTE
+= outgoing_nbytes
;
1544 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1546 if (combined_after_bytes
)
1547 move_gap_both (GPT
+ combined_after_bytes
,
1548 GPT_BYTE
+ combined_after_bytes
);
1553 adjust_overlays_for_insert (PT
, nchars
);
1554 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1555 PT_BYTE
+ outgoing_nbytes
,
1556 combined_before_bytes
, combined_after_bytes
, 0);
1558 #ifdef USE_TEXT_PROPERTIES
1559 if (BUF_INTERVALS (current_buffer
) != 0)
1560 offset_intervals (current_buffer
, PT
, nchars
);
1563 /* Get the intervals for the part of the string we are inserting--
1564 not including the combined-before bytes. */
1565 intervals
= BUF_INTERVALS (buf
);
1566 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1567 intervals
= copy_intervals (intervals
, from
, nchars
);
1569 /* Insert those intervals. */
1570 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1573 int pos
= PT
, pos_byte
= PT_BYTE
;
1575 adjust_point (nchars
+ combined_after_bytes
,
1576 outgoing_nbytes
+ combined_after_bytes
);
1578 if (combined_after_bytes
)
1579 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1580 combined_after_bytes
);
1582 if (combined_before_bytes
)
1583 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1587 /* This function should be called after moving gap to FROM and before
1588 altering text between FROM and TO. This adjusts various position
1589 keepers and markers as if the text is deleted. Don't forget to
1590 call adjust_after_replace after you actually alter the text. */
1593 adjust_before_replace (from
, from_byte
, to
, to_byte
)
1594 int from
, from_byte
, to
, to_byte
;
1596 Lisp_Object deletion
;
1598 if (! EQ (current_buffer
->undo_list
, Qt
))
1599 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1603 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1605 if (! EQ (current_buffer
->undo_list
, Qt
))
1606 record_delete (from
, deletion
);
1608 adjust_overlays_for_delete (from
, to
- from
);
1611 /* Record undo information and adjust markers and position keepers for
1612 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1613 chars (LEN_BYTE bytes) which resides in the gap just after
1616 PREV_TEXT nil means the new text was just inserted. */
1619 adjust_after_replace (from
, from_byte
, prev_text
, len
, len_byte
)
1620 int from
, from_byte
, len
, len_byte
;
1621 Lisp_Object prev_text
;
1623 int combined_before_bytes
1624 = count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
);
1625 int combined_after_bytes
1626 = count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
);
1627 /* This flag tells if we combine some bytes with a character before
1628 FROM. This happens even if combined_before_bytes is zero. */
1629 int combine_before
= (combined_before_bytes
1630 || (len
== 0 && combined_after_bytes
));
1632 int nchars_del
= 0, nbytes_del
= 0;
1634 if (STRINGP (prev_text
))
1636 nchars_del
= XSTRING (prev_text
)->size
;
1637 nbytes_del
= STRING_BYTES (XSTRING (prev_text
));
1640 if (combine_before
&& from
== BEGV
1641 || combined_after_bytes
&& from
== ZV
)
1643 /* We can't combine bytes nor signal an error here. So, let's
1644 pretend that the new text is just a single space. */
1646 combined_before_bytes
= combined_after_bytes
= 0;
1650 if (combined_after_bytes
)
1652 Lisp_Object deletion
;
1655 if (! EQ (current_buffer
->undo_list
, Qt
))
1656 deletion
= make_buffer_string_both (from
, from_byte
,
1657 from
+ combined_after_bytes
,
1658 from_byte
+ combined_after_bytes
,
1661 adjust_markers_for_record_delete (from
, from_byte
,
1662 from
+ combined_after_bytes
,
1663 from_byte
+ combined_after_bytes
);
1665 if (! EQ (current_buffer
->undo_list
, Qt
))
1666 record_delete (from
+ nchars_del
, deletion
);
1669 if (combined_before_bytes
1670 || len_byte
== 0 && combined_after_bytes
> 0)
1672 Lisp_Object deletion
;
1675 if (! EQ (current_buffer
->undo_list
, Qt
))
1676 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1677 from
, from_byte
, 1);
1678 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1680 if (! EQ (current_buffer
->undo_list
, Qt
))
1681 record_delete (from
- 1, deletion
);
1684 /* Update various buffer positions for the new text. */
1685 GAP_SIZE
-= len_byte
;
1687 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1688 GPT
+= len
; GPT_BYTE
+= len_byte
;
1689 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1691 /* The gap should be at character boundary. */
1692 if (combined_after_bytes
)
1693 move_gap_both (GPT
+ combined_after_bytes
,
1694 GPT_BYTE
+ combined_after_bytes
);
1696 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1698 combined_before_bytes
, combined_after_bytes
);
1699 if (! EQ (current_buffer
->undo_list
, Qt
))
1702 record_delete (from
- combine_before
, prev_text
);
1704 record_insert (from
- 1, len
- combined_before_bytes
+ 1);
1706 record_insert (from
, len
);
1709 if (len
> nchars_del
)
1710 adjust_overlays_for_insert (from
, len
- nchars_del
);
1711 else if (len
< nchars_del
)
1712 adjust_overlays_for_delete (from
, nchars_del
- len
);
1713 #ifdef USE_TEXT_PROPERTIES
1714 if (BUF_INTERVALS (current_buffer
) != 0)
1716 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1721 int pos
= PT
, pos_byte
= PT_BYTE
;
1724 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1726 if (combined_after_bytes
)
1728 if (combined_before_bytes
== len_byte
)
1729 /* This is the case that all new bytes are combined. */
1730 combined_before_bytes
+= combined_after_bytes
;
1732 combine_bytes (from
+ len
, from_byte
+ len_byte
,
1733 combined_after_bytes
);
1735 if (combined_before_bytes
)
1736 combine_bytes (from
, from_byte
, combined_before_bytes
);
1739 /* As byte combining will decrease Z, we must check this again. */
1740 if (Z
- GPT
< end_unchanged
)
1741 end_unchanged
= Z
- GPT
;
1746 evaporate_overlays (from
);
1750 /* Record undo information, adjust markers and position keepers for an
1751 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1752 text already exists in the current buffer but character length (TO
1753 - FROM) may be incorrect, the correct length is NEWLEN. */
1756 adjust_after_insert (from
, from_byte
, to
, to_byte
, newlen
)
1757 int from
, from_byte
, to
, to_byte
, newlen
;
1759 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
1762 move_gap_both (to
, to_byte
);
1763 GAP_SIZE
+= len_byte
;
1764 GPT
-= len
; GPT_BYTE
-= len_byte
;
1765 ZV
-= len
; ZV_BYTE
-= len_byte
;
1766 Z
-= len
; Z_BYTE
-= len_byte
;
1767 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1770 /* Replace the text from character positions FROM to TO with NEW,
1771 If PREPARE is nonzero, call prepare_to_modify_buffer.
1772 If INHERIT, the newly inserted text should inherit text properties
1773 from the surrounding non-deleted text. */
1775 /* Note that this does not yet handle markers quite right.
1776 Also it needs to record a single undo-entry that does a replacement
1777 rather than a separate delete and insert.
1778 That way, undo will also handle markers properly.
1780 But if MARKERS is 0, don't relocate markers. */
1783 replace_range (from
, to
, new, prepare
, inherit
, markers
)
1785 int from
, to
, prepare
, inherit
, markers
;
1787 int inschars
= XSTRING (new)->size
;
1788 int insbytes
= STRING_BYTES (XSTRING (new));
1789 int from_byte
, to_byte
;
1790 int nbytes_del
, nchars_del
;
1791 register Lisp_Object temp
;
1792 struct gcpro gcpro1
;
1793 int combined_before_bytes
, combined_after_bytes
;
1794 int adjusted_inschars
;
1796 int outgoing_insbytes
= insbytes
;
1797 Lisp_Object deletion
;
1805 int range_length
= to
- from
;
1806 prepare_to_modify_buffer (from
, to
, &from
);
1807 to
= from
+ range_length
;
1812 /* Make args be valid */
1818 from_byte
= CHAR_TO_BYTE (from
);
1819 to_byte
= CHAR_TO_BYTE (to
);
1821 nchars_del
= to
- from
;
1822 nbytes_del
= to_byte
- from_byte
;
1824 if (nbytes_del
<= 0 && insbytes
== 0)
1827 /* Make OUTGOING_INSBYTES describe the text
1828 as it will be inserted in this buffer. */
1830 if (NILP (current_buffer
->enable_multibyte_characters
))
1831 outgoing_insbytes
= inschars
;
1832 else if (! STRING_MULTIBYTE (new))
1834 = count_size_as_multibyte (XSTRING (new)->data
, insbytes
);
1836 /* Make sure point-max won't overflow after this insertion. */
1837 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1838 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1839 error ("Maximum buffer size exceeded");
1843 /* Make sure the gap is somewhere in or next to what we are deleting. */
1845 gap_right (from
, from_byte
);
1847 gap_left (to
, to_byte
, 0);
1849 /* Even if we don't record for undo, we must keep the original text
1850 because we may have to recover it because of inappropriate byte
1852 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1855 /* Relocate all markers pointing into the new, larger gap
1856 to point at the end of the text before the gap.
1857 Do this before recording the deletion,
1858 so that undo handles this after reinserting the text. */
1859 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1861 GAP_SIZE
+= nbytes_del
;
1864 ZV_BYTE
-= nbytes_del
;
1865 Z_BYTE
-= nbytes_del
;
1867 GPT_BYTE
= from_byte
;
1868 *(GPT_ADDR
) = 0; /* Put an anchor. */
1873 if (GPT
- BEG
< beg_unchanged
)
1874 beg_unchanged
= GPT
- BEG
;
1875 if (Z
- GPT
< end_unchanged
)
1876 end_unchanged
= Z
- GPT
;
1878 if (GAP_SIZE
< insbytes
)
1879 make_gap (insbytes
- GAP_SIZE
);
1881 /* Copy the string text into the buffer, perhaps converting
1882 between single-byte and multibyte. */
1883 copy_text (XSTRING (new)->data
, GPT_ADDR
, insbytes
,
1884 STRING_MULTIBYTE (new),
1885 ! NILP (current_buffer
->enable_multibyte_characters
));
1887 /* We have copied text into the gap, but we have not marked
1888 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1889 here, for both the previous text and the following text.
1890 Meanwhile, GPT_ADDR does point to
1891 the text that has been stored by copy_text. */
1893 combined_before_bytes
1894 = count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1895 combined_after_bytes
1896 = count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1898 if (combined_before_bytes
&& from
== BEGV
1899 || combined_after_bytes
&& from
== ZV
)
1901 /* Bytes are being combined across the region boundary. We
1902 should avoid it. We recover the original contents before
1903 signaling an error. */
1904 bcopy (XSTRING (deletion
)->data
, GPT_ADDR
, nbytes_del
);
1905 GAP_SIZE
-= nbytes_del
;
1908 ZV_BYTE
+= nbytes_del
;
1909 Z_BYTE
+= nbytes_del
;
1910 GPT
= from
+ nchars_del
;
1911 GPT_BYTE
= from_byte
+ nbytes_del
;
1912 *(GPT_ADDR
) = 0; /* Put an anchor. */
1914 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
, 0, 0, 0);
1916 byte_combining_error ();
1920 /* Record deletion of the surrounding text that combines with
1921 the insertion. This, together with recording the insertion,
1922 will add up to the right stuff in the undo list.
1924 But there is no need to actually delete the combining bytes
1925 from the buffer and reinsert them. */
1927 if (combined_after_bytes
)
1929 Lisp_Object deletion
;
1932 if (! EQ (current_buffer
->undo_list
, Qt
))
1933 deletion
= make_buffer_string_both (from
, from_byte
,
1934 from
+ combined_after_bytes
,
1935 from_byte
+ combined_after_bytes
,
1938 adjust_markers_for_record_delete (from
, from_byte
,
1939 from
+ combined_after_bytes
,
1940 from_byte
+ combined_after_bytes
);
1941 if (! EQ (current_buffer
->undo_list
, Qt
))
1942 record_delete (from
+ nchars_del
, deletion
);
1945 if (combined_before_bytes
)
1947 Lisp_Object deletion
;
1950 if (! EQ (current_buffer
->undo_list
, Qt
))
1951 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1952 from
, from_byte
, 1);
1953 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1955 if (! EQ (current_buffer
->undo_list
, Qt
))
1956 record_delete (from
- 1, deletion
);
1959 if (! EQ (current_buffer
->undo_list
, Qt
))
1961 record_delete (from
- !!combined_before_bytes
, deletion
);
1962 record_insert (from
- !!combined_before_bytes
,
1963 (inschars
- combined_before_bytes
1964 + !!combined_before_bytes
));
1967 GAP_SIZE
-= outgoing_insbytes
;
1971 GPT_BYTE
+= outgoing_insbytes
;
1972 ZV_BYTE
+= outgoing_insbytes
;
1973 Z_BYTE
+= outgoing_insbytes
;
1974 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1976 if (combined_after_bytes
)
1977 move_gap_both (GPT
+ combined_after_bytes
,
1978 GPT_BYTE
+ combined_after_bytes
);
1983 /* Adjust the overlay center as needed. This must be done after
1984 adjusting the markers that bound the overlays. */
1985 adjust_overlays_for_delete (from
, nchars_del
);
1986 adjust_overlays_for_insert (from
, inschars
);
1988 adjust_markers_for_insert (from
, from_byte
,
1989 from
+ inschars
, from_byte
+ outgoing_insbytes
,
1990 combined_before_bytes
, combined_after_bytes
, 0);
1992 #ifdef USE_TEXT_PROPERTIES
1993 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1995 /* Get the intervals for the part of the string we are inserting--
1996 not including the combined-before bytes. */
1997 intervals
= XSTRING (new)->intervals
;
1998 /* Insert those intervals. */
1999 graft_intervals_into_buffer (intervals
, from
, inschars
,
2000 current_buffer
, inherit
);
2003 /* Relocate point as if it were a marker. */
2005 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
2006 (from_byte
+ outgoing_insbytes
2007 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
2009 if (combined_after_bytes
)
2011 if (combined_before_bytes
== outgoing_insbytes
)
2012 /* This is the case that all new bytes are combined. */
2013 combined_before_bytes
+= combined_after_bytes
;
2015 combine_bytes (from
+ inschars
, from_byte
+ outgoing_insbytes
,
2016 combined_after_bytes
);
2018 if (combined_before_bytes
)
2019 combine_bytes (from
, from_byte
, combined_before_bytes
);
2021 /* As byte combining will decrease Z, we must check this again. */
2022 if (Z
- GPT
< end_unchanged
)
2023 end_unchanged
= Z
- GPT
;
2025 if (outgoing_insbytes
== 0)
2026 evaporate_overlays (from
);
2033 signal_after_change (from
, nchars_del
, GPT
- from
);
2036 /* Delete characters in current buffer
2037 from FROM up to (but not including) TO.
2038 If TO comes before FROM, we delete nothing. */
2041 del_range (from
, to
)
2042 register int from
, to
;
2044 del_range_1 (from
, to
, 1);
2047 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
2050 del_range_1 (from
, to
, prepare
)
2051 int from
, to
, prepare
;
2053 int from_byte
, to_byte
;
2055 #if !NO_PROMPT_IN_BUFFER
2056 if (INTEGERP (current_buffer
->minibuffer_prompt_length
))
2058 /* Don't delete part of a mini-buffer prompt. */
2059 int len
= XFASTINT (current_buffer
->minibuffer_prompt_length
);
2060 from
= max (from
, len
);
2062 #endif /* !NO_PROMPT_IN_BUFFER */
2064 /* Make args be valid */
2075 int range_length
= to
- from
;
2076 prepare_to_modify_buffer (from
, to
, &from
);
2077 to
= from
+ range_length
;
2080 from_byte
= CHAR_TO_BYTE (from
);
2081 to_byte
= CHAR_TO_BYTE (to
);
2083 del_range_2 (from
, from_byte
, to
, to_byte
);
2084 signal_after_change (from
, to
- from
, 0);
2087 /* Like del_range_1 but args are byte positions, not char positions. */
2090 del_range_byte (from_byte
, to_byte
, prepare
)
2091 int from_byte
, to_byte
, prepare
;
2095 /* Make args be valid */
2096 if (from_byte
< BEGV_BYTE
)
2097 from_byte
= BEGV_BYTE
;
2098 if (to_byte
> ZV_BYTE
)
2101 if (to_byte
<= from_byte
)
2104 from
= BYTE_TO_CHAR (from_byte
);
2105 to
= BYTE_TO_CHAR (to_byte
);
2109 int old_from
= from
, old_to
= Z
- to
;
2110 int range_length
= to
- from
;
2111 prepare_to_modify_buffer (from
, to
, &from
);
2112 to
= from
+ range_length
;
2114 if (old_from
!= from
)
2115 from_byte
= CHAR_TO_BYTE (from
);
2116 if (old_to
== Z
- to
)
2117 to_byte
= CHAR_TO_BYTE (to
);
2120 del_range_2 (from
, from_byte
, to
, to_byte
);
2121 signal_after_change (from
, to
- from
, 0);
2124 /* Like del_range_1, but positions are specified both as charpos
2128 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
2129 int from
, from_byte
, to
, to_byte
, prepare
;
2131 /* Make args be valid */
2132 if (from_byte
< BEGV_BYTE
)
2133 from_byte
= BEGV_BYTE
;
2134 if (to_byte
> ZV_BYTE
)
2137 if (to_byte
<= from_byte
)
2147 int old_from
= from
, old_to
= Z
- to
;
2148 int range_length
= to
- from
;
2149 prepare_to_modify_buffer (from
, to
, &from
);
2150 to
= from
+ range_length
;
2152 if (old_from
!= from
)
2153 from_byte
= CHAR_TO_BYTE (from
);
2154 if (old_to
== Z
- to
)
2155 to_byte
= CHAR_TO_BYTE (to
);
2158 del_range_2 (from
, from_byte
, to
, to_byte
);
2159 signal_after_change (from
, to
- from
, 0);
2162 /* Delete a range of text, specified both as character positions
2163 and byte positions. FROM and TO are character positions,
2164 while FROM_BYTE and TO_BYTE are byte positions. */
2167 del_range_2 (from
, from_byte
, to
, to_byte
)
2168 int from
, from_byte
, to
, to_byte
;
2170 register int nbytes_del
, nchars_del
;
2171 int combined_after_bytes
;
2172 Lisp_Object deletion
;
2177 nchars_del
= to
- from
;
2178 nbytes_del
= to_byte
- from_byte
;
2180 /* Make sure the gap is somewhere in or next to what we are deleting. */
2182 gap_right (from
, from_byte
);
2184 gap_left (to
, to_byte
, 0);
2186 combined_after_bytes
2187 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
2188 Z_BYTE
- to_byte
, from
, from_byte
);
2189 if (combined_after_bytes
)
2191 if (from
== BEGV
|| to
== ZV
)
2192 byte_combining_error ();
2193 from_byte_1
= from_byte
;
2194 DEC_POS (from_byte_1
);
2197 from_byte_1
= from_byte
;
2199 if (! EQ (current_buffer
->undo_list
, Qt
))
2201 = make_buffer_string_both (from
- !!combined_after_bytes
,
2203 to
+ combined_after_bytes
,
2204 to_byte
+ combined_after_bytes
, 1);
2205 if (combined_after_bytes
)
2206 /* COMBINED_AFTER_BYTES nonzero means that the above code moved
2207 the gap. We must move the gap again to a proper place. */
2208 move_gap_both (from
, from_byte
);
2210 /* Relocate all markers pointing into the new, larger gap
2211 to point at the end of the text before the gap.
2212 Do this before recording the deletion,
2213 so that undo handles this after reinserting the text. */
2214 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
2215 if (combined_after_bytes
)
2217 /* Adjust markers for the phony deletion
2218 that we are about to call record_undo for. */
2220 /* Here we delete the markers that formerly
2221 pointed at TO ... TO + COMBINED_AFTER_BYTES.
2222 But because of the call to adjust_markers_for_delete, above,
2223 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
2224 adjust_markers_for_record_delete (from
, from_byte
,
2225 from
+ combined_after_bytes
,
2226 from_byte
+ combined_after_bytes
);
2228 adjust_markers_for_record_delete (from
- 1, from_byte_1
,
2231 if (! EQ (current_buffer
->undo_list
, Qt
))
2232 record_delete (from
- !!combined_after_bytes
, deletion
);
2235 /* Relocate point as if it were a marker. */
2237 adjust_point (from
- (PT
< to
? PT
: to
),
2238 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
2240 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2241 offset_intervals (current_buffer
, from
, - nchars_del
);
2243 /* Adjust the overlay center as needed. This must be done after
2244 adjusting the markers that bound the overlays. */
2245 adjust_overlays_for_delete (from
, nchars_del
);
2247 GAP_SIZE
+= nbytes_del
;
2248 ZV_BYTE
-= nbytes_del
;
2249 Z_BYTE
-= nbytes_del
;
2253 GPT_BYTE
= from_byte
;
2255 if (combined_after_bytes
)
2256 move_gap_both (GPT
+ combined_after_bytes
,
2257 GPT_BYTE
+ combined_after_bytes
);
2259 *(GPT_ADDR
) = 0; /* Put an anchor. */
2264 if (GPT
- BEG
< beg_unchanged
)
2265 beg_unchanged
= GPT
- BEG
;
2266 if (Z
- GPT
< end_unchanged
)
2267 end_unchanged
= Z
- GPT
;
2269 if (combined_after_bytes
)
2271 /* Adjust markers for byte combining. As we have already
2272 adjuted markers without concerning byte combining, here we
2273 must concern only byte combining. */
2274 adjust_markers_for_replace (from
, from_byte
, 0, 0, 0, 0,
2275 0, combined_after_bytes
);
2276 combine_bytes (from
, from_byte
, combined_after_bytes
);
2278 record_insert (GPT
- 1, 1);
2280 if (Z
- GPT
< end_unchanged
)
2281 end_unchanged
= Z
- GPT
;
2286 evaporate_overlays (from
);
2289 /* Call this if you're about to change the region of BUFFER from
2290 character positions START to END. This checks the read-only
2291 properties of the region, calls the necessary modification hooks,
2292 and warns the next redisplay that it should pay attention to that
2296 modify_region (buffer
, start
, end
)
2297 struct buffer
*buffer
;
2300 struct buffer
*old_buffer
= current_buffer
;
2302 if (buffer
!= old_buffer
)
2303 set_buffer_internal (buffer
);
2305 prepare_to_modify_buffer (start
, end
, NULL
);
2307 if (start
- 1 < beg_unchanged
2308 || (unchanged_modified
== MODIFF
2309 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
2310 beg_unchanged
= start
- 1;
2311 if (Z
- end
< end_unchanged
2312 || (unchanged_modified
== MODIFF
2313 && overlay_unchanged_modified
== OVERLAY_MODIFF
))
2314 end_unchanged
= Z
- end
;
2316 if (MODIFF
<= SAVE_MODIFF
)
2317 record_first_change ();
2320 buffer
->point_before_scroll
= Qnil
;
2322 if (buffer
!= old_buffer
)
2323 set_buffer_internal (old_buffer
);
2326 /* Check that it is okay to modify the buffer between START and END,
2327 which are char positions.
2329 Run the before-change-function, if any. If intervals are in use,
2330 verify that the text to be modified is not read-only, and call
2331 any modification properties the text may have.
2333 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2334 by holding its value temporarily in a marker. */
2337 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
2341 if (!NILP (current_buffer
->read_only
))
2342 Fbarf_if_buffer_read_only ();
2344 /* Let redisplay consider other windows than selected_window
2345 if modifying another buffer. */
2346 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
2347 ++windows_or_buffers_changed
;
2349 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2350 if (BUF_INTERVALS (current_buffer
) != 0)
2354 Lisp_Object preserve_marker
;
2355 struct gcpro gcpro1
;
2356 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2357 GCPRO1 (preserve_marker
);
2358 verify_interval_modification (current_buffer
, start
, end
);
2359 *preserve_ptr
= marker_position (preserve_marker
);
2360 unchain_marker (preserve_marker
);
2364 verify_interval_modification (current_buffer
, start
, end
);
2367 #ifdef CLASH_DETECTION
2368 if (!NILP (current_buffer
->file_truename
)
2369 /* Make binding buffer-file-name to nil effective. */
2370 && !NILP (current_buffer
->filename
)
2371 && SAVE_MODIFF
>= MODIFF
)
2372 lock_file (current_buffer
->file_truename
);
2374 /* At least warn if this file has changed on disk since it was visited. */
2375 if (!NILP (current_buffer
->filename
)
2376 && SAVE_MODIFF
>= MODIFF
2377 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2378 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
2379 call1 (intern ("ask-user-about-supersession-threat"),
2380 current_buffer
->filename
);
2381 #endif /* not CLASH_DETECTION */
2383 signal_before_change (start
, end
, preserve_ptr
);
2385 if (current_buffer
->newline_cache
)
2386 invalidate_region_cache (current_buffer
,
2387 current_buffer
->newline_cache
,
2388 start
- BEG
, Z
- end
);
2389 if (current_buffer
->width_run_cache
)
2390 invalidate_region_cache (current_buffer
,
2391 current_buffer
->width_run_cache
,
2392 start
- BEG
, Z
- end
);
2394 Vdeactivate_mark
= Qt
;
2397 /* These macros work with an argument named `preserve_ptr'
2398 and a local variable named `preserve_marker'. */
2400 #define PRESERVE_VALUE \
2401 if (preserve_ptr && NILP (preserve_marker)) \
2402 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2404 #define RESTORE_VALUE \
2405 if (! NILP (preserve_marker)) \
2407 *preserve_ptr = marker_position (preserve_marker); \
2408 unchain_marker (preserve_marker); \
2411 #define PRESERVE_START_END \
2412 if (NILP (start_marker)) \
2413 start_marker = Fcopy_marker (start, Qnil); \
2414 if (NILP (end_marker)) \
2415 end_marker = Fcopy_marker (end, Qnil);
2417 #define FETCH_START \
2418 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2421 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2423 /* Signal a change to the buffer immediately before it happens.
2424 START_INT and END_INT are the bounds of the text to be changed.
2426 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2427 by holding its value temporarily in a marker. */
2430 signal_before_change (start_int
, end_int
, preserve_ptr
)
2431 int start_int
, end_int
;
2434 Lisp_Object start
, end
;
2435 Lisp_Object start_marker
, end_marker
;
2436 Lisp_Object preserve_marker
;
2437 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2439 if (inhibit_modification_hooks
)
2442 start
= make_number (start_int
);
2443 end
= make_number (end_int
);
2444 preserve_marker
= Qnil
;
2445 start_marker
= Qnil
;
2447 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2449 /* If buffer is unmodified, run a special hook for that case. */
2450 if (SAVE_MODIFF
>= MODIFF
2451 && !NILP (Vfirst_change_hook
)
2452 && !NILP (Vrun_hooks
))
2456 call1 (Vrun_hooks
, Qfirst_change_hook
);
2459 /* Run the before-change-function if any.
2460 We don't bother "binding" this variable to nil
2461 because it is obsolete anyway and new code should not use it. */
2462 if (!NILP (Vbefore_change_function
))
2466 call2 (Vbefore_change_function
, FETCH_START
, FETCH_END
);
2469 /* Now run the before-change-functions if any. */
2470 if (!NILP (Vbefore_change_functions
))
2472 Lisp_Object args
[3];
2473 Lisp_Object before_change_functions
;
2474 Lisp_Object after_change_functions
;
2475 struct gcpro gcpro1
, gcpro2
;
2480 /* "Bind" before-change-functions and after-change-functions
2481 to nil--but in a way that errors don't know about.
2482 That way, if there's an error in them, they will stay nil. */
2483 before_change_functions
= Vbefore_change_functions
;
2484 after_change_functions
= Vafter_change_functions
;
2485 Vbefore_change_functions
= Qnil
;
2486 Vafter_change_functions
= Qnil
;
2487 GCPRO2 (before_change_functions
, after_change_functions
);
2489 /* Actually run the hook functions. */
2490 args
[0] = Qbefore_change_functions
;
2491 args
[1] = FETCH_START
;
2492 args
[2] = FETCH_END
;
2493 run_hook_list_with_args (before_change_functions
, 3, args
);
2495 /* "Unbind" the variables we "bound" to nil. */
2496 Vbefore_change_functions
= before_change_functions
;
2497 Vafter_change_functions
= after_change_functions
;
2501 if (!NILP (current_buffer
->overlays_before
)
2502 || !NILP (current_buffer
->overlays_after
))
2505 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2506 FETCH_START
, FETCH_END
, Qnil
);
2509 if (! NILP (start_marker
))
2510 free_marker (start_marker
);
2511 if (! NILP (end_marker
))
2512 free_marker (end_marker
);
2517 /* Signal a change immediately after it happens.
2518 CHARPOS is the character position of the start of the changed text.
2519 LENDEL is the number of characters of the text before the change.
2520 (Not the whole buffer; just the part that was changed.)
2521 LENINS is the number of characters in that part of the text
2522 after the change. */
2525 signal_after_change (charpos
, lendel
, lenins
)
2526 int charpos
, lendel
, lenins
;
2528 if (inhibit_modification_hooks
)
2531 /* If we are deferring calls to the after-change functions
2532 and there are no before-change functions,
2533 just record the args that we were going to use. */
2534 if (! NILP (Vcombine_after_change_calls
)
2535 && NILP (Vbefore_change_function
) && NILP (Vbefore_change_functions
)
2536 && NILP (current_buffer
->overlays_before
)
2537 && NILP (current_buffer
->overlays_after
))
2541 if (!NILP (combine_after_change_list
)
2542 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2543 Fcombine_after_change_execute ();
2545 elt
= Fcons (make_number (charpos
- BEG
),
2546 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2547 Fcons (make_number (lenins
- lendel
), Qnil
)));
2548 combine_after_change_list
2549 = Fcons (elt
, combine_after_change_list
);
2550 combine_after_change_buffer
= Fcurrent_buffer ();
2555 if (!NILP (combine_after_change_list
))
2556 Fcombine_after_change_execute ();
2558 /* Run the after-change-function if any.
2559 We don't bother "binding" this variable to nil
2560 because it is obsolete anyway and new code should not use it. */
2561 if (!NILP (Vafter_change_function
))
2562 call3 (Vafter_change_function
,
2563 make_number (charpos
), make_number (charpos
+ lenins
),
2564 make_number (lendel
));
2566 if (!NILP (Vafter_change_functions
))
2568 Lisp_Object args
[4];
2569 Lisp_Object before_change_functions
;
2570 Lisp_Object after_change_functions
;
2571 struct gcpro gcpro1
, gcpro2
;
2573 /* "Bind" before-change-functions and after-change-functions
2574 to nil--but in a way that errors don't know about.
2575 That way, if there's an error in them, they will stay nil. */
2576 before_change_functions
= Vbefore_change_functions
;
2577 after_change_functions
= Vafter_change_functions
;
2578 Vbefore_change_functions
= Qnil
;
2579 Vafter_change_functions
= Qnil
;
2580 GCPRO2 (before_change_functions
, after_change_functions
);
2582 /* Actually run the hook functions. */
2583 args
[0] = Qafter_change_functions
;
2584 XSETFASTINT (args
[1], charpos
);
2585 XSETFASTINT (args
[2], charpos
+ lenins
);
2586 XSETFASTINT (args
[3], lendel
);
2587 run_hook_list_with_args (after_change_functions
,
2590 /* "Unbind" the variables we "bound" to nil. */
2591 Vbefore_change_functions
= before_change_functions
;
2592 Vafter_change_functions
= after_change_functions
;
2596 if (!NILP (current_buffer
->overlays_before
)
2597 || !NILP (current_buffer
->overlays_after
))
2598 report_overlay_modification (make_number (charpos
),
2599 make_number (charpos
+ lenins
),
2601 make_number (charpos
),
2602 make_number (charpos
+ lenins
),
2603 make_number (lendel
));
2605 /* After an insertion, call the text properties
2606 insert-behind-hooks or insert-in-front-hooks. */
2608 report_interval_modification (make_number (charpos
),
2609 make_number (charpos
+ lenins
));
2613 Fcombine_after_change_execute_1 (val
)
2616 Vcombine_after_change_calls
= val
;
2620 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2621 Scombine_after_change_execute
, 0, 0, 0,
2622 "This function is for use internally in `combine-after-change-calls'.")
2625 int count
= specpdl_ptr
- specpdl
;
2626 int beg
, end
, change
;
2630 if (NILP (combine_after_change_list
))
2633 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2635 Fset_buffer (combine_after_change_buffer
);
2637 /* # chars unchanged at beginning of buffer. */
2639 /* # chars unchanged at end of buffer. */
2641 /* Total amount of insertion (negative for deletion). */
2644 /* Scan the various individual changes,
2645 accumulating the range info in BEG, END and CHANGE. */
2646 for (tail
= combine_after_change_list
; CONSP (tail
);
2647 tail
= XCONS (tail
)->cdr
)
2650 int thisbeg
, thisend
, thischange
;
2652 /* Extract the info from the next element. */
2653 elt
= XCONS (tail
)->car
;
2656 thisbeg
= XINT (XCONS (elt
)->car
);
2658 elt
= XCONS (elt
)->cdr
;
2661 thisend
= XINT (XCONS (elt
)->car
);
2663 elt
= XCONS (elt
)->cdr
;
2666 thischange
= XINT (XCONS (elt
)->car
);
2668 /* Merge this range into the accumulated range. */
2669 change
+= thischange
;
2676 /* Get the current start and end positions of the range
2677 that was changed. */
2681 /* We are about to handle these, so discard them. */
2682 combine_after_change_list
= Qnil
;
2684 /* Now run the after-change functions for real.
2685 Turn off the flag that defers them. */
2686 record_unwind_protect (Fcombine_after_change_execute_1
,
2687 Vcombine_after_change_calls
);
2688 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2690 return unbind_to (count
, Qnil
);
2696 staticpro (&combine_after_change_list
);
2697 combine_after_change_list
= Qnil
;
2698 combine_after_change_buffer
= Qnil
;
2700 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2701 "Non-nil means enable debugging checks for invalid marker positions.");
2702 check_markers_debug_flag
= 0;
2703 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2704 "Used internally by the `combine-after-change-calls' macro.");
2705 Vcombine_after_change_calls
= Qnil
;
2707 defsubr (&Scombine_after_change_execute
);