Remove still more cal-tex code duplication
[emacs.git] / src / insdel.c
blob1dceb67bffac868b7f00547a7eb797a21d1fbcf6
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2012
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 3 of the License, or
10 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <setjmp.h>
24 #include <intprops.h>
26 #include "lisp.h"
27 #include "intervals.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "window.h"
31 #include "blockinput.h"
32 #include "region-cache.h"
34 static void insert_from_string_1 (Lisp_Object string,
35 ptrdiff_t pos, ptrdiff_t pos_byte,
36 ptrdiff_t nchars, ptrdiff_t nbytes,
37 int inherit, int before_markers);
38 static void insert_from_buffer_1 (struct buffer *buf,
39 ptrdiff_t from, ptrdiff_t nchars,
40 int inherit);
41 static void gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, int newgap);
42 static void gap_right (ptrdiff_t charpos, ptrdiff_t bytepos);
44 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
45 describing changes which happened while combine_after_change_calls
46 was nonzero. We use this to decide how to call them
47 once the deferral ends.
49 In each element.
50 BEG-UNCHANGED is the number of chars before the changed range.
51 END-UNCHANGED is the number of chars after the changed range,
52 and CHANGE-AMOUNT is the number of characters inserted by the change
53 (negative for a deletion). */
54 static Lisp_Object combine_after_change_list;
56 /* Buffer which combine_after_change_list is about. */
57 static Lisp_Object combine_after_change_buffer;
59 Lisp_Object Qinhibit_modification_hooks;
61 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
63 /* Also used in marker.c to enable expensive marker checks. */
65 #ifdef MARKER_DEBUG
67 static void
68 check_markers (void)
70 register struct Lisp_Marker *tail;
71 int multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
73 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
75 if (tail->buffer->text != current_buffer->text)
76 abort ();
77 if (tail->charpos > Z)
78 abort ();
79 if (tail->bytepos > Z_BYTE)
80 abort ();
81 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
82 abort ();
86 #else /* not MARKER_DEBUG */
88 #define check_markers() do { } while (0)
90 #endif /* MARKER_DEBUG */
92 /* Move gap to position CHARPOS.
93 Note that this can quit! */
95 void
96 move_gap (ptrdiff_t charpos)
98 move_gap_both (charpos, charpos_to_bytepos (charpos));
101 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
102 Note that this can quit! */
104 void
105 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
107 if (bytepos < GPT_BYTE)
108 gap_left (charpos, bytepos, 0);
109 else if (bytepos > GPT_BYTE)
110 gap_right (charpos, bytepos);
113 /* Move the gap to a position less than the current GPT.
114 BYTEPOS describes the new position as a byte position,
115 and CHARPOS is the corresponding char position.
116 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
118 static void
119 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, int newgap)
121 register unsigned char *to, *from;
122 register ptrdiff_t i;
123 ptrdiff_t new_s1;
125 if (!newgap)
126 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
128 i = GPT_BYTE;
129 to = GAP_END_ADDR;
130 from = GPT_ADDR;
131 new_s1 = GPT_BYTE;
133 /* Now copy the characters. To move the gap down,
134 copy characters up. */
136 while (1)
138 /* I gets number of characters left to copy. */
139 i = new_s1 - bytepos;
140 if (i == 0)
141 break;
142 /* If a quit is requested, stop copying now.
143 Change BYTEPOS to be where we have actually moved the gap to. */
144 if (QUITP)
146 bytepos = new_s1;
147 charpos = BYTE_TO_CHAR (bytepos);
148 break;
150 /* Move at most 32000 chars before checking again for a quit. */
151 if (i > 32000)
152 i = 32000;
153 new_s1 -= i;
154 from -= i, to -= i;
155 memmove (to, from, i);
158 /* Adjust buffer data structure, to put the gap at BYTEPOS.
159 BYTEPOS is where the loop above stopped, which may be what
160 was specified or may be where a quit was detected. */
161 GPT_BYTE = bytepos;
162 GPT = charpos;
163 eassert (charpos <= bytepos);
164 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
165 QUIT;
168 /* Move the gap to a position greater than the current GPT.
169 BYTEPOS describes the new position as a byte position,
170 and CHARPOS is the corresponding char position. */
172 static void
173 gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
175 register unsigned char *to, *from;
176 register ptrdiff_t i;
177 ptrdiff_t new_s1;
179 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
181 i = GPT_BYTE;
182 from = GAP_END_ADDR;
183 to = GPT_ADDR;
184 new_s1 = GPT_BYTE;
186 /* Now copy the characters. To move the gap up,
187 copy characters down. */
189 while (1)
191 /* I gets number of characters left to copy. */
192 i = bytepos - new_s1;
193 if (i == 0)
194 break;
195 /* If a quit is requested, stop copying now.
196 Change BYTEPOS to be where we have actually moved the gap to. */
197 if (QUITP)
199 bytepos = new_s1;
200 charpos = BYTE_TO_CHAR (bytepos);
201 break;
203 /* Move at most 32000 chars before checking again for a quit. */
204 if (i > 32000)
205 i = 32000;
206 new_s1 += i;
207 memmove (to, from, i);
208 from += i, to += i;
211 GPT = charpos;
212 GPT_BYTE = bytepos;
213 eassert (charpos <= bytepos);
214 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
215 QUIT;
218 /* Adjust all markers for a deletion
219 whose range in bytes is FROM_BYTE to TO_BYTE.
220 The range in charpos is FROM to TO.
222 This function assumes that the gap is adjacent to
223 or inside of the range being deleted. */
225 void
226 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
227 ptrdiff_t to, ptrdiff_t to_byte)
229 Lisp_Object marker;
230 register struct Lisp_Marker *m;
231 register ptrdiff_t charpos;
233 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
235 charpos = m->charpos;
236 eassert (charpos <= Z);
238 /* If the marker is after the deletion,
239 relocate by number of chars / bytes deleted. */
240 if (charpos > to)
242 m->charpos -= to - from;
243 m->bytepos -= to_byte - from_byte;
245 /* Here's the case where a marker is inside text being deleted. */
246 else if (charpos > from)
248 if (! m->insertion_type)
249 { /* Normal markers will end up at the beginning of the
250 re-inserted text after undoing a deletion, and must be
251 adjusted to move them to the correct place. */
252 XSETMISC (marker, m);
253 record_marker_adjustment (marker, from - charpos);
255 else if (charpos < to)
256 { /* Before-insertion markers will automatically move forward
257 upon re-inserting the deleted text, so we have to arrange
258 for them to move backward to the correct position. */
259 XSETMISC (marker, m);
260 record_marker_adjustment (marker, to - charpos);
262 m->charpos = from;
263 m->bytepos = from_byte;
265 /* Here's the case where a before-insertion marker is immediately
266 before the deleted region. */
267 else if (charpos == from && m->insertion_type)
269 /* Undoing the change uses normal insertion, which will
270 incorrectly make MARKER move forward, so we arrange for it
271 to then move backward to the correct place at the beginning
272 of the deleted region. */
273 XSETMISC (marker, m);
274 record_marker_adjustment (marker, to - from);
280 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
281 to TO / TO_BYTE. We have to relocate the charpos of every marker
282 that points after the insertion (but not their bytepos).
284 When a marker points at the insertion point,
285 we advance it if either its insertion-type is t
286 or BEFORE_MARKERS is true. */
288 static void
289 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
290 ptrdiff_t to, ptrdiff_t to_byte, int before_markers)
292 struct Lisp_Marker *m;
293 int adjusted = 0;
294 ptrdiff_t nchars = to - from;
295 ptrdiff_t nbytes = to_byte - from_byte;
297 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
299 eassert (m->bytepos >= m->charpos
300 && m->bytepos - m->charpos <= Z_BYTE - Z);
302 if (m->bytepos == from_byte)
304 if (m->insertion_type || before_markers)
306 m->bytepos = to_byte;
307 m->charpos = to;
308 if (m->insertion_type)
309 adjusted = 1;
312 else if (m->bytepos > from_byte)
314 m->bytepos += nbytes;
315 m->charpos += nchars;
319 /* Adjusting only markers whose insertion-type is t may result in
320 - disordered start and end in overlays, and
321 - disordered overlays in the slot `overlays_before' of current_buffer. */
322 if (adjusted)
324 fix_start_end_in_overlays (from, to);
325 fix_overlays_before (current_buffer, from, to);
329 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
331 This is used only when the value of point changes due to an insert
332 or delete; it does not represent a conceptual change in point as a
333 marker. In particular, point is not crossing any interval
334 boundaries, so there's no need to use the usual SET_PT macro. In
335 fact it would be incorrect to do so, because either the old or the
336 new value of point is out of sync with the current set of
337 intervals. */
339 static void
340 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
342 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
343 /* In a single-byte buffer, the two positions must be equal. */
344 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
347 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
348 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
349 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
350 an insertion. */
352 static void
353 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
354 ptrdiff_t old_chars, ptrdiff_t old_bytes,
355 ptrdiff_t new_chars, ptrdiff_t new_bytes)
357 register struct Lisp_Marker *m;
358 ptrdiff_t prev_to_byte = from_byte + old_bytes;
359 ptrdiff_t diff_chars = new_chars - old_chars;
360 ptrdiff_t diff_bytes = new_bytes - old_bytes;
362 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
364 if (m->bytepos >= prev_to_byte)
366 m->charpos += diff_chars;
367 m->bytepos += diff_bytes;
369 else if (m->bytepos > from_byte)
371 m->charpos = from;
372 m->bytepos = from_byte;
376 check_markers ();
380 void
381 buffer_overflow (void)
383 error ("Maximum buffer size exceeded");
386 /* Make the gap NBYTES_ADDED bytes longer. */
388 static void
389 make_gap_larger (ptrdiff_t nbytes_added)
391 Lisp_Object tem;
392 ptrdiff_t real_gap_loc;
393 ptrdiff_t real_gap_loc_byte;
394 ptrdiff_t old_gap_size;
395 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
396 enum { enough_for_a_while = 2000 };
398 if (BUF_BYTES_MAX - current_size < nbytes_added)
399 buffer_overflow ();
401 /* If we have to get more space, get enough to last a while;
402 but do not exceed the maximum buffer size. */
403 nbytes_added = min (nbytes_added + enough_for_a_while,
404 BUF_BYTES_MAX - current_size);
406 enlarge_buffer_text (current_buffer, nbytes_added);
408 /* Prevent quitting in move_gap. */
409 tem = Vinhibit_quit;
410 Vinhibit_quit = Qt;
412 real_gap_loc = GPT;
413 real_gap_loc_byte = GPT_BYTE;
414 old_gap_size = GAP_SIZE;
416 /* Call the newly allocated space a gap at the end of the whole space. */
417 GPT = Z + GAP_SIZE;
418 GPT_BYTE = Z_BYTE + GAP_SIZE;
419 GAP_SIZE = nbytes_added;
421 /* Move the new gap down to be consecutive with the end of the old one.
422 This adjusts the markers properly too. */
423 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
425 /* Now combine the two into one large gap. */
426 GAP_SIZE += old_gap_size;
427 GPT = real_gap_loc;
428 GPT_BYTE = real_gap_loc_byte;
430 /* Put an anchor. */
431 *(Z_ADDR) = 0;
433 Vinhibit_quit = tem;
436 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
438 /* Make the gap NBYTES_REMOVED bytes shorter. */
440 static void
441 make_gap_smaller (ptrdiff_t nbytes_removed)
443 Lisp_Object tem;
444 ptrdiff_t real_gap_loc;
445 ptrdiff_t real_gap_loc_byte;
446 ptrdiff_t real_Z;
447 ptrdiff_t real_Z_byte;
448 ptrdiff_t real_beg_unchanged;
449 ptrdiff_t new_gap_size;
451 /* Make sure the gap is at least 20 bytes. */
452 if (GAP_SIZE - nbytes_removed < 20)
453 nbytes_removed = GAP_SIZE - 20;
455 /* Prevent quitting in move_gap. */
456 tem = Vinhibit_quit;
457 Vinhibit_quit = Qt;
459 real_gap_loc = GPT;
460 real_gap_loc_byte = GPT_BYTE;
461 new_gap_size = GAP_SIZE - nbytes_removed;
462 real_Z = Z;
463 real_Z_byte = Z_BYTE;
464 real_beg_unchanged = BEG_UNCHANGED;
466 /* Pretend that the last unwanted part of the gap is the entire gap,
467 and that the first desired part of the gap is part of the buffer
468 text. */
469 memset (GPT_ADDR, 0, new_gap_size);
470 GPT += new_gap_size;
471 GPT_BYTE += new_gap_size;
472 Z += new_gap_size;
473 Z_BYTE += new_gap_size;
474 GAP_SIZE = nbytes_removed;
476 /* Move the unwanted pretend gap to the end of the buffer. This
477 adjusts the markers properly too. */
478 gap_right (Z, Z_BYTE);
480 enlarge_buffer_text (current_buffer, -nbytes_removed);
482 /* Now restore the desired gap. */
483 GAP_SIZE = new_gap_size;
484 GPT = real_gap_loc;
485 GPT_BYTE = real_gap_loc_byte;
486 Z = real_Z;
487 Z_BYTE = real_Z_byte;
488 BEG_UNCHANGED = real_beg_unchanged;
490 /* Put an anchor. */
491 *(Z_ADDR) = 0;
493 Vinhibit_quit = tem;
496 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
498 void
499 make_gap (ptrdiff_t nbytes_added)
501 if (nbytes_added >= 0)
502 make_gap_larger (nbytes_added);
503 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
504 else
505 make_gap_smaller (-nbytes_added);
506 #endif
509 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
510 FROM_MULTIBYTE says whether the incoming text is multibyte.
511 TO_MULTIBYTE says whether to store the text as multibyte.
512 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
514 Return the number of bytes stored at TO_ADDR. */
516 ptrdiff_t
517 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
518 ptrdiff_t nbytes, int from_multibyte, int to_multibyte)
520 if (from_multibyte == to_multibyte)
522 memcpy (to_addr, from_addr, nbytes);
523 return nbytes;
525 else if (from_multibyte)
527 ptrdiff_t nchars = 0;
528 ptrdiff_t bytes_left = nbytes;
530 while (bytes_left > 0)
532 int thislen, c;
533 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
534 if (! ASCII_CHAR_P (c))
535 c &= 0xFF;
536 *to_addr++ = c;
537 from_addr += thislen;
538 bytes_left -= thislen;
539 nchars++;
541 return nchars;
543 else
545 unsigned char *initial_to_addr = to_addr;
547 /* Convert single-byte to multibyte. */
548 while (nbytes > 0)
550 int c = *from_addr++;
552 if (!ASCII_CHAR_P (c))
554 c = BYTE8_TO_CHAR (c);
555 to_addr += CHAR_STRING (c, to_addr);
556 nbytes--;
558 else
559 /* Special case for speed. */
560 *to_addr++ = c, nbytes--;
562 return to_addr - initial_to_addr;
566 /* Insert a string of specified length before point.
567 This function judges multibyteness based on
568 enable_multibyte_characters in the current buffer;
569 it never converts between single-byte and multibyte.
571 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
572 prepare_to_modify_buffer could relocate the text. */
574 void
575 insert (const char *string, ptrdiff_t nbytes)
577 if (nbytes > 0)
579 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
580 insert_1_both (string, len, nbytes, 0, 1, 0);
581 opoint = PT - len;
582 signal_after_change (opoint, 0, len);
583 update_compositions (opoint, PT, CHECK_BORDER);
587 /* Likewise, but inherit text properties from neighboring characters. */
589 void
590 insert_and_inherit (const char *string, ptrdiff_t nbytes)
592 if (nbytes > 0)
594 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
595 insert_1_both (string, len, nbytes, 1, 1, 0);
596 opoint = PT - len;
597 signal_after_change (opoint, 0, len);
598 update_compositions (opoint, PT, CHECK_BORDER);
602 /* Insert the character C before point. Do not inherit text properties. */
604 void
605 insert_char (int c)
607 unsigned char str[MAX_MULTIBYTE_LENGTH];
608 int len;
610 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
611 len = CHAR_STRING (c, str);
612 else
614 len = 1;
615 str[0] = c;
618 insert ((char *) str, len);
621 /* Insert the null-terminated string S before point. */
623 void
624 insert_string (const char *s)
626 insert (s, strlen (s));
629 /* Like `insert' except that all markers pointing at the place where
630 the insertion happens are adjusted to point after it.
631 Don't use this function to insert part of a Lisp string,
632 since gc could happen and relocate it. */
634 void
635 insert_before_markers (const char *string, ptrdiff_t nbytes)
637 if (nbytes > 0)
639 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
640 insert_1_both (string, len, nbytes, 0, 1, 1);
641 opoint = PT - len;
642 signal_after_change (opoint, 0, len);
643 update_compositions (opoint, PT, CHECK_BORDER);
647 /* Likewise, but inherit text properties from neighboring characters. */
649 void
650 insert_before_markers_and_inherit (const char *string,
651 ptrdiff_t nbytes)
653 if (nbytes > 0)
655 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
656 insert_1_both (string, len, nbytes, 1, 1, 1);
657 opoint = PT - len;
658 signal_after_change (opoint, 0, len);
659 update_compositions (opoint, PT, CHECK_BORDER);
663 /* Subroutine used by the insert functions above. */
665 void
666 insert_1 (const char *string, ptrdiff_t nbytes,
667 int inherit, int prepare, int before_markers)
669 insert_1_both (string, chars_in_text ((unsigned char *) string, nbytes),
670 nbytes, inherit, prepare, before_markers);
674 #ifdef BYTE_COMBINING_DEBUG
676 /* See if the bytes before POS/POS_BYTE combine with bytes
677 at the start of STRING to form a single character.
678 If so, return the number of bytes at the start of STRING
679 which combine in this way. Otherwise, return 0. */
682 count_combining_before (const unsigned char *string, ptrdiff_t length,
683 ptrdiff_t pos, ptrdiff_t pos_byte)
685 int len, combining_bytes;
686 const unsigned char *p;
688 if (NILP (current_buffer->enable_multibyte_characters))
689 return 0;
691 /* At first, we can exclude the following cases:
692 (1) STRING[0] can't be a following byte of multibyte sequence.
693 (2) POS is the start of the current buffer.
694 (3) A character before POS is not a multibyte character. */
695 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
696 return 0;
697 if (pos_byte == BEG_BYTE) /* case (2) */
698 return 0;
699 len = 1;
700 p = BYTE_POS_ADDR (pos_byte - 1);
701 while (! CHAR_HEAD_P (*p)) p--, len++;
702 if (! LEADING_CODE_P (*p)) /* case (3) */
703 return 0;
705 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
706 if (combining_bytes <= 0)
707 /* The character preceding POS is, complete and no room for
708 combining bytes (combining_bytes == 0), or an independent 8-bit
709 character (combining_bytes < 0). */
710 return 0;
712 /* We have a combination situation. Count the bytes at STRING that
713 may combine. */
714 p = string + 1;
715 while (!CHAR_HEAD_P (*p) && p < string + length)
716 p++;
718 return (combining_bytes < p - string ? combining_bytes : p - string);
721 /* See if the bytes after POS/POS_BYTE combine with bytes
722 at the end of STRING to form a single character.
723 If so, return the number of bytes after POS/POS_BYTE
724 which combine in this way. Otherwise, return 0. */
727 count_combining_after (const unsigned char *string,
728 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
730 ptrdiff_t opos_byte = pos_byte;
731 ptrdiff_t i;
732 ptrdiff_t bytes;
733 unsigned char *bufp;
735 if (NILP (current_buffer->enable_multibyte_characters))
736 return 0;
738 /* At first, we can exclude the following cases:
739 (1) The last byte of STRING is an ASCII.
740 (2) POS is the last of the current buffer.
741 (3) A character at POS can't be a following byte of multibyte
742 character. */
743 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
744 return 0;
745 if (pos_byte == Z_BYTE) /* case (2) */
746 return 0;
747 bufp = BYTE_POS_ADDR (pos_byte);
748 if (CHAR_HEAD_P (*bufp)) /* case (3) */
749 return 0;
751 i = length - 1;
752 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
754 i--;
756 if (i < 0)
758 /* All characters in STRING are not character head. We must
759 check also preceding bytes at POS. We are sure that the gap
760 is at POS. */
761 unsigned char *p = BEG_ADDR;
762 i = pos_byte - 2;
763 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
764 i--;
765 if (i < 0 || !LEADING_CODE_P (p[i]))
766 return 0;
768 bytes = BYTES_BY_CHAR_HEAD (p[i]);
769 return (bytes <= pos_byte - 1 - i + length
771 : bytes - (pos_byte - 1 - i + length));
773 if (!LEADING_CODE_P (string[i]))
774 return 0;
776 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
777 bufp++, pos_byte++;
778 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
780 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
783 #endif
786 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
787 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
788 are the same as in insert_1. */
790 void
791 insert_1_both (const char *string,
792 ptrdiff_t nchars, ptrdiff_t nbytes,
793 int inherit, int prepare, int before_markers)
795 if (nchars == 0)
796 return;
798 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
799 nchars = nbytes;
801 if (prepare)
802 /* Do this before moving and increasing the gap,
803 because the before-change hooks might move the gap
804 or make it smaller. */
805 prepare_to_modify_buffer (PT, PT, NULL);
807 if (PT != GPT)
808 move_gap_both (PT, PT_BYTE);
809 if (GAP_SIZE < nbytes)
810 make_gap (nbytes - GAP_SIZE);
812 #ifdef BYTE_COMBINING_DEBUG
813 if (count_combining_before (string, nbytes, PT, PT_BYTE)
814 || count_combining_after (string, nbytes, PT, PT_BYTE))
815 abort ();
816 #endif
818 /* Record deletion of the surrounding text that combines with
819 the insertion. This, together with recording the insertion,
820 will add up to the right stuff in the undo list. */
821 record_insert (PT, nchars);
822 MODIFF++;
823 CHARS_MODIFF = MODIFF;
825 memcpy (GPT_ADDR, string, nbytes);
827 GAP_SIZE -= nbytes;
828 GPT += nchars;
829 ZV += nchars;
830 Z += nchars;
831 GPT_BYTE += nbytes;
832 ZV_BYTE += nbytes;
833 Z_BYTE += nbytes;
834 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
836 eassert (GPT <= GPT_BYTE);
838 /* The insert may have been in the unchanged region, so check again. */
839 if (Z - GPT < END_UNCHANGED)
840 END_UNCHANGED = Z - GPT;
842 adjust_overlays_for_insert (PT, nchars);
843 adjust_markers_for_insert (PT, PT_BYTE,
844 PT + nchars, PT_BYTE + nbytes,
845 before_markers);
847 if (buffer_intervals (current_buffer))
848 offset_intervals (current_buffer, PT, nchars);
850 if (!inherit && buffer_intervals (current_buffer))
851 set_text_properties (make_number (PT), make_number (PT + nchars),
852 Qnil, Qnil, Qnil);
854 adjust_point (nchars, nbytes);
856 check_markers ();
859 /* Insert the part of the text of STRING, a Lisp object assumed to be
860 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
861 starting at position POS / POS_BYTE. If the text of STRING has properties,
862 copy them into the buffer.
864 It does not work to use `insert' for this, because a GC could happen
865 before we copy the stuff into the buffer, and relocate the string
866 without insert noticing. */
868 void
869 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
870 ptrdiff_t length, ptrdiff_t length_byte, int inherit)
872 ptrdiff_t opoint = PT;
874 if (SCHARS (string) == 0)
875 return;
877 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
878 inherit, 0);
879 signal_after_change (opoint, 0, PT - opoint);
880 update_compositions (opoint, PT, CHECK_BORDER);
883 /* Like `insert_from_string' except that all markers pointing
884 at the place where the insertion happens are adjusted to point after it. */
886 void
887 insert_from_string_before_markers (Lisp_Object string,
888 ptrdiff_t pos, ptrdiff_t pos_byte,
889 ptrdiff_t length, ptrdiff_t length_byte,
890 int inherit)
892 ptrdiff_t opoint = PT;
894 if (SCHARS (string) == 0)
895 return;
897 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
898 inherit, 1);
899 signal_after_change (opoint, 0, PT - opoint);
900 update_compositions (opoint, PT, CHECK_BORDER);
903 /* Subroutine of the insertion functions above. */
905 static void
906 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
907 ptrdiff_t nchars, ptrdiff_t nbytes,
908 int inherit, int before_markers)
910 struct gcpro gcpro1;
911 ptrdiff_t outgoing_nbytes = nbytes;
912 INTERVAL intervals;
914 /* Make OUTGOING_NBYTES describe the text
915 as it will be inserted in this buffer. */
917 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
918 outgoing_nbytes = nchars;
919 else if (! STRING_MULTIBYTE (string))
920 outgoing_nbytes
921 = count_size_as_multibyte (SDATA (string) + pos_byte,
922 nbytes);
924 GCPRO1 (string);
925 /* Do this before moving and increasing the gap,
926 because the before-change hooks might move the gap
927 or make it smaller. */
928 prepare_to_modify_buffer (PT, PT, NULL);
930 if (PT != GPT)
931 move_gap_both (PT, PT_BYTE);
932 if (GAP_SIZE < outgoing_nbytes)
933 make_gap (outgoing_nbytes - GAP_SIZE);
934 UNGCPRO;
936 /* Copy the string text into the buffer, perhaps converting
937 between single-byte and multibyte. */
938 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
939 STRING_MULTIBYTE (string),
940 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
942 #ifdef BYTE_COMBINING_DEBUG
943 /* We have copied text into the gap, but we have not altered
944 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
945 to these functions and get the same results as we would
946 have got earlier on. Meanwhile, PT_ADDR does point to
947 the text that has been stored by copy_text. */
948 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
949 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
950 abort ();
951 #endif
953 record_insert (PT, nchars);
954 MODIFF++;
955 CHARS_MODIFF = MODIFF;
957 GAP_SIZE -= outgoing_nbytes;
958 GPT += nchars;
959 ZV += nchars;
960 Z += nchars;
961 GPT_BYTE += outgoing_nbytes;
962 ZV_BYTE += outgoing_nbytes;
963 Z_BYTE += outgoing_nbytes;
964 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
966 eassert (GPT <= GPT_BYTE);
968 /* The insert may have been in the unchanged region, so check again. */
969 if (Z - GPT < END_UNCHANGED)
970 END_UNCHANGED = Z - GPT;
972 adjust_overlays_for_insert (PT, nchars);
973 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
974 PT_BYTE + outgoing_nbytes,
975 before_markers);
977 offset_intervals (current_buffer, PT, nchars);
979 intervals = string_intervals (string);
980 /* Get the intervals for the part of the string we are inserting. */
981 if (nbytes < SBYTES (string))
982 intervals = copy_intervals (intervals, pos, nchars);
984 /* Insert those intervals. */
985 graft_intervals_into_buffer (intervals, PT, nchars,
986 current_buffer, inherit);
988 adjust_point (nchars, outgoing_nbytes);
990 check_markers ();
993 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
994 starting at GPT_ADDR. */
996 void
997 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes)
999 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1000 nchars = nbytes;
1002 record_insert (GPT, nchars);
1003 MODIFF++;
1005 GAP_SIZE -= nbytes;
1006 GPT += nchars;
1007 ZV += nchars;
1008 Z += nchars;
1009 GPT_BYTE += nbytes;
1010 ZV_BYTE += nbytes;
1011 Z_BYTE += nbytes;
1012 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1014 eassert (GPT <= GPT_BYTE);
1016 adjust_overlays_for_insert (GPT - nchars, nchars);
1017 adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
1018 GPT, GPT_BYTE, 0);
1020 if (buffer_intervals (current_buffer))
1022 offset_intervals (current_buffer, GPT - nchars, nchars);
1023 graft_intervals_into_buffer (NULL, GPT - nchars, nchars,
1024 current_buffer, 0);
1027 if (GPT - nchars < PT)
1028 adjust_point (nchars, nbytes);
1030 check_markers ();
1033 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1034 current buffer. If the text in BUF has properties, they are absorbed
1035 into the current buffer.
1037 It does not work to use `insert' for this, because a malloc could happen
1038 and relocate BUF's text before the copy happens. */
1040 void
1041 insert_from_buffer (struct buffer *buf,
1042 ptrdiff_t charpos, ptrdiff_t nchars, int inherit)
1044 ptrdiff_t opoint = PT;
1046 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1047 signal_after_change (opoint, 0, PT - opoint);
1048 update_compositions (opoint, PT, CHECK_BORDER);
1051 static void
1052 insert_from_buffer_1 (struct buffer *buf,
1053 ptrdiff_t from, ptrdiff_t nchars, int inherit)
1055 ptrdiff_t chunk, chunk_expanded;
1056 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1057 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1058 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1059 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1060 INTERVAL intervals;
1062 /* Make OUTGOING_NBYTES describe the text
1063 as it will be inserted in this buffer. */
1065 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1066 outgoing_nbytes = nchars;
1067 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1069 ptrdiff_t outgoing_before_gap = 0;
1070 ptrdiff_t outgoing_after_gap = 0;
1072 if (from < BUF_GPT (buf))
1074 chunk = BUF_GPT_BYTE (buf) - from_byte;
1075 if (chunk > incoming_nbytes)
1076 chunk = incoming_nbytes;
1077 outgoing_before_gap
1078 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1079 chunk);
1081 else
1082 chunk = 0;
1084 if (chunk < incoming_nbytes)
1085 outgoing_after_gap
1086 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1087 from_byte + chunk),
1088 incoming_nbytes - chunk);
1090 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1093 /* Do this before moving and increasing the gap,
1094 because the before-change hooks might move the gap
1095 or make it smaller. */
1096 prepare_to_modify_buffer (PT, PT, NULL);
1098 if (PT != GPT)
1099 move_gap_both (PT, PT_BYTE);
1100 if (GAP_SIZE < outgoing_nbytes)
1101 make_gap (outgoing_nbytes - GAP_SIZE);
1103 if (from < BUF_GPT (buf))
1105 chunk = BUF_GPT_BYTE (buf) - from_byte;
1106 if (chunk > incoming_nbytes)
1107 chunk = incoming_nbytes;
1108 /* Record number of output bytes, so we know where
1109 to put the output from the second copy_text. */
1110 chunk_expanded
1111 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1112 GPT_ADDR, chunk,
1113 ! NILP (BVAR (buf, enable_multibyte_characters)),
1114 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1116 else
1117 chunk_expanded = chunk = 0;
1119 if (chunk < incoming_nbytes)
1120 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1121 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1122 ! NILP (BVAR (buf, enable_multibyte_characters)),
1123 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1125 #ifdef BYTE_COMBINING_DEBUG
1126 /* We have copied text into the gap, but we have not altered
1127 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1128 to these functions and get the same results as we would
1129 have got earlier on. Meanwhile, GPT_ADDR does point to
1130 the text that has been stored by copy_text. */
1131 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1132 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1133 abort ();
1134 #endif
1136 record_insert (PT, nchars);
1137 MODIFF++;
1138 CHARS_MODIFF = MODIFF;
1140 GAP_SIZE -= outgoing_nbytes;
1141 GPT += nchars;
1142 ZV += nchars;
1143 Z += nchars;
1144 GPT_BYTE += outgoing_nbytes;
1145 ZV_BYTE += outgoing_nbytes;
1146 Z_BYTE += outgoing_nbytes;
1147 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1149 eassert (GPT <= GPT_BYTE);
1151 /* The insert may have been in the unchanged region, so check again. */
1152 if (Z - GPT < END_UNCHANGED)
1153 END_UNCHANGED = Z - GPT;
1155 adjust_overlays_for_insert (PT, nchars);
1156 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1157 PT_BYTE + outgoing_nbytes,
1160 if (buffer_intervals (current_buffer))
1161 offset_intervals (current_buffer, PT, nchars);
1163 /* Get the intervals for the part of the string we are inserting. */
1164 intervals = buffer_intervals (buf);
1165 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1167 if (buf == current_buffer && PT <= from)
1168 from += nchars;
1169 intervals = copy_intervals (intervals, from, nchars);
1172 /* Insert those intervals. */
1173 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1175 adjust_point (nchars, outgoing_nbytes);
1178 /* Record undo information and adjust markers and position keepers for
1179 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1180 chars (LEN_BYTE bytes) which resides in the gap just after
1181 GPT_ADDR.
1183 PREV_TEXT nil means the new text was just inserted. */
1185 static void
1186 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1187 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1189 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1191 #ifdef BYTE_COMBINING_DEBUG
1192 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1193 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1194 abort ();
1195 #endif
1197 if (STRINGP (prev_text))
1199 nchars_del = SCHARS (prev_text);
1200 nbytes_del = SBYTES (prev_text);
1203 /* Update various buffer positions for the new text. */
1204 GAP_SIZE -= len_byte;
1205 ZV += len; Z+= len;
1206 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1207 GPT += len; GPT_BYTE += len_byte;
1208 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1210 if (nchars_del > 0)
1211 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1212 len, len_byte);
1213 else
1214 adjust_markers_for_insert (from, from_byte,
1215 from + len, from_byte + len_byte, 0);
1217 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1219 if (nchars_del > 0)
1220 record_delete (from, prev_text);
1221 record_insert (from, len);
1224 if (len > nchars_del)
1225 adjust_overlays_for_insert (from, len - nchars_del);
1226 else if (len < nchars_del)
1227 adjust_overlays_for_delete (from, nchars_del - len);
1229 if (buffer_intervals (current_buffer))
1230 offset_intervals (current_buffer, from, len - nchars_del);
1232 if (from < PT)
1233 adjust_point (len - nchars_del, len_byte - nbytes_del);
1235 /* As byte combining will decrease Z, we must check this again. */
1236 if (Z - GPT < END_UNCHANGED)
1237 END_UNCHANGED = Z - GPT;
1239 check_markers ();
1241 if (len == 0)
1242 evaporate_overlays (from);
1243 MODIFF++;
1244 CHARS_MODIFF = MODIFF;
1247 /* Record undo information, adjust markers and position keepers for an
1248 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1249 text already exists in the current buffer but character length (TO
1250 - FROM) may be incorrect, the correct length is NEWLEN. */
1252 void
1253 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1254 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1256 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1258 if (GPT != to)
1259 move_gap_both (to, to_byte);
1260 GAP_SIZE += len_byte;
1261 GPT -= len; GPT_BYTE -= len_byte;
1262 ZV -= len; ZV_BYTE -= len_byte;
1263 Z -= len; Z_BYTE -= len_byte;
1264 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1267 /* Replace the text from character positions FROM to TO with NEW,
1268 If PREPARE is nonzero, call prepare_to_modify_buffer.
1269 If INHERIT, the newly inserted text should inherit text properties
1270 from the surrounding non-deleted text. */
1272 /* Note that this does not yet handle markers quite right.
1273 Also it needs to record a single undo-entry that does a replacement
1274 rather than a separate delete and insert.
1275 That way, undo will also handle markers properly.
1277 But if MARKERS is 0, don't relocate markers. */
1279 void
1280 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1281 int prepare, int inherit, int markers)
1283 ptrdiff_t inschars = SCHARS (new);
1284 ptrdiff_t insbytes = SBYTES (new);
1285 ptrdiff_t from_byte, to_byte;
1286 ptrdiff_t nbytes_del, nchars_del;
1287 struct gcpro gcpro1;
1288 INTERVAL intervals;
1289 ptrdiff_t outgoing_insbytes = insbytes;
1290 Lisp_Object deletion;
1292 check_markers ();
1294 GCPRO1 (new);
1295 deletion = Qnil;
1297 if (prepare)
1299 ptrdiff_t range_length = to - from;
1300 prepare_to_modify_buffer (from, to, &from);
1301 to = from + range_length;
1304 UNGCPRO;
1306 /* Make args be valid. */
1307 if (from < BEGV)
1308 from = BEGV;
1309 if (to > ZV)
1310 to = ZV;
1312 from_byte = CHAR_TO_BYTE (from);
1313 to_byte = CHAR_TO_BYTE (to);
1315 nchars_del = to - from;
1316 nbytes_del = to_byte - from_byte;
1318 if (nbytes_del <= 0 && insbytes == 0)
1319 return;
1321 /* Make OUTGOING_INSBYTES describe the text
1322 as it will be inserted in this buffer. */
1324 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1325 outgoing_insbytes = inschars;
1326 else if (! STRING_MULTIBYTE (new))
1327 outgoing_insbytes
1328 = count_size_as_multibyte (SDATA (new), insbytes);
1330 GCPRO1 (new);
1332 /* Make sure the gap is somewhere in or next to what we are deleting. */
1333 if (from > GPT)
1334 gap_right (from, from_byte);
1335 if (to < GPT)
1336 gap_left (to, to_byte, 0);
1338 /* Even if we don't record for undo, we must keep the original text
1339 because we may have to recover it because of inappropriate byte
1340 combining. */
1341 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1342 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1344 GAP_SIZE += nbytes_del;
1345 ZV -= nchars_del;
1346 Z -= nchars_del;
1347 ZV_BYTE -= nbytes_del;
1348 Z_BYTE -= nbytes_del;
1349 GPT = from;
1350 GPT_BYTE = from_byte;
1351 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1353 eassert (GPT <= GPT_BYTE);
1355 if (GPT - BEG < BEG_UNCHANGED)
1356 BEG_UNCHANGED = GPT - BEG;
1357 if (Z - GPT < END_UNCHANGED)
1358 END_UNCHANGED = Z - GPT;
1360 if (GAP_SIZE < outgoing_insbytes)
1361 make_gap (outgoing_insbytes - GAP_SIZE);
1363 /* Copy the string text into the buffer, perhaps converting
1364 between single-byte and multibyte. */
1365 copy_text (SDATA (new), GPT_ADDR, insbytes,
1366 STRING_MULTIBYTE (new),
1367 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1369 #ifdef BYTE_COMBINING_DEBUG
1370 /* We have copied text into the gap, but we have not marked
1371 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1372 here, for both the previous text and the following text.
1373 Meanwhile, GPT_ADDR does point to
1374 the text that has been stored by copy_text. */
1375 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1376 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1377 abort ();
1378 #endif
1380 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1382 /* Record the insertion first, so that when we undo,
1383 the deletion will be undone first. Thus, undo
1384 will insert before deleting, and thus will keep
1385 the markers before and after this text separate. */
1386 record_insert (from + SCHARS (deletion), inschars);
1387 record_delete (from, deletion);
1390 GAP_SIZE -= outgoing_insbytes;
1391 GPT += inschars;
1392 ZV += inschars;
1393 Z += inschars;
1394 GPT_BYTE += outgoing_insbytes;
1395 ZV_BYTE += outgoing_insbytes;
1396 Z_BYTE += outgoing_insbytes;
1397 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1399 eassert (GPT <= GPT_BYTE);
1401 /* Adjust the overlay center as needed. This must be done after
1402 adjusting the markers that bound the overlays. */
1403 adjust_overlays_for_delete (from, nchars_del);
1404 adjust_overlays_for_insert (from, inschars);
1406 /* Adjust markers for the deletion and the insertion. */
1407 if (markers)
1408 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1409 inschars, outgoing_insbytes);
1411 offset_intervals (current_buffer, from, inschars - nchars_del);
1413 /* Get the intervals for the part of the string we are inserting--
1414 not including the combined-before bytes. */
1415 intervals = string_intervals (new);
1416 /* Insert those intervals. */
1417 graft_intervals_into_buffer (intervals, from, inschars,
1418 current_buffer, inherit);
1420 /* Relocate point as if it were a marker. */
1421 if (from < PT)
1422 adjust_point ((from + inschars - (PT < to ? PT : to)),
1423 (from_byte + outgoing_insbytes
1424 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1426 if (outgoing_insbytes == 0)
1427 evaporate_overlays (from);
1429 check_markers ();
1431 MODIFF++;
1432 CHARS_MODIFF = MODIFF;
1433 UNGCPRO;
1435 signal_after_change (from, nchars_del, GPT - from);
1436 update_compositions (from, GPT, CHECK_BORDER);
1439 /* Replace the text from character positions FROM to TO with
1440 the text in INS of length INSCHARS.
1441 Keep the text properties that applied to the old characters
1442 (extending them to all the new chars if there are more new chars).
1444 Note that this does not yet handle markers quite right.
1446 If MARKERS is nonzero, relocate markers.
1448 Unlike most functions at this level, never call
1449 prepare_to_modify_buffer and never call signal_after_change. */
1451 void
1452 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1453 ptrdiff_t to, ptrdiff_t to_byte,
1454 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1455 int markers)
1457 ptrdiff_t nbytes_del, nchars_del;
1459 check_markers ();
1461 nchars_del = to - from;
1462 nbytes_del = to_byte - from_byte;
1464 if (nbytes_del <= 0 && insbytes == 0)
1465 return;
1467 /* Make sure the gap is somewhere in or next to what we are deleting. */
1468 if (from > GPT)
1469 gap_right (from, from_byte);
1470 if (to < GPT)
1471 gap_left (to, to_byte, 0);
1473 GAP_SIZE += nbytes_del;
1474 ZV -= nchars_del;
1475 Z -= nchars_del;
1476 ZV_BYTE -= nbytes_del;
1477 Z_BYTE -= nbytes_del;
1478 GPT = from;
1479 GPT_BYTE = from_byte;
1480 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1482 eassert (GPT <= GPT_BYTE);
1484 if (GPT - BEG < BEG_UNCHANGED)
1485 BEG_UNCHANGED = GPT - BEG;
1486 if (Z - GPT < END_UNCHANGED)
1487 END_UNCHANGED = Z - GPT;
1489 if (GAP_SIZE < insbytes)
1490 make_gap (insbytes - GAP_SIZE);
1492 /* Copy the replacement text into the buffer. */
1493 memcpy (GPT_ADDR, ins, insbytes);
1495 #ifdef BYTE_COMBINING_DEBUG
1496 /* We have copied text into the gap, but we have not marked
1497 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1498 here, for both the previous text and the following text.
1499 Meanwhile, GPT_ADDR does point to
1500 the text that has been stored by copy_text. */
1501 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1502 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1503 abort ();
1504 #endif
1506 GAP_SIZE -= insbytes;
1507 GPT += inschars;
1508 ZV += inschars;
1509 Z += inschars;
1510 GPT_BYTE += insbytes;
1511 ZV_BYTE += insbytes;
1512 Z_BYTE += insbytes;
1513 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1515 eassert (GPT <= GPT_BYTE);
1517 /* Adjust the overlay center as needed. This must be done after
1518 adjusting the markers that bound the overlays. */
1519 if (nchars_del != inschars)
1521 adjust_overlays_for_insert (from, inschars);
1522 adjust_overlays_for_delete (from + inschars, nchars_del);
1525 /* Adjust markers for the deletion and the insertion. */
1526 if (markers
1527 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1528 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1529 inschars, insbytes);
1531 offset_intervals (current_buffer, from, inschars - nchars_del);
1533 /* Relocate point as if it were a marker. */
1534 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1536 if (PT < to)
1537 /* PT was within the deleted text. Move it to FROM. */
1538 adjust_point (from - PT, from_byte - PT_BYTE);
1539 else
1540 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1543 if (insbytes == 0)
1544 evaporate_overlays (from);
1546 check_markers ();
1548 MODIFF++;
1549 CHARS_MODIFF = MODIFF;
1552 /* Delete characters in current buffer
1553 from FROM up to (but not including) TO.
1554 If TO comes before FROM, we delete nothing. */
1556 void
1557 del_range (ptrdiff_t from, ptrdiff_t to)
1559 del_range_1 (from, to, 1, 0);
1562 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1563 RET_STRING says to return the deleted text. */
1565 Lisp_Object
1566 del_range_1 (ptrdiff_t from, ptrdiff_t to, int prepare, int ret_string)
1568 ptrdiff_t from_byte, to_byte;
1569 Lisp_Object deletion;
1570 struct gcpro gcpro1;
1572 /* Make args be valid */
1573 if (from < BEGV)
1574 from = BEGV;
1575 if (to > ZV)
1576 to = ZV;
1578 if (to <= from)
1579 return Qnil;
1581 if (prepare)
1583 ptrdiff_t range_length = to - from;
1584 prepare_to_modify_buffer (from, to, &from);
1585 to = min (ZV, from + range_length);
1588 from_byte = CHAR_TO_BYTE (from);
1589 to_byte = CHAR_TO_BYTE (to);
1591 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1592 GCPRO1 (deletion);
1593 signal_after_change (from, to - from, 0);
1594 update_compositions (from, from, CHECK_HEAD);
1595 UNGCPRO;
1596 return deletion;
1599 /* Like del_range_1 but args are byte positions, not char positions. */
1601 void
1602 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, int prepare)
1604 ptrdiff_t from, to;
1606 /* Make args be valid */
1607 if (from_byte < BEGV_BYTE)
1608 from_byte = BEGV_BYTE;
1609 if (to_byte > ZV_BYTE)
1610 to_byte = ZV_BYTE;
1612 if (to_byte <= from_byte)
1613 return;
1615 from = BYTE_TO_CHAR (from_byte);
1616 to = BYTE_TO_CHAR (to_byte);
1618 if (prepare)
1620 ptrdiff_t old_from = from, old_to = Z - to;
1621 ptrdiff_t range_length = to - from;
1622 prepare_to_modify_buffer (from, to, &from);
1623 to = from + range_length;
1625 if (old_from != from)
1626 from_byte = CHAR_TO_BYTE (from);
1627 if (to > ZV)
1629 to = ZV;
1630 to_byte = ZV_BYTE;
1632 else if (old_to == Z - to)
1633 to_byte = CHAR_TO_BYTE (to);
1636 del_range_2 (from, from_byte, to, to_byte, 0);
1637 signal_after_change (from, to - from, 0);
1638 update_compositions (from, from, CHECK_HEAD);
1641 /* Like del_range_1, but positions are specified both as charpos
1642 and bytepos. */
1644 void
1645 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1646 ptrdiff_t to, ptrdiff_t to_byte, int prepare)
1648 /* Make args be valid */
1649 if (from_byte < BEGV_BYTE)
1650 from_byte = BEGV_BYTE;
1651 if (to_byte > ZV_BYTE)
1652 to_byte = ZV_BYTE;
1654 if (to_byte <= from_byte)
1655 return;
1657 if (from < BEGV)
1658 from = BEGV;
1659 if (to > ZV)
1660 to = ZV;
1662 if (prepare)
1664 ptrdiff_t old_from = from, old_to = Z - to;
1665 ptrdiff_t range_length = to - from;
1666 prepare_to_modify_buffer (from, to, &from);
1667 to = from + range_length;
1669 if (old_from != from)
1670 from_byte = CHAR_TO_BYTE (from);
1671 if (to > ZV)
1673 to = ZV;
1674 to_byte = ZV_BYTE;
1676 else if (old_to == Z - to)
1677 to_byte = CHAR_TO_BYTE (to);
1680 del_range_2 (from, from_byte, to, to_byte, 0);
1681 signal_after_change (from, to - from, 0);
1682 update_compositions (from, from, CHECK_HEAD);
1685 /* Delete a range of text, specified both as character positions
1686 and byte positions. FROM and TO are character positions,
1687 while FROM_BYTE and TO_BYTE are byte positions.
1688 If RET_STRING is true, the deleted area is returned as a string. */
1690 Lisp_Object
1691 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1692 ptrdiff_t to, ptrdiff_t to_byte, int ret_string)
1694 register ptrdiff_t nbytes_del, nchars_del;
1695 Lisp_Object deletion;
1697 check_markers ();
1699 nchars_del = to - from;
1700 nbytes_del = to_byte - from_byte;
1702 /* Make sure the gap is somewhere in or next to what we are deleting. */
1703 if (from > GPT)
1704 gap_right (from, from_byte);
1705 if (to < GPT)
1706 gap_left (to, to_byte, 0);
1708 #ifdef BYTE_COMBINING_DEBUG
1709 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1710 Z_BYTE - to_byte, from, from_byte))
1711 abort ();
1712 #endif
1714 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1715 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1716 else
1717 deletion = Qnil;
1719 /* Relocate all markers pointing into the new, larger gap
1720 to point at the end of the text before the gap.
1721 Do this before recording the deletion,
1722 so that undo handles this after reinserting the text. */
1723 adjust_markers_for_delete (from, from_byte, to, to_byte);
1725 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1726 record_delete (from, deletion);
1727 MODIFF++;
1728 CHARS_MODIFF = MODIFF;
1730 /* Relocate point as if it were a marker. */
1731 if (from < PT)
1732 adjust_point (from - (PT < to ? PT : to),
1733 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1735 offset_intervals (current_buffer, from, - nchars_del);
1737 /* Adjust the overlay center as needed. This must be done after
1738 adjusting the markers that bound the overlays. */
1739 adjust_overlays_for_delete (from, nchars_del);
1741 GAP_SIZE += nbytes_del;
1742 ZV_BYTE -= nbytes_del;
1743 Z_BYTE -= nbytes_del;
1744 ZV -= nchars_del;
1745 Z -= nchars_del;
1746 GPT = from;
1747 GPT_BYTE = from_byte;
1748 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1749 /* Put an anchor, unless called from decode_coding_object which
1750 needs to access the previous gap contents. */
1751 *(GPT_ADDR) = 0;
1753 eassert (GPT <= GPT_BYTE);
1755 if (GPT - BEG < BEG_UNCHANGED)
1756 BEG_UNCHANGED = GPT - BEG;
1757 if (Z - GPT < END_UNCHANGED)
1758 END_UNCHANGED = Z - GPT;
1760 check_markers ();
1762 evaporate_overlays (from);
1764 return deletion;
1767 /* Call this if you're about to change the region of BUFFER from
1768 character positions START to END. This checks the read-only
1769 properties of the region, calls the necessary modification hooks,
1770 and warns the next redisplay that it should pay attention to that
1771 area.
1773 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
1774 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1776 void
1777 modify_region (struct buffer *buffer, ptrdiff_t start, ptrdiff_t end,
1778 int preserve_chars_modiff)
1780 struct buffer *old_buffer = current_buffer;
1782 if (buffer != old_buffer)
1783 set_buffer_internal (buffer);
1785 prepare_to_modify_buffer (start, end, NULL);
1787 BUF_COMPUTE_UNCHANGED (buffer, start - 1, end);
1789 if (MODIFF <= SAVE_MODIFF)
1790 record_first_change ();
1791 MODIFF++;
1792 if (! preserve_chars_modiff)
1793 CHARS_MODIFF = MODIFF;
1795 bset_point_before_scroll (buffer, Qnil);
1797 if (buffer != old_buffer)
1798 set_buffer_internal (old_buffer);
1801 /* Check that it is okay to modify the buffer between START and END,
1802 which are char positions.
1804 Run the before-change-function, if any. If intervals are in use,
1805 verify that the text to be modified is not read-only, and call
1806 any modification properties the text may have.
1808 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1809 by holding its value temporarily in a marker. */
1811 void
1812 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1813 ptrdiff_t *preserve_ptr)
1815 struct buffer *base_buffer;
1817 if (!NILP (BVAR (current_buffer, read_only)))
1818 Fbarf_if_buffer_read_only ();
1820 /* Let redisplay consider other windows than selected_window
1821 if modifying another buffer. */
1822 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1823 ++windows_or_buffers_changed;
1825 if (buffer_intervals (current_buffer))
1827 if (preserve_ptr)
1829 Lisp_Object preserve_marker;
1830 struct gcpro gcpro1;
1831 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1832 GCPRO1 (preserve_marker);
1833 verify_interval_modification (current_buffer, start, end);
1834 *preserve_ptr = marker_position (preserve_marker);
1835 unchain_marker (XMARKER (preserve_marker));
1836 UNGCPRO;
1838 else
1839 verify_interval_modification (current_buffer, start, end);
1842 /* For indirect buffers, use the base buffer to check clashes. */
1843 if (current_buffer->base_buffer != 0)
1844 base_buffer = current_buffer->base_buffer;
1845 else
1846 base_buffer = current_buffer;
1848 #ifdef CLASH_DETECTION
1849 if (!NILP (BVAR (base_buffer, file_truename))
1850 /* Make binding buffer-file-name to nil effective. */
1851 && !NILP (BVAR (base_buffer, filename))
1852 && SAVE_MODIFF >= MODIFF)
1853 lock_file (BVAR (base_buffer, file_truename));
1854 #else
1855 /* At least warn if this file has changed on disk since it was visited. */
1856 if (!NILP (BVAR (base_buffer, filename))
1857 && SAVE_MODIFF >= MODIFF
1858 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1859 && !NILP (Ffile_exists_p (BVAR (base_buffer, filename))))
1860 call1 (intern ("ask-user-about-supersession-threat"),
1861 BVAR (base_buffer,filename));
1862 #endif /* not CLASH_DETECTION */
1864 /* If `select-active-regions' is non-nil, save the region text. */
1865 if (!NILP (BVAR (current_buffer, mark_active))
1866 && !inhibit_modification_hooks
1867 && XMARKER (BVAR (current_buffer, mark))->buffer
1868 && NILP (Vsaved_region_selection)
1869 && (EQ (Vselect_active_regions, Qonly)
1870 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1871 : (!NILP (Vselect_active_regions)
1872 && !NILP (Vtransient_mark_mode))))
1874 ptrdiff_t b = XMARKER (BVAR (current_buffer, mark))->charpos;
1875 ptrdiff_t e = PT;
1876 if (b < e)
1877 Vsaved_region_selection = make_buffer_string (b, e, 0);
1878 else if (b > e)
1879 Vsaved_region_selection = make_buffer_string (e, b, 0);
1882 signal_before_change (start, end, preserve_ptr);
1884 if (current_buffer->newline_cache)
1885 invalidate_region_cache (current_buffer,
1886 current_buffer->newline_cache,
1887 start - BEG, Z - end);
1888 if (current_buffer->width_run_cache)
1889 invalidate_region_cache (current_buffer,
1890 current_buffer->width_run_cache,
1891 start - BEG, Z - end);
1893 Vdeactivate_mark = Qt;
1896 /* These macros work with an argument named `preserve_ptr'
1897 and a local variable named `preserve_marker'. */
1899 #define PRESERVE_VALUE \
1900 if (preserve_ptr && NILP (preserve_marker)) \
1901 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1903 #define RESTORE_VALUE \
1904 if (! NILP (preserve_marker)) \
1906 *preserve_ptr = marker_position (preserve_marker); \
1907 unchain_marker (XMARKER (preserve_marker)); \
1910 #define PRESERVE_START_END \
1911 if (NILP (start_marker)) \
1912 start_marker = Fcopy_marker (start, Qnil); \
1913 if (NILP (end_marker)) \
1914 end_marker = Fcopy_marker (end, Qnil);
1916 #define FETCH_START \
1917 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1919 #define FETCH_END \
1920 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1922 /* Set a variable to nil if an error occurred.
1923 Don't change the variable if there was no error.
1924 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1925 VARIABLE is the variable to maybe set to nil.
1926 NO-ERROR-FLAG is nil if there was an error,
1927 anything else meaning no error (so this function does nothing). */
1928 static Lisp_Object
1929 reset_var_on_error (Lisp_Object val)
1931 if (NILP (XCDR (val)))
1932 Fset (XCAR (val), Qnil);
1933 return Qnil;
1936 /* Signal a change to the buffer immediately before it happens.
1937 START_INT and END_INT are the bounds of the text to be changed.
1939 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1940 by holding its value temporarily in a marker. */
1942 static void
1943 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1944 ptrdiff_t *preserve_ptr)
1946 Lisp_Object start, end;
1947 Lisp_Object start_marker, end_marker;
1948 Lisp_Object preserve_marker;
1949 struct gcpro gcpro1, gcpro2, gcpro3;
1950 ptrdiff_t count = SPECPDL_INDEX ();
1952 if (inhibit_modification_hooks)
1953 return;
1955 start = make_number (start_int);
1956 end = make_number (end_int);
1957 preserve_marker = Qnil;
1958 start_marker = Qnil;
1959 end_marker = Qnil;
1960 GCPRO3 (preserve_marker, start_marker, end_marker);
1962 specbind (Qinhibit_modification_hooks, Qt);
1964 /* If buffer is unmodified, run a special hook for that case. The
1965 check for Vfirst_change_hook is just a minor optimization. */
1966 if (SAVE_MODIFF >= MODIFF
1967 && !NILP (Vfirst_change_hook))
1969 PRESERVE_VALUE;
1970 PRESERVE_START_END;
1971 Frun_hooks (1, &Qfirst_change_hook);
1974 /* Now run the before-change-functions if any. */
1975 if (!NILP (Vbefore_change_functions))
1977 Lisp_Object args[3];
1978 Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
1980 PRESERVE_VALUE;
1981 PRESERVE_START_END;
1983 /* Mark before-change-functions to be reset to nil in case of error. */
1984 record_unwind_protect (reset_var_on_error, rvoe_arg);
1986 /* Actually run the hook functions. */
1987 args[0] = Qbefore_change_functions;
1988 args[1] = FETCH_START;
1989 args[2] = FETCH_END;
1990 Frun_hook_with_args (3, args);
1992 /* There was no error: unarm the reset_on_error. */
1993 XSETCDR (rvoe_arg, Qt);
1996 if (buffer_has_overlays ())
1998 PRESERVE_VALUE;
1999 report_overlay_modification (FETCH_START, FETCH_END, 0,
2000 FETCH_START, FETCH_END, Qnil);
2003 if (! NILP (start_marker))
2004 free_marker (start_marker);
2005 if (! NILP (end_marker))
2006 free_marker (end_marker);
2007 RESTORE_VALUE;
2008 UNGCPRO;
2010 unbind_to (count, Qnil);
2013 /* Signal a change immediately after it happens.
2014 CHARPOS is the character position of the start of the changed text.
2015 LENDEL is the number of characters of the text before the change.
2016 (Not the whole buffer; just the part that was changed.)
2017 LENINS is the number of characters in that part of the text
2018 after the change. */
2020 void
2021 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2023 ptrdiff_t count = SPECPDL_INDEX ();
2024 if (inhibit_modification_hooks)
2025 return;
2027 /* If we are deferring calls to the after-change functions
2028 and there are no before-change functions,
2029 just record the args that we were going to use. */
2030 if (! NILP (Vcombine_after_change_calls)
2031 && NILP (Vbefore_change_functions)
2032 && !buffer_has_overlays ())
2034 Lisp_Object elt;
2036 if (!NILP (combine_after_change_list)
2037 && current_buffer != XBUFFER (combine_after_change_buffer))
2038 Fcombine_after_change_execute ();
2040 elt = Fcons (make_number (charpos - BEG),
2041 Fcons (make_number (Z - (charpos - lendel + lenins)),
2042 Fcons (make_number (lenins - lendel), Qnil)));
2043 combine_after_change_list
2044 = Fcons (elt, combine_after_change_list);
2045 combine_after_change_buffer = Fcurrent_buffer ();
2047 return;
2050 if (!NILP (combine_after_change_list))
2051 Fcombine_after_change_execute ();
2053 specbind (Qinhibit_modification_hooks, Qt);
2055 if (!NILP (Vafter_change_functions))
2057 Lisp_Object args[4];
2058 Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
2060 /* Mark after-change-functions to be reset to nil in case of error. */
2061 record_unwind_protect (reset_var_on_error, rvoe_arg);
2063 /* Actually run the hook functions. */
2064 args[0] = Qafter_change_functions;
2065 XSETFASTINT (args[1], charpos);
2066 XSETFASTINT (args[2], charpos + lenins);
2067 XSETFASTINT (args[3], lendel);
2068 Frun_hook_with_args (4, args);
2070 /* There was no error: unarm the reset_on_error. */
2071 XSETCDR (rvoe_arg, Qt);
2074 if (buffer_has_overlays ())
2075 report_overlay_modification (make_number (charpos),
2076 make_number (charpos + lenins),
2078 make_number (charpos),
2079 make_number (charpos + lenins),
2080 make_number (lendel));
2082 /* After an insertion, call the text properties
2083 insert-behind-hooks or insert-in-front-hooks. */
2084 if (lendel == 0)
2085 report_interval_modification (make_number (charpos),
2086 make_number (charpos + lenins));
2088 unbind_to (count, Qnil);
2091 static Lisp_Object
2092 Fcombine_after_change_execute_1 (Lisp_Object val)
2094 Vcombine_after_change_calls = val;
2095 return val;
2098 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2099 Scombine_after_change_execute, 0, 0, 0,
2100 doc: /* This function is for use internally in `combine-after-change-calls'. */)
2101 (void)
2103 ptrdiff_t count = SPECPDL_INDEX ();
2104 ptrdiff_t beg, end, change;
2105 ptrdiff_t begpos, endpos;
2106 Lisp_Object tail;
2108 if (NILP (combine_after_change_list))
2109 return Qnil;
2111 /* It is rare for combine_after_change_buffer to be invalid, but
2112 possible. It can happen when combine-after-change-calls is
2113 non-nil, and insertion calls a file handler (e.g. through
2114 lock_file) which scribbles into a temp file -- cyd */
2115 if (!BUFFERP (combine_after_change_buffer)
2116 || NILP (BVAR (XBUFFER (combine_after_change_buffer), name)))
2118 combine_after_change_list = Qnil;
2119 return Qnil;
2122 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2124 Fset_buffer (combine_after_change_buffer);
2126 /* # chars unchanged at beginning of buffer. */
2127 beg = Z - BEG;
2128 /* # chars unchanged at end of buffer. */
2129 end = beg;
2130 /* Total amount of insertion (negative for deletion). */
2131 change = 0;
2133 /* Scan the various individual changes,
2134 accumulating the range info in BEG, END and CHANGE. */
2135 for (tail = combine_after_change_list; CONSP (tail);
2136 tail = XCDR (tail))
2138 Lisp_Object elt;
2139 ptrdiff_t thisbeg, thisend, thischange;
2141 /* Extract the info from the next element. */
2142 elt = XCAR (tail);
2143 if (! CONSP (elt))
2144 continue;
2145 thisbeg = XINT (XCAR (elt));
2147 elt = XCDR (elt);
2148 if (! CONSP (elt))
2149 continue;
2150 thisend = XINT (XCAR (elt));
2152 elt = XCDR (elt);
2153 if (! CONSP (elt))
2154 continue;
2155 thischange = XINT (XCAR (elt));
2157 /* Merge this range into the accumulated range. */
2158 change += thischange;
2159 if (thisbeg < beg)
2160 beg = thisbeg;
2161 if (thisend < end)
2162 end = thisend;
2165 /* Get the current start and end positions of the range
2166 that was changed. */
2167 begpos = BEG + beg;
2168 endpos = Z - end;
2170 /* We are about to handle these, so discard them. */
2171 combine_after_change_list = Qnil;
2173 /* Now run the after-change functions for real.
2174 Turn off the flag that defers them. */
2175 record_unwind_protect (Fcombine_after_change_execute_1,
2176 Vcombine_after_change_calls);
2177 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2178 update_compositions (begpos, endpos, CHECK_ALL);
2180 return unbind_to (count, Qnil);
2183 void
2184 syms_of_insdel (void)
2186 staticpro (&combine_after_change_list);
2187 staticpro (&combine_after_change_buffer);
2188 combine_after_change_list = Qnil;
2189 combine_after_change_buffer = Qnil;
2191 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2192 doc: /* Used internally by the `combine-after-change-calls' macro. */);
2193 Vcombine_after_change_calls = Qnil;
2195 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2196 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2197 This affects `before-change-functions' and `after-change-functions',
2198 as well as hooks attached to text properties and overlays. */);
2199 inhibit_modification_hooks = 0;
2200 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2202 defsubr (&Scombine_after_change_execute);