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 bytes before POS/POS_BYTE combine with bytes
883 at the start of STRING to form a single character.
884 If so, return the number of bytes at the start of STRING
885 which combine in this way. Otherwise, return 0. */
888 count_combining_before (string
, length
, pos
, pos_byte
)
889 unsigned char *string
;
893 int opos
= pos
, opos_byte
= pos_byte
;
895 unsigned char *p
= string
;
897 if (NILP (current_buffer
->enable_multibyte_characters
))
899 if (length
== 0 || CHAR_HEAD_P (*string
))
903 c
= FETCH_BYTE (pos_byte
- 1);
904 if (ASCII_BYTE_P (c
))
906 DEC_BOTH (pos
, pos_byte
);
907 c
= FETCH_BYTE (pos_byte
);
908 if (! BASE_LEADING_CODE_P (c
))
911 /* We have a combination situation.
912 Count the bytes at STRING that will combine. */
913 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
919 /* See if the bytes after POS/POS_BYTE combine with bytes
920 at the end of STRING to form a single character.
921 If so, return the number of bytes after POS/POS_BYTE
922 which combine in this way. Otherwise, return 0. */
925 count_combining_after (string
, length
, pos
, pos_byte
)
926 unsigned char *string
;
930 int opos
= pos
, opos_byte
= pos_byte
;
934 if (NILP (current_buffer
->enable_multibyte_characters
))
936 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1]))
939 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
945 /* All characters in `string' are not character head.
946 We must check also preceding bytes at POS.
947 We are sure that the gap is at POS. */
950 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
952 if (i
< 0 || !BASE_LEADING_CODE_P (string
[i
]))
955 else if (!BASE_LEADING_CODE_P (string
[i
]))
960 c
= FETCH_BYTE (pos_byte
);
963 while (pos_byte
< Z_BYTE
)
965 c
= FETCH_BYTE (pos_byte
);
971 return pos_byte
- opos_byte
;
974 /* Adjust the position TARGET/TARGET_BYTE for the combining of NBYTES
975 following the position POS/POS_BYTE to the character preceding POS.
976 If TARGET is after POS+NBYTES, we only have to adjust the character
977 position TARGET, else, if TARGET is after POS, we have to adjust
978 both the character position TARGET and the byte position
979 TARGET_BYTE, else we don't have to do any adjustment. */
981 #define ADJUST_CHAR_POS(target, target_byte) \
983 if (target > pos + nbytes) \
985 else if (target >= pos) \
988 target_byte = pos_byte + nbytes; \
992 /* Combine NBYTES stray trailing-codes, which were formerly separate
993 characters, with the preceding character. These bytes
994 are located after position POS / POS_BYTE, and the preceding character
995 is located just before that position.
997 This function does not adjust markers for byte combining. That
998 should be done in advance by the functions
999 adjust_markers_for_insert or adjust_markers_for_replace. */
1002 combine_bytes (pos
, pos_byte
, nbytes
)
1003 int pos
, pos_byte
, nbytes
;
1005 adjust_overlays_for_delete (pos
, nbytes
);
1007 ADJUST_CHAR_POS (BUF_PT (current_buffer
), BUF_PT_BYTE (current_buffer
));
1008 ADJUST_CHAR_POS (GPT
, GPT_BYTE
);
1009 ADJUST_CHAR_POS (Z
, Z_BYTE
);
1010 ADJUST_CHAR_POS (ZV
, ZV_BYTE
);
1012 if (BUF_INTERVALS (current_buffer
) != 0)
1013 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1014 offset_intervals (current_buffer
, pos
, - nbytes
);
1018 byte_combining_error ()
1020 error ("Byte combining across boundary of accessible buffer text inhibitted");
1023 /* If we are going to combine bytes at POS which is at a narrowed
1024 region boundary, signal an error. */
1025 #define CHECK_BYTE_COMBINING_FOR_INSERT(pos) \
1027 if (combined_before_bytes && pos == BEGV \
1028 || combined_after_bytes && pos == ZV) \
1029 byte_combining_error (); \
1033 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1034 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
1035 are the same as in insert_1. */
1038 insert_1_both (string
, nchars
, nbytes
, inherit
, prepare
, before_markers
)
1039 register unsigned char *string
;
1040 register int nchars
, nbytes
;
1041 int inherit
, prepare
, before_markers
;
1043 register Lisp_Object temp
;
1044 int combined_before_bytes
, combined_after_bytes
;
1046 if (NILP (current_buffer
->enable_multibyte_characters
))
1050 /* Do this before moving and increasing the gap,
1051 because the before-change hooks might move the gap
1052 or make it smaller. */
1053 prepare_to_modify_buffer (PT
, PT
, NULL
);
1056 move_gap_both (PT
, PT_BYTE
);
1057 if (GAP_SIZE
< nbytes
)
1058 make_gap (nbytes
- GAP_SIZE
);
1060 combined_before_bytes
1061 = count_combining_before (string
, nbytes
, PT
, PT_BYTE
);
1062 combined_after_bytes
1063 = count_combining_after (string
, nbytes
, PT
, PT_BYTE
);
1064 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1066 /* Record deletion of the surrounding text that combines with
1067 the insertion. This, together with recording the insertion,
1068 will add up to the right stuff in the undo list.
1070 But there is no need to actually delete the combining bytes
1071 from the buffer and reinsert them. */
1073 if (combined_after_bytes
)
1075 Lisp_Object deletion
;
1078 if (! EQ (current_buffer
->undo_list
, Qt
))
1079 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1080 PT
+ combined_after_bytes
,
1081 PT_BYTE
+ combined_after_bytes
, 1);
1083 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1084 PT
+ combined_after_bytes
,
1085 PT_BYTE
+ combined_after_bytes
);
1086 if (! EQ (current_buffer
->undo_list
, Qt
))
1087 record_delete (PT
, deletion
);
1090 if (combined_before_bytes
)
1092 Lisp_Object deletion
;
1095 if (! EQ (current_buffer
->undo_list
, Qt
))
1096 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1098 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1100 if (! EQ (current_buffer
->undo_list
, Qt
))
1101 record_delete (PT
- 1, deletion
);
1104 record_insert (PT
- !!combined_before_bytes
,
1105 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1108 bcopy (string
, GPT_ADDR
, nbytes
);
1111 /* When we have combining at the end of the insertion,
1112 this is the character position before the combined character. */
1119 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1121 if (combined_after_bytes
)
1122 move_gap_both (GPT
+ combined_after_bytes
,
1123 GPT_BYTE
+ combined_after_bytes
);
1128 adjust_overlays_for_insert (PT
, nchars
);
1129 adjust_markers_for_insert (PT
, PT_BYTE
,
1130 PT
+ nchars
, PT_BYTE
+ nbytes
,
1131 combined_before_bytes
, combined_after_bytes
,
1134 #ifdef USE_TEXT_PROPERTIES
1135 if (BUF_INTERVALS (current_buffer
) != 0)
1136 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES. */
1137 offset_intervals (current_buffer
, PT
, nchars
);
1139 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
1140 Fset_text_properties (make_number (PT
), make_number (PT
+ nchars
),
1145 int pos
= PT
, pos_byte
= PT_BYTE
;
1147 adjust_point (nchars
+ combined_after_bytes
,
1148 nbytes
+ combined_after_bytes
);
1150 if (combined_after_bytes
)
1151 combine_bytes (pos
+ nchars
, pos_byte
+ nbytes
, combined_after_bytes
);
1153 if (combined_before_bytes
)
1154 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1160 /* Insert the part of the text of STRING, a Lisp object assumed to be
1161 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1162 starting at position POS / POS_BYTE. If the text of STRING has properties,
1163 copy them into the buffer.
1165 It does not work to use `insert' for this, because a GC could happen
1166 before we bcopy the stuff into the buffer, and relocate the string
1167 without insert noticing. */
1170 insert_from_string (string
, pos
, pos_byte
, length
, length_byte
, inherit
)
1172 register int pos
, pos_byte
, length
, length_byte
;
1176 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1178 signal_after_change (opoint
, 0, PT
- opoint
);
1181 /* Like `insert_from_string' except that all markers pointing
1182 at the place where the insertion happens are adjusted to point after it. */
1185 insert_from_string_before_markers (string
, pos
, pos_byte
,
1186 length
, length_byte
, inherit
)
1188 register int pos
, pos_byte
, length
, length_byte
;
1192 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1194 signal_after_change (opoint
, 0, PT
- opoint
);
1197 /* Subroutine of the insertion functions above. */
1200 insert_from_string_1 (string
, pos
, pos_byte
, nchars
, nbytes
,
1201 inherit
, before_markers
)
1203 register int pos
, pos_byte
, nchars
, nbytes
;
1204 int inherit
, before_markers
;
1206 register Lisp_Object temp
;
1207 struct gcpro gcpro1
;
1208 int outgoing_nbytes
= nbytes
;
1209 int combined_before_bytes
, combined_after_bytes
;
1210 int adjusted_nchars
;
1213 /* Make OUTGOING_NBYTES describe the text
1214 as it will be inserted in this buffer. */
1216 if (NILP (current_buffer
->enable_multibyte_characters
))
1217 outgoing_nbytes
= nchars
;
1218 else if (! STRING_MULTIBYTE (string
))
1220 = count_size_as_multibyte (&XSTRING (string
)->data
[pos_byte
],
1224 /* Do this before moving and increasing the gap,
1225 because the before-change hooks might move the gap
1226 or make it smaller. */
1227 prepare_to_modify_buffer (PT
, PT
, NULL
);
1230 move_gap_both (PT
, PT_BYTE
);
1231 if (GAP_SIZE
< outgoing_nbytes
)
1232 make_gap (outgoing_nbytes
- GAP_SIZE
);
1235 /* Copy the string text into the buffer, perhaps converting
1236 between single-byte and multibyte. */
1237 copy_text (XSTRING (string
)->data
+ pos_byte
, GPT_ADDR
, nbytes
,
1238 STRING_MULTIBYTE (string
),
1239 ! NILP (current_buffer
->enable_multibyte_characters
));
1241 /* We have copied text into the gap, but we have not altered
1242 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1243 to these functions and get the same results as we would
1244 have got earlier on. Meanwhile, PT_ADDR does point to
1245 the text that has been stored by copy_text. */
1247 combined_before_bytes
1248 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1249 combined_after_bytes
1250 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1252 unsigned char save
= *(GPT_ADDR
);
1254 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1258 /* Record deletion of the surrounding text that combines with
1259 the insertion. This, together with recording the insertion,
1260 will add up to the right stuff in the undo list.
1262 But there is no need to actually delete the combining bytes
1263 from the buffer and reinsert them. */
1265 if (combined_after_bytes
)
1267 Lisp_Object deletion
;
1270 if (! EQ (current_buffer
->undo_list
, Qt
))
1271 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1272 PT
+ combined_after_bytes
,
1273 PT_BYTE
+ combined_after_bytes
, 1);
1275 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1276 PT
+ combined_after_bytes
,
1277 PT_BYTE
+ combined_after_bytes
);
1278 if (! EQ (current_buffer
->undo_list
, Qt
))
1279 record_delete (PT
, deletion
);
1282 if (combined_before_bytes
)
1284 Lisp_Object deletion
;
1287 if (! EQ (current_buffer
->undo_list
, Qt
))
1288 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1290 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1292 if (! EQ (current_buffer
->undo_list
, Qt
))
1293 record_delete (PT
- 1, deletion
);
1296 record_insert (PT
- !!combined_before_bytes
,
1297 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1300 GAP_SIZE
-= outgoing_nbytes
;
1304 GPT_BYTE
+= outgoing_nbytes
;
1305 ZV_BYTE
+= outgoing_nbytes
;
1306 Z_BYTE
+= outgoing_nbytes
;
1307 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1309 if (combined_after_bytes
)
1310 move_gap_both (GPT
+ combined_after_bytes
,
1311 GPT_BYTE
+ combined_after_bytes
);
1316 adjust_overlays_for_insert (PT
, nchars
);
1317 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1318 PT_BYTE
+ outgoing_nbytes
,
1319 combined_before_bytes
, combined_after_bytes
,
1322 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
1323 offset_intervals (current_buffer
, PT
, nchars
);
1325 intervals
= XSTRING (string
)->intervals
;
1326 /* Get the intervals for the part of the string we are inserting--
1327 not including the combined-before bytes. */
1328 if (nbytes
< STRING_BYTES (XSTRING (string
)))
1329 intervals
= copy_intervals (intervals
, pos
, nchars
);
1331 /* Insert those intervals. */
1332 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1333 current_buffer
, inherit
);
1336 int pos
= PT
, pos_byte
= PT_BYTE
;
1338 adjust_point (nchars
+ combined_after_bytes
,
1339 outgoing_nbytes
+ combined_after_bytes
);
1341 if (combined_after_bytes
)
1342 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1343 combined_after_bytes
);
1345 if (combined_before_bytes
)
1346 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1350 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1351 current buffer. If the text in BUF has properties, they are absorbed
1352 into the current buffer.
1354 It does not work to use `insert' for this, because a malloc could happen
1355 and relocate BUF's text before the bcopy happens. */
1358 insert_from_buffer (buf
, charpos
, nchars
, inherit
)
1360 int charpos
, nchars
;
1365 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1366 signal_after_change (opoint
, 0, PT
- opoint
);
1370 insert_from_buffer_1 (buf
, from
, nchars
, inherit
)
1375 register Lisp_Object temp
;
1376 int chunk
, chunk_expanded
;
1377 int from_byte
= buf_charpos_to_bytepos (buf
, from
);
1378 int to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1379 int incoming_nbytes
= to_byte
- from_byte
;
1380 int outgoing_nbytes
= incoming_nbytes
;
1381 int combined_before_bytes
, combined_after_bytes
;
1382 int adjusted_nchars
;
1385 /* Make OUTGOING_NBYTES describe the text
1386 as it will be inserted in this buffer. */
1388 if (NILP (current_buffer
->enable_multibyte_characters
))
1389 outgoing_nbytes
= nchars
;
1390 else if (NILP (buf
->enable_multibyte_characters
))
1392 int outgoing_before_gap
= 0;
1393 int outgoing_after_gap
= 0;
1395 if (from
< BUF_GPT (buf
))
1397 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1398 if (chunk
> incoming_nbytes
)
1399 chunk
= incoming_nbytes
;
1401 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1407 if (chunk
< incoming_nbytes
)
1409 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1411 incoming_nbytes
- chunk
);
1413 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1416 /* Make sure point-max won't overflow after this insertion. */
1417 XSETINT (temp
, outgoing_nbytes
+ Z
);
1418 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1419 error ("Maximum buffer size exceeded");
1421 /* Do this before moving and increasing the gap,
1422 because the before-change hooks might move the gap
1423 or make it smaller. */
1424 prepare_to_modify_buffer (PT
, PT
, NULL
);
1427 move_gap_both (PT
, PT_BYTE
);
1428 if (GAP_SIZE
< outgoing_nbytes
)
1429 make_gap (outgoing_nbytes
- GAP_SIZE
);
1431 if (from
< BUF_GPT (buf
))
1433 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1434 if (chunk
> incoming_nbytes
)
1435 chunk
= incoming_nbytes
;
1436 /* Record number of output bytes, so we know where
1437 to put the output from the second copy_text. */
1439 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1441 ! NILP (buf
->enable_multibyte_characters
),
1442 ! NILP (current_buffer
->enable_multibyte_characters
));
1445 chunk_expanded
= chunk
= 0;
1447 if (chunk
< incoming_nbytes
)
1448 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1449 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1450 ! NILP (buf
->enable_multibyte_characters
),
1451 ! NILP (current_buffer
->enable_multibyte_characters
));
1453 /* We have copied text into the gap, but we have not altered
1454 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1455 to these functions and get the same results as we would
1456 have got earlier on. Meanwhile, GPT_ADDR does point to
1457 the text that has been stored by copy_text. */
1458 combined_before_bytes
1459 = count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1460 combined_after_bytes
1461 = count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
);
1463 unsigned char save
= *(GPT_ADDR
);
1465 CHECK_BYTE_COMBINING_FOR_INSERT (PT
);
1469 /* Record deletion of the surrounding text that combines with
1470 the insertion. This, together with recording the insertion,
1471 will add up to the right stuff in the undo list.
1473 But there is no need to actually delete the combining bytes
1474 from the buffer and reinsert them. */
1476 if (combined_after_bytes
)
1478 Lisp_Object deletion
;
1481 if (! EQ (current_buffer
->undo_list
, Qt
))
1482 deletion
= make_buffer_string_both (PT
, PT_BYTE
,
1483 PT
+ combined_after_bytes
,
1484 PT_BYTE
+ combined_after_bytes
, 1);
1486 adjust_markers_for_record_delete (PT
, PT_BYTE
,
1487 PT
+ combined_after_bytes
,
1488 PT_BYTE
+ combined_after_bytes
);
1489 if (! EQ (current_buffer
->undo_list
, Qt
))
1490 record_delete (PT
, deletion
);
1493 if (combined_before_bytes
)
1495 Lisp_Object deletion
;
1498 if (! EQ (current_buffer
->undo_list
, Qt
))
1499 deletion
= make_buffer_string_both (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1501 adjust_markers_for_record_delete (PT
- 1, CHAR_TO_BYTE (PT
- 1),
1503 if (! EQ (current_buffer
->undo_list
, Qt
))
1504 record_delete (PT
- 1, deletion
);
1507 record_insert (PT
- !!combined_before_bytes
,
1508 nchars
- combined_before_bytes
+ !!combined_before_bytes
);
1511 GAP_SIZE
-= outgoing_nbytes
;
1515 GPT_BYTE
+= outgoing_nbytes
;
1516 ZV_BYTE
+= outgoing_nbytes
;
1517 Z_BYTE
+= outgoing_nbytes
;
1518 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1520 if (combined_after_bytes
)
1521 move_gap_both (GPT
+ combined_after_bytes
,
1522 GPT_BYTE
+ combined_after_bytes
);
1527 adjust_overlays_for_insert (PT
, nchars
);
1528 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1529 PT_BYTE
+ outgoing_nbytes
,
1530 combined_before_bytes
, combined_after_bytes
, 0);
1532 #ifdef USE_TEXT_PROPERTIES
1533 if (BUF_INTERVALS (current_buffer
) != 0)
1534 offset_intervals (current_buffer
, PT
, nchars
);
1537 /* Get the intervals for the part of the string we are inserting--
1538 not including the combined-before bytes. */
1539 intervals
= BUF_INTERVALS (buf
);
1540 if (outgoing_nbytes
< BUF_Z_BYTE (buf
) - BUF_BEG_BYTE (buf
))
1541 intervals
= copy_intervals (intervals
, from
, nchars
);
1543 /* Insert those intervals. */
1544 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1547 int pos
= PT
, pos_byte
= PT_BYTE
;
1549 adjust_point (nchars
+ combined_after_bytes
,
1550 outgoing_nbytes
+ combined_after_bytes
);
1552 if (combined_after_bytes
)
1553 combine_bytes (pos
+ nchars
, pos_byte
+ outgoing_nbytes
,
1554 combined_after_bytes
);
1556 if (combined_before_bytes
)
1557 combine_bytes (pos
, pos_byte
, combined_before_bytes
);
1561 /* This function should be called after moving gap to FROM and before
1562 altering text between FROM and TO. This adjusts various position
1563 keepers and markers as if the text is deleted. Don't forget to
1564 call adjust_after_replace after you actually alter the text. */
1567 adjust_before_replace (from
, from_byte
, to
, to_byte
)
1568 int from
, from_byte
, to
, to_byte
;
1570 Lisp_Object deletion
;
1572 if (! EQ (current_buffer
->undo_list
, Qt
))
1573 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1577 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1579 if (! EQ (current_buffer
->undo_list
, Qt
))
1580 record_delete (from
, deletion
);
1582 adjust_overlays_for_delete (from
, to
- from
);
1585 /* Record undo information and adjust markers and position keepers for
1586 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1587 chars (LEN_BYTE bytes) which resides in the gap just after
1590 PREV_TEXT nil means the new text was just inserted. */
1593 adjust_after_replace (from
, from_byte
, prev_text
, len
, len_byte
)
1594 int from
, from_byte
, len
, len_byte
;
1595 Lisp_Object prev_text
;
1597 int combined_before_bytes
1598 = count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
);
1599 int combined_after_bytes
1600 = count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
);
1601 /* This flag tells if we combine some bytes with a character before
1602 FROM. This happens even if combined_before_bytes is zero. */
1603 int combine_before
= (combined_before_bytes
1604 || (len
== 0 && combined_after_bytes
));
1606 int nchars_del
= 0, nbytes_del
= 0;
1608 if (STRINGP (prev_text
))
1610 nchars_del
= XSTRING (prev_text
)->size
;
1611 nbytes_del
= STRING_BYTES (XSTRING (prev_text
));
1614 if (combine_before
&& from
== BEGV
1615 || combined_after_bytes
&& from
== ZV
)
1617 /* We can't combine bytes nor signal an error here. So, let's
1618 pretend that the new text is just a single space. */
1620 combined_before_bytes
= combined_after_bytes
= 0;
1624 if (combined_after_bytes
)
1626 Lisp_Object deletion
;
1629 if (! EQ (current_buffer
->undo_list
, Qt
))
1630 deletion
= make_buffer_string_both (from
, from_byte
,
1631 from
+ combined_after_bytes
,
1632 from_byte
+ combined_after_bytes
,
1635 adjust_markers_for_record_delete (from
, from_byte
,
1636 from
+ combined_after_bytes
,
1637 from_byte
+ combined_after_bytes
);
1639 if (! EQ (current_buffer
->undo_list
, Qt
))
1640 record_delete (from
+ nchars_del
, deletion
);
1643 if (combined_before_bytes
1644 || len_byte
== 0 && combined_after_bytes
> 0)
1646 Lisp_Object deletion
;
1649 if (! EQ (current_buffer
->undo_list
, Qt
))
1650 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1651 from
, from_byte
, 1);
1652 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1654 if (! EQ (current_buffer
->undo_list
, Qt
))
1655 record_delete (from
- 1, deletion
);
1658 /* Update various buffer positions for the new text. */
1659 GAP_SIZE
-= len_byte
;
1661 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1662 GPT
+= len
; GPT_BYTE
+= len_byte
;
1663 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1665 /* The gap should be at character boundary. */
1666 if (combined_after_bytes
)
1667 move_gap_both (GPT
+ combined_after_bytes
,
1668 GPT_BYTE
+ combined_after_bytes
);
1670 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1672 combined_before_bytes
, combined_after_bytes
);
1673 if (! EQ (current_buffer
->undo_list
, Qt
))
1676 record_delete (from
- combine_before
, prev_text
);
1678 record_insert (from
- 1, len
- combined_before_bytes
+ 1);
1680 record_insert (from
, len
);
1683 if (len
> nchars_del
)
1684 adjust_overlays_for_insert (from
, len
- nchars_del
);
1685 else if (len
< nchars_del
)
1686 adjust_overlays_for_delete (from
, nchars_del
- len
);
1687 #ifdef USE_TEXT_PROPERTIES
1688 if (BUF_INTERVALS (current_buffer
) != 0)
1690 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1695 int pos
= PT
, pos_byte
= PT_BYTE
;
1698 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1700 if (combined_after_bytes
)
1702 if (combined_before_bytes
== len_byte
)
1703 /* This is the case that all new bytes are combined. */
1704 combined_before_bytes
+= combined_after_bytes
;
1706 combine_bytes (from
+ len
, from_byte
+ len_byte
,
1707 combined_after_bytes
);
1709 if (combined_before_bytes
)
1710 combine_bytes (from
, from_byte
, combined_before_bytes
);
1713 /* As byte combining will decrease Z, we must check this again. */
1714 if (Z
- GPT
< END_UNCHANGED
)
1715 END_UNCHANGED
= Z
- GPT
;
1720 evaporate_overlays (from
);
1724 /* Record undo information, adjust markers and position keepers for an
1725 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1726 text already exists in the current buffer but character length (TO
1727 - FROM) may be incorrect, the correct length is NEWLEN. */
1730 adjust_after_insert (from
, from_byte
, to
, to_byte
, newlen
)
1731 int from
, from_byte
, to
, to_byte
, newlen
;
1733 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
1736 move_gap_both (to
, to_byte
);
1737 GAP_SIZE
+= len_byte
;
1738 GPT
-= len
; GPT_BYTE
-= len_byte
;
1739 ZV
-= len
; ZV_BYTE
-= len_byte
;
1740 Z
-= len
; Z_BYTE
-= len_byte
;
1741 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1744 /* Replace the text from character positions FROM to TO with NEW,
1745 If PREPARE is nonzero, call prepare_to_modify_buffer.
1746 If INHERIT, the newly inserted text should inherit text properties
1747 from the surrounding non-deleted text. */
1749 /* Note that this does not yet handle markers quite right.
1750 Also it needs to record a single undo-entry that does a replacement
1751 rather than a separate delete and insert.
1752 That way, undo will also handle markers properly.
1754 But if MARKERS is 0, don't relocate markers. */
1757 replace_range (from
, to
, new, prepare
, inherit
, markers
)
1759 int from
, to
, prepare
, inherit
, markers
;
1761 int inschars
= XSTRING (new)->size
;
1762 int insbytes
= STRING_BYTES (XSTRING (new));
1763 int from_byte
, to_byte
;
1764 int nbytes_del
, nchars_del
;
1765 register Lisp_Object temp
;
1766 struct gcpro gcpro1
;
1767 int combined_before_bytes
, combined_after_bytes
;
1768 int adjusted_inschars
;
1770 int outgoing_insbytes
= insbytes
;
1771 Lisp_Object deletion
;
1779 int range_length
= to
- from
;
1780 prepare_to_modify_buffer (from
, to
, &from
);
1781 to
= from
+ range_length
;
1786 /* Make args be valid */
1792 from_byte
= CHAR_TO_BYTE (from
);
1793 to_byte
= CHAR_TO_BYTE (to
);
1795 nchars_del
= to
- from
;
1796 nbytes_del
= to_byte
- from_byte
;
1798 if (nbytes_del
<= 0 && insbytes
== 0)
1801 /* Make OUTGOING_INSBYTES describe the text
1802 as it will be inserted in this buffer. */
1804 if (NILP (current_buffer
->enable_multibyte_characters
))
1805 outgoing_insbytes
= inschars
;
1806 else if (! STRING_MULTIBYTE (new))
1808 = count_size_as_multibyte (XSTRING (new)->data
, insbytes
);
1810 /* Make sure point-max won't overflow after this insertion. */
1811 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1812 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1813 error ("Maximum buffer size exceeded");
1817 /* Make sure the gap is somewhere in or next to what we are deleting. */
1819 gap_right (from
, from_byte
);
1821 gap_left (to
, to_byte
, 0);
1823 /* Even if we don't record for undo, we must keep the original text
1824 because we may have to recover it because of inappropriate byte
1826 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1829 /* Relocate all markers pointing into the new, larger gap
1830 to point at the end of the text before the gap.
1831 Do this before recording the deletion,
1832 so that undo handles this after reinserting the text. */
1833 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1835 GAP_SIZE
+= nbytes_del
;
1838 ZV_BYTE
-= nbytes_del
;
1839 Z_BYTE
-= nbytes_del
;
1841 GPT_BYTE
= from_byte
;
1842 *(GPT_ADDR
) = 0; /* Put an anchor. */
1847 if (GPT
- BEG
< BEG_UNCHANGED
)
1848 BEG_UNCHANGED
= GPT
- BEG
;
1849 if (Z
- GPT
< END_UNCHANGED
)
1850 END_UNCHANGED
= Z
- GPT
;
1852 if (GAP_SIZE
< insbytes
)
1853 make_gap (insbytes
- GAP_SIZE
);
1855 /* Copy the string text into the buffer, perhaps converting
1856 between single-byte and multibyte. */
1857 copy_text (XSTRING (new)->data
, GPT_ADDR
, insbytes
,
1858 STRING_MULTIBYTE (new),
1859 ! NILP (current_buffer
->enable_multibyte_characters
));
1861 /* We have copied text into the gap, but we have not marked
1862 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1863 here, for both the previous text and the following text.
1864 Meanwhile, GPT_ADDR does point to
1865 the text that has been stored by copy_text. */
1867 combined_before_bytes
1868 = count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1869 combined_after_bytes
1870 = count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
);
1872 if (combined_before_bytes
&& from
== BEGV
1873 || combined_after_bytes
&& from
== ZV
)
1875 /* Bytes are being combined across the region boundary. We
1876 should avoid it. We recover the original contents before
1877 signaling an error. */
1878 bcopy (XSTRING (deletion
)->data
, GPT_ADDR
, nbytes_del
);
1879 GAP_SIZE
-= nbytes_del
;
1882 ZV_BYTE
+= nbytes_del
;
1883 Z_BYTE
+= nbytes_del
;
1884 GPT
= from
+ nchars_del
;
1885 GPT_BYTE
= from_byte
+ nbytes_del
;
1886 *(GPT_ADDR
) = 0; /* Put an anchor. */
1888 adjust_markers_for_insert (from
, from_byte
, to
, to_byte
, 0, 0, 0);
1890 byte_combining_error ();
1894 /* Record deletion of the surrounding text that combines with
1895 the insertion. This, together with recording the insertion,
1896 will add up to the right stuff in the undo list.
1898 But there is no need to actually delete the combining bytes
1899 from the buffer and reinsert them. */
1901 if (combined_after_bytes
)
1903 Lisp_Object deletion
;
1906 if (! EQ (current_buffer
->undo_list
, Qt
))
1907 deletion
= make_buffer_string_both (from
, from_byte
,
1908 from
+ combined_after_bytes
,
1909 from_byte
+ combined_after_bytes
,
1912 adjust_markers_for_record_delete (from
, from_byte
,
1913 from
+ combined_after_bytes
,
1914 from_byte
+ combined_after_bytes
);
1915 if (! EQ (current_buffer
->undo_list
, Qt
))
1916 record_delete (from
+ nchars_del
, deletion
);
1919 if (combined_before_bytes
)
1921 Lisp_Object deletion
;
1924 if (! EQ (current_buffer
->undo_list
, Qt
))
1925 deletion
= make_buffer_string_both (from
- 1, CHAR_TO_BYTE (from
- 1),
1926 from
, from_byte
, 1);
1927 adjust_markers_for_record_delete (from
- 1, CHAR_TO_BYTE (from
- 1),
1929 if (! EQ (current_buffer
->undo_list
, Qt
))
1930 record_delete (from
- 1, deletion
);
1933 if (! EQ (current_buffer
->undo_list
, Qt
))
1935 record_delete (from
- !!combined_before_bytes
, deletion
);
1936 record_insert (from
- !!combined_before_bytes
,
1937 (inschars
- combined_before_bytes
1938 + !!combined_before_bytes
));
1941 GAP_SIZE
-= outgoing_insbytes
;
1945 GPT_BYTE
+= outgoing_insbytes
;
1946 ZV_BYTE
+= outgoing_insbytes
;
1947 Z_BYTE
+= outgoing_insbytes
;
1948 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1950 if (combined_after_bytes
)
1951 move_gap_both (GPT
+ combined_after_bytes
,
1952 GPT_BYTE
+ combined_after_bytes
);
1957 /* Adjust the overlay center as needed. This must be done after
1958 adjusting the markers that bound the overlays. */
1959 adjust_overlays_for_delete (from
, nchars_del
);
1960 adjust_overlays_for_insert (from
, inschars
);
1962 adjust_markers_for_insert (from
, from_byte
,
1963 from
+ inschars
, from_byte
+ outgoing_insbytes
,
1964 combined_before_bytes
, combined_after_bytes
, 0);
1966 #ifdef USE_TEXT_PROPERTIES
1967 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1969 /* Get the intervals for the part of the string we are inserting--
1970 not including the combined-before bytes. */
1971 intervals
= XSTRING (new)->intervals
;
1972 /* Insert those intervals. */
1973 graft_intervals_into_buffer (intervals
, from
, inschars
,
1974 current_buffer
, inherit
);
1977 /* Relocate point as if it were a marker. */
1979 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1980 (from_byte
+ outgoing_insbytes
1981 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1983 if (combined_after_bytes
)
1985 if (combined_before_bytes
== outgoing_insbytes
)
1986 /* This is the case that all new bytes are combined. */
1987 combined_before_bytes
+= combined_after_bytes
;
1989 combine_bytes (from
+ inschars
, from_byte
+ outgoing_insbytes
,
1990 combined_after_bytes
);
1992 if (combined_before_bytes
)
1993 combine_bytes (from
, from_byte
, combined_before_bytes
);
1995 /* As byte combining will decrease Z, we must check this again. */
1996 if (Z
- GPT
< END_UNCHANGED
)
1997 END_UNCHANGED
= Z
- GPT
;
1999 if (outgoing_insbytes
== 0)
2000 evaporate_overlays (from
);
2007 signal_after_change (from
, nchars_del
, GPT
- from
);
2010 /* Delete characters in current buffer
2011 from FROM up to (but not including) TO.
2012 If TO comes before FROM, we delete nothing. */
2015 del_range (from
, to
)
2016 register int from
, to
;
2018 del_range_1 (from
, to
, 1);
2021 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer. */
2024 del_range_1 (from
, to
, prepare
)
2025 int from
, to
, prepare
;
2027 int from_byte
, to_byte
;
2029 if (INTEGERP (current_buffer
->minibuffer_prompt_length
))
2031 /* Don't delete part of a mini-buffer prompt. */
2032 int len
= XFASTINT (current_buffer
->minibuffer_prompt_length
);
2033 from
= max (from
, len
);
2036 /* Make args be valid */
2047 int range_length
= to
- from
;
2048 prepare_to_modify_buffer (from
, to
, &from
);
2049 to
= from
+ range_length
;
2052 from_byte
= CHAR_TO_BYTE (from
);
2053 to_byte
= CHAR_TO_BYTE (to
);
2055 del_range_2 (from
, from_byte
, to
, to_byte
);
2056 signal_after_change (from
, to
- from
, 0);
2059 /* Like del_range_1 but args are byte positions, not char positions. */
2062 del_range_byte (from_byte
, to_byte
, prepare
)
2063 int from_byte
, to_byte
, prepare
;
2067 /* Make args be valid */
2068 if (from_byte
< BEGV_BYTE
)
2069 from_byte
= BEGV_BYTE
;
2070 if (to_byte
> ZV_BYTE
)
2073 if (to_byte
<= from_byte
)
2076 from
= BYTE_TO_CHAR (from_byte
);
2077 to
= BYTE_TO_CHAR (to_byte
);
2081 int old_from
= from
, old_to
= Z
- to
;
2082 int range_length
= to
- from
;
2083 prepare_to_modify_buffer (from
, to
, &from
);
2084 to
= from
+ range_length
;
2086 if (old_from
!= from
)
2087 from_byte
= CHAR_TO_BYTE (from
);
2088 if (old_to
== Z
- to
)
2089 to_byte
= CHAR_TO_BYTE (to
);
2092 del_range_2 (from
, from_byte
, to
, to_byte
);
2093 signal_after_change (from
, to
- from
, 0);
2096 /* Like del_range_1, but positions are specified both as charpos
2100 del_range_both (from
, from_byte
, to
, to_byte
, prepare
)
2101 int from
, from_byte
, to
, to_byte
, prepare
;
2103 /* Make args be valid */
2104 if (from_byte
< BEGV_BYTE
)
2105 from_byte
= BEGV_BYTE
;
2106 if (to_byte
> ZV_BYTE
)
2109 if (to_byte
<= from_byte
)
2119 int old_from
= from
, old_to
= Z
- to
;
2120 int range_length
= to
- from
;
2121 prepare_to_modify_buffer (from
, to
, &from
);
2122 to
= from
+ range_length
;
2124 if (old_from
!= from
)
2125 from_byte
= CHAR_TO_BYTE (from
);
2126 if (old_to
== Z
- to
)
2127 to_byte
= CHAR_TO_BYTE (to
);
2130 del_range_2 (from
, from_byte
, to
, to_byte
);
2131 signal_after_change (from
, to
- from
, 0);
2134 /* Delete a range of text, specified both as character positions
2135 and byte positions. FROM and TO are character positions,
2136 while FROM_BYTE and TO_BYTE are byte positions. */
2139 del_range_2 (from
, from_byte
, to
, to_byte
)
2140 int from
, from_byte
, to
, to_byte
;
2142 register int nbytes_del
, nchars_del
;
2143 int combined_after_bytes
;
2144 Lisp_Object deletion
;
2149 nchars_del
= to
- from
;
2150 nbytes_del
= to_byte
- from_byte
;
2152 /* Make sure the gap is somewhere in or next to what we are deleting. */
2154 gap_right (from
, from_byte
);
2156 gap_left (to
, to_byte
, 0);
2158 combined_after_bytes
2159 = count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
2160 Z_BYTE
- to_byte
, from
, from_byte
);
2161 if (combined_after_bytes
)
2163 if (from
== BEGV
|| to
== ZV
)
2164 byte_combining_error ();
2165 from_byte_1
= from_byte
;
2166 DEC_POS (from_byte_1
);
2169 from_byte_1
= from_byte
;
2171 if (! EQ (current_buffer
->undo_list
, Qt
))
2173 = make_buffer_string_both (from
- !!combined_after_bytes
,
2175 to
+ combined_after_bytes
,
2176 to_byte
+ combined_after_bytes
, 1);
2177 if (combined_after_bytes
)
2178 /* COMBINED_AFTER_BYTES nonzero means that the above code moved
2179 the gap. We must move the gap again to a proper place. */
2180 move_gap_both (from
, from_byte
);
2182 /* Relocate all markers pointing into the new, larger gap
2183 to point at the end of the text before the gap.
2184 Do this before recording the deletion,
2185 so that undo handles this after reinserting the text. */
2186 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
2187 if (combined_after_bytes
)
2189 /* Adjust markers for the phony deletion
2190 that we are about to call record_undo for. */
2192 /* Here we delete the markers that formerly
2193 pointed at TO ... TO + COMBINED_AFTER_BYTES.
2194 But because of the call to adjust_markers_for_delete, above,
2195 they now point at FROM ... FROM + COMBINED_AFTER_BYTES. */
2196 adjust_markers_for_record_delete (from
, from_byte
,
2197 from
+ combined_after_bytes
,
2198 from_byte
+ combined_after_bytes
);
2200 adjust_markers_for_record_delete (from
- 1, from_byte_1
,
2203 if (! EQ (current_buffer
->undo_list
, Qt
))
2204 record_delete (from
- !!combined_after_bytes
, deletion
);
2207 /* Relocate point as if it were a marker. */
2209 adjust_point (from
- (PT
< to
? PT
: to
),
2210 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
2212 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2213 offset_intervals (current_buffer
, from
, - nchars_del
);
2215 /* Adjust the overlay center as needed. This must be done after
2216 adjusting the markers that bound the overlays. */
2217 adjust_overlays_for_delete (from
, nchars_del
);
2219 GAP_SIZE
+= nbytes_del
;
2220 ZV_BYTE
-= nbytes_del
;
2221 Z_BYTE
-= nbytes_del
;
2225 GPT_BYTE
= from_byte
;
2227 if (combined_after_bytes
)
2228 move_gap_both (GPT
+ combined_after_bytes
,
2229 GPT_BYTE
+ combined_after_bytes
);
2231 *(GPT_ADDR
) = 0; /* Put an anchor. */
2236 if (GPT
- BEG
< BEG_UNCHANGED
)
2237 BEG_UNCHANGED
= GPT
- BEG
;
2238 if (Z
- GPT
< END_UNCHANGED
)
2239 END_UNCHANGED
= Z
- GPT
;
2241 if (combined_after_bytes
)
2243 /* Adjust markers for byte combining. As we have already
2244 adjuted markers without concerning byte combining, here we
2245 must concern only byte combining. */
2246 adjust_markers_for_replace (from
, from_byte
, 0, 0, 0, 0,
2247 0, combined_after_bytes
);
2248 combine_bytes (from
, from_byte
, combined_after_bytes
);
2250 record_insert (GPT
- 1, 1);
2252 if (Z
- GPT
< END_UNCHANGED
)
2253 END_UNCHANGED
= Z
- GPT
;
2258 evaporate_overlays (from
);
2261 /* Call this if you're about to change the region of BUFFER from
2262 character positions START to END. This checks the read-only
2263 properties of the region, calls the necessary modification hooks,
2264 and warns the next redisplay that it should pay attention to that
2268 modify_region (buffer
, start
, end
)
2269 struct buffer
*buffer
;
2272 struct buffer
*old_buffer
= current_buffer
;
2274 if (buffer
!= old_buffer
)
2275 set_buffer_internal (buffer
);
2277 prepare_to_modify_buffer (start
, end
, NULL
);
2279 BUF_COMPUTE_UNCHANGED (buffer
, start
- 1, end
);
2281 if (MODIFF
<= SAVE_MODIFF
)
2282 record_first_change ();
2285 buffer
->point_before_scroll
= Qnil
;
2287 if (buffer
!= old_buffer
)
2288 set_buffer_internal (old_buffer
);
2291 /* Check that it is okay to modify the buffer between START and END,
2292 which are char positions.
2294 Run the before-change-function, if any. If intervals are in use,
2295 verify that the text to be modified is not read-only, and call
2296 any modification properties the text may have.
2298 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2299 by holding its value temporarily in a marker. */
2302 prepare_to_modify_buffer (start
, end
, preserve_ptr
)
2306 if (!NILP (current_buffer
->read_only
))
2307 Fbarf_if_buffer_read_only ();
2309 /* Let redisplay consider other windows than selected_window
2310 if modifying another buffer. */
2311 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
2312 ++windows_or_buffers_changed
;
2314 /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
2315 if (BUF_INTERVALS (current_buffer
) != 0)
2319 Lisp_Object preserve_marker
;
2320 struct gcpro gcpro1
;
2321 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2322 GCPRO1 (preserve_marker
);
2323 verify_interval_modification (current_buffer
, start
, end
);
2324 *preserve_ptr
= marker_position (preserve_marker
);
2325 unchain_marker (preserve_marker
);
2329 verify_interval_modification (current_buffer
, start
, end
);
2332 #ifdef CLASH_DETECTION
2333 if (!NILP (current_buffer
->file_truename
)
2334 /* Make binding buffer-file-name to nil effective. */
2335 && !NILP (current_buffer
->filename
)
2336 && SAVE_MODIFF
>= MODIFF
)
2337 lock_file (current_buffer
->file_truename
);
2339 /* At least warn if this file has changed on disk since it was visited. */
2340 if (!NILP (current_buffer
->filename
)
2341 && SAVE_MODIFF
>= MODIFF
2342 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2343 && !NILP (Ffile_exists_p (current_buffer
->filename
)))
2344 call1 (intern ("ask-user-about-supersession-threat"),
2345 current_buffer
->filename
);
2346 #endif /* not CLASH_DETECTION */
2348 signal_before_change (start
, end
, preserve_ptr
);
2350 if (current_buffer
->newline_cache
)
2351 invalidate_region_cache (current_buffer
,
2352 current_buffer
->newline_cache
,
2353 start
- BEG
, Z
- end
);
2354 if (current_buffer
->width_run_cache
)
2355 invalidate_region_cache (current_buffer
,
2356 current_buffer
->width_run_cache
,
2357 start
- BEG
, Z
- end
);
2359 Vdeactivate_mark
= Qt
;
2362 /* These macros work with an argument named `preserve_ptr'
2363 and a local variable named `preserve_marker'. */
2365 #define PRESERVE_VALUE \
2366 if (preserve_ptr && NILP (preserve_marker)) \
2367 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2369 #define RESTORE_VALUE \
2370 if (! NILP (preserve_marker)) \
2372 *preserve_ptr = marker_position (preserve_marker); \
2373 unchain_marker (preserve_marker); \
2376 #define PRESERVE_START_END \
2377 if (NILP (start_marker)) \
2378 start_marker = Fcopy_marker (start, Qnil); \
2379 if (NILP (end_marker)) \
2380 end_marker = Fcopy_marker (end, Qnil);
2382 #define FETCH_START \
2383 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2386 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2388 /* Signal a change to the buffer immediately before it happens.
2389 START_INT and END_INT are the bounds of the text to be changed.
2391 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2392 by holding its value temporarily in a marker. */
2395 signal_before_change (start_int
, end_int
, preserve_ptr
)
2396 int start_int
, end_int
;
2399 Lisp_Object start
, end
;
2400 Lisp_Object start_marker
, end_marker
;
2401 Lisp_Object preserve_marker
;
2402 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2404 if (inhibit_modification_hooks
)
2407 start
= make_number (start_int
);
2408 end
= make_number (end_int
);
2409 preserve_marker
= Qnil
;
2410 start_marker
= Qnil
;
2412 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2414 /* If buffer is unmodified, run a special hook for that case. */
2415 if (SAVE_MODIFF
>= MODIFF
2416 && !NILP (Vfirst_change_hook
)
2417 && !NILP (Vrun_hooks
))
2421 call1 (Vrun_hooks
, Qfirst_change_hook
);
2424 /* Run the before-change-function if any.
2425 We don't bother "binding" this variable to nil
2426 because it is obsolete anyway and new code should not use it. */
2427 if (!NILP (Vbefore_change_function
))
2431 call2 (Vbefore_change_function
, FETCH_START
, FETCH_END
);
2434 /* Now run the before-change-functions if any. */
2435 if (!NILP (Vbefore_change_functions
))
2437 Lisp_Object args
[3];
2438 Lisp_Object before_change_functions
;
2439 Lisp_Object after_change_functions
;
2440 struct gcpro gcpro1
, gcpro2
;
2445 /* "Bind" before-change-functions and after-change-functions
2446 to nil--but in a way that errors don't know about.
2447 That way, if there's an error in them, they will stay nil. */
2448 before_change_functions
= Vbefore_change_functions
;
2449 after_change_functions
= Vafter_change_functions
;
2450 Vbefore_change_functions
= Qnil
;
2451 Vafter_change_functions
= Qnil
;
2452 GCPRO2 (before_change_functions
, after_change_functions
);
2454 /* Actually run the hook functions. */
2455 args
[0] = Qbefore_change_functions
;
2456 args
[1] = FETCH_START
;
2457 args
[2] = FETCH_END
;
2458 run_hook_list_with_args (before_change_functions
, 3, args
);
2460 /* "Unbind" the variables we "bound" to nil. */
2461 Vbefore_change_functions
= before_change_functions
;
2462 Vafter_change_functions
= after_change_functions
;
2466 if (!NILP (current_buffer
->overlays_before
)
2467 || !NILP (current_buffer
->overlays_after
))
2470 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2471 FETCH_START
, FETCH_END
, Qnil
);
2474 if (! NILP (start_marker
))
2475 free_marker (start_marker
);
2476 if (! NILP (end_marker
))
2477 free_marker (end_marker
);
2482 /* Signal a change immediately after it happens.
2483 CHARPOS is the character position of the start of the changed text.
2484 LENDEL is the number of characters of the text before the change.
2485 (Not the whole buffer; just the part that was changed.)
2486 LENINS is the number of characters in that part of the text
2487 after the change. */
2490 signal_after_change (charpos
, lendel
, lenins
)
2491 int charpos
, lendel
, lenins
;
2493 if (inhibit_modification_hooks
)
2496 /* If we are deferring calls to the after-change functions
2497 and there are no before-change functions,
2498 just record the args that we were going to use. */
2499 if (! NILP (Vcombine_after_change_calls
)
2500 && NILP (Vbefore_change_function
) && NILP (Vbefore_change_functions
)
2501 && NILP (current_buffer
->overlays_before
)
2502 && NILP (current_buffer
->overlays_after
))
2506 if (!NILP (combine_after_change_list
)
2507 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2508 Fcombine_after_change_execute ();
2510 elt
= Fcons (make_number (charpos
- BEG
),
2511 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2512 Fcons (make_number (lenins
- lendel
), Qnil
)));
2513 combine_after_change_list
2514 = Fcons (elt
, combine_after_change_list
);
2515 combine_after_change_buffer
= Fcurrent_buffer ();
2520 if (!NILP (combine_after_change_list
))
2521 Fcombine_after_change_execute ();
2523 /* Run the after-change-function if any.
2524 We don't bother "binding" this variable to nil
2525 because it is obsolete anyway and new code should not use it. */
2526 if (!NILP (Vafter_change_function
))
2527 call3 (Vafter_change_function
,
2528 make_number (charpos
), make_number (charpos
+ lenins
),
2529 make_number (lendel
));
2531 if (!NILP (Vafter_change_functions
))
2533 Lisp_Object args
[4];
2534 Lisp_Object before_change_functions
;
2535 Lisp_Object after_change_functions
;
2536 struct gcpro gcpro1
, gcpro2
;
2538 /* "Bind" before-change-functions and after-change-functions
2539 to nil--but in a way that errors don't know about.
2540 That way, if there's an error in them, they will stay nil. */
2541 before_change_functions
= Vbefore_change_functions
;
2542 after_change_functions
= Vafter_change_functions
;
2543 Vbefore_change_functions
= Qnil
;
2544 Vafter_change_functions
= Qnil
;
2545 GCPRO2 (before_change_functions
, after_change_functions
);
2547 /* Actually run the hook functions. */
2548 args
[0] = Qafter_change_functions
;
2549 XSETFASTINT (args
[1], charpos
);
2550 XSETFASTINT (args
[2], charpos
+ lenins
);
2551 XSETFASTINT (args
[3], lendel
);
2552 run_hook_list_with_args (after_change_functions
,
2555 /* "Unbind" the variables we "bound" to nil. */
2556 Vbefore_change_functions
= before_change_functions
;
2557 Vafter_change_functions
= after_change_functions
;
2561 if (!NILP (current_buffer
->overlays_before
)
2562 || !NILP (current_buffer
->overlays_after
))
2563 report_overlay_modification (make_number (charpos
),
2564 make_number (charpos
+ lenins
),
2566 make_number (charpos
),
2567 make_number (charpos
+ lenins
),
2568 make_number (lendel
));
2570 /* After an insertion, call the text properties
2571 insert-behind-hooks or insert-in-front-hooks. */
2573 report_interval_modification (make_number (charpos
),
2574 make_number (charpos
+ lenins
));
2578 Fcombine_after_change_execute_1 (val
)
2581 Vcombine_after_change_calls
= val
;
2585 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2586 Scombine_after_change_execute
, 0, 0, 0,
2587 "This function is for use internally in `combine-after-change-calls'.")
2590 int count
= specpdl_ptr
- specpdl
;
2591 int beg
, end
, change
;
2595 if (NILP (combine_after_change_list
))
2598 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2600 Fset_buffer (combine_after_change_buffer
);
2602 /* # chars unchanged at beginning of buffer. */
2604 /* # chars unchanged at end of buffer. */
2606 /* Total amount of insertion (negative for deletion). */
2609 /* Scan the various individual changes,
2610 accumulating the range info in BEG, END and CHANGE. */
2611 for (tail
= combine_after_change_list
; CONSP (tail
);
2612 tail
= XCONS (tail
)->cdr
)
2615 int thisbeg
, thisend
, thischange
;
2617 /* Extract the info from the next element. */
2618 elt
= XCONS (tail
)->car
;
2621 thisbeg
= XINT (XCONS (elt
)->car
);
2623 elt
= XCONS (elt
)->cdr
;
2626 thisend
= XINT (XCONS (elt
)->car
);
2628 elt
= XCONS (elt
)->cdr
;
2631 thischange
= XINT (XCONS (elt
)->car
);
2633 /* Merge this range into the accumulated range. */
2634 change
+= thischange
;
2641 /* Get the current start and end positions of the range
2642 that was changed. */
2646 /* We are about to handle these, so discard them. */
2647 combine_after_change_list
= Qnil
;
2649 /* Now run the after-change functions for real.
2650 Turn off the flag that defers them. */
2651 record_unwind_protect (Fcombine_after_change_execute_1
,
2652 Vcombine_after_change_calls
);
2653 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2655 return unbind_to (count
, Qnil
);
2661 staticpro (&combine_after_change_list
);
2662 combine_after_change_list
= Qnil
;
2663 combine_after_change_buffer
= Qnil
;
2665 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2666 "Non-nil means enable debugging checks for invalid marker positions.");
2667 check_markers_debug_flag
= 0;
2668 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2669 "Used internally by the `combine-after-change-calls' macro.");
2670 Vcombine_after_change_calls
= Qnil
;
2672 defsubr (&Scombine_after_change_execute
);