*** empty log message ***
[emacs.git] / src / insdel.c
blob281cb0109711ce38f148ed9e1178c934aa01312f
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 86,93,94,95,97,98, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "intervals.h"
26 #include "buffer.h"
27 #include "charset.h"
28 #include "window.h"
29 #include "blockinput.h"
30 #include "region-cache.h"
32 #ifndef NULL
33 #define NULL 0
34 #endif
36 static void insert_from_string_1 P_ ((Lisp_Object, int, int, int, int, int, int));
37 static void insert_from_buffer_1 ();
38 static void gap_left P_ ((int, int, int));
39 static void gap_right P_ ((int, int));
40 static void adjust_markers_gap_motion P_ ((int, int, int));
41 static void adjust_markers_for_insert P_ ((int, int, int, int, int));
42 void adjust_markers_for_delete P_ ((int, int, int, int));
43 static void adjust_markers_for_replace P_ ((int, int, int, int, int, int));
44 static void adjust_point P_ ((int, int));
46 Lisp_Object Fcombine_after_change_execute ();
48 /* Non-nil means don't call the after-change-functions right away,
49 just record an element in Vcombine_after_change_calls_list. */
50 Lisp_Object Vcombine_after_change_calls;
52 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
53 describing changes which happened while combine_after_change_calls
54 was nonzero. We use this to decide how to call them
55 once the deferral ends.
57 In each element.
58 BEG-UNCHANGED is the number of chars before the changed range.
59 END-UNCHANGED is the number of chars after the changed range,
60 and CHANGE-AMOUNT is the number of characters inserted by the change
61 (negative for a deletion). */
62 Lisp_Object combine_after_change_list;
64 /* Buffer which combine_after_change_list is about. */
65 Lisp_Object combine_after_change_buffer;
67 Lisp_Object Qinhibit_modification_hooks;
70 /* Check all markers in the current buffer, looking for something invalid. */
72 static int check_markers_debug_flag;
74 #define CHECK_MARKERS() \
75 if (check_markers_debug_flag) \
76 check_markers (); \
77 else
79 void
80 check_markers ()
82 register Lisp_Object tail;
83 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
85 tail = BUF_MARKERS (current_buffer);
87 while (! NILP (tail))
89 if (XMARKER (tail)->buffer->text != current_buffer->text)
90 abort ();
91 if (XMARKER (tail)->charpos > Z)
92 abort ();
93 if (XMARKER (tail)->bytepos > Z_BYTE)
94 abort ();
95 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (XMARKER (tail)->bytepos)))
96 abort ();
98 tail = XMARKER (tail)->chain;
102 /* Move gap to position CHARPOS.
103 Note that this can quit! */
105 void
106 move_gap (charpos)
107 int charpos;
109 move_gap_both (charpos, charpos_to_bytepos (charpos));
112 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
113 Note that this can quit! */
115 void
116 move_gap_both (charpos, bytepos)
117 int charpos, bytepos;
119 if (bytepos < GPT_BYTE)
120 gap_left (charpos, bytepos, 0);
121 else if (bytepos > GPT_BYTE)
122 gap_right (charpos, bytepos);
125 /* Move the gap to a position less than the current GPT.
126 BYTEPOS describes the new position as a byte position,
127 and CHARPOS is the corresponding char position.
128 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
130 static void
131 gap_left (charpos, bytepos, newgap)
132 register int charpos, bytepos;
133 int newgap;
135 register unsigned char *to, *from;
136 register int i;
137 int new_s1;
139 if (!newgap)
140 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
142 i = GPT_BYTE;
143 to = GAP_END_ADDR;
144 from = GPT_ADDR;
145 new_s1 = GPT_BYTE;
147 /* Now copy the characters. To move the gap down,
148 copy characters up. */
150 while (1)
152 /* I gets number of characters left to copy. */
153 i = new_s1 - bytepos;
154 if (i == 0)
155 break;
156 /* If a quit is requested, stop copying now.
157 Change BYTEPOS to be where we have actually moved the gap to. */
158 if (QUITP)
160 bytepos = new_s1;
161 charpos = BYTE_TO_CHAR (bytepos);
162 break;
164 /* Move at most 32000 chars before checking again for a quit. */
165 if (i > 32000)
166 i = 32000;
167 #ifdef GAP_USE_BCOPY
168 if (i >= 128
169 /* bcopy is safe if the two areas of memory do not overlap
170 or on systems where bcopy is always safe for moving upward. */
171 && (BCOPY_UPWARD_SAFE
172 || to - from >= 128))
174 /* If overlap is not safe, avoid it by not moving too many
175 characters at once. */
176 if (!BCOPY_UPWARD_SAFE && i > to - from)
177 i = to - from;
178 new_s1 -= i;
179 from -= i, to -= i;
180 bcopy (from, to, i);
182 else
183 #endif
185 new_s1 -= i;
186 while (--i >= 0)
187 *--to = *--from;
191 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
192 BYTEPOS is where the loop above stopped, which may be what was specified
193 or may be where a quit was detected. */
194 adjust_markers_gap_motion (bytepos, GPT_BYTE, GAP_SIZE);
195 GPT_BYTE = bytepos;
196 GPT = charpos;
197 if (bytepos < charpos)
198 abort ();
199 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
200 QUIT;
203 /* Move the gap to a position greater than than the current GPT.
204 BYTEPOS describes the new position as a byte position,
205 and CHARPOS is the corresponding char position. */
207 static void
208 gap_right (charpos, bytepos)
209 register int charpos, bytepos;
211 register unsigned char *to, *from;
212 register int i;
213 int new_s1;
215 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
217 i = GPT_BYTE;
218 from = GAP_END_ADDR;
219 to = GPT_ADDR;
220 new_s1 = GPT_BYTE;
222 /* Now copy the characters. To move the gap up,
223 copy characters down. */
225 while (1)
227 /* I gets number of characters left to copy. */
228 i = bytepos - new_s1;
229 if (i == 0)
230 break;
231 /* If a quit is requested, stop copying now.
232 Change BYTEPOS to be where we have actually moved the gap to. */
233 if (QUITP)
235 bytepos = new_s1;
236 charpos = BYTE_TO_CHAR (bytepos);
237 break;
239 /* Move at most 32000 chars before checking again for a quit. */
240 if (i > 32000)
241 i = 32000;
242 #ifdef GAP_USE_BCOPY
243 if (i >= 128
244 /* bcopy is safe if the two areas of memory do not overlap
245 or on systems where bcopy is always safe for moving downward. */
246 && (BCOPY_DOWNWARD_SAFE
247 || from - to >= 128))
249 /* If overlap is not safe, avoid it by not moving too many
250 characters at once. */
251 if (!BCOPY_DOWNWARD_SAFE && i > from - to)
252 i = from - to;
253 new_s1 += i;
254 bcopy (from, to, i);
255 from += i, to += i;
257 else
258 #endif
260 new_s1 += i;
261 while (--i >= 0)
262 *to++ = *from++;
266 adjust_markers_gap_motion (GPT_BYTE + GAP_SIZE, bytepos + GAP_SIZE,
267 - GAP_SIZE);
268 GPT = charpos;
269 GPT_BYTE = bytepos;
270 if (bytepos < charpos)
271 abort ();
272 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
273 QUIT;
276 /* Add AMOUNT to the byte position of every marker in the current buffer
277 whose current byte position is between FROM (exclusive) and TO (inclusive).
279 Also, any markers past the outside of that interval, in the direction
280 of adjustment, are first moved back to the near end of the interval
281 and then adjusted by AMOUNT.
283 When the latter adjustment is done, if AMOUNT is negative,
284 we record the adjustment for undo. (This case happens only for
285 deletion.)
287 The markers' character positions are not altered,
288 because gap motion does not affect character positions. */
290 int adjust_markers_test;
292 static void
293 adjust_markers_gap_motion (from, to, amount)
294 register int from, to, amount;
296 /* Now that a marker has a bytepos, not counting the gap,
297 nothing needs to be done here. */
298 #if 0
299 Lisp_Object marker;
300 register struct Lisp_Marker *m;
301 register int mpos;
303 marker = BUF_MARKERS (current_buffer);
305 while (!NILP (marker))
307 m = XMARKER (marker);
308 mpos = m->bytepos;
309 if (amount > 0)
311 if (mpos > to && mpos < to + amount)
313 if (adjust_markers_test)
314 abort ();
315 mpos = to + amount;
318 else
320 /* Here's the case where a marker is inside text being deleted.
321 AMOUNT can be negative for gap motion, too,
322 but then this range contains no markers. */
323 if (mpos > from + amount && mpos <= from)
325 if (adjust_markers_test)
326 abort ();
327 mpos = from + amount;
330 if (mpos > from && mpos <= to)
331 mpos += amount;
332 m->bufpos = mpos;
333 marker = m->chain;
335 #endif
338 /* Adjust all markers for a deletion
339 whose range in bytes is FROM_BYTE to TO_BYTE.
340 The range in charpos is FROM to TO.
342 This function assumes that the gap is adjacent to
343 or inside of the range being deleted. */
345 void
346 adjust_markers_for_delete (from, from_byte, to, to_byte)
347 register int from, from_byte, to, to_byte;
349 Lisp_Object marker;
350 register struct Lisp_Marker *m;
351 register int charpos;
353 marker = BUF_MARKERS (current_buffer);
355 while (!NILP (marker))
357 m = XMARKER (marker);
358 charpos = m->charpos;
360 if (charpos > Z)
361 abort ();
363 /* If the marker is after the deletion,
364 relocate by number of chars / bytes deleted. */
365 if (charpos > to)
367 m->charpos -= to - from;
368 m->bytepos -= to_byte - from_byte;
370 /* Here's the case where a marker is inside text being deleted. */
371 else if (charpos > from)
373 if (! m->insertion_type)
374 /* Normal markers will end up at the beginning of the
375 re-inserted text after undoing a deletion, and must be
376 adjusted to move them to the correct place. */
377 record_marker_adjustment (marker, from - charpos);
378 else if (charpos < to)
379 /* Before-insertion markers will automatically move forward
380 upon re-inserting the deleted text, so we have to arrange
381 for them to move backward to the correct position. */
382 record_marker_adjustment (marker, charpos - to);
384 m->charpos = from;
385 m->bytepos = from_byte;
387 /* Here's the case where a before-insertion marker is immediately
388 before the deleted region. */
389 else if (charpos == from && m->insertion_type)
391 /* Undoing the change uses normal insertion, which will
392 incorrectly make MARKER move forward, so we arrange for it
393 to then move backward to the correct place at the beginning
394 of the deleted region. */
395 record_marker_adjustment (marker, to - from);
398 marker = m->chain;
403 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
404 to TO / TO_BYTE. We have to relocate the charpos of every marker
405 that points after the insertion (but not their bytepos).
407 When a marker points at the insertion point,
408 we advance it if either its insertion-type is t
409 or BEFORE_MARKERS is true. */
411 static void
412 adjust_markers_for_insert (from, from_byte, to, to_byte, before_markers)
413 register int from, from_byte, to, to_byte;
414 int before_markers;
416 Lisp_Object marker;
417 int adjusted = 0;
418 int nchars = to - from;
419 int nbytes = to_byte - from_byte;
421 marker = BUF_MARKERS (current_buffer);
423 while (!NILP (marker))
425 register struct Lisp_Marker *m = XMARKER (marker);
427 /* In a single-byte buffer, a marker's two positions must be
428 equal. */
429 if (Z == Z_BYTE)
431 if (m->charpos != m->bytepos)
432 abort ();
435 if (m->bytepos == from_byte)
437 if (m->insertion_type || before_markers)
439 m->bytepos = to_byte;
440 m->charpos = to;
441 if (m->insertion_type)
442 adjusted = 1;
445 else if (m->bytepos > from_byte)
447 m->bytepos += nbytes;
448 m->charpos += nchars;
451 marker = m->chain;
454 /* Adjusting only markers whose insertion-type is t may result in
455 disordered overlays in the slot `overlays_before'. */
456 if (adjusted)
457 fix_overlays_before (current_buffer, from, to);
460 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
462 This is used only when the value of point changes due to an insert
463 or delete; it does not represent a conceptual change in point as a
464 marker. In particular, point is not crossing any interval
465 boundaries, so there's no need to use the usual SET_PT macro. In
466 fact it would be incorrect to do so, because either the old or the
467 new value of point is out of sync with the current set of
468 intervals. */
470 static void
471 adjust_point (nchars, nbytes)
472 int nchars, nbytes;
474 BUF_PT (current_buffer) += nchars;
475 BUF_PT_BYTE (current_buffer) += nbytes;
477 /* In a single-byte buffer, the two positions must be equal. */
478 if (ZV == ZV_BYTE
479 && PT != PT_BYTE)
480 abort ();
483 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
484 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
485 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
486 an insertion. */
488 static void
489 adjust_markers_for_replace (from, from_byte, old_chars, old_bytes,
490 new_chars, new_bytes)
491 int from, from_byte, old_chars, old_bytes, new_chars, new_bytes;
493 Lisp_Object marker = BUF_MARKERS (current_buffer);
494 int prev_to_byte = from_byte + old_bytes;
495 int diff_chars = new_chars - old_chars;
496 int diff_bytes = new_bytes - old_bytes;
498 while (!NILP (marker))
500 register struct Lisp_Marker *m = XMARKER (marker);
502 if (m->bytepos >= prev_to_byte)
504 m->charpos += diff_chars;
505 m->bytepos += diff_bytes;
507 else if (m->bytepos > from_byte)
509 m->charpos = from;
510 m->bytepos = from_byte;
513 marker = m->chain;
516 CHECK_MARKERS ();
520 /* Make the gap NBYTES_ADDED bytes longer. */
522 void
523 make_gap_larger (nbytes_added)
524 int nbytes_added;
526 Lisp_Object tem;
527 int real_gap_loc;
528 int real_gap_loc_byte;
529 int old_gap_size;
531 /* If we have to get more space, get enough to last a while. */
532 nbytes_added += 2000;
534 /* Don't allow a buffer size that won't fit in an int
535 even if it will fit in a Lisp integer.
536 That won't work because so many places use `int'.
538 Make sure we don't introduce overflows in the calculation. */
540 if (Z_BYTE - BEG_BYTE + GAP_SIZE
541 >= (((EMACS_INT) 1 << (min (VALBITS, BITS_PER_INT) - 1)) - 1
542 - nbytes_added))
543 error ("Buffer exceeds maximum size");
545 enlarge_buffer_text (current_buffer, nbytes_added);
547 /* Prevent quitting in move_gap. */
548 tem = Vinhibit_quit;
549 Vinhibit_quit = Qt;
551 real_gap_loc = GPT;
552 real_gap_loc_byte = GPT_BYTE;
553 old_gap_size = GAP_SIZE;
555 /* Call the newly allocated space a gap at the end of the whole space. */
556 GPT = Z + GAP_SIZE;
557 GPT_BYTE = Z_BYTE + GAP_SIZE;
558 GAP_SIZE = nbytes_added;
560 /* Move the new gap down to be consecutive with the end of the old one.
561 This adjusts the markers properly too. */
562 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
564 /* Now combine the two into one large gap. */
565 GAP_SIZE += old_gap_size;
566 GPT = real_gap_loc;
567 GPT_BYTE = real_gap_loc_byte;
569 /* Put an anchor. */
570 *(Z_ADDR) = 0;
572 Vinhibit_quit = tem;
576 /* Make the gap NBYTES_REMOVED bytes shorter. */
578 void
579 make_gap_smaller (nbytes_removed)
580 int nbytes_removed;
582 Lisp_Object tem;
583 int real_gap_loc;
584 int real_gap_loc_byte;
585 int real_Z;
586 int real_Z_byte;
587 int real_beg_unchanged;
588 int new_gap_size;
590 /* Make sure the gap is at least 20 bytes. */
591 if (GAP_SIZE - nbytes_removed < 20)
592 nbytes_removed = GAP_SIZE - 20;
594 /* Prevent quitting in move_gap. */
595 tem = Vinhibit_quit;
596 Vinhibit_quit = Qt;
598 real_gap_loc = GPT;
599 real_gap_loc_byte = GPT_BYTE;
600 new_gap_size = GAP_SIZE - nbytes_removed;
601 real_Z = Z;
602 real_Z_byte = Z_BYTE;
603 real_beg_unchanged = BEG_UNCHANGED;
605 /* Pretend that the last unwanted part of the gap is the entire gap,
606 and that the first desired part of the gap is part of the buffer
607 text. */
608 bzero (GPT_ADDR, new_gap_size);
609 GPT += new_gap_size;
610 GPT_BYTE += new_gap_size;
611 Z += new_gap_size;
612 Z_BYTE += new_gap_size;
613 GAP_SIZE = nbytes_removed;
615 /* Move the unwanted pretend gap to the end of the buffer. This
616 adjusts the markers properly too. */
617 gap_right (Z, Z_BYTE);
619 enlarge_buffer_text (current_buffer, -nbytes_removed);
621 /* Now restore the desired gap. */
622 GAP_SIZE = new_gap_size;
623 GPT = real_gap_loc;
624 GPT_BYTE = real_gap_loc_byte;
625 Z = real_Z;
626 Z_BYTE = real_Z_byte;
627 BEG_UNCHANGED = real_beg_unchanged;
629 /* Put an anchor. */
630 *(Z_ADDR) = 0;
632 Vinhibit_quit = tem;
635 void
636 make_gap (nbytes_added)
637 int nbytes_added;
639 if (nbytes_added >= 0)
640 make_gap_larger (nbytes_added);
641 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
642 else
643 make_gap_smaller (-nbytes_added);
644 #endif
647 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
648 FROM_MULTIBYTE says whether the incoming text is multibyte.
649 TO_MULTIBYTE says whether to store the text as multibyte.
650 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
652 Return the number of bytes stored at TO_ADDR. */
655 copy_text (from_addr, to_addr, nbytes,
656 from_multibyte, to_multibyte)
657 const unsigned char *from_addr;
658 unsigned char *to_addr;
659 int nbytes;
660 int from_multibyte, to_multibyte;
662 if (from_multibyte == to_multibyte)
664 bcopy (from_addr, to_addr, nbytes);
665 return nbytes;
667 else if (from_multibyte)
669 int nchars = 0;
670 int bytes_left = nbytes;
671 Lisp_Object tbl = Qnil;
673 /* We set the variable tbl to the reverse table of
674 Vnonascii_translation_table in advance. */
675 if (CHAR_TABLE_P (Vnonascii_translation_table))
677 tbl = Fchar_table_extra_slot (Vnonascii_translation_table,
678 make_number (0));
679 if (!CHAR_TABLE_P (tbl))
680 tbl = Qnil;
683 /* Convert multibyte to single byte. */
684 while (bytes_left > 0)
686 int thislen, c;
687 c = STRING_CHAR_AND_LENGTH (from_addr, bytes_left, thislen);
688 if (!SINGLE_BYTE_CHAR_P (c))
689 c = multibyte_char_to_unibyte (c, tbl);
690 *to_addr++ = c;
691 from_addr += thislen;
692 bytes_left -= thislen;
693 nchars++;
695 return nchars;
697 else
699 unsigned char *initial_to_addr = to_addr;
701 /* Convert single-byte to multibyte. */
702 while (nbytes > 0)
704 int c = *from_addr++;
706 if (c >= 0200)
708 c = unibyte_char_to_multibyte (c);
709 to_addr += CHAR_STRING (c, to_addr);
710 nbytes--;
712 else
713 /* Special case for speed. */
714 *to_addr++ = c, nbytes--;
716 return to_addr - initial_to_addr;
720 /* Return the number of bytes it would take
721 to convert some single-byte text to multibyte.
722 The single-byte text consists of NBYTES bytes at PTR. */
725 count_size_as_multibyte (ptr, nbytes)
726 const unsigned char *ptr;
727 int nbytes;
729 int i;
730 int outgoing_nbytes = 0;
732 for (i = 0; i < nbytes; i++)
734 unsigned int c = *ptr++;
736 if (c < 0200)
737 outgoing_nbytes++;
738 else
740 c = unibyte_char_to_multibyte (c);
741 outgoing_nbytes += CHAR_BYTES (c);
745 return outgoing_nbytes;
748 /* Insert a string of specified length before point.
749 This function judges multibyteness based on
750 enable_multibyte_characters in the current buffer;
751 it never converts between single-byte and multibyte.
753 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
754 prepare_to_modify_buffer could relocate the text. */
756 void
757 insert (string, nbytes)
758 register const unsigned char *string;
759 register int nbytes;
761 if (nbytes > 0)
763 int opoint = PT;
764 insert_1 (string, nbytes, 0, 1, 0);
765 signal_after_change (opoint, 0, PT - opoint);
766 update_compositions (opoint, PT, CHECK_BORDER);
770 /* Likewise, but inherit text properties from neighboring characters. */
772 void
773 insert_and_inherit (string, nbytes)
774 register const unsigned char *string;
775 register int nbytes;
777 if (nbytes > 0)
779 int opoint = PT;
780 insert_1 (string, nbytes, 1, 1, 0);
781 signal_after_change (opoint, 0, PT - opoint);
782 update_compositions (opoint, PT, CHECK_BORDER);
786 /* Insert the character C before point. Do not inherit text properties. */
788 void
789 insert_char (c)
790 int c;
792 unsigned char str[MAX_MULTIBYTE_LENGTH];
793 int len;
795 if (! NILP (current_buffer->enable_multibyte_characters))
796 len = CHAR_STRING (c, str);
797 else
799 len = 1;
800 str[0] = c;
803 insert (str, len);
806 /* Insert the null-terminated string S before point. */
808 void
809 insert_string (s)
810 const char *s;
812 insert (s, strlen (s));
815 /* Like `insert' except that all markers pointing at the place where
816 the insertion happens are adjusted to point after it.
817 Don't use this function to insert part of a Lisp string,
818 since gc could happen and relocate it. */
820 void
821 insert_before_markers (string, nbytes)
822 const unsigned char *string;
823 register int nbytes;
825 if (nbytes > 0)
827 int opoint = PT;
829 insert_1 (string, nbytes, 0, 1, 1);
830 signal_after_change (opoint, 0, PT - opoint);
831 update_compositions (opoint, PT, CHECK_BORDER);
835 /* Likewise, but inherit text properties from neighboring characters. */
837 void
838 insert_before_markers_and_inherit (string, nbytes)
839 const unsigned char *string;
840 register int nbytes;
842 if (nbytes > 0)
844 int opoint = PT;
846 insert_1 (string, nbytes, 1, 1, 1);
847 signal_after_change (opoint, 0, PT - opoint);
848 update_compositions (opoint, PT, CHECK_BORDER);
852 /* Subroutine used by the insert functions above. */
854 void
855 insert_1 (string, nbytes, inherit, prepare, before_markers)
856 register const unsigned char *string;
857 register int nbytes;
858 int inherit, prepare, before_markers;
860 insert_1_both (string, chars_in_text (string, nbytes), nbytes,
861 inherit, prepare, before_markers);
865 #ifdef BYTE_COMBINING_DEBUG
867 /* See if the bytes before POS/POS_BYTE combine with bytes
868 at the start of STRING to form a single character.
869 If so, return the number of bytes at the start of STRING
870 which combine in this way. Otherwise, return 0. */
873 count_combining_before (string, length, pos, pos_byte)
874 const unsigned char *string;
875 int length;
876 int pos, pos_byte;
878 int len, combining_bytes;
879 const unsigned char *p;
881 if (NILP (current_buffer->enable_multibyte_characters))
882 return 0;
884 /* At first, we can exclude the following cases:
885 (1) STRING[0] can't be a following byte of multibyte sequence.
886 (2) POS is the start of the current buffer.
887 (3) A character before POS is not a multibyte character. */
888 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
889 return 0;
890 if (pos_byte == BEG_BYTE) /* case (2) */
891 return 0;
892 len = 1;
893 p = BYTE_POS_ADDR (pos_byte - 1);
894 while (! CHAR_HEAD_P (*p)) p--, len++;
895 if (! BASE_LEADING_CODE_P (*p)) /* case (3) */
896 return 0;
898 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
899 if (combining_bytes <= 0)
900 /* The character preceding POS is, complete and no room for
901 combining bytes (combining_bytes == 0), or an independent 8-bit
902 character (combining_bytes < 0). */
903 return 0;
905 /* We have a combination situation. Count the bytes at STRING that
906 may combine. */
907 p = string + 1;
908 while (!CHAR_HEAD_P (*p) && p < string + length)
909 p++;
911 return (combining_bytes < p - string ? combining_bytes : p - string);
914 /* See if the bytes after POS/POS_BYTE combine with bytes
915 at the end of STRING to form a single character.
916 If so, return the number of bytes after POS/POS_BYTE
917 which combine in this way. Otherwise, return 0. */
920 count_combining_after (string, length, pos, pos_byte)
921 const unsigned char *string;
922 int length;
923 int pos, pos_byte;
925 int opos_byte = pos_byte;
926 int i;
927 int bytes;
928 unsigned char *bufp;
930 if (NILP (current_buffer->enable_multibyte_characters))
931 return 0;
933 /* At first, we can exclude the following cases:
934 (1) The last byte of STRING is an ASCII.
935 (2) POS is the last of the current buffer.
936 (3) A character at POS can't be a following byte of multibyte
937 character. */
938 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
939 return 0;
940 if (pos_byte == Z_BYTE) /* case (2) */
941 return 0;
942 bufp = BYTE_POS_ADDR (pos_byte);
943 if (CHAR_HEAD_P (*bufp)) /* case (3) */
944 return 0;
946 i = length - 1;
947 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
949 i--;
951 if (i < 0)
953 /* All characters in STRING are not character head. We must
954 check also preceding bytes at POS. We are sure that the gap
955 is at POS. */
956 unsigned char *p = BEG_ADDR;
957 i = pos_byte - 2;
958 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
959 i--;
960 if (i < 0 || !BASE_LEADING_CODE_P (p[i]))
961 return 0;
963 bytes = BYTES_BY_CHAR_HEAD (p[i]);
964 return (bytes <= pos_byte - 1 - i + length
966 : bytes - (pos_byte - 1 - i + length));
968 if (!BASE_LEADING_CODE_P (string[i]))
969 return 0;
971 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
972 bufp++, pos_byte++;
973 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
975 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
978 #endif
981 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
982 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
983 are the same as in insert_1. */
985 void
986 insert_1_both (string, nchars, nbytes, inherit, prepare, before_markers)
987 register const unsigned char *string;
988 register int nchars, nbytes;
989 int inherit, prepare, before_markers;
991 if (nchars == 0)
992 return;
994 if (NILP (current_buffer->enable_multibyte_characters))
995 nchars = nbytes;
997 if (prepare)
998 /* Do this before moving and increasing the gap,
999 because the before-change hooks might move the gap
1000 or make it smaller. */
1001 prepare_to_modify_buffer (PT, PT, NULL);
1003 if (PT != GPT)
1004 move_gap_both (PT, PT_BYTE);
1005 if (GAP_SIZE < nbytes)
1006 make_gap (nbytes - GAP_SIZE);
1008 #ifdef BYTE_COMBINING_DEBUG
1009 if (count_combining_before (string, nbytes, PT, PT_BYTE)
1010 || count_combining_after (string, nbytes, PT, PT_BYTE))
1011 abort ();
1012 #endif
1014 /* Record deletion of the surrounding text that combines with
1015 the insertion. This, together with recording the insertion,
1016 will add up to the right stuff in the undo list. */
1017 record_insert (PT, nchars);
1018 MODIFF++;
1020 bcopy (string, GPT_ADDR, nbytes);
1022 GAP_SIZE -= nbytes;
1023 GPT += nchars;
1024 ZV += nchars;
1025 Z += nchars;
1026 GPT_BYTE += nbytes;
1027 ZV_BYTE += nbytes;
1028 Z_BYTE += nbytes;
1029 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1031 if (GPT_BYTE < GPT)
1032 abort ();
1034 /* The insert may have been in the unchanged region, so check again. */
1035 if (Z - GPT < END_UNCHANGED)
1036 END_UNCHANGED = Z - GPT;
1038 adjust_overlays_for_insert (PT, nchars);
1039 adjust_markers_for_insert (PT, PT_BYTE,
1040 PT + nchars, PT_BYTE + nbytes,
1041 before_markers);
1043 if (BUF_INTERVALS (current_buffer) != 0)
1044 offset_intervals (current_buffer, PT, nchars);
1046 if (!inherit && BUF_INTERVALS (current_buffer) != 0)
1047 set_text_properties (make_number (PT), make_number (PT + nchars),
1048 Qnil, Qnil, Qnil);
1050 adjust_point (nchars, nbytes);
1052 CHECK_MARKERS ();
1055 /* Insert the part of the text of STRING, a Lisp object assumed to be
1056 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1057 starting at position POS / POS_BYTE. If the text of STRING has properties,
1058 copy them into the buffer.
1060 It does not work to use `insert' for this, because a GC could happen
1061 before we bcopy the stuff into the buffer, and relocate the string
1062 without insert noticing. */
1064 void
1065 insert_from_string (string, pos, pos_byte, length, length_byte, inherit)
1066 Lisp_Object string;
1067 register int pos, pos_byte, length, length_byte;
1068 int inherit;
1070 int opoint = PT;
1071 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1072 inherit, 0);
1073 signal_after_change (opoint, 0, PT - opoint);
1074 update_compositions (opoint, PT, CHECK_BORDER);
1077 /* Like `insert_from_string' except that all markers pointing
1078 at the place where the insertion happens are adjusted to point after it. */
1080 void
1081 insert_from_string_before_markers (string, pos, pos_byte,
1082 length, length_byte, inherit)
1083 Lisp_Object string;
1084 register int pos, pos_byte, length, length_byte;
1085 int inherit;
1087 int opoint = PT;
1088 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
1089 inherit, 1);
1090 signal_after_change (opoint, 0, PT - opoint);
1091 update_compositions (opoint, PT, CHECK_BORDER);
1094 /* Subroutine of the insertion functions above. */
1096 static void
1097 insert_from_string_1 (string, pos, pos_byte, nchars, nbytes,
1098 inherit, before_markers)
1099 Lisp_Object string;
1100 register int pos, pos_byte, nchars, nbytes;
1101 int inherit, before_markers;
1103 struct gcpro gcpro1;
1104 int outgoing_nbytes = nbytes;
1105 INTERVAL intervals;
1107 /* Make OUTGOING_NBYTES describe the text
1108 as it will be inserted in this buffer. */
1110 if (NILP (current_buffer->enable_multibyte_characters))
1111 outgoing_nbytes = nchars;
1112 else if (! STRING_MULTIBYTE (string))
1113 outgoing_nbytes
1114 = count_size_as_multibyte (SDATA (string) + pos_byte,
1115 nbytes);
1117 GCPRO1 (string);
1118 /* Do this before moving and increasing the gap,
1119 because the before-change hooks might move the gap
1120 or make it smaller. */
1121 prepare_to_modify_buffer (PT, PT, NULL);
1123 if (PT != GPT)
1124 move_gap_both (PT, PT_BYTE);
1125 if (GAP_SIZE < outgoing_nbytes)
1126 make_gap (outgoing_nbytes - GAP_SIZE);
1127 UNGCPRO;
1129 /* Copy the string text into the buffer, perhaps converting
1130 between single-byte and multibyte. */
1131 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
1132 STRING_MULTIBYTE (string),
1133 ! NILP (current_buffer->enable_multibyte_characters));
1135 #ifdef BYTE_COMBINING_DEBUG
1136 /* We have copied text into the gap, but we have not altered
1137 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1138 to these functions and get the same results as we would
1139 have got earlier on. Meanwhile, PT_ADDR does point to
1140 the text that has been stored by copy_text. */
1141 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1142 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1143 abort ();
1144 #endif
1146 record_insert (PT, nchars);
1147 MODIFF++;
1149 GAP_SIZE -= outgoing_nbytes;
1150 GPT += nchars;
1151 ZV += nchars;
1152 Z += nchars;
1153 GPT_BYTE += outgoing_nbytes;
1154 ZV_BYTE += outgoing_nbytes;
1155 Z_BYTE += outgoing_nbytes;
1156 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1158 if (GPT_BYTE < GPT)
1159 abort ();
1161 /* The insert may have been in the unchanged region, so check again. */
1162 if (Z - GPT < END_UNCHANGED)
1163 END_UNCHANGED = Z - GPT;
1165 adjust_overlays_for_insert (PT, nchars);
1166 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1167 PT_BYTE + outgoing_nbytes,
1168 before_markers);
1170 offset_intervals (current_buffer, PT, nchars);
1172 intervals = STRING_INTERVALS (string);
1173 /* Get the intervals for the part of the string we are inserting. */
1174 if (nbytes < SBYTES (string))
1175 intervals = copy_intervals (intervals, pos, nchars);
1177 /* Insert those intervals. */
1178 graft_intervals_into_buffer (intervals, PT, nchars,
1179 current_buffer, inherit);
1181 adjust_point (nchars, outgoing_nbytes);
1184 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1185 current buffer. If the text in BUF has properties, they are absorbed
1186 into the current buffer.
1188 It does not work to use `insert' for this, because a malloc could happen
1189 and relocate BUF's text before the bcopy happens. */
1191 void
1192 insert_from_buffer (buf, charpos, nchars, inherit)
1193 struct buffer *buf;
1194 int charpos, nchars;
1195 int inherit;
1197 int opoint = PT;
1199 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1200 signal_after_change (opoint, 0, PT - opoint);
1201 update_compositions (opoint, PT, CHECK_BORDER);
1204 static void
1205 insert_from_buffer_1 (buf, from, nchars, inherit)
1206 struct buffer *buf;
1207 int from, nchars;
1208 int inherit;
1210 register Lisp_Object temp;
1211 int chunk, chunk_expanded;
1212 int from_byte = buf_charpos_to_bytepos (buf, from);
1213 int to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1214 int incoming_nbytes = to_byte - from_byte;
1215 int outgoing_nbytes = incoming_nbytes;
1216 INTERVAL intervals;
1218 /* Make OUTGOING_NBYTES describe the text
1219 as it will be inserted in this buffer. */
1221 if (NILP (current_buffer->enable_multibyte_characters))
1222 outgoing_nbytes = nchars;
1223 else if (NILP (buf->enable_multibyte_characters))
1225 int outgoing_before_gap = 0;
1226 int outgoing_after_gap = 0;
1228 if (from < BUF_GPT (buf))
1230 chunk = BUF_GPT_BYTE (buf) - from_byte;
1231 if (chunk > incoming_nbytes)
1232 chunk = incoming_nbytes;
1233 outgoing_before_gap
1234 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1235 chunk);
1237 else
1238 chunk = 0;
1240 if (chunk < incoming_nbytes)
1241 outgoing_after_gap
1242 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1243 from_byte + chunk),
1244 incoming_nbytes - chunk);
1246 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1249 /* Make sure point-max won't overflow after this insertion. */
1250 XSETINT (temp, outgoing_nbytes + Z);
1251 if (outgoing_nbytes + Z != XINT (temp))
1252 error ("Maximum buffer size exceeded");
1254 /* Do this before moving and increasing the gap,
1255 because the before-change hooks might move the gap
1256 or make it smaller. */
1257 prepare_to_modify_buffer (PT, PT, NULL);
1259 if (PT != GPT)
1260 move_gap_both (PT, PT_BYTE);
1261 if (GAP_SIZE < outgoing_nbytes)
1262 make_gap (outgoing_nbytes - GAP_SIZE);
1264 if (from < BUF_GPT (buf))
1266 chunk = BUF_GPT_BYTE (buf) - from_byte;
1267 if (chunk > incoming_nbytes)
1268 chunk = incoming_nbytes;
1269 /* Record number of output bytes, so we know where
1270 to put the output from the second copy_text. */
1271 chunk_expanded
1272 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1273 GPT_ADDR, chunk,
1274 ! NILP (buf->enable_multibyte_characters),
1275 ! NILP (current_buffer->enable_multibyte_characters));
1277 else
1278 chunk_expanded = chunk = 0;
1280 if (chunk < incoming_nbytes)
1281 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1282 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1283 ! NILP (buf->enable_multibyte_characters),
1284 ! NILP (current_buffer->enable_multibyte_characters));
1286 #ifdef BYTE_COMBINING_DEBUG
1287 /* We have copied text into the gap, but we have not altered
1288 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1289 to these functions and get the same results as we would
1290 have got earlier on. Meanwhile, GPT_ADDR does point to
1291 the text that has been stored by copy_text. */
1292 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1293 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1294 abort ();
1295 #endif
1297 record_insert (PT, nchars);
1298 MODIFF++;
1300 GAP_SIZE -= outgoing_nbytes;
1301 GPT += nchars;
1302 ZV += nchars;
1303 Z += nchars;
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 (GPT_BYTE < GPT)
1310 abort ();
1312 /* The insert may have been in the unchanged region, so check again. */
1313 if (Z - GPT < END_UNCHANGED)
1314 END_UNCHANGED = Z - GPT;
1316 adjust_overlays_for_insert (PT, nchars);
1317 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1318 PT_BYTE + outgoing_nbytes,
1321 if (BUF_INTERVALS (current_buffer) != 0)
1322 offset_intervals (current_buffer, PT, nchars);
1324 /* Get the intervals for the part of the string we are inserting. */
1325 intervals = BUF_INTERVALS (buf);
1326 if (outgoing_nbytes < BUF_Z_BYTE (buf) - BUF_BEG_BYTE (buf))
1328 if (buf == current_buffer && PT <= from)
1329 from += nchars;
1330 intervals = copy_intervals (intervals, from, nchars);
1333 /* Insert those intervals. */
1334 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1336 adjust_point (nchars, outgoing_nbytes);
1339 /* Record undo information and adjust markers and position keepers for
1340 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1341 chars (LEN_BYTE bytes) which resides in the gap just after
1342 GPT_ADDR.
1344 PREV_TEXT nil means the new text was just inserted. */
1346 void
1347 adjust_after_replace (from, from_byte, prev_text, len, len_byte)
1348 int from, from_byte, len, len_byte;
1349 Lisp_Object prev_text;
1351 int nchars_del = 0, nbytes_del = 0;
1353 #ifdef BYTE_COMBINING_DEBUG
1354 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1355 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1356 abort ();
1357 #endif
1359 if (STRINGP (prev_text))
1361 nchars_del = SCHARS (prev_text);
1362 nbytes_del = SBYTES (prev_text);
1365 /* Update various buffer positions for the new text. */
1366 GAP_SIZE -= len_byte;
1367 ZV += len; Z+= len;
1368 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1369 GPT += len; GPT_BYTE += len_byte;
1370 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1372 if (nchars_del > 0)
1373 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1374 len, len_byte);
1375 else
1376 adjust_markers_for_insert (from, from_byte,
1377 from + len, from_byte + len_byte, 0);
1379 if (! EQ (current_buffer->undo_list, Qt))
1381 if (nchars_del > 0)
1382 record_delete (from, prev_text);
1383 record_insert (from, len);
1386 if (len > nchars_del)
1387 adjust_overlays_for_insert (from, len - nchars_del);
1388 else if (len < nchars_del)
1389 adjust_overlays_for_delete (from, nchars_del - len);
1390 if (BUF_INTERVALS (current_buffer) != 0)
1392 offset_intervals (current_buffer, from, len - nchars_del);
1395 if (from < PT)
1396 adjust_point (len - nchars_del, len_byte - nbytes_del);
1398 /* As byte combining will decrease Z, we must check this again. */
1399 if (Z - GPT < END_UNCHANGED)
1400 END_UNCHANGED = Z - GPT;
1402 CHECK_MARKERS ();
1404 if (len == 0)
1405 evaporate_overlays (from);
1406 MODIFF++;
1409 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1410 This is for use when undo is not enabled in the current buffer. */
1412 void
1413 adjust_after_replace_noundo (from, from_byte, nchars_del, nbytes_del, len, len_byte)
1414 int from, from_byte, nchars_del, nbytes_del, len, len_byte;
1416 #ifdef BYTE_COMBINING_DEBUG
1417 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1418 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1419 abort ();
1420 #endif
1422 /* Update various buffer positions for the new text. */
1423 GAP_SIZE -= len_byte;
1424 ZV += len; Z+= len;
1425 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1426 GPT += len; GPT_BYTE += len_byte;
1427 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1429 if (nchars_del > 0)
1430 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1431 len, len_byte);
1432 else
1433 adjust_markers_for_insert (from, from_byte,
1434 from + len, from_byte + len_byte, 0);
1436 if (len > nchars_del)
1437 adjust_overlays_for_insert (from, len - nchars_del);
1438 else if (len < nchars_del)
1439 adjust_overlays_for_delete (from, nchars_del - len);
1440 if (BUF_INTERVALS (current_buffer) != 0)
1442 offset_intervals (current_buffer, from, len - nchars_del);
1445 if (from < PT)
1446 adjust_point (len - nchars_del, len_byte - nbytes_del);
1448 /* As byte combining will decrease Z, we must check this again. */
1449 if (Z - GPT < END_UNCHANGED)
1450 END_UNCHANGED = Z - GPT;
1452 CHECK_MARKERS ();
1454 if (len == 0)
1455 evaporate_overlays (from);
1456 MODIFF++;
1459 /* Record undo information, adjust markers and position keepers for an
1460 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1461 text already exists in the current buffer but character length (TO
1462 - FROM) may be incorrect, the correct length is NEWLEN. */
1464 void
1465 adjust_after_insert (from, from_byte, to, to_byte, newlen)
1466 int from, from_byte, to, to_byte, newlen;
1468 int len = to - from, len_byte = to_byte - from_byte;
1470 if (GPT != to)
1471 move_gap_both (to, to_byte);
1472 GAP_SIZE += len_byte;
1473 GPT -= len; GPT_BYTE -= len_byte;
1474 ZV -= len; ZV_BYTE -= len_byte;
1475 Z -= len; Z_BYTE -= len_byte;
1476 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1479 /* Replace the text from character positions FROM to TO with NEW,
1480 If PREPARE is nonzero, call prepare_to_modify_buffer.
1481 If INHERIT, the newly inserted text should inherit text properties
1482 from the surrounding non-deleted text. */
1484 /* Note that this does not yet handle markers quite right.
1485 Also it needs to record a single undo-entry that does a replacement
1486 rather than a separate delete and insert.
1487 That way, undo will also handle markers properly.
1489 But if MARKERS is 0, don't relocate markers. */
1491 void
1492 replace_range (from, to, new, prepare, inherit, markers)
1493 Lisp_Object new;
1494 int from, to, prepare, inherit, markers;
1496 int inschars = SCHARS (new);
1497 int insbytes = SBYTES (new);
1498 int from_byte, to_byte;
1499 int nbytes_del, nchars_del;
1500 register Lisp_Object temp;
1501 struct gcpro gcpro1;
1502 INTERVAL intervals;
1503 int outgoing_insbytes = insbytes;
1504 Lisp_Object deletion;
1506 CHECK_MARKERS ();
1508 GCPRO1 (new);
1509 deletion = Qnil;
1511 if (prepare)
1513 int range_length = to - from;
1514 prepare_to_modify_buffer (from, to, &from);
1515 to = from + range_length;
1518 UNGCPRO;
1520 /* Make args be valid */
1521 if (from < BEGV)
1522 from = BEGV;
1523 if (to > ZV)
1524 to = ZV;
1526 from_byte = CHAR_TO_BYTE (from);
1527 to_byte = CHAR_TO_BYTE (to);
1529 nchars_del = to - from;
1530 nbytes_del = to_byte - from_byte;
1532 if (nbytes_del <= 0 && insbytes == 0)
1533 return;
1535 /* Make OUTGOING_INSBYTES describe the text
1536 as it will be inserted in this buffer. */
1538 if (NILP (current_buffer->enable_multibyte_characters))
1539 outgoing_insbytes = inschars;
1540 else if (! STRING_MULTIBYTE (new))
1541 outgoing_insbytes
1542 = count_size_as_multibyte (SDATA (new), insbytes);
1544 /* Make sure point-max won't overflow after this insertion. */
1545 XSETINT (temp, Z_BYTE - nbytes_del + insbytes);
1546 if (Z_BYTE - nbytes_del + insbytes != XINT (temp))
1547 error ("Maximum buffer size exceeded");
1549 GCPRO1 (new);
1551 /* Make sure the gap is somewhere in or next to what we are deleting. */
1552 if (from > GPT)
1553 gap_right (from, from_byte);
1554 if (to < GPT)
1555 gap_left (to, to_byte, 0);
1557 /* Even if we don't record for undo, we must keep the original text
1558 because we may have to recover it because of inappropriate byte
1559 combining. */
1560 if (! EQ (current_buffer->undo_list, Qt))
1561 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1563 GAP_SIZE += nbytes_del;
1564 ZV -= nchars_del;
1565 Z -= nchars_del;
1566 ZV_BYTE -= nbytes_del;
1567 Z_BYTE -= nbytes_del;
1568 GPT = from;
1569 GPT_BYTE = from_byte;
1570 *(GPT_ADDR) = 0; /* Put an anchor. */
1572 if (GPT_BYTE < GPT)
1573 abort ();
1575 if (GPT - BEG < BEG_UNCHANGED)
1576 BEG_UNCHANGED = GPT - BEG;
1577 if (Z - GPT < END_UNCHANGED)
1578 END_UNCHANGED = Z - GPT;
1580 if (GAP_SIZE < insbytes)
1581 make_gap (insbytes - GAP_SIZE);
1583 /* Copy the string text into the buffer, perhaps converting
1584 between single-byte and multibyte. */
1585 copy_text (SDATA (new), GPT_ADDR, insbytes,
1586 STRING_MULTIBYTE (new),
1587 ! NILP (current_buffer->enable_multibyte_characters));
1589 #ifdef BYTE_COMBINING_DEBUG
1590 /* We have copied text into the gap, but we have not marked
1591 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1592 here, for both the previous text and the following text.
1593 Meanwhile, GPT_ADDR does point to
1594 the text that has been stored by copy_text. */
1595 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1596 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1597 abort ();
1598 #endif
1600 if (! EQ (current_buffer->undo_list, Qt))
1602 record_delete (from, deletion);
1603 record_insert (from, inschars);
1606 GAP_SIZE -= outgoing_insbytes;
1607 GPT += inschars;
1608 ZV += inschars;
1609 Z += inschars;
1610 GPT_BYTE += outgoing_insbytes;
1611 ZV_BYTE += outgoing_insbytes;
1612 Z_BYTE += outgoing_insbytes;
1613 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1615 if (GPT_BYTE < GPT)
1616 abort ();
1618 /* Adjust the overlay center as needed. This must be done after
1619 adjusting the markers that bound the overlays. */
1620 adjust_overlays_for_delete (from, nchars_del);
1621 adjust_overlays_for_insert (from, inschars);
1623 /* Adjust markers for the deletion and the insertion. */
1624 if (markers)
1625 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1626 inschars, outgoing_insbytes);
1628 offset_intervals (current_buffer, from, inschars - nchars_del);
1630 /* Get the intervals for the part of the string we are inserting--
1631 not including the combined-before bytes. */
1632 intervals = STRING_INTERVALS (new);
1633 /* Insert those intervals. */
1634 graft_intervals_into_buffer (intervals, from, inschars,
1635 current_buffer, inherit);
1637 /* Relocate point as if it were a marker. */
1638 if (from < PT)
1639 adjust_point ((from + inschars - (PT < to ? PT : to)),
1640 (from_byte + outgoing_insbytes
1641 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1643 if (outgoing_insbytes == 0)
1644 evaporate_overlays (from);
1646 CHECK_MARKERS ();
1648 MODIFF++;
1649 UNGCPRO;
1651 signal_after_change (from, nchars_del, GPT - from);
1652 update_compositions (from, GPT, CHECK_BORDER);
1655 /* Delete characters in current buffer
1656 from FROM up to (but not including) TO.
1657 If TO comes before FROM, we delete nothing. */
1659 void
1660 del_range (from, to)
1661 register int from, to;
1663 del_range_1 (from, to, 1, 0);
1666 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1667 RET_STRING says to return the deleted text. */
1669 Lisp_Object
1670 del_range_1 (from, to, prepare, ret_string)
1671 int from, to, prepare, ret_string;
1673 int from_byte, to_byte;
1674 Lisp_Object deletion;
1675 struct gcpro gcpro1;
1677 /* Make args be valid */
1678 if (from < BEGV)
1679 from = BEGV;
1680 if (to > ZV)
1681 to = ZV;
1683 if (to <= from)
1684 return Qnil;
1686 if (prepare)
1688 int range_length = to - from;
1689 prepare_to_modify_buffer (from, to, &from);
1690 to = min (ZV, from + range_length);
1693 from_byte = CHAR_TO_BYTE (from);
1694 to_byte = CHAR_TO_BYTE (to);
1696 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1697 GCPRO1(deletion);
1698 signal_after_change (from, to - from, 0);
1699 update_compositions (from, from, CHECK_HEAD);
1700 UNGCPRO;
1701 return deletion;
1704 /* Like del_range_1 but args are byte positions, not char positions. */
1706 void
1707 del_range_byte (from_byte, to_byte, prepare)
1708 int from_byte, to_byte, prepare;
1710 int from, to;
1712 /* Make args be valid */
1713 if (from_byte < BEGV_BYTE)
1714 from_byte = BEGV_BYTE;
1715 if (to_byte > ZV_BYTE)
1716 to_byte = ZV_BYTE;
1718 if (to_byte <= from_byte)
1719 return;
1721 from = BYTE_TO_CHAR (from_byte);
1722 to = BYTE_TO_CHAR (to_byte);
1724 if (prepare)
1726 int old_from = from, old_to = Z - to;
1727 int range_length = to - from;
1728 prepare_to_modify_buffer (from, to, &from);
1729 to = from + range_length;
1731 if (old_from != from)
1732 from_byte = CHAR_TO_BYTE (from);
1733 if (to > ZV)
1735 to = ZV;
1736 to_byte = ZV_BYTE;
1738 else if (old_to == Z - to)
1739 to_byte = CHAR_TO_BYTE (to);
1742 del_range_2 (from, from_byte, to, to_byte, 0);
1743 signal_after_change (from, to - from, 0);
1744 update_compositions (from, from, CHECK_HEAD);
1747 /* Like del_range_1, but positions are specified both as charpos
1748 and bytepos. */
1750 void
1751 del_range_both (from, from_byte, to, to_byte, prepare)
1752 int from, from_byte, to, to_byte, prepare;
1754 /* Make args be valid */
1755 if (from_byte < BEGV_BYTE)
1756 from_byte = BEGV_BYTE;
1757 if (to_byte > ZV_BYTE)
1758 to_byte = ZV_BYTE;
1760 if (to_byte <= from_byte)
1761 return;
1763 if (from < BEGV)
1764 from = BEGV;
1765 if (to > ZV)
1766 to = ZV;
1768 if (prepare)
1770 int old_from = from, old_to = Z - to;
1771 int range_length = to - from;
1772 prepare_to_modify_buffer (from, to, &from);
1773 to = from + range_length;
1775 if (old_from != from)
1776 from_byte = CHAR_TO_BYTE (from);
1777 if (to > ZV)
1779 to = ZV;
1780 to_byte = ZV_BYTE;
1782 else if (old_to == Z - to)
1783 to_byte = CHAR_TO_BYTE (to);
1786 del_range_2 (from, from_byte, to, to_byte, 0);
1787 signal_after_change (from, to - from, 0);
1788 update_compositions (from, from, CHECK_HEAD);
1791 /* Delete a range of text, specified both as character positions
1792 and byte positions. FROM and TO are character positions,
1793 while FROM_BYTE and TO_BYTE are byte positions.
1794 If RET_STRING is true, the deleted area is returned as a string. */
1796 Lisp_Object
1797 del_range_2 (from, from_byte, to, to_byte, ret_string)
1798 int from, from_byte, to, to_byte, ret_string;
1800 register int nbytes_del, nchars_del;
1801 Lisp_Object deletion;
1803 CHECK_MARKERS ();
1805 nchars_del = to - from;
1806 nbytes_del = to_byte - from_byte;
1808 /* Make sure the gap is somewhere in or next to what we are deleting. */
1809 if (from > GPT)
1810 gap_right (from, from_byte);
1811 if (to < GPT)
1812 gap_left (to, to_byte, 0);
1814 #ifdef BYTE_COMBINING_DEBUG
1815 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1816 Z_BYTE - to_byte, from, from_byte))
1817 abort ();
1818 #endif
1820 if (ret_string || ! EQ (current_buffer->undo_list, Qt))
1821 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1822 else
1823 deletion = Qnil;
1825 /* Relocate all markers pointing into the new, larger gap
1826 to point at the end of the text before the gap.
1827 Do this before recording the deletion,
1828 so that undo handles this after reinserting the text. */
1829 adjust_markers_for_delete (from, from_byte, to, to_byte);
1831 if (! EQ (current_buffer->undo_list, Qt))
1832 record_delete (from, deletion);
1833 MODIFF++;
1835 /* Relocate point as if it were a marker. */
1836 if (from < PT)
1837 adjust_point (from - (PT < to ? PT : to),
1838 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1840 offset_intervals (current_buffer, from, - nchars_del);
1842 /* Adjust the overlay center as needed. This must be done after
1843 adjusting the markers that bound the overlays. */
1844 adjust_overlays_for_delete (from, nchars_del);
1846 GAP_SIZE += nbytes_del;
1847 ZV_BYTE -= nbytes_del;
1848 Z_BYTE -= nbytes_del;
1849 ZV -= nchars_del;
1850 Z -= nchars_del;
1851 GPT = from;
1852 GPT_BYTE = from_byte;
1853 *(GPT_ADDR) = 0; /* Put an anchor. */
1855 if (GPT_BYTE < GPT)
1856 abort ();
1858 if (GPT - BEG < BEG_UNCHANGED)
1859 BEG_UNCHANGED = GPT - BEG;
1860 if (Z - GPT < END_UNCHANGED)
1861 END_UNCHANGED = Z - GPT;
1863 CHECK_MARKERS ();
1865 evaporate_overlays (from);
1867 return deletion;
1870 /* Call this if you're about to change the region of BUFFER from
1871 character positions START to END. This checks the read-only
1872 properties of the region, calls the necessary modification hooks,
1873 and warns the next redisplay that it should pay attention to that
1874 area. */
1876 void
1877 modify_region (buffer, start, end)
1878 struct buffer *buffer;
1879 int start, end;
1881 struct buffer *old_buffer = current_buffer;
1883 if (buffer != old_buffer)
1884 set_buffer_internal (buffer);
1886 prepare_to_modify_buffer (start, end, NULL);
1888 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
1890 if (MODIFF <= SAVE_MODIFF)
1891 record_first_change ();
1892 MODIFF++;
1894 buffer->point_before_scroll = Qnil;
1896 if (buffer != old_buffer)
1897 set_buffer_internal (old_buffer);
1900 /* Check that it is okay to modify the buffer between START and END,
1901 which are char positions.
1903 Run the before-change-function, if any. If intervals are in use,
1904 verify that the text to be modified is not read-only, and call
1905 any modification properties the text may have.
1907 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1908 by holding its value temporarily in a marker. */
1910 void
1911 prepare_to_modify_buffer (start, end, preserve_ptr)
1912 int start, end;
1913 int *preserve_ptr;
1915 if (!NILP (current_buffer->read_only))
1916 Fbarf_if_buffer_read_only ();
1918 /* Let redisplay consider other windows than selected_window
1919 if modifying another buffer. */
1920 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1921 ++windows_or_buffers_changed;
1923 if (BUF_INTERVALS (current_buffer) != 0)
1925 if (preserve_ptr)
1927 Lisp_Object preserve_marker;
1928 struct gcpro gcpro1;
1929 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1930 GCPRO1 (preserve_marker);
1931 verify_interval_modification (current_buffer, start, end);
1932 *preserve_ptr = marker_position (preserve_marker);
1933 unchain_marker (preserve_marker);
1934 UNGCPRO;
1936 else
1937 verify_interval_modification (current_buffer, start, end);
1940 #ifdef CLASH_DETECTION
1941 if (!NILP (current_buffer->file_truename)
1942 /* Make binding buffer-file-name to nil effective. */
1943 && !NILP (current_buffer->filename)
1944 && SAVE_MODIFF >= MODIFF)
1945 lock_file (current_buffer->file_truename);
1946 #else
1947 /* At least warn if this file has changed on disk since it was visited. */
1948 if (!NILP (current_buffer->filename)
1949 && SAVE_MODIFF >= MODIFF
1950 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1951 && !NILP (Ffile_exists_p (current_buffer->filename)))
1952 call1 (intern ("ask-user-about-supersession-threat"),
1953 current_buffer->filename);
1954 #endif /* not CLASH_DETECTION */
1956 signal_before_change (start, end, preserve_ptr);
1958 if (current_buffer->newline_cache)
1959 invalidate_region_cache (current_buffer,
1960 current_buffer->newline_cache,
1961 start - BEG, Z - end);
1962 if (current_buffer->width_run_cache)
1963 invalidate_region_cache (current_buffer,
1964 current_buffer->width_run_cache,
1965 start - BEG, Z - end);
1967 Vdeactivate_mark = Qt;
1970 /* These macros work with an argument named `preserve_ptr'
1971 and a local variable named `preserve_marker'. */
1973 #define PRESERVE_VALUE \
1974 if (preserve_ptr && NILP (preserve_marker)) \
1975 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1977 #define RESTORE_VALUE \
1978 if (! NILP (preserve_marker)) \
1980 *preserve_ptr = marker_position (preserve_marker); \
1981 unchain_marker (preserve_marker); \
1984 #define PRESERVE_START_END \
1985 if (NILP (start_marker)) \
1986 start_marker = Fcopy_marker (start, Qnil); \
1987 if (NILP (end_marker)) \
1988 end_marker = Fcopy_marker (end, Qnil);
1990 #define FETCH_START \
1991 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1993 #define FETCH_END \
1994 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1996 /* Signal a change to the buffer immediately before it happens.
1997 START_INT and END_INT are the bounds of the text to be changed.
1999 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2000 by holding its value temporarily in a marker. */
2002 void
2003 signal_before_change (start_int, end_int, preserve_ptr)
2004 int start_int, end_int;
2005 int *preserve_ptr;
2007 Lisp_Object start, end;
2008 Lisp_Object start_marker, end_marker;
2009 Lisp_Object preserve_marker;
2010 struct gcpro gcpro1, gcpro2, gcpro3;
2012 if (inhibit_modification_hooks)
2013 return;
2015 start = make_number (start_int);
2016 end = make_number (end_int);
2017 preserve_marker = Qnil;
2018 start_marker = Qnil;
2019 end_marker = Qnil;
2020 GCPRO3 (preserve_marker, start_marker, end_marker);
2022 /* If buffer is unmodified, run a special hook for that case. */
2023 if (SAVE_MODIFF >= MODIFF
2024 && !NILP (Vfirst_change_hook)
2025 && !NILP (Vrun_hooks))
2027 PRESERVE_VALUE;
2028 PRESERVE_START_END;
2029 call1 (Vrun_hooks, Qfirst_change_hook);
2032 /* Now run the before-change-functions if any. */
2033 if (!NILP (Vbefore_change_functions))
2035 Lisp_Object args[3];
2036 Lisp_Object before_change_functions;
2037 Lisp_Object after_change_functions;
2038 struct gcpro gcpro1, gcpro2;
2039 struct buffer *old = current_buffer;
2040 struct buffer *new;
2042 PRESERVE_VALUE;
2043 PRESERVE_START_END;
2045 /* "Bind" before-change-functions and after-change-functions
2046 to nil--but in a way that errors don't know about.
2047 That way, if there's an error in them, they will stay nil. */
2048 before_change_functions = Vbefore_change_functions;
2049 after_change_functions = Vafter_change_functions;
2050 Vbefore_change_functions = Qnil;
2051 Vafter_change_functions = Qnil;
2052 GCPRO2 (before_change_functions, after_change_functions);
2054 /* Actually run the hook functions. */
2055 args[0] = Qbefore_change_functions;
2056 args[1] = FETCH_START;
2057 args[2] = FETCH_END;
2058 run_hook_list_with_args (before_change_functions, 3, args);
2060 /* "Unbind" the variables we "bound" to nil. Beware a
2061 buffer-local hook which changes the buffer when run (e.g. W3). */
2062 if (old != current_buffer)
2064 new = current_buffer;
2065 set_buffer_internal (old);
2066 Vbefore_change_functions = before_change_functions;
2067 Vafter_change_functions = after_change_functions;
2068 set_buffer_internal (new);
2070 else
2072 Vbefore_change_functions = before_change_functions;
2073 Vafter_change_functions = after_change_functions;
2075 UNGCPRO;
2078 if (!NILP (current_buffer->overlays_before)
2079 || !NILP (current_buffer->overlays_after))
2081 PRESERVE_VALUE;
2082 report_overlay_modification (FETCH_START, FETCH_END, 0,
2083 FETCH_START, FETCH_END, Qnil);
2086 if (! NILP (start_marker))
2087 free_marker (start_marker);
2088 if (! NILP (end_marker))
2089 free_marker (end_marker);
2090 RESTORE_VALUE;
2091 UNGCPRO;
2094 /* Signal a change immediately after it happens.
2095 CHARPOS is the character position of the start of the changed text.
2096 LENDEL is the number of characters of the text before the change.
2097 (Not the whole buffer; just the part that was changed.)
2098 LENINS is the number of characters in that part of the text
2099 after the change. */
2101 void
2102 signal_after_change (charpos, lendel, lenins)
2103 int charpos, lendel, lenins;
2105 if (inhibit_modification_hooks)
2106 return;
2108 /* If we are deferring calls to the after-change functions
2109 and there are no before-change functions,
2110 just record the args that we were going to use. */
2111 if (! NILP (Vcombine_after_change_calls)
2112 && NILP (Vbefore_change_functions)
2113 && NILP (current_buffer->overlays_before)
2114 && NILP (current_buffer->overlays_after))
2116 Lisp_Object elt;
2118 if (!NILP (combine_after_change_list)
2119 && current_buffer != XBUFFER (combine_after_change_buffer))
2120 Fcombine_after_change_execute ();
2122 elt = Fcons (make_number (charpos - BEG),
2123 Fcons (make_number (Z - (charpos - lendel + lenins)),
2124 Fcons (make_number (lenins - lendel), Qnil)));
2125 combine_after_change_list
2126 = Fcons (elt, combine_after_change_list);
2127 combine_after_change_buffer = Fcurrent_buffer ();
2129 return;
2132 if (!NILP (combine_after_change_list))
2133 Fcombine_after_change_execute ();
2135 if (!NILP (Vafter_change_functions))
2137 Lisp_Object args[4];
2138 Lisp_Object before_change_functions;
2139 Lisp_Object after_change_functions;
2140 struct buffer *old = current_buffer;
2141 struct buffer *new;
2142 struct gcpro gcpro1, gcpro2;
2144 /* "Bind" before-change-functions and after-change-functions
2145 to nil--but in a way that errors don't know about.
2146 That way, if there's an error in them, they will stay nil. */
2147 before_change_functions = Vbefore_change_functions;
2148 after_change_functions = Vafter_change_functions;
2149 Vbefore_change_functions = Qnil;
2150 Vafter_change_functions = Qnil;
2151 GCPRO2 (before_change_functions, after_change_functions);
2153 /* Actually run the hook functions. */
2154 args[0] = Qafter_change_functions;
2155 XSETFASTINT (args[1], charpos);
2156 XSETFASTINT (args[2], charpos + lenins);
2157 XSETFASTINT (args[3], lendel);
2158 run_hook_list_with_args (after_change_functions,
2159 4, args);
2161 /* "Unbind" the variables we "bound" to nil. Beware a
2162 buffer-local hook which changes the buffer when run (e.g. W3). */
2163 if (old != current_buffer)
2165 new = current_buffer;
2166 set_buffer_internal (old);
2167 Vbefore_change_functions = before_change_functions;
2168 Vafter_change_functions = after_change_functions;
2169 set_buffer_internal (new);
2171 else
2173 Vbefore_change_functions = before_change_functions;
2174 Vafter_change_functions = after_change_functions;
2176 UNGCPRO;
2179 if (!NILP (current_buffer->overlays_before)
2180 || !NILP (current_buffer->overlays_after))
2181 report_overlay_modification (make_number (charpos),
2182 make_number (charpos + lenins),
2184 make_number (charpos),
2185 make_number (charpos + lenins),
2186 make_number (lendel));
2188 /* After an insertion, call the text properties
2189 insert-behind-hooks or insert-in-front-hooks. */
2190 if (lendel == 0)
2191 report_interval_modification (make_number (charpos),
2192 make_number (charpos + lenins));
2195 Lisp_Object
2196 Fcombine_after_change_execute_1 (val)
2197 Lisp_Object val;
2199 Vcombine_after_change_calls = val;
2200 return val;
2203 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2204 Scombine_after_change_execute, 0, 0, 0,
2205 doc: /* This function is for use internally in `combine-after-change-calls'. */)
2208 int count = SPECPDL_INDEX ();
2209 int beg, end, change;
2210 int begpos, endpos;
2211 Lisp_Object tail;
2213 if (NILP (combine_after_change_list))
2214 return Qnil;
2216 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2218 Fset_buffer (combine_after_change_buffer);
2220 /* # chars unchanged at beginning of buffer. */
2221 beg = Z - BEG;
2222 /* # chars unchanged at end of buffer. */
2223 end = beg;
2224 /* Total amount of insertion (negative for deletion). */
2225 change = 0;
2227 /* Scan the various individual changes,
2228 accumulating the range info in BEG, END and CHANGE. */
2229 for (tail = combine_after_change_list; CONSP (tail);
2230 tail = XCDR (tail))
2232 Lisp_Object elt;
2233 int thisbeg, thisend, thischange;
2235 /* Extract the info from the next element. */
2236 elt = XCAR (tail);
2237 if (! CONSP (elt))
2238 continue;
2239 thisbeg = XINT (XCAR (elt));
2241 elt = XCDR (elt);
2242 if (! CONSP (elt))
2243 continue;
2244 thisend = XINT (XCAR (elt));
2246 elt = XCDR (elt);
2247 if (! CONSP (elt))
2248 continue;
2249 thischange = XINT (XCAR (elt));
2251 /* Merge this range into the accumulated range. */
2252 change += thischange;
2253 if (thisbeg < beg)
2254 beg = thisbeg;
2255 if (thisend < end)
2256 end = thisend;
2259 /* Get the current start and end positions of the range
2260 that was changed. */
2261 begpos = BEG + beg;
2262 endpos = Z - end;
2264 /* We are about to handle these, so discard them. */
2265 combine_after_change_list = Qnil;
2267 /* Now run the after-change functions for real.
2268 Turn off the flag that defers them. */
2269 record_unwind_protect (Fcombine_after_change_execute_1,
2270 Vcombine_after_change_calls);
2271 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2272 update_compositions (begpos, endpos, CHECK_ALL);
2274 return unbind_to (count, Qnil);
2277 void
2278 syms_of_insdel ()
2280 staticpro (&combine_after_change_list);
2281 combine_after_change_list = Qnil;
2282 combine_after_change_buffer = Qnil;
2284 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag,
2285 doc: /* Non-nil means enable debugging checks for invalid marker positions. */);
2286 check_markers_debug_flag = 0;
2287 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls,
2288 doc: /* Used internally by the `combine-after-change-calls' macro. */);
2289 Vcombine_after_change_calls = Qnil;
2291 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks,
2292 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2293 This affects `before-change-functions' and `after-change-functions',
2294 as well as hooks attached to text properties and overlays. */);
2295 inhibit_modification_hooks = 0;
2296 Qinhibit_modification_hooks = intern ("inhibit-modification-hooks");
2297 staticpro (&Qinhibit_modification_hooks);
2299 defsubr (&Scombine_after_change_execute);