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
;
142 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
149 /* Now copy the characters. To move the gap down,
150 copy characters up. */
154 /* I gets number of characters left to copy. */
155 i
= new_s1
- bytepos
;
158 /* If a quit is requested, stop copying now.
159 Change BYTEPOS to be where we have actually moved the gap to. */
163 charpos
= BYTE_TO_CHAR (bytepos
);
166 /* Move at most 32000 chars before checking again for a quit. */
171 /* bcopy is safe if the two areas of memory do not overlap
172 or on systems where bcopy is always safe for moving upward. */
173 && (BCOPY_UPWARD_SAFE
174 || to
- from
>= 128))
176 /* If overlap is not safe, avoid it by not moving too many
177 characters at once. */
178 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
193 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
194 BYTEPOS is where the loop above stopped, which may be what was specified
195 or may be where a quit was detected. */
196 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
199 if (bytepos
< charpos
)
201 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
205 /* Move the gap to a position greater than than the current GPT.
206 BYTEPOS describes the new position as a byte position,
207 and CHARPOS is the corresponding char position. */
210 gap_right (charpos
, bytepos
)
211 register int charpos
, bytepos
;
213 register unsigned char *to
, *from
;
217 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
224 /* Now copy the characters. To move the gap up,
225 copy characters down. */
229 /* I gets number of characters left to copy. */
230 i
= bytepos
- new_s1
;
233 /* If a quit is requested, stop copying now.
234 Change BYTEPOS to be where we have actually moved the gap to. */
238 charpos
= BYTE_TO_CHAR (bytepos
);
241 /* Move at most 32000 chars before checking again for a quit. */
246 /* bcopy is safe if the two areas of memory do not overlap
247 or on systems where bcopy is always safe for moving downward. */
248 && (BCOPY_DOWNWARD_SAFE
249 || from
- to
>= 128))
251 /* If overlap is not safe, avoid it by not moving too many
252 characters at once. */
253 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
268 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
272 if (bytepos
< charpos
)
274 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
278 /* Add AMOUNT to the byte position of every marker in the current buffer
279 whose current byte position is between FROM (exclusive) and TO (inclusive).
281 Also, any markers past the outside of that interval, in the direction
282 of adjustment, are first moved back to the near end of the interval
283 and then adjusted by AMOUNT.
285 When the latter adjustment is done, if AMOUNT is negative,
286 we record the adjustment for undo. (This case happens only for
289 The markers' character positions are not altered,
290 because gap motion does not affect character positions. */
292 int adjust_markers_test
;
295 adjust_markers_gap_motion (from
, to
, amount
)
296 register int from
, to
, amount
;
298 /* Now that a marker has a bytepos, not counting the gap,
299 nothing needs to be done here. */
302 register struct Lisp_Marker
*m
;
305 marker
= BUF_MARKERS (current_buffer
);
307 while (!NILP (marker
))
309 m
= XMARKER (marker
);
313 if (mpos
> to
&& mpos
< to
+ amount
)
315 if (adjust_markers_test
)
322 /* Here's the case where a marker is inside text being deleted.
323 AMOUNT can be negative for gap motion, too,
324 but then this range contains no markers. */
325 if (mpos
> from
+ amount
&& mpos
<= from
)
327 if (adjust_markers_test
)
329 mpos
= from
+ amount
;
332 if (mpos
> from
&& mpos
<= to
)
340 /* Adjust all markers for a deletion
341 whose range in bytes is FROM_BYTE to TO_BYTE.
342 The range in charpos is FROM to TO.
344 This function assumes that the gap is adjacent to
345 or inside of the range being deleted. */
348 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
)
349 register int from
, from_byte
, to
, to_byte
;
352 register struct Lisp_Marker
*m
;
353 register int charpos
;
355 marker
= BUF_MARKERS (current_buffer
);
357 while (!NILP (marker
))
359 m
= XMARKER (marker
);
360 charpos
= m
->charpos
;
365 /* If the marker is after the deletion,
366 relocate by number of chars / bytes deleted. */
369 m
->charpos
-= to
- from
;
370 m
->bytepos
-= to_byte
- from_byte
;
373 /* Here's the case where a marker is inside text being deleted. */
374 else if (charpos
> from
)
376 record_marker_adjustment (marker
, from
- charpos
);
378 m
->bytepos
= from_byte
;
386 /* Adjust all markers for calling record_delete for combining bytes.
387 whose range in bytes is FROM_BYTE to TO_BYTE.
388 The range in charpos is FROM to TO. */
391 adjust_markers_for_record_delete (from
, from_byte
, to
, to_byte
)
392 register int from
, from_byte
, to
, to_byte
;
395 register struct Lisp_Marker
*m
;
396 register int charpos
;
398 marker
= BUF_MARKERS (current_buffer
);
400 while (!NILP (marker
))
402 m
= XMARKER (marker
);
403 charpos
= m
->charpos
;
405 /* If the marker is after the deletion,
406 relocate by number of chars / bytes deleted. */
409 /* Here's the case where a marker is inside text being deleted. */
410 else if (charpos
> from
)
411 record_marker_adjustment (marker
, from
- charpos
);
417 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
418 to TO / TO_BYTE. We have to relocate the charpos of every marker
419 that points after the insertion (but not their bytepos).
421 COMBINED_BEFORE_BYTES is the number of bytes at the start of the insertion
422 that combine into one character with the text before the insertion.
423 COMBINED_AFTER_BYTES is the number of bytes after the insertion
424 that combine into one character with the last inserted bytes.
426 When a marker points at the insertion point,
427 we advance it if either its insertion-type is t
428 or BEFORE_MARKERS is true. */
431 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
,
432 combined_before_bytes
, combined_after_bytes
,
434 register int from
, from_byte
, to
, to_byte
;
435 int combined_before_bytes
, combined_after_bytes
, before_markers
;
439 int nchars
= to
- from
;
440 int nbytes
= to_byte
- from_byte
;
442 marker
= BUF_MARKERS (current_buffer
);
444 while (!NILP (marker
))
446 register struct Lisp_Marker
*m
= XMARKER (marker
);
448 /* In a single-byte buffer, a marker's two positions must be equal.
449 (If this insertion is going to combine characters, Z will
450 become different from Z_BYTE, but they might be the same now.
451 If so, the two OLD positions of the marker should be equal.) */
454 if (m
->charpos
!= m
->bytepos
)
458 if (m
->bytepos
== from_byte
)
460 if (m
->insertion_type
|| before_markers
)
462 m
->bytepos
= to_byte
+ combined_after_bytes
;
463 m
->charpos
= to
- combined_before_bytes
;
464 /* Point the marker before the combined character,
465 so that undoing the insertion puts it back where it was. */
466 if (combined_after_bytes
)
467 DEC_BOTH (m
->charpos
, m
->bytepos
);
468 if (m
->insertion_type
)
471 else if (combined_before_bytes
)
473 /* This marker doesn't "need relocation",
474 but don't leave it pointing in the middle of a character.
475 Point the marker after the combined character,
476 so that undoing the insertion puts it back where it was. */
477 m
->bytepos
+= combined_before_bytes
;
478 if (combined_before_bytes
== nbytes
)
479 /* All new bytes plus combined_after_bytes (if any)
481 m
->bytepos
+= combined_after_bytes
;
484 /* If a marker was pointing into the combining bytes
485 after the insertion, don't leave it there
486 in the middle of a character. */
487 else if (combined_after_bytes
&& m
->bytepos
>= from_byte
488 && m
->bytepos
< from_byte
+ combined_after_bytes
)
490 /* Put it after the combining bytes. */
491 m
->bytepos
= to_byte
+ combined_after_bytes
;
492 m
->charpos
= to
- combined_before_bytes
;
493 /* Now move it back before the combined character,
494 so that undoing the insertion will put it where it was. */
495 DEC_BOTH (m
->charpos
, m
->bytepos
);
497 else if (m
->bytepos
> from_byte
)
499 m
->bytepos
+= nbytes
;
500 m
->charpos
+= nchars
- combined_after_bytes
- combined_before_bytes
;
506 /* Adjusting only markers whose insertion-type is t may result in
507 disordered overlays in the slot `overlays_before'. */
509 fix_overlays_before (current_buffer
, from
, to
);
512 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
514 This is used only when the value of point changes due to an insert
515 or delete; it does not represent a conceptual change in point as a
516 marker. In particular, point is not crossing any interval
517 boundaries, so there's no need to use the usual SET_PT macro. In
518 fact it would be incorrect to do so, because either the old or the
519 new value of point is out of sync with the current set of
523 adjust_point (nchars
, nbytes
)
526 BUF_PT (current_buffer
) += nchars
;
527 BUF_PT_BYTE (current_buffer
) += nbytes
;
529 /* In a single-byte buffer, the two positions must be equal. */
535 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
536 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
539 See the comment of adjust_markers_for_insert for the args
540 COMBINED_BEFORE_BYTES and COMBINED_AFTER_BYTES. */
543 adjust_markers_for_replace (from
, from_byte
, old_chars
, old_bytes
,
544 new_chars
, new_bytes
,
545 combined_before_bytes
, combined_after_bytes
)
546 int from
, from_byte
, old_chars
, old_bytes
, new_chars
, new_bytes
;
547 int combined_before_bytes
, combined_after_bytes
;
549 Lisp_Object marker
= BUF_MARKERS (current_buffer
);
550 int prev_to_byte
= from_byte
+ old_bytes
;
552 = (new_chars
- combined_before_bytes
) - (old_chars
+ combined_after_bytes
);
553 int diff_bytes
= new_bytes
- old_bytes
;
555 while (!NILP (marker
))
557 register struct Lisp_Marker
*m
= XMARKER (marker
);
559 if (m
->bytepos
>= prev_to_byte
561 /* If this is an insertion (replacing 0 chars),
562 reject the case of a marker that is at the
563 insertion point and should stay before the insertion. */
564 || m
->bytepos
> from_byte
|| m
->insertion_type
))
566 if (m
->bytepos
< prev_to_byte
+ combined_after_bytes
)
568 /* Put it after the combining bytes. */
569 m
->bytepos
= from_byte
+ new_bytes
+ combined_after_bytes
;
570 m
->charpos
= from
+ new_chars
- combined_before_bytes
;
574 m
->charpos
+= diff_chars
;
575 m
->bytepos
+= diff_bytes
;
578 else if (m
->bytepos
>= from_byte
)
581 m
->bytepos
= from_byte
+ combined_before_bytes
;
582 /* If all new bytes are combined in addition to that there
583 are after combining bytes, we must set byte position of
584 the marker after the after combining bytes. */
585 if (combined_before_bytes
== new_bytes
)
586 m
->bytepos
+= combined_after_bytes
;
596 /* Make the gap NBYTES_ADDED bytes longer. */
599 make_gap (nbytes_added
)
602 unsigned char *result
;
605 int real_gap_loc_byte
;
608 /* If we have to get more space, get enough to last a while. */
609 nbytes_added
+= 2000;
611 /* Don't allow a buffer size that won't fit in an int
612 even if it will fit in a Lisp integer.
613 That won't work because so many places use `int'. */
615 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
616 >= ((unsigned) 1 << (min (BITS_PER_INT
, VALBITS
) - 1)))
617 error ("Buffer exceeds maximum size");
620 /* We allocate extra 1-byte `\0' at the tail for anchoring a search. */
621 result
= BUFFER_REALLOC (BEG_ADDR
, (Z_BYTE
- BEG_BYTE
622 + GAP_SIZE
+ nbytes_added
+ 1));
630 /* We can't unblock until the new address is properly stored. */
634 /* Prevent quitting in move_gap. */
639 real_gap_loc_byte
= GPT_BYTE
;
640 old_gap_size
= GAP_SIZE
;
642 /* Call the newly allocated space a gap at the end of the whole space. */
644 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
645 GAP_SIZE
= nbytes_added
;
647 /* Move the new gap down to be consecutive with the end of the old one.
648 This adjusts the markers properly too. */
649 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
651 /* Now combine the two into one large gap. */
652 GAP_SIZE
+= old_gap_size
;
654 GPT_BYTE
= real_gap_loc_byte
;
662 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
663 FROM_MULTIBYTE says whether the incoming text is multibyte.
664 TO_MULTIBYTE says whether to store the text as multibyte.
665 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
667 Return the number of bytes stored at TO_ADDR. */
670 copy_text (from_addr
, to_addr
, nbytes
,
671 from_multibyte
, to_multibyte
)
672 unsigned char *from_addr
;
673 unsigned char *to_addr
;
675 int from_multibyte
, to_multibyte
;
677 if (from_multibyte
== to_multibyte
)
679 bcopy (from_addr
, to_addr
, nbytes
);
682 else if (from_multibyte
)
685 int bytes_left
= nbytes
;
686 Lisp_Object tbl
= Qnil
, temp
;
688 /* We set the variable tbl to the reverse table of
689 Vnonascii_translation_table in advance. */
690 if (CHAR_TABLE_P (Vnonascii_translation_table
))
692 tbl
= Fchar_table_extra_slot (Vnonascii_translation_table
,
694 if (!CHAR_TABLE_P (tbl
))
698 /* Convert multibyte to single byte. */
699 while (bytes_left
> 0)
701 int thislen
, c
, c_save
;
702 c
= c_save
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
703 if (!SINGLE_BYTE_CHAR_P (c
))
704 c
= multibyte_char_to_unibyte (c
, tbl
);
706 from_addr
+= thislen
;
707 bytes_left
-= thislen
;
714 unsigned char *initial_to_addr
= to_addr
;
716 /* Convert single-byte to multibyte. */
719 int c
= *from_addr
++;
720 unsigned char workbuf
[4], *str
;
725 || (c
>= 0200 && !NILP (Vnonascii_translation_table
))))
727 c
= unibyte_char_to_multibyte (c
);
728 len
= CHAR_STRING (c
, workbuf
, str
);
729 bcopy (str
, to_addr
, len
);
734 /* Special case for speed. */
735 *to_addr
++ = c
, nbytes
--;
737 return to_addr
- initial_to_addr
;
741 /* Return the number of bytes it would take
742 to convert some single-byte text to multibyte.
743 The single-byte text consists of NBYTES bytes at PTR. */
746 count_size_as_multibyte (ptr
, nbytes
)
751 int outgoing_nbytes
= 0;
753 for (i
= 0; i
< nbytes
; i
++)
755 unsigned int c
= *ptr
++;
757 if (c
< 0200 || (c
< 0240 && NILP (Vnonascii_translation_table
)))
761 c
= unibyte_char_to_multibyte (c
);
762 outgoing_nbytes
+= CHAR_BYTES (c
);
766 return outgoing_nbytes
;
769 /* Insert a string of specified length before point.
770 This function judges multibyteness based on
771 enable_multibyte_characters in the current buffer;
772 it never converts between single-byte and multibyte.
774 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
775 prepare_to_modify_buffer could relocate the text. */
778 insert (string
, nbytes
)
779 register unsigned char *string
;
785 insert_1 (string
, nbytes
, 0, 1, 0);
786 signal_after_change (opoint
, 0, PT
- opoint
);
790 /* Likewise, but inherit text properties from neighboring characters. */
793 insert_and_inherit (string
, nbytes
)
794 register unsigned char *string
;
800 insert_1 (string
, nbytes
, 1, 1, 0);
801 signal_after_change (opoint
, 0, PT
- opoint
);
805 /* Insert the character C before point. Do not inherit text properties. */
811 unsigned char workbuf
[4], *str
;
814 if (! NILP (current_buffer
->enable_multibyte_characters
))
815 len
= CHAR_STRING (c
, workbuf
, str
);
826 /* Insert the null-terminated string S before point. */
832 insert (s
, strlen (s
));
835 /* Like `insert' except that all markers pointing at the place where
836 the insertion happens are adjusted to point after it.
837 Don't use this function to insert part of a Lisp string,
838 since gc could happen and relocate it. */
841 insert_before_markers (string
, nbytes
)
842 unsigned char *string
;
849 insert_1 (string
, nbytes
, 0, 1, 1);
850 signal_after_change (opoint
, 0, PT
- opoint
);
854 /* Likewise, but inherit text properties from neighboring characters. */
857 insert_before_markers_and_inherit (string
, nbytes
)
858 unsigned char *string
;
865 insert_1 (string
, nbytes
, 1, 1, 1);
866 signal_after_change (opoint
, 0, PT
- opoint
);
870 /* Subroutine used by the insert functions above. */
873 insert_1 (string
, nbytes
, inherit
, prepare
, before_markers
)
874 register unsigned char *string
;
876 int inherit
, prepare
, before_markers
;
878 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
879 inherit
, prepare
, before_markers
);
882 /* See if the byte sequence at STR1 of length LEN1 combine with the
883 byte sequence at STR2 of length LEN2 to form a single composite
884 character. If so, return the number of bytes at the start of STR2
885 which combine in this way. Otherwise, return 0. If STR3 is not
886 NULL, it is a byte sequence of length LEN3 to be appended to STR1
887 before checking the combining. */
889 count_combining_composition (str1
, len1
, str2
, len2
, str3
, len3
)
890 unsigned char *str1
, *str2
, *str3
;
891 int len1
, len2
, len3
;
893 int len
= len1
+ len2
+ len3
;
894 unsigned char *buf
= (unsigned char *) alloca (len
+ 1);
897 bcopy (str1
, buf
, len1
);
900 bcopy (str3
, buf
+ len1
, len3
);
903 bcopy (str2
, buf
+ len1
, len2
);
905 PARSE_MULTIBYTE_SEQ (buf
, len
, bytes
);
906 return (bytes
<= len1
? 0 : bytes
- len1
);
909 /* See if the bytes before POS/POS_BYTE combine with bytes
910 at the start of STRING to form a single character.
911 If so, return the number of bytes at the start of STRING
912 which combine in this way. Otherwise, return 0. */
915 count_combining_before (string
, length
, pos
, pos_byte
)
916 unsigned char *string
;
920 int len
, combining_bytes
;
923 if (NILP (current_buffer
->enable_multibyte_characters
))
926 /* At first, we can exclude the following cases:
927 (1) STRING[0] can't be a following byte of multibyte sequence.
928 (2) POS is the start of the current buffer.
929 (3) A character before POS is not a multibyte character. */
930 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
932 if (pos_byte
== BEG_BYTE
) /* case (2) */
935 p
= BYTE_POS_ADDR (pos_byte
- 1);
936 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
937 if (! BASE_LEADING_CODE_P (*p
)) /* case (3) */
940 /* A sequence of a composite character requires a special handling. */
941 if (*p
== LEADING_CODE_COMPOSITION
)
942 return count_combining_composition (p
, len
, string
, length
, NULL
, 0);
944 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
945 if (combining_bytes
<= 0)
946 /* The character preceding POS is, complete and no room for
947 combining bytes (combining_bytes == 0), or an independent 8-bit
948 character (combining_bytes < 0). */
951 /* We have a combination situation. Count the bytes at STRING that
954 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
957 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
960 /* See if the bytes after POS/POS_BYTE combine with bytes
961 at the end of STRING to form a single character.
962 If so, return the number of bytes after POS/POS_BYTE
963 which combine in this way. Otherwise, return 0. */
966 count_combining_after (string
, length
, pos
, pos_byte
)
967 unsigned char *string
;
971 int opos
= pos
, opos_byte
= pos_byte
;
976 if (NILP (current_buffer
->enable_multibyte_characters
))
979 /* At first, we can exclude the following cases:
980 (1) The last byte of STRING is an ASCII.
981 (2) POS is the last of the current buffer.
982 (3) A character at POS can't be a following byte of multibyte
984 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1])) /* case (1) */
986 if (pos_byte
== Z_BYTE
) /* case (2) */
988 bufp
= BYTE_POS_ADDR (pos_byte
);
989 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
993 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
999 /* All characters in STRING are not character head. We must
1000 check also preceding bytes at POS. We are sure that the gap
1002 unsigned char *p
= BEG_ADDR
;
1004 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
1006 if (i
< 0 || !BASE_LEADING_CODE_P (p
[i
]))
1008 /* A sequence of a composite character requires a special handling. */
1009 if (p
[i
] == LEADING_CODE_COMPOSITION
)
1010 return count_combining_composition (p
+ i
, pos_byte
- 1 - i
,
1011 bufp
, Z_BYTE
- pos_byte
,
1013 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
1014 return (bytes
<= pos_byte
- 1 - i
+ length
1016 : bytes
- (pos_byte
- 1 - i
+ length
));
1018 if (!BASE_LEADING_CODE_P (string
[i
]))
1020 /* A sequence of a composite character requires a special handling. */
1021 if (string
[i
] == LEADING_CODE_COMPOSITION
)
1022 return count_combining_composition (string
+ i
, length
- i
,
1023 bufp
, Z_BYTE
- pos_byte
, NULL
, 0);
1025 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
1027 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
1029 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
1032 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
1033 following the position POS/POS_BYTE to the character preceding POS.
1034 If TARGET is after POS+NBYTES, we only have to adjust the character
1035 position TARGET, else, if TARGET is after POS, we have to adjust
1036 both the character position TARGET and the byte position
1037 TARGET_BYTE, else we don't have to do any adjustment. */
1039 #define ADJUST_CHAR_POS(target, target_byte) \
1041 if (target > pos + nbytes) \
1043 else if (target >= pos) \
1046 target_byte = pos_byte + nbytes; \
1050 /* Combine NBYTES stray trailing-codes, which were formerly separate
1051 characters, with the preceding character. These bytes
1052 are located after position POS / POS_BYTE, and the preceding character
1053 is located just before that position.
1055 This function does not adjust markers for byte combining. That
1056 should be done in advance by the functions
1057 adjust_markers_for_insert or adjust_markers_for_replace. */
1060 combine_bytes (pos
, pos_byte
, nbytes
)
1061 int pos
, pos_byte
, nbytes
;
1063 adjust_overlays_for_delete (pos
, nbytes
);
1065 ADJUST_CHAR_POS (BUF_PT (current_buffer
), BUF_PT_BYTE (current_buffer
));
1066 ADJUST_CHAR_POS (GPT
, GPT_BYTE
);
1067 ADJUST_CHAR_POS (Z
, Z_BYTE
);
1068 ADJUST_CHAR_POS (ZV
, ZV_BYTE
);
1070 if (BUF_INTERVALS (current_buffer
) != 0)
1071 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1072 offset_intervals (current_buffer
, pos
, - nbytes
);
1076 byte_combining_error ()
1078 error ("Byte combining across boundary of accessible buffer text inhibitted");
1081 /* If we are going to combine bytes at POS which is at a narrowed
1082 region boundary, signal an error. */
1083 #define CHECK_BYTE_COMBINING_FOR_INSERT(pos) \
1085 if (combined_before_bytes && pos == BEGV \
1086 || combined_after_bytes && pos == ZV) \
1087 byte_combining_error (); \
1091 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1092 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
1093 are the same as in insert_1. */
1096 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
1097 register unsigned char *string
;
1098 register int nchars
, nbytes
;
1099 int inherit
, prepare
, before_markers
;
1101 register Lisp_Object temp
;
1102 int combined_before_bytes
, combined_after_bytes
;
1104 if (NILP (current_buffer
->enable_multibyte_characters
))
1108 /* Do this before moving and increasing the gap,
1109 because the before-change hooks might move the gap
1110 or make it smaller. */
1111 prepare_to_modify_buffer (PT
, PT
, NULL
);
1114 move_gap_both (PT
, PT_BYTE
);
1115 if (GAP_SIZE
< nbytes
)
1116 make_gap (nbytes
- GAP_SIZE
);
1118 combined_before_bytes
1119 = count_combining_before (string
, nbytes
, PT
, PT_BYTE
);
1120 combined_after_bytes
1121 = count_combining_after (string
, nbytes
, PT
, PT_BYTE
);
1122 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1124 /* Record deletion of the surrounding text that combines with
1125 the insertion. This, together with recording the insertion,
1126 will add up to the right stuff in the undo list.
1128 But there is no need to actually delete the combining bytes
1129 from the buffer and reinsert them. */
1131 if (combined_after_bytes
)
1133 Lisp_Object deletion
;
1136 if (! EQ (current_buffer
->undo_list
, Qt
))
1137 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1138 PT
+ combined_after_bytes
,
1139 PT_BYTE
+ combined_after_bytes
, 1);
1141 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1142 PT
+ combined_after_bytes
,
1143 PT_BYTE
+ combined_after_bytes
);
1144 if (! EQ (current_buffer
->undo_list
, Qt
))
1145 record_delete (PT
, deletion
);
1148 if (combined_before_bytes
)
1150 Lisp_Object deletion
;
1153 if (! EQ (current_buffer
->undo_list
, Qt
))
1154 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1156 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1158 if (! EQ (current_buffer
->undo_list
, Qt
))
1159 record_delete (PT
- 1, deletion
);
1162 record_insert (PT
- !!combined_before_bytes
,
1163 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1166 bcopy (string
, GPT_ADDR
, nbytes
);
1169 /* When we have combining at the end of the insertion,
1170 this is the character position before the combined character. */
1177 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1179 if (combined_after_bytes
)
1180 move_gap_both (GPT
+ combined_after_bytes
,
1181 GPT_BYTE
+ combined_after_bytes
);
1186 adjust_overlays_for_insert (PT
, nchars
);
1187 adjust_markers_for_insert (PT
, PT_BYTE
,
1188 PT
+ nchars
, PT_BYTE
+ nbytes
,
1189 combined_before_bytes
, combined_after_bytes
,
1192 #ifdef USE_TEXT_PROPERTIES
1193 if (BUF_INTERVALS (current_buffer
) != 0)
1194 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1195 offset_intervals (current_buffer
, PT
, nchars
);
1197 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
1198 Fset_text_properties (make_number (PT
), make_number (PT
+ nchars
),
1203 int pos
= PT
, pos_byte
= PT_BYTE
;
1205 adjust_point (nchars
+ combined_after_bytes
,
1206 nbytes
+ combined_after_bytes
);
1208 if (combined_after_bytes
)
1209 combine_bytes (pos
+ nchars
, pos_byte
+ nbytes
, combined_after_bytes
);
1211 if (combined_before_bytes
)
1212 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1218 /* Insert the part of the text of STRING, a Lisp object assumed to be
1219 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1220 starting at position POS / POS_BYTE. If the text of STRING has properties,
1221 copy them into the buffer.
1223 It does not work to use `insert' for this, because a GC could happen
1224 before we bcopy the stuff into the buffer, and relocate the string
1225 without insert noticing. */
1228 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
1230 register int pos
, pos_byte
, length
, length_byte
;
1234 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1236 signal_after_change (opoint
, 0, PT
- opoint
);
1239 /* Like `insert_from_string' except that all markers pointing
1240 at the place where the insertion happens are adjusted to point after it. */
1243 insert_from_string_before_markers (string
, pos
, pos_byte
,
1244 length
, length_byte
, inherit
)
1246 register int pos
, pos_byte
, length
, length_byte
;
1250 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1252 signal_after_change (opoint
, 0, PT
- opoint
);
1255 /* Subroutine of the insertion functions above. */
1258 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1259 inherit
, before_markers
)
1261 register int pos
, pos_byte
, nchars
, nbytes
;
1262 int inherit
, before_markers
;
1264 register Lisp_Object temp
;
1265 struct gcpro gcpro1
;
1266 int outgoing_nbytes
= nbytes
;
1267 int combined_before_bytes
, combined_after_bytes
;
1268 int adjusted_nchars
;
1271 /* Make OUTGOING_NBYTES describe the text
1272 as it will be inserted in this buffer. */
1274 if (NILP (current_buffer
->enable_multibyte_characters
))
1275 outgoing_nbytes
= nchars
;
1276 else if (! STRING_MULTIBYTE (string
))
1278 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
1282 /* Do this before moving and increasing the gap,
1283 because the before-change hooks might move the gap
1284 or make it smaller. */
1285 prepare_to_modify_buffer (PT
, PT
, NULL
);
1288 move_gap_both (PT
, PT_BYTE
);
1289 if (GAP_SIZE
< outgoing_nbytes
)
1290 make_gap (outgoing_nbytes
- GAP_SIZE
);
1293 /* Copy the string text into the buffer, perhaps converting
1294 between single-byte and multibyte. */
1295 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1296 STRING_MULTIBYTE (string
),
1297 ! NILP (current_buffer
->enable_multibyte_characters
));
1299 /* We have copied text into the gap, but we have not altered
1300 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1301 to these functions and get the same results as we would
1302 have got earlier on. Meanwhile, PT_ADDR does point to
1303 the text that has been stored by copy_text. */
1305 combined_before_bytes
1306 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1307 combined_after_bytes
1308 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1310 unsigned char save
= *(GPT_ADDR
);
1312 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1316 /* Record deletion of the surrounding text that combines with
1317 the insertion. This, together with recording the insertion,
1318 will add up to the right stuff in the undo list.
1320 But there is no need to actually delete the combining bytes
1321 from the buffer and reinsert them. */
1323 if (combined_after_bytes
)
1325 Lisp_Object deletion
;
1328 if (! EQ (current_buffer
->undo_list
, Qt
))
1329 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1330 PT
+ combined_after_bytes
,
1331 PT_BYTE
+ combined_after_bytes
, 1);
1333 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1334 PT
+ combined_after_bytes
,
1335 PT_BYTE
+ combined_after_bytes
);
1336 if (! EQ (current_buffer
->undo_list
, Qt
))
1337 record_delete (PT
, deletion
);
1340 if (combined_before_bytes
)
1342 Lisp_Object deletion
;
1345 if (! EQ (current_buffer
->undo_list
, Qt
))
1346 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1348 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1350 if (! EQ (current_buffer
->undo_list
, Qt
))
1351 record_delete (PT
- 1, deletion
);
1354 record_insert (PT
- !!combined_before_bytes
,
1355 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1358 GAP_SIZE
-= outgoing_nbytes
;
1362 GPT_BYTE
+= outgoing_nbytes
;
1363 ZV_BYTE
+= outgoing_nbytes
;
1364 Z_BYTE
+= outgoing_nbytes
;
1365 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1367 if (combined_after_bytes
)
1368 move_gap_both (GPT
+ combined_after_bytes
,
1369 GPT_BYTE
+ combined_after_bytes
);
1374 adjust_overlays_for_insert (PT
, nchars
);
1375 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1376 PT_BYTE
+ outgoing_nbytes
,
1377 combined_before_bytes
, combined_after_bytes
,
1380 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1381 offset_intervals (current_buffer
, PT
, nchars
);
1383 intervals
= XSTRING (string
)->intervals
;
1384 /* Get the intervals for the part of the string we are inserting--
1385 not including the combined-before bytes. */
1386 if (nbytes
< STRING_BYTES (XSTRING (string
)))
1387 intervals
= copy_intervals (intervals
, pos
, nchars
);
1389 /* Insert those intervals. */
1390 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1391 current_buffer
, inherit
);
1394 int pos
= PT
, pos_byte
= PT_BYTE
;
1396 adjust_point (nchars
+ combined_after_bytes
,
1397 outgoing_nbytes
+ combined_after_bytes
);
1399 if (combined_after_bytes
)
1400 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1401 combined_after_bytes
);
1403 if (combined_before_bytes
)
1404 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1408 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1409 current buffer. If the text in BUF has properties, they are absorbed
1410 into the current buffer.
1412 It does not work to use `insert' for this, because a malloc could happen
1413 and relocate BUF's text before the bcopy happens. */
1416 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1418 int charpos
, nchars
;
1423 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1424 signal_after_change (opoint
, 0, PT
- opoint
);
1428 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1433 register Lisp_Object temp
;
1434 int chunk
, chunk_expanded
;
1435 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1436 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1437 int incoming_nbytes
= to_byte
- from_byte
;
1438 int outgoing_nbytes
= incoming_nbytes
;
1439 int combined_before_bytes
, combined_after_bytes
;
1440 int adjusted_nchars
;
1443 /* Make OUTGOING_NBYTES describe the text
1444 as it will be inserted in this buffer. */
1446 if (NILP (current_buffer
->enable_multibyte_characters
))
1447 outgoing_nbytes
= nchars
;
1448 else if (NILP (buf
->enable_multibyte_characters
))
1450 int outgoing_before_gap
= 0;
1451 int outgoing_after_gap
= 0;
1453 if (from
< BUF_GPT (buf
))
1455 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1456 if (chunk
> incoming_nbytes
)
1457 chunk
= incoming_nbytes
;
1459 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1465 if (chunk
< incoming_nbytes
)
1467 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1469 incoming_nbytes
- chunk
);
1471 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1474 /* Make sure point-max won't overflow after this insertion. */
1475 XSETINT (temp
, outgoing_nbytes
+ Z
);
1476 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1477 error ("Maximum buffer size exceeded");
1479 /* Do this before moving and increasing the gap,
1480 because the before-change hooks might move the gap
1481 or make it smaller. */
1482 prepare_to_modify_buffer (PT
, PT
, NULL
);
1485 move_gap_both (PT
, PT_BYTE
);
1486 if (GAP_SIZE
< outgoing_nbytes
)
1487 make_gap (outgoing_nbytes
- GAP_SIZE
);
1489 if (from
< BUF_GPT (buf
))
1491 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1492 if (chunk
> incoming_nbytes
)
1493 chunk
= incoming_nbytes
;
1494 /* Record number of output bytes, so we know where
1495 to put the output from the second copy_text. */
1497 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1499 ! NILP (buf
->enable_multibyte_characters
),
1500 ! NILP (current_buffer
->enable_multibyte_characters
));
1503 chunk_expanded
= chunk
= 0;
1505 if (chunk
< incoming_nbytes
)
1506 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1507 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1508 ! NILP (buf
->enable_multibyte_characters
),
1509 ! NILP (current_buffer
->enable_multibyte_characters
));
1511 /* We have copied text into the gap, but we have not altered
1512 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1513 to these functions and get the same results as we would
1514 have got earlier on. Meanwhile, GPT_ADDR does point to
1515 the text that has been stored by copy_text. */
1516 combined_before_bytes
1517 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1518 combined_after_bytes
1519 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1521 unsigned char save
= *(GPT_ADDR
);
1523 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1527 /* Record deletion of the surrounding text that combines with
1528 the insertion. This, together with recording the insertion,
1529 will add up to the right stuff in the undo list.
1531 But there is no need to actually delete the combining bytes
1532 from the buffer and reinsert them. */
1534 if (combined_after_bytes
)
1536 Lisp_Object deletion
;
1539 if (! EQ (current_buffer
->undo_list
, Qt
))
1540 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1541 PT
+ combined_after_bytes
,
1542 PT_BYTE
+ combined_after_bytes
, 1);
1544 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1545 PT
+ combined_after_bytes
,
1546 PT_BYTE
+ combined_after_bytes
);
1547 if (! EQ (current_buffer
->undo_list
, Qt
))
1548 record_delete (PT
, deletion
);
1551 if (combined_before_bytes
)
1553 Lisp_Object deletion
;
1556 if (! EQ (current_buffer
->undo_list
, Qt
))
1557 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1559 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1561 if (! EQ (current_buffer
->undo_list
, Qt
))
1562 record_delete (PT
- 1, deletion
);
1565 record_insert (PT
- !!combined_before_bytes
,
1566 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1569 GAP_SIZE
-= outgoing_nbytes
;
1573 GPT_BYTE
+= outgoing_nbytes
;
1574 ZV_BYTE
+= outgoing_nbytes
;
1575 Z_BYTE
+= outgoing_nbytes
;
1576 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1578 if (combined_after_bytes
)
1579 move_gap_both (GPT
+ combined_after_bytes
,
1580 GPT_BYTE
+ combined_after_bytes
);
1585 adjust_overlays_for_insert (PT
, nchars
);
1586 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1587 PT_BYTE
+ outgoing_nbytes
,
1588 combined_before_bytes
, combined_after_bytes
, 0);
1590 #ifdef USE_TEXT_PROPERTIES
1591 if (BUF_INTERVALS (current_buffer
) != 0)
1592 offset_intervals (current_buffer
, PT
, nchars
);
1595 /* Get the intervals for the part of the string we are inserting--
1596 not including the combined-before bytes. */
1597 intervals
= BUF_INTERVALS (buf
);
1598 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1599 intervals
= copy_intervals (intervals
, from
, nchars
);
1601 /* Insert those intervals. */
1602 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1605 int pos
= PT
, pos_byte
= PT_BYTE
;
1607 adjust_point (nchars
+ combined_after_bytes
,
1608 outgoing_nbytes
+ combined_after_bytes
);
1610 if (combined_after_bytes
)
1611 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1612 combined_after_bytes
);
1614 if (combined_before_bytes
)
1615 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1619 /* This function should be called after moving gap to FROM and before
1620 altering text between FROM and TO. This adjusts various position
1621 keepers and markers as if the text is deleted. Don't forget to
1622 call adjust_after_replace after you actually alter the text. */
1625 adjust_before_replace (from
, from_byte
, to
, to_byte
)
1626 int from
, from_byte
, to
, to_byte
;
1628 Lisp_Object deletion
;
1630 if (! EQ (current_buffer
->undo_list
, Qt
))
1631 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1635 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1637 if (! EQ (current_buffer
->undo_list
, Qt
))
1638 record_delete (from
, deletion
);
1640 adjust_overlays_for_delete (from
, to
- from
);
1643 /* Record undo information and adjust markers and position keepers for
1644 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1645 chars (LEN_BYTE bytes) which resides in the gap just after
1648 PREV_TEXT nil means the new text was just inserted. */
1651 adjust_after_replace (from
, from_byte
, prev_text
, len
, len_byte
)
1652 int from
, from_byte
, len
, len_byte
;
1653 Lisp_Object prev_text
;
1655 int combined_before_bytes
1656 = count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
);
1657 int combined_after_bytes
1658 = count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
);
1659 /* This flag tells if we combine some bytes with a character before
1660 FROM. This happens even if combined_before_bytes is zero. */
1661 int combine_before
= (combined_before_bytes
1662 || (len
== 0 && combined_after_bytes
));
1664 int nchars_del
= 0, nbytes_del
= 0;
1666 if (STRINGP (prev_text
))
1668 nchars_del
= XSTRING (prev_text
)->size
;
1669 nbytes_del
= STRING_BYTES (XSTRING (prev_text
));
1672 if (combine_before
&& from
== BEGV
1673 || combined_after_bytes
&& from
== ZV
)
1675 /* We can't combine bytes nor signal an error here. So, let's
1676 pretend that the new text is just a single space. */
1678 combined_before_bytes
= combined_after_bytes
= 0;
1682 if (combined_after_bytes
)
1684 Lisp_Object deletion
;
1687 if (! EQ (current_buffer
->undo_list
, Qt
))
1688 deletion
= make_buffer_string_both (from
, from_byte
,
1689 from
+ combined_after_bytes
,
1690 from_byte
+ combined_after_bytes
,
1693 adjust_markers_for_record_delete (from
, from_byte
,
1694 from
+ combined_after_bytes
,
1695 from_byte
+ combined_after_bytes
);
1697 if (! EQ (current_buffer
->undo_list
, Qt
))
1698 record_delete (from
+ nchars_del
, deletion
);
1701 if (combined_before_bytes
1702 || len_byte
== 0 && combined_after_bytes
> 0)
1704 Lisp_Object deletion
;
1707 if (! EQ (current_buffer
->undo_list
, Qt
))
1708 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1709 from
, from_byte
, 1);
1710 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1712 if (! EQ (current_buffer
->undo_list
, Qt
))
1713 record_delete (from
- 1, deletion
);
1716 /* Update various buffer positions for the new text. */
1717 GAP_SIZE
-= len_byte
;
1719 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1720 GPT
+= len
; GPT_BYTE
+= len_byte
;
1721 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1723 /* The gap should be at character boundary. */
1724 if (combined_after_bytes
)
1725 move_gap_both (GPT
+ combined_after_bytes
,
1726 GPT_BYTE
+ combined_after_bytes
);
1728 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1730 combined_before_bytes
, combined_after_bytes
);
1731 if (! EQ (current_buffer
->undo_list
, Qt
))
1734 record_delete (from
- combine_before
, prev_text
);
1736 record_insert (from
- 1, len
- combined_before_bytes
+ 1);
1738 record_insert (from
, len
);
1741 if (len
> nchars_del
)
1742 adjust_overlays_for_insert (from
, len
- nchars_del
);
1743 else if (len
< nchars_del
)
1744 adjust_overlays_for_delete (from
, nchars_del
- len
);
1745 #ifdef USE_TEXT_PROPERTIES
1746 if (BUF_INTERVALS (current_buffer
) != 0)
1748 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1753 int pos
= PT
, pos_byte
= PT_BYTE
;
1756 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1758 if (combined_after_bytes
)
1760 if (combined_before_bytes
== len_byte
)
1761 /* This is the case that all new bytes are combined. */
1762 combined_before_bytes
+= combined_after_bytes
;
1764 combine_bytes (from
+ len
, from_byte
+ len_byte
,
1765 combined_after_bytes
);
1767 if (combined_before_bytes
)
1768 combine_bytes (from
, from_byte
, combined_before_bytes
);
1771 /* As byte combining will decrease Z, we must check this again. */
1772 if (Z
- GPT
< END_UNCHANGED
)
1773 END_UNCHANGED
= Z
- GPT
;
1778 evaporate_overlays (from
);
1782 /* Record undo information, adjust markers and position keepers for an
1783 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1784 text already exists in the current buffer but character length (TO
1785 - FROM) may be incorrect, the correct length is NEWLEN. */
1788 adjust_after_insert (from
, from_byte
, to
, to_byte
, newlen
)
1789 int from
, from_byte
, to
, to_byte
, newlen
;
1791 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
1794 move_gap_both (to
, to_byte
);
1795 GAP_SIZE
+= len_byte
;
1796 GPT
-= len
; GPT_BYTE
-= len_byte
;
1797 ZV
-= len
; ZV_BYTE
-= len_byte
;
1798 Z
-= len
; Z_BYTE
-= len_byte
;
1799 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1802 /* Replace the text from character positions FROM to TO with NEW,
1803 If PREPARE is nonzero, call prepare_to_modify_buffer.
1804 If INHERIT, the newly inserted text should inherit text properties
1805 from the surrounding non-deleted text. */
1807 /* Note that this does not yet handle markers quite right.
1808 Also it needs to record a single undo-entry that does a replacement
1809 rather than a separate delete and insert.
1810 That way, undo will also handle markers properly.
1812 But if MARKERS is 0, don't relocate markers. */
1815 replace_range (from
, to
, new, prepare
, inherit
, markers
)
1817 int from
, to
, prepare
, inherit
, markers
;
1819 int inschars
= XSTRING (new)->size
;
1820 int insbytes
= STRING_BYTES (XSTRING (new));
1821 int from_byte
, to_byte
;
1822 int nbytes_del
, nchars_del
;
1823 register Lisp_Object temp
;
1824 struct gcpro gcpro1
;
1825 int combined_before_bytes
, combined_after_bytes
;
1826 int adjusted_inschars
;
1828 int outgoing_insbytes
= insbytes
;
1829 Lisp_Object deletion
;
1837 int range_length
= to
- from
;
1838 prepare_to_modify_buffer (from
, to
, &from
);
1839 to
= from
+ range_length
;
1844 /* Make args be valid */
1850 from_byte
= CHAR_TO_BYTE (from
);
1851 to_byte
= CHAR_TO_BYTE (to
);
1853 nchars_del
= to
- from
;
1854 nbytes_del
= to_byte
- from_byte
;
1856 if (nbytes_del
<= 0 && insbytes
== 0)
1859 /* Make OUTGOING_INSBYTES describe the text
1860 as it will be inserted in this buffer. */
1862 if (NILP (current_buffer
->enable_multibyte_characters
))
1863 outgoing_insbytes
= inschars
;
1864 else if (! STRING_MULTIBYTE (new))
1866 = count_size_as_multibyte (XSTRING (new)->data
, insbytes
);
1868 /* Make sure point-max won't overflow after this insertion. */
1869 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1870 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1871 error ("Maximum buffer size exceeded");
1875 /* Make sure the gap is somewhere in or next to what we are deleting. */
1877 gap_right (from
, from_byte
);
1879 gap_left (to
, to_byte
, 0);
1881 /* Even if we don't record for undo, we must keep the original text
1882 because we may have to recover it because of inappropriate byte
1884 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1887 /* Relocate all markers pointing into the new, larger gap
1888 to point at the end of the text before the gap.
1889 Do this before recording the deletion,
1890 so that undo handles this after reinserting the text. */
1891 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1893 GAP_SIZE
+= nbytes_del
;
1896 ZV_BYTE
-= nbytes_del
;
1897 Z_BYTE
-= nbytes_del
;
1899 GPT_BYTE
= from_byte
;
1900 *(GPT_ADDR
) = 0; /* Put an anchor. */
1905 if (GPT
- BEG
< BEG_UNCHANGED
)
1906 BEG_UNCHANGED
= GPT
- BEG
;
1907 if (Z
- GPT
< END_UNCHANGED
)
1908 END_UNCHANGED
= Z
- GPT
;
1910 if (GAP_SIZE
< insbytes
)
1911 make_gap (insbytes
- GAP_SIZE
);
1913 /* Copy the string text into the buffer, perhaps converting
1914 between single-byte and multibyte. */
1915 copy_text (XSTRING (new)->data
, GPT_ADDR
, insbytes
,
1916 STRING_MULTIBYTE (new),
1917 ! NILP (current_buffer
->enable_multibyte_characters
));
1919 /* We have copied text into the gap, but we have not marked
1920 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1921 here, for both the previous text and the following text.
1922 Meanwhile, GPT_ADDR does point to
1923 the text that has been stored by copy_text. */
1925 combined_before_bytes
1926 = count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1927 combined_after_bytes
1928 = count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1930 if (combined_before_bytes
&& from
== BEGV
1931 || combined_after_bytes
&& from
== ZV
)
1933 /* Bytes are being combined across the region boundary. We
1934 should avoid it. We recover the original contents before
1935 signaling an error. */
1936 bcopy (XSTRING (deletion
)->data
, GPT_ADDR
, nbytes_del
);
1937 GAP_SIZE
-= nbytes_del
;
1940 ZV_BYTE
+= nbytes_del
;
1941 Z_BYTE
+= nbytes_del
;
1942 GPT
= from
+ nchars_del
;
1943 GPT_BYTE
= from_byte
+ nbytes_del
;
1944 *(GPT_ADDR
) = 0; /* Put an anchor. */
1946 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
, 0, 0, 0);
1948 byte_combining_error ();
1952 /* Record deletion of the surrounding text that combines with
1953 the insertion. This, together with recording the insertion,
1954 will add up to the right stuff in the undo list.
1956 But there is no need to actually delete the combining bytes
1957 from the buffer and reinsert them. */
1959 if (combined_after_bytes
)
1961 Lisp_Object deletion
;
1964 if (! EQ (current_buffer
->undo_list
, Qt
))
1965 deletion
= make_buffer_string_both (from
, from_byte
,
1966 from
+ combined_after_bytes
,
1967 from_byte
+ combined_after_bytes
,
1970 adjust_markers_for_record_delete (from
, from_byte
,
1971 from
+ combined_after_bytes
,
1972 from_byte
+ combined_after_bytes
);
1973 if (! EQ (current_buffer
->undo_list
, Qt
))
1974 record_delete (from
+ nchars_del
, deletion
);
1977 if (combined_before_bytes
)
1979 Lisp_Object deletion
;
1982 if (! EQ (current_buffer
->undo_list
, Qt
))
1983 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1984 from
, from_byte
, 1);
1985 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1987 if (! EQ (current_buffer
->undo_list
, Qt
))
1988 record_delete (from
- 1, deletion
);
1991 if (! EQ (current_buffer
->undo_list
, Qt
))
1993 record_delete (from
- !!combined_before_bytes
, deletion
);
1994 record_insert (from
- !!combined_before_bytes
,
1995 (inschars
- combined_before_bytes
1996 + !!combined_before_bytes
));
1999 GAP_SIZE
-= outgoing_insbytes
;
2003 GPT_BYTE
+= outgoing_insbytes
;
2004 ZV_BYTE
+= outgoing_insbytes
;
2005 Z_BYTE
+= outgoing_insbytes
;
2006 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
2008 if (combined_after_bytes
)
2009 move_gap_both (GPT
+ combined_after_bytes
,
2010 GPT_BYTE
+ combined_after_bytes
);
2015 /* Adjust the overlay center as needed. This must be done after
2016 adjusting the markers that bound the overlays. */
2017 adjust_overlays_for_delete (from
, nchars_del
);
2018 adjust_overlays_for_insert (from
, inschars
);
2020 adjust_markers_for_insert (from
, from_byte
,
2021 from
+ inschars
, from_byte
+ outgoing_insbytes
,
2022 combined_before_bytes
, combined_after_bytes
, 0);
2024 #ifdef USE_TEXT_PROPERTIES
2025 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
2027 /* Get the intervals for the part of the string we are inserting--
2028 not including the combined-before bytes. */
2029 intervals
= XSTRING (new)->intervals
;
2030 /* Insert those intervals. */
2031 graft_intervals_into_buffer (intervals
, from
, inschars
,
2032 current_buffer
, inherit
);
2035 /* Relocate point as if it were a marker. */
2037 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
2038 (from_byte
+ outgoing_insbytes
2039 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
2041 if (combined_after_bytes
)
2043 if (combined_before_bytes
== outgoing_insbytes
)
2044 /* This is the case that all new bytes are combined. */
2045 combined_before_bytes
+= combined_after_bytes
;
2047 combine_bytes (from
+ inschars
, from_byte
+ outgoing_insbytes
,
2048 combined_after_bytes
);
2050 if (combined_before_bytes
)
2051 combine_bytes (from
, from_byte
, combined_before_bytes
);
2053 /* As byte combining will decrease Z, we must check this again. */
2054 if (Z
- GPT
< END_UNCHANGED
)
2055 END_UNCHANGED
= Z
- GPT
;
2057 if (outgoing_insbytes
== 0)
2058 evaporate_overlays (from
);
2065 signal_after_change (from
, nchars_del
, GPT
- from
);
2068 /* Delete characters in current buffer
2069 from FROM up to (but not including) TO.
2070 If TO comes before FROM, we delete nothing. */
2073 del_range (from
, to
)
2074 register int from
, to
;
2076 del_range_1 (from
, to
, 1);
2079 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
2082 del_range_1 (from
, to
, prepare
)
2083 int from
, to
, prepare
;
2085 int from_byte
, to_byte
;
2087 if (INTEGERP (current_buffer
->minibuffer_prompt_length
))
2089 /* Don't delete part of a mini-buffer prompt. */
2090 int len
= XFASTINT (current_buffer
->minibuffer_prompt_length
);
2091 from
= max (from
, len
);
2094 /* Make args be valid */
2105 int range_length
= to
- from
;
2106 prepare_to_modify_buffer (from
, to
, &from
);
2107 to
= from
+ range_length
;
2110 from_byte
= CHAR_TO_BYTE (from
);
2111 to_byte
= CHAR_TO_BYTE (to
);
2113 del_range_2 (from
, from_byte
, to
, to_byte
);
2114 signal_after_change (from
, to
- from
, 0);
2117 /* Like del_range_1 but args are byte positions, not char positions. */
2120 del_range_byte (from_byte
, to_byte
, prepare
)
2121 int from_byte
, to_byte
, prepare
;
2125 /* Make args be valid */
2126 if (from_byte
< BEGV_BYTE
)
2127 from_byte
= BEGV_BYTE
;
2128 if (to_byte
> ZV_BYTE
)
2131 if (to_byte
<= from_byte
)
2134 from
= BYTE_TO_CHAR (from_byte
);
2135 to
= BYTE_TO_CHAR (to_byte
);
2139 int old_from
= from
, old_to
= Z
- to
;
2140 int range_length
= to
- from
;
2141 prepare_to_modify_buffer (from
, to
, &from
);
2142 to
= from
+ range_length
;
2144 if (old_from
!= from
)
2145 from_byte
= CHAR_TO_BYTE (from
);
2146 if (old_to
== Z
- to
)
2147 to_byte
= CHAR_TO_BYTE (to
);
2150 del_range_2 (from
, from_byte
, to
, to_byte
);
2151 signal_after_change (from
, to
- from
, 0);
2154 /* Like del_range_1, but positions are specified both as charpos
2158 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
2159 int from
, from_byte
, to
, to_byte
, prepare
;
2161 /* Make args be valid */
2162 if (from_byte
< BEGV_BYTE
)
2163 from_byte
= BEGV_BYTE
;
2164 if (to_byte
> ZV_BYTE
)
2167 if (to_byte
<= from_byte
)
2177 int old_from
= from
, old_to
= Z
- to
;
2178 int range_length
= to
- from
;
2179 prepare_to_modify_buffer (from
, to
, &from
);
2180 to
= from
+ range_length
;
2182 if (old_from
!= from
)
2183 from_byte
= CHAR_TO_BYTE (from
);
2184 if (old_to
== Z
- to
)
2185 to_byte
= CHAR_TO_BYTE (to
);
2188 del_range_2 (from
, from_byte
, to
, to_byte
);
2189 signal_after_change (from
, to
- from
, 0);
2192 /* Delete a range of text, specified both as character positions
2193 and byte positions. FROM and TO are character positions,
2194 while FROM_BYTE and TO_BYTE are byte positions. */
2197 del_range_2 (from
, from_byte
, to
, to_byte
)
2198 int from
, from_byte
, to
, to_byte
;
2200 register int nbytes_del
, nchars_del
;
2201 int combined_after_bytes
;
2202 Lisp_Object deletion
;
2207 nchars_del
= to
- from
;
2208 nbytes_del
= to_byte
- from_byte
;
2210 /* Make sure the gap is somewhere in or next to what we are deleting. */
2212 gap_right (from
, from_byte
);
2214 gap_left (to
, to_byte
, 0);
2216 combined_after_bytes
2217 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
2218 Z_BYTE
- to_byte
, from
, from_byte
);
2219 if (combined_after_bytes
)
2221 if (from
== BEGV
|| to
== ZV
)
2222 byte_combining_error ();
2223 from_byte_1
= from_byte
;
2224 DEC_POS (from_byte_1
);
2227 from_byte_1
= from_byte
;
2229 if (! EQ (current_buffer
->undo_list
, Qt
))
2231 = make_buffer_string_both (from
- !!combined_after_bytes
,
2233 to
+ combined_after_bytes
,
2234 to_byte
+ combined_after_bytes
, 1);
2235 if (combined_after_bytes
)
2236 /* COMBINED_AFTER_BYTES nonzero means that the above code moved
2237 the gap. We must move the gap again to a proper place. */
2238 move_gap_both (from
, from_byte
);
2240 /* Relocate all markers pointing into the new, larger gap
2241 to point at the end of the text before the gap.
2242 Do this before recording the deletion,
2243 so that undo handles this after reinserting the text. */
2244 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
2245 if (combined_after_bytes
)
2247 /* Adjust markers for the phony deletion
2248 that we are about to call record_undo for. */
2250 /* Here we delete the markers that formerly
2251 pointed at TO ... TO + COMBINED_AFTER_BYTES.
2252 But because of the call to adjust_markers_for_delete, above,
2253 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
2254 adjust_markers_for_record_delete (from
, from_byte
,
2255 from
+ combined_after_bytes
,
2256 from_byte
+ combined_after_bytes
);
2258 adjust_markers_for_record_delete (from
- 1, from_byte_1
,
2261 if (! EQ (current_buffer
->undo_list
, Qt
))
2262 record_delete (from
- !!combined_after_bytes
, deletion
);
2265 /* Relocate point as if it were a marker. */
2267 adjust_point (from
- (PT
< to
? PT
: to
),
2268 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
2270 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2271 offset_intervals (current_buffer
, from
, - nchars_del
);
2273 /* Adjust the overlay center as needed. This must be done after
2274 adjusting the markers that bound the overlays. */
2275 adjust_overlays_for_delete (from
, nchars_del
);
2277 GAP_SIZE
+= nbytes_del
;
2278 ZV_BYTE
-= nbytes_del
;
2279 Z_BYTE
-= nbytes_del
;
2283 GPT_BYTE
= from_byte
;
2285 if (combined_after_bytes
)
2286 move_gap_both (GPT
+ combined_after_bytes
,
2287 GPT_BYTE
+ combined_after_bytes
);
2289 *(GPT_ADDR
) = 0; /* Put an anchor. */
2294 if (GPT
- BEG
< BEG_UNCHANGED
)
2295 BEG_UNCHANGED
= GPT
- BEG
;
2296 if (Z
- GPT
< END_UNCHANGED
)
2297 END_UNCHANGED
= Z
- GPT
;
2299 if (combined_after_bytes
)
2301 /* Adjust markers for byte combining. As we have already
2302 adjuted markers without concerning byte combining, here we
2303 must concern only byte combining. */
2304 adjust_markers_for_replace (from
, from_byte
, 0, 0, 0, 0,
2305 0, combined_after_bytes
);
2306 combine_bytes (from
, from_byte
, combined_after_bytes
);
2308 record_insert (GPT
- 1, 1);
2310 if (Z
- GPT
< END_UNCHANGED
)
2311 END_UNCHANGED
= Z
- GPT
;
2316 evaporate_overlays (from
);
2319 /* Call this if you're about to change the region of BUFFER from
2320 character positions START to END. This checks the read-only
2321 properties of the region, calls the necessary modification hooks,
2322 and warns the next redisplay that it should pay attention to that
2326 modify_region (buffer
, start
, end
)
2327 struct buffer
*buffer
;
2330 struct buffer
*old_buffer
= current_buffer
;
2332 if (buffer
!= old_buffer
)
2333 set_buffer_internal (buffer
);
2335 prepare_to_modify_buffer (start
, end
, NULL
);
2337 BUF_COMPUTE_UNCHANGED (buffer
, start
- 1, end
);
2339 if (MODIFF
<= SAVE_MODIFF
)
2340 record_first_change ();
2343 buffer
->point_before_scroll
= Qnil
;
2345 if (buffer
!= old_buffer
)
2346 set_buffer_internal (old_buffer
);
2349 /* Check that it is okay to modify the buffer between START and END,
2350 which are char positions.
2352 Run the before-change-function, if any. If intervals are in use,
2353 verify that the text to be modified is not read-only, and call
2354 any modification properties the text may have.
2356 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2357 by holding its value temporarily in a marker. */
2360 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
2364 if (!NILP (current_buffer
->read_only
))
2365 Fbarf_if_buffer_read_only ();
2367 /* Let redisplay consider other windows than selected_window
2368 if modifying another buffer. */
2369 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
2370 ++windows_or_buffers_changed
;
2372 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2373 if (BUF_INTERVALS (current_buffer
) != 0)
2377 Lisp_Object preserve_marker
;
2378 struct gcpro gcpro1
;
2379 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2380 GCPRO1 (preserve_marker
);
2381 verify_interval_modification (current_buffer
, start
, end
);
2382 *preserve_ptr
= marker_position (preserve_marker
);
2383 unchain_marker (preserve_marker
);
2387 verify_interval_modification (current_buffer
, start
, end
);
2390 #ifdef CLASH_DETECTION
2391 if (!NILP (current_buffer
->file_truename
)
2392 /* Make binding buffer-file-name to nil effective. */
2393 && !NILP (current_buffer
->filename
)
2394 && SAVE_MODIFF
>= MODIFF
)
2395 lock_file (current_buffer
->file_truename
);
2397 /* At least warn if this file has changed on disk since it was visited. */
2398 if (!NILP (current_buffer
->filename
)
2399 && SAVE_MODIFF
>= MODIFF
2400 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2401 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
2402 call1 (intern ("ask-user-about-supersession-threat"),
2403 current_buffer
->filename
);
2404 #endif /* not CLASH_DETECTION */
2406 signal_before_change (start
, end
, preserve_ptr
);
2408 if (current_buffer
->newline_cache
)
2409 invalidate_region_cache (current_buffer
,
2410 current_buffer
->newline_cache
,
2411 start
- BEG
, Z
- end
);
2412 if (current_buffer
->width_run_cache
)
2413 invalidate_region_cache (current_buffer
,
2414 current_buffer
->width_run_cache
,
2415 start
- BEG
, Z
- end
);
2417 Vdeactivate_mark
= Qt
;
2420 /* These macros work with an argument named `preserve_ptr'
2421 and a local variable named `preserve_marker'. */
2423 #define PRESERVE_VALUE \
2424 if (preserve_ptr && NILP (preserve_marker)) \
2425 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2427 #define RESTORE_VALUE \
2428 if (! NILP (preserve_marker)) \
2430 *preserve_ptr = marker_position (preserve_marker); \
2431 unchain_marker (preserve_marker); \
2434 #define PRESERVE_START_END \
2435 if (NILP (start_marker)) \
2436 start_marker = Fcopy_marker (start, Qnil); \
2437 if (NILP (end_marker)) \
2438 end_marker = Fcopy_marker (end, Qnil);
2440 #define FETCH_START \
2441 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2444 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2446 /* Signal a change to the buffer immediately before it happens.
2447 START_INT and END_INT are the bounds of the text to be changed.
2449 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2450 by holding its value temporarily in a marker. */
2453 signal_before_change (start_int
, end_int
, preserve_ptr
)
2454 int start_int
, end_int
;
2457 Lisp_Object start
, end
;
2458 Lisp_Object start_marker
, end_marker
;
2459 Lisp_Object preserve_marker
;
2460 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2462 if (inhibit_modification_hooks
)
2465 start
= make_number (start_int
);
2466 end
= make_number (end_int
);
2467 preserve_marker
= Qnil
;
2468 start_marker
= Qnil
;
2470 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2472 /* If buffer is unmodified, run a special hook for that case. */
2473 if (SAVE_MODIFF
>= MODIFF
2474 && !NILP (Vfirst_change_hook
)
2475 && !NILP (Vrun_hooks
))
2479 call1 (Vrun_hooks
, Qfirst_change_hook
);
2482 /* Run the before-change-function if any.
2483 We don't bother "binding" this variable to nil
2484 because it is obsolete anyway and new code should not use it. */
2485 if (!NILP (Vbefore_change_function
))
2489 call2 (Vbefore_change_function
, FETCH_START
, FETCH_END
);
2492 /* Now run the before-change-functions if any. */
2493 if (!NILP (Vbefore_change_functions
))
2495 Lisp_Object args
[3];
2496 Lisp_Object before_change_functions
;
2497 Lisp_Object after_change_functions
;
2498 struct gcpro gcpro1
, gcpro2
;
2503 /* "Bind" before-change-functions and after-change-functions
2504 to nil--but in a way that errors don't know about.
2505 That way, if there's an error in them, they will stay nil. */
2506 before_change_functions
= Vbefore_change_functions
;
2507 after_change_functions
= Vafter_change_functions
;
2508 Vbefore_change_functions
= Qnil
;
2509 Vafter_change_functions
= Qnil
;
2510 GCPRO2 (before_change_functions
, after_change_functions
);
2512 /* Actually run the hook functions. */
2513 args
[0] = Qbefore_change_functions
;
2514 args
[1] = FETCH_START
;
2515 args
[2] = FETCH_END
;
2516 run_hook_list_with_args (before_change_functions
, 3, args
);
2518 /* "Unbind" the variables we "bound" to nil. */
2519 Vbefore_change_functions
= before_change_functions
;
2520 Vafter_change_functions
= after_change_functions
;
2524 if (!NILP (current_buffer
->overlays_before
)
2525 || !NILP (current_buffer
->overlays_after
))
2528 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2529 FETCH_START
, FETCH_END
, Qnil
);
2532 if (! NILP (start_marker
))
2533 free_marker (start_marker
);
2534 if (! NILP (end_marker
))
2535 free_marker (end_marker
);
2540 /* Signal a change immediately after it happens.
2541 CHARPOS is the character position of the start of the changed text.
2542 LENDEL is the number of characters of the text before the change.
2543 (Not the whole buffer; just the part that was changed.)
2544 LENINS is the number of characters in that part of the text
2545 after the change. */
2548 signal_after_change (charpos
, lendel
, lenins
)
2549 int charpos
, lendel
, lenins
;
2551 if (inhibit_modification_hooks
)
2554 /* If we are deferring calls to the after-change functions
2555 and there are no before-change functions,
2556 just record the args that we were going to use. */
2557 if (! NILP (Vcombine_after_change_calls
)
2558 && NILP (Vbefore_change_function
) && NILP (Vbefore_change_functions
)
2559 && NILP (current_buffer
->overlays_before
)
2560 && NILP (current_buffer
->overlays_after
))
2564 if (!NILP (combine_after_change_list
)
2565 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2566 Fcombine_after_change_execute ();
2568 elt
= Fcons (make_number (charpos
- BEG
),
2569 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2570 Fcons (make_number (lenins
- lendel
), Qnil
)));
2571 combine_after_change_list
2572 = Fcons (elt
, combine_after_change_list
);
2573 combine_after_change_buffer
= Fcurrent_buffer ();
2578 if (!NILP (combine_after_change_list
))
2579 Fcombine_after_change_execute ();
2581 /* Run the after-change-function if any.
2582 We don't bother "binding" this variable to nil
2583 because it is obsolete anyway and new code should not use it. */
2584 if (!NILP (Vafter_change_function
))
2585 call3 (Vafter_change_function
,
2586 make_number (charpos
), make_number (charpos
+ lenins
),
2587 make_number (lendel
));
2589 if (!NILP (Vafter_change_functions
))
2591 Lisp_Object args
[4];
2592 Lisp_Object before_change_functions
;
2593 Lisp_Object after_change_functions
;
2594 struct gcpro gcpro1
, gcpro2
;
2596 /* "Bind" before-change-functions and after-change-functions
2597 to nil--but in a way that errors don't know about.
2598 That way, if there's an error in them, they will stay nil. */
2599 before_change_functions
= Vbefore_change_functions
;
2600 after_change_functions
= Vafter_change_functions
;
2601 Vbefore_change_functions
= Qnil
;
2602 Vafter_change_functions
= Qnil
;
2603 GCPRO2 (before_change_functions
, after_change_functions
);
2605 /* Actually run the hook functions. */
2606 args
[0] = Qafter_change_functions
;
2607 XSETFASTINT (args
[1], charpos
);
2608 XSETFASTINT (args
[2], charpos
+ lenins
);
2609 XSETFASTINT (args
[3], lendel
);
2610 run_hook_list_with_args (after_change_functions
,
2613 /* "Unbind" the variables we "bound" to nil. */
2614 Vbefore_change_functions
= before_change_functions
;
2615 Vafter_change_functions
= after_change_functions
;
2619 if (!NILP (current_buffer
->overlays_before
)
2620 || !NILP (current_buffer
->overlays_after
))
2621 report_overlay_modification (make_number (charpos
),
2622 make_number (charpos
+ lenins
),
2624 make_number (charpos
),
2625 make_number (charpos
+ lenins
),
2626 make_number (lendel
));
2628 /* After an insertion, call the text properties
2629 insert-behind-hooks or insert-in-front-hooks. */
2631 report_interval_modification (make_number (charpos
),
2632 make_number (charpos
+ lenins
));
2636 Fcombine_after_change_execute_1 (val
)
2639 Vcombine_after_change_calls
= val
;
2643 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2644 Scombine_after_change_execute
, 0, 0, 0,
2645 "This function is for use internally in `combine-after-change-calls'.")
2648 int count
= specpdl_ptr
- specpdl
;
2649 int beg
, end
, change
;
2653 if (NILP (combine_after_change_list
))
2656 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2658 Fset_buffer (combine_after_change_buffer
);
2660 /* # chars unchanged at beginning of buffer. */
2662 /* # chars unchanged at end of buffer. */
2664 /* Total amount of insertion (negative for deletion). */
2667 /* Scan the various individual changes,
2668 accumulating the range info in BEG, END and CHANGE. */
2669 for (tail
= combine_after_change_list
; CONSP (tail
);
2670 tail
= XCONS (tail
)->cdr
)
2673 int thisbeg
, thisend
, thischange
;
2675 /* Extract the info from the next element. */
2676 elt
= XCONS (tail
)->car
;
2679 thisbeg
= XINT (XCONS (elt
)->car
);
2681 elt
= XCONS (elt
)->cdr
;
2684 thisend
= XINT (XCONS (elt
)->car
);
2686 elt
= XCONS (elt
)->cdr
;
2689 thischange
= XINT (XCONS (elt
)->car
);
2691 /* Merge this range into the accumulated range. */
2692 change
+= thischange
;
2699 /* Get the current start and end positions of the range
2700 that was changed. */
2704 /* We are about to handle these, so discard them. */
2705 combine_after_change_list
= Qnil
;
2707 /* Now run the after-change functions for real.
2708 Turn off the flag that defers them. */
2709 record_unwind_protect (Fcombine_after_change_execute_1
,
2710 Vcombine_after_change_calls
);
2711 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2713 return unbind_to (count
, Qnil
);
2719 staticpro (&combine_after_change_list
);
2720 combine_after_change_list
= Qnil
;
2721 combine_after_change_buffer
= Qnil
;
2723 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2724 "Non-nil means enable debugging checks for invalid marker positions.");
2725 check_markers_debug_flag
= 0;
2726 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2727 "Used internally by the `combine-after-change-calls' macro.");
2728 Vcombine_after_change_calls
= Qnil
;
2730 defsubr (&Scombine_after_change_execute
);