* insdel.c (prepare_to_modify_buffer): Force redisplay if
[emacs.git] / src / insdel.c
blob98dd97bdd0806139add034afcaa62259ba872701
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2013 Free Software
3 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>
23 #include <intprops.h>
25 #include "lisp.h"
26 #include "intervals.h"
27 #include "character.h"
28 #include "buffer.h"
29 #include "window.h"
30 #include "blockinput.h"
31 #include "region-cache.h"
33 static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35 static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
36 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37 static void gap_right (ptrdiff_t, ptrdiff_t);
39 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
41 was non-nil. We use this to decide how to call them
42 once the deferral ends.
44 In each element.
45 BEG-UNCHANGED is the number of chars before the changed range.
46 END-UNCHANGED is the number of chars after the changed range,
47 and CHANGE-AMOUNT is the number of characters inserted by the change
48 (negative for a deletion). */
49 static Lisp_Object combine_after_change_list;
51 /* Buffer which combine_after_change_list is about. */
52 static Lisp_Object combine_after_change_buffer;
54 Lisp_Object Qinhibit_modification_hooks;
56 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
58 /* Also used in marker.c to enable expensive marker checks. */
60 #ifdef MARKER_DEBUG
62 static void
63 check_markers (void)
65 struct Lisp_Marker *tail;
66 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
68 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
70 if (tail->buffer->text != current_buffer->text)
71 emacs_abort ();
72 if (tail->charpos > Z)
73 emacs_abort ();
74 if (tail->bytepos > Z_BYTE)
75 emacs_abort ();
76 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
77 emacs_abort ();
81 #else /* not MARKER_DEBUG */
83 #define check_markers() do { } while (0)
85 #endif /* MARKER_DEBUG */
87 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
88 Note that this can quit! */
90 void
91 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
93 eassert (charpos == BYTE_TO_CHAR (bytepos)
94 && bytepos == CHAR_TO_BYTE (charpos));
95 if (bytepos < GPT_BYTE)
96 gap_left (charpos, bytepos, 0);
97 else if (bytepos > GPT_BYTE)
98 gap_right (charpos, bytepos);
101 /* Move the gap to a position less than the current GPT.
102 BYTEPOS describes the new position as a byte position,
103 and CHARPOS is the corresponding char position.
104 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
106 static void
107 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
109 unsigned char *to, *from;
110 ptrdiff_t i;
111 ptrdiff_t new_s1;
113 if (!newgap)
114 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
116 i = GPT_BYTE;
117 to = GAP_END_ADDR;
118 from = GPT_ADDR;
119 new_s1 = GPT_BYTE;
121 /* Now copy the characters. To move the gap down,
122 copy characters up. */
124 while (1)
126 /* I gets number of characters left to copy. */
127 i = new_s1 - bytepos;
128 if (i == 0)
129 break;
130 /* If a quit is requested, stop copying now.
131 Change BYTEPOS to be where we have actually moved the gap to. */
132 if (QUITP)
134 bytepos = new_s1;
135 charpos = BYTE_TO_CHAR (bytepos);
136 break;
138 /* Move at most 32000 chars before checking again for a quit. */
139 if (i > 32000)
140 i = 32000;
141 new_s1 -= i;
142 from -= i, to -= i;
143 memmove (to, from, i);
146 /* Adjust buffer data structure, to put the gap at BYTEPOS.
147 BYTEPOS is where the loop above stopped, which may be what
148 was specified or may be where a quit was detected. */
149 GPT_BYTE = bytepos;
150 GPT = charpos;
151 eassert (charpos <= bytepos);
152 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
153 QUIT;
156 /* Move the gap to a position greater than the current GPT.
157 BYTEPOS describes the new position as a byte position,
158 and CHARPOS is the corresponding char position. */
160 static void
161 gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
163 register unsigned char *to, *from;
164 register ptrdiff_t i;
165 ptrdiff_t new_s1;
167 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
169 i = GPT_BYTE;
170 from = GAP_END_ADDR;
171 to = GPT_ADDR;
172 new_s1 = GPT_BYTE;
174 /* Now copy the characters. To move the gap up,
175 copy characters down. */
177 while (1)
179 /* I gets number of characters left to copy. */
180 i = bytepos - new_s1;
181 if (i == 0)
182 break;
183 /* If a quit is requested, stop copying now.
184 Change BYTEPOS to be where we have actually moved the gap to. */
185 if (QUITP)
187 bytepos = new_s1;
188 charpos = BYTE_TO_CHAR (bytepos);
189 break;
191 /* Move at most 32000 chars before checking again for a quit. */
192 if (i > 32000)
193 i = 32000;
194 new_s1 += i;
195 memmove (to, from, i);
196 from += i, to += i;
199 GPT = charpos;
200 GPT_BYTE = bytepos;
201 eassert (charpos <= bytepos);
202 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
203 QUIT;
206 /* Adjust all markers for a deletion
207 whose range in bytes is FROM_BYTE to TO_BYTE.
208 The range in charpos is FROM to TO.
210 This function assumes that the gap is adjacent to
211 or inside of the range being deleted. */
213 void
214 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
215 ptrdiff_t to, ptrdiff_t to_byte)
217 Lisp_Object marker;
218 register struct Lisp_Marker *m;
219 register ptrdiff_t charpos;
221 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
223 charpos = m->charpos;
224 eassert (charpos <= Z);
226 /* If the marker is after the deletion,
227 relocate by number of chars / bytes deleted. */
228 if (charpos > to)
230 m->charpos -= to - from;
231 m->bytepos -= to_byte - from_byte;
233 /* Here's the case where a marker is inside text being deleted. */
234 else if (charpos > from)
236 if (! m->insertion_type)
237 { /* Normal markers will end up at the beginning of the
238 re-inserted text after undoing a deletion, and must be
239 adjusted to move them to the correct place. */
240 XSETMISC (marker, m);
241 record_marker_adjustment (marker, from - charpos);
243 else if (charpos < to)
244 { /* Before-insertion markers will automatically move forward
245 upon re-inserting the deleted text, so we have to arrange
246 for them to move backward to the correct position. */
247 XSETMISC (marker, m);
248 record_marker_adjustment (marker, to - charpos);
250 m->charpos = from;
251 m->bytepos = from_byte;
253 /* Here's the case where a before-insertion marker is immediately
254 before the deleted region. */
255 else if (charpos == from && m->insertion_type)
257 /* Undoing the change uses normal insertion, which will
258 incorrectly make MARKER move forward, so we arrange for it
259 to then move backward to the correct place at the beginning
260 of the deleted region. */
261 XSETMISC (marker, m);
262 record_marker_adjustment (marker, to - from);
268 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
269 to TO / TO_BYTE. We have to relocate the charpos of every marker
270 that points after the insertion (but not their bytepos).
272 When a marker points at the insertion point,
273 we advance it if either its insertion-type is t
274 or BEFORE_MARKERS is true. */
276 static void
277 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
278 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
280 struct Lisp_Marker *m;
281 bool adjusted = 0;
282 ptrdiff_t nchars = to - from;
283 ptrdiff_t nbytes = to_byte - from_byte;
285 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
287 eassert (m->bytepos >= m->charpos
288 && m->bytepos - m->charpos <= Z_BYTE - Z);
290 if (m->bytepos == from_byte)
292 if (m->insertion_type || before_markers)
294 m->bytepos = to_byte;
295 m->charpos = to;
296 if (m->insertion_type)
297 adjusted = 1;
300 else if (m->bytepos > from_byte)
302 m->bytepos += nbytes;
303 m->charpos += nchars;
307 /* Adjusting only markers whose insertion-type is t may result in
308 - disordered start and end in overlays, and
309 - disordered overlays in the slot `overlays_before' of current_buffer. */
310 if (adjusted)
312 fix_start_end_in_overlays (from, to);
313 fix_overlays_before (current_buffer, from, to);
317 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
319 This is used only when the value of point changes due to an insert
320 or delete; it does not represent a conceptual change in point as a
321 marker. In particular, point is not crossing any interval
322 boundaries, so there's no need to use the usual SET_PT macro. In
323 fact it would be incorrect to do so, because either the old or the
324 new value of point is out of sync with the current set of
325 intervals. */
327 static void
328 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
330 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
331 /* In a single-byte buffer, the two positions must be equal. */
332 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
335 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
336 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
337 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
338 an insertion. */
340 static void
341 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
342 ptrdiff_t old_chars, ptrdiff_t old_bytes,
343 ptrdiff_t new_chars, ptrdiff_t new_bytes)
345 register struct Lisp_Marker *m;
346 ptrdiff_t prev_to_byte = from_byte + old_bytes;
347 ptrdiff_t diff_chars = new_chars - old_chars;
348 ptrdiff_t diff_bytes = new_bytes - old_bytes;
350 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
352 if (m->bytepos >= prev_to_byte)
354 m->charpos += diff_chars;
355 m->bytepos += diff_bytes;
357 else if (m->bytepos > from_byte)
359 m->charpos = from;
360 m->bytepos = from_byte;
364 check_markers ();
368 void
369 buffer_overflow (void)
371 error ("Maximum buffer size exceeded");
374 /* Make the gap NBYTES_ADDED bytes longer. */
376 static void
377 make_gap_larger (ptrdiff_t nbytes_added)
379 Lisp_Object tem;
380 ptrdiff_t real_gap_loc;
381 ptrdiff_t real_gap_loc_byte;
382 ptrdiff_t old_gap_size;
383 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
385 if (BUF_BYTES_MAX - current_size < nbytes_added)
386 buffer_overflow ();
388 /* If we have to get more space, get enough to last a while;
389 but do not exceed the maximum buffer size. */
390 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
391 BUF_BYTES_MAX - current_size);
393 enlarge_buffer_text (current_buffer, nbytes_added);
395 /* Prevent quitting in move_gap. */
396 tem = Vinhibit_quit;
397 Vinhibit_quit = Qt;
399 real_gap_loc = GPT;
400 real_gap_loc_byte = GPT_BYTE;
401 old_gap_size = GAP_SIZE;
403 /* Call the newly allocated space a gap at the end of the whole space. */
404 GPT = Z + GAP_SIZE;
405 GPT_BYTE = Z_BYTE + GAP_SIZE;
406 GAP_SIZE = nbytes_added;
408 /* Move the new gap down to be consecutive with the end of the old one. */
409 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
411 /* Now combine the two into one large gap. */
412 GAP_SIZE += old_gap_size;
413 GPT = real_gap_loc;
414 GPT_BYTE = real_gap_loc_byte;
416 /* Put an anchor. */
417 *(Z_ADDR) = 0;
419 Vinhibit_quit = tem;
422 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
424 /* Make the gap NBYTES_REMOVED bytes shorter. */
426 static void
427 make_gap_smaller (ptrdiff_t nbytes_removed)
429 Lisp_Object tem;
430 ptrdiff_t real_gap_loc;
431 ptrdiff_t real_gap_loc_byte;
432 ptrdiff_t real_Z;
433 ptrdiff_t real_Z_byte;
434 ptrdiff_t real_beg_unchanged;
435 ptrdiff_t new_gap_size;
437 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
438 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
439 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
441 /* Prevent quitting in move_gap. */
442 tem = Vinhibit_quit;
443 Vinhibit_quit = Qt;
445 real_gap_loc = GPT;
446 real_gap_loc_byte = GPT_BYTE;
447 new_gap_size = GAP_SIZE - nbytes_removed;
448 real_Z = Z;
449 real_Z_byte = Z_BYTE;
450 real_beg_unchanged = BEG_UNCHANGED;
452 /* Pretend that the last unwanted part of the gap is the entire gap,
453 and that the first desired part of the gap is part of the buffer
454 text. */
455 memset (GPT_ADDR, 0, new_gap_size);
456 GPT += new_gap_size;
457 GPT_BYTE += new_gap_size;
458 Z += new_gap_size;
459 Z_BYTE += new_gap_size;
460 GAP_SIZE = nbytes_removed;
462 /* Move the unwanted pretend gap to the end of the buffer. */
463 gap_right (Z, Z_BYTE);
465 enlarge_buffer_text (current_buffer, -nbytes_removed);
467 /* Now restore the desired gap. */
468 GAP_SIZE = new_gap_size;
469 GPT = real_gap_loc;
470 GPT_BYTE = real_gap_loc_byte;
471 Z = real_Z;
472 Z_BYTE = real_Z_byte;
473 BEG_UNCHANGED = real_beg_unchanged;
475 /* Put an anchor. */
476 *(Z_ADDR) = 0;
478 Vinhibit_quit = tem;
481 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
483 void
484 make_gap (ptrdiff_t nbytes_added)
486 if (nbytes_added >= 0)
487 make_gap_larger (nbytes_added);
488 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
489 else
490 make_gap_smaller (-nbytes_added);
491 #endif
494 /* Add NBYTES to B's gap. It's enough to temporarily
495 fake current_buffer and avoid real switch to B. */
497 void
498 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
500 struct buffer *oldb = current_buffer;
502 current_buffer = b;
503 make_gap (nbytes);
504 current_buffer = oldb;
507 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
508 FROM_MULTIBYTE says whether the incoming text is multibyte.
509 TO_MULTIBYTE says whether to store the text as multibyte.
510 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
512 Return the number of bytes stored at TO_ADDR. */
514 ptrdiff_t
515 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
516 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
518 if (from_multibyte == to_multibyte)
520 memcpy (to_addr, from_addr, nbytes);
521 return nbytes;
523 else if (from_multibyte)
525 ptrdiff_t nchars = 0;
526 ptrdiff_t bytes_left = nbytes;
528 while (bytes_left > 0)
530 int thislen, c;
531 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
532 if (! ASCII_CHAR_P (c))
533 c &= 0xFF;
534 *to_addr++ = c;
535 from_addr += thislen;
536 bytes_left -= thislen;
537 nchars++;
539 return nchars;
541 else
543 unsigned char *initial_to_addr = to_addr;
545 /* Convert single-byte to multibyte. */
546 while (nbytes > 0)
548 int c = *from_addr++;
550 if (!ASCII_CHAR_P (c))
552 c = BYTE8_TO_CHAR (c);
553 to_addr += CHAR_STRING (c, to_addr);
554 nbytes--;
556 else
557 /* Special case for speed. */
558 *to_addr++ = c, nbytes--;
560 return to_addr - initial_to_addr;
564 /* Insert a string of specified length before point.
565 This function judges multibyteness based on
566 enable_multibyte_characters in the current buffer;
567 it never converts between single-byte and multibyte.
569 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
570 prepare_to_modify_buffer could relocate the text. */
572 void
573 insert (const char *string, ptrdiff_t nbytes)
575 if (nbytes > 0)
577 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
578 insert_1_both (string, len, nbytes, 0, 1, 0);
579 opoint = PT - len;
580 signal_after_change (opoint, 0, len);
581 update_compositions (opoint, PT, CHECK_BORDER);
585 /* Likewise, but inherit text properties from neighboring characters. */
587 void
588 insert_and_inherit (const char *string, ptrdiff_t nbytes)
590 if (nbytes > 0)
592 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
593 insert_1_both (string, len, nbytes, 1, 1, 0);
594 opoint = PT - len;
595 signal_after_change (opoint, 0, len);
596 update_compositions (opoint, PT, CHECK_BORDER);
600 /* Insert the character C before point. Do not inherit text properties. */
602 void
603 insert_char (int c)
605 unsigned char str[MAX_MULTIBYTE_LENGTH];
606 int len;
608 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
609 len = CHAR_STRING (c, str);
610 else
612 len = 1;
613 str[0] = c;
616 insert ((char *) str, len);
619 /* Insert the null-terminated string S before point. */
621 void
622 insert_string (const char *s)
624 insert (s, strlen (s));
627 /* Like `insert' except that all markers pointing at the place where
628 the insertion happens are adjusted to point after it.
629 Don't use this function to insert part of a Lisp string,
630 since gc could happen and relocate it. */
632 void
633 insert_before_markers (const char *string, ptrdiff_t nbytes)
635 if (nbytes > 0)
637 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
638 insert_1_both (string, len, nbytes, 0, 1, 1);
639 opoint = PT - len;
640 signal_after_change (opoint, 0, len);
641 update_compositions (opoint, PT, CHECK_BORDER);
645 /* Likewise, but inherit text properties from neighboring characters. */
647 void
648 insert_before_markers_and_inherit (const char *string,
649 ptrdiff_t nbytes)
651 if (nbytes > 0)
653 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
654 insert_1_both (string, len, nbytes, 1, 1, 1);
655 opoint = PT - len;
656 signal_after_change (opoint, 0, len);
657 update_compositions (opoint, PT, CHECK_BORDER);
661 /* Subroutine used by the insert functions above. */
663 void
664 insert_1 (const char *string, ptrdiff_t nbytes,
665 bool inherit, bool prepare, bool before_markers)
667 insert_1_both (string, chars_in_text ((unsigned char *) string, nbytes),
668 nbytes, inherit, prepare, before_markers);
672 #ifdef BYTE_COMBINING_DEBUG
674 /* See if the bytes before POS/POS_BYTE combine with bytes
675 at the start of STRING to form a single character.
676 If so, return the number of bytes at the start of STRING
677 which combine in this way. Otherwise, return 0. */
680 count_combining_before (const unsigned char *string, ptrdiff_t length,
681 ptrdiff_t pos, ptrdiff_t pos_byte)
683 int len, combining_bytes;
684 const unsigned char *p;
686 if (NILP (current_buffer->enable_multibyte_characters))
687 return 0;
689 /* At first, we can exclude the following cases:
690 (1) STRING[0] can't be a following byte of multibyte sequence.
691 (2) POS is the start of the current buffer.
692 (3) A character before POS is not a multibyte character. */
693 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
694 return 0;
695 if (pos_byte == BEG_BYTE) /* case (2) */
696 return 0;
697 len = 1;
698 p = BYTE_POS_ADDR (pos_byte - 1);
699 while (! CHAR_HEAD_P (*p)) p--, len++;
700 if (! LEADING_CODE_P (*p)) /* case (3) */
701 return 0;
703 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
704 if (combining_bytes <= 0)
705 /* The character preceding POS is, complete and no room for
706 combining bytes (combining_bytes == 0), or an independent 8-bit
707 character (combining_bytes < 0). */
708 return 0;
710 /* We have a combination situation. Count the bytes at STRING that
711 may combine. */
712 p = string + 1;
713 while (!CHAR_HEAD_P (*p) && p < string + length)
714 p++;
716 return (combining_bytes < p - string ? combining_bytes : p - string);
719 /* See if the bytes after POS/POS_BYTE combine with bytes
720 at the end of STRING to form a single character.
721 If so, return the number of bytes after POS/POS_BYTE
722 which combine in this way. Otherwise, return 0. */
725 count_combining_after (const unsigned char *string,
726 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
728 ptrdiff_t opos_byte = pos_byte;
729 ptrdiff_t i;
730 ptrdiff_t bytes;
731 unsigned char *bufp;
733 if (NILP (current_buffer->enable_multibyte_characters))
734 return 0;
736 /* At first, we can exclude the following cases:
737 (1) The last byte of STRING is an ASCII.
738 (2) POS is the last of the current buffer.
739 (3) A character at POS can't be a following byte of multibyte
740 character. */
741 if (length > 0 && ASCII_BYTE_P (string[length - 1])) /* case (1) */
742 return 0;
743 if (pos_byte == Z_BYTE) /* case (2) */
744 return 0;
745 bufp = BYTE_POS_ADDR (pos_byte);
746 if (CHAR_HEAD_P (*bufp)) /* case (3) */
747 return 0;
749 i = length - 1;
750 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
752 i--;
754 if (i < 0)
756 /* All characters in STRING are not character head. We must
757 check also preceding bytes at POS. We are sure that the gap
758 is at POS. */
759 unsigned char *p = BEG_ADDR;
760 i = pos_byte - 2;
761 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
762 i--;
763 if (i < 0 || !LEADING_CODE_P (p[i]))
764 return 0;
766 bytes = BYTES_BY_CHAR_HEAD (p[i]);
767 return (bytes <= pos_byte - 1 - i + length
769 : bytes - (pos_byte - 1 - i + length));
771 if (!LEADING_CODE_P (string[i]))
772 return 0;
774 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
775 bufp++, pos_byte++;
776 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
778 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
781 #endif
784 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
785 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
786 are the same as in insert_1. */
788 void
789 insert_1_both (const char *string,
790 ptrdiff_t nchars, ptrdiff_t nbytes,
791 bool inherit, bool prepare, bool before_markers)
793 if (nchars == 0)
794 return;
796 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
797 nchars = nbytes;
799 if (prepare)
800 /* Do this before moving and increasing the gap,
801 because the before-change hooks might move the gap
802 or make it smaller. */
803 prepare_to_modify_buffer (PT, PT, NULL);
805 if (PT != GPT)
806 move_gap_both (PT, PT_BYTE);
807 if (GAP_SIZE < nbytes)
808 make_gap (nbytes - GAP_SIZE);
810 #ifdef BYTE_COMBINING_DEBUG
811 if (count_combining_before (string, nbytes, PT, PT_BYTE)
812 || count_combining_after (string, nbytes, PT, PT_BYTE))
813 emacs_abort ();
814 #endif
816 /* Record deletion of the surrounding text that combines with
817 the insertion. This, together with recording the insertion,
818 will add up to the right stuff in the undo list. */
819 record_insert (PT, nchars);
820 MODIFF++;
821 CHARS_MODIFF = MODIFF;
823 memcpy (GPT_ADDR, string, nbytes);
825 GAP_SIZE -= nbytes;
826 GPT += nchars;
827 ZV += nchars;
828 Z += nchars;
829 GPT_BYTE += nbytes;
830 ZV_BYTE += nbytes;
831 Z_BYTE += nbytes;
832 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
834 eassert (GPT <= GPT_BYTE);
836 /* The insert may have been in the unchanged region, so check again. */
837 if (Z - GPT < END_UNCHANGED)
838 END_UNCHANGED = Z - GPT;
840 adjust_overlays_for_insert (PT, nchars);
841 adjust_markers_for_insert (PT, PT_BYTE,
842 PT + nchars, PT_BYTE + nbytes,
843 before_markers);
845 offset_intervals (current_buffer, PT, nchars);
847 if (!inherit && buffer_intervals (current_buffer))
848 set_text_properties (make_number (PT), make_number (PT + nchars),
849 Qnil, Qnil, Qnil);
851 adjust_point (nchars, nbytes);
853 check_markers ();
856 /* Insert the part of the text of STRING, a Lisp object assumed to be
857 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
858 starting at position POS / POS_BYTE. If the text of STRING has properties,
859 copy them into the buffer.
861 It does not work to use `insert' for this, because a GC could happen
862 before we copy the stuff into the buffer, and relocate the string
863 without insert noticing. */
865 void
866 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
867 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
869 ptrdiff_t opoint = PT;
871 if (SCHARS (string) == 0)
872 return;
874 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
875 inherit, 0);
876 signal_after_change (opoint, 0, PT - opoint);
877 update_compositions (opoint, PT, CHECK_BORDER);
880 /* Like `insert_from_string' except that all markers pointing
881 at the place where the insertion happens are adjusted to point after it. */
883 void
884 insert_from_string_before_markers (Lisp_Object string,
885 ptrdiff_t pos, ptrdiff_t pos_byte,
886 ptrdiff_t length, ptrdiff_t length_byte,
887 bool inherit)
889 ptrdiff_t opoint = PT;
891 if (SCHARS (string) == 0)
892 return;
894 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
895 inherit, 1);
896 signal_after_change (opoint, 0, PT - opoint);
897 update_compositions (opoint, PT, CHECK_BORDER);
900 /* Subroutine of the insertion functions above. */
902 static void
903 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
904 ptrdiff_t nchars, ptrdiff_t nbytes,
905 bool inherit, bool before_markers)
907 struct gcpro gcpro1;
908 ptrdiff_t outgoing_nbytes = nbytes;
909 INTERVAL intervals;
911 /* Make OUTGOING_NBYTES describe the text
912 as it will be inserted in this buffer. */
914 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
915 outgoing_nbytes = nchars;
916 else if (! STRING_MULTIBYTE (string))
917 outgoing_nbytes
918 = count_size_as_multibyte (SDATA (string) + pos_byte,
919 nbytes);
921 GCPRO1 (string);
922 /* Do this before moving and increasing the gap,
923 because the before-change hooks might move the gap
924 or make it smaller. */
925 prepare_to_modify_buffer (PT, PT, NULL);
927 if (PT != GPT)
928 move_gap_both (PT, PT_BYTE);
929 if (GAP_SIZE < outgoing_nbytes)
930 make_gap (outgoing_nbytes - GAP_SIZE);
931 UNGCPRO;
933 /* Copy the string text into the buffer, perhaps converting
934 between single-byte and multibyte. */
935 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
936 STRING_MULTIBYTE (string),
937 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
939 #ifdef BYTE_COMBINING_DEBUG
940 /* We have copied text into the gap, but we have not altered
941 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
942 to these functions and get the same results as we would
943 have got earlier on. Meanwhile, PT_ADDR does point to
944 the text that has been stored by copy_text. */
945 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
946 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
947 emacs_abort ();
948 #endif
950 record_insert (PT, nchars);
951 MODIFF++;
952 CHARS_MODIFF = MODIFF;
954 GAP_SIZE -= outgoing_nbytes;
955 GPT += nchars;
956 ZV += nchars;
957 Z += nchars;
958 GPT_BYTE += outgoing_nbytes;
959 ZV_BYTE += outgoing_nbytes;
960 Z_BYTE += outgoing_nbytes;
961 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
963 eassert (GPT <= GPT_BYTE);
965 /* The insert may have been in the unchanged region, so check again. */
966 if (Z - GPT < END_UNCHANGED)
967 END_UNCHANGED = Z - GPT;
969 adjust_overlays_for_insert (PT, nchars);
970 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
971 PT_BYTE + outgoing_nbytes,
972 before_markers);
974 offset_intervals (current_buffer, PT, nchars);
976 intervals = string_intervals (string);
977 /* Get the intervals for the part of the string we are inserting. */
978 if (nbytes < SBYTES (string))
979 intervals = copy_intervals (intervals, pos, nchars);
981 /* Insert those intervals. */
982 graft_intervals_into_buffer (intervals, PT, nchars,
983 current_buffer, inherit);
985 adjust_point (nchars, outgoing_nbytes);
987 check_markers ();
990 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
991 starting at GPT_ADDR. */
993 void
994 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes)
996 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
997 nchars = nbytes;
999 record_insert (GPT, nchars);
1000 MODIFF++;
1002 GAP_SIZE -= nbytes;
1003 GPT += nchars;
1004 ZV += nchars;
1005 Z += nchars;
1006 GPT_BYTE += nbytes;
1007 ZV_BYTE += nbytes;
1008 Z_BYTE += nbytes;
1009 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1011 eassert (GPT <= GPT_BYTE);
1013 adjust_overlays_for_insert (GPT - nchars, nchars);
1014 adjust_markers_for_insert (GPT - nchars, GPT_BYTE - nbytes,
1015 GPT, GPT_BYTE, 0);
1017 if (buffer_intervals (current_buffer))
1019 offset_intervals (current_buffer, GPT - nchars, nchars);
1020 graft_intervals_into_buffer (NULL, GPT - nchars, nchars,
1021 current_buffer, 0);
1024 if (GPT - nchars < PT)
1025 adjust_point (nchars, nbytes);
1027 check_markers ();
1030 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1031 current buffer. If the text in BUF has properties, they are absorbed
1032 into the current buffer.
1034 It does not work to use `insert' for this, because a malloc could happen
1035 and relocate BUF's text before the copy happens. */
1037 void
1038 insert_from_buffer (struct buffer *buf,
1039 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1041 ptrdiff_t opoint = PT;
1043 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1044 signal_after_change (opoint, 0, PT - opoint);
1045 update_compositions (opoint, PT, CHECK_BORDER);
1048 static void
1049 insert_from_buffer_1 (struct buffer *buf,
1050 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1052 ptrdiff_t chunk, chunk_expanded;
1053 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1054 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1055 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1056 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1057 INTERVAL intervals;
1059 /* Make OUTGOING_NBYTES describe the text
1060 as it will be inserted in this buffer. */
1062 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1063 outgoing_nbytes = nchars;
1064 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1066 ptrdiff_t outgoing_before_gap = 0;
1067 ptrdiff_t outgoing_after_gap = 0;
1069 if (from < BUF_GPT (buf))
1071 chunk = BUF_GPT_BYTE (buf) - from_byte;
1072 if (chunk > incoming_nbytes)
1073 chunk = incoming_nbytes;
1074 outgoing_before_gap
1075 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1076 chunk);
1078 else
1079 chunk = 0;
1081 if (chunk < incoming_nbytes)
1082 outgoing_after_gap
1083 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1084 from_byte + chunk),
1085 incoming_nbytes - chunk);
1087 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1090 /* Do this before moving and increasing the gap,
1091 because the before-change hooks might move the gap
1092 or make it smaller. */
1093 prepare_to_modify_buffer (PT, PT, NULL);
1095 if (PT != GPT)
1096 move_gap_both (PT, PT_BYTE);
1097 if (GAP_SIZE < outgoing_nbytes)
1098 make_gap (outgoing_nbytes - GAP_SIZE);
1100 if (from < BUF_GPT (buf))
1102 chunk = BUF_GPT_BYTE (buf) - from_byte;
1103 if (chunk > incoming_nbytes)
1104 chunk = incoming_nbytes;
1105 /* Record number of output bytes, so we know where
1106 to put the output from the second copy_text. */
1107 chunk_expanded
1108 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1109 GPT_ADDR, chunk,
1110 ! NILP (BVAR (buf, enable_multibyte_characters)),
1111 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1113 else
1114 chunk_expanded = chunk = 0;
1116 if (chunk < incoming_nbytes)
1117 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1118 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1119 ! NILP (BVAR (buf, enable_multibyte_characters)),
1120 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1122 #ifdef BYTE_COMBINING_DEBUG
1123 /* We have copied text into the gap, but we have not altered
1124 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1125 to these functions and get the same results as we would
1126 have got earlier on. Meanwhile, GPT_ADDR does point to
1127 the text that has been stored by copy_text. */
1128 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1129 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1130 emacs_abort ();
1131 #endif
1133 record_insert (PT, nchars);
1134 MODIFF++;
1135 CHARS_MODIFF = MODIFF;
1137 GAP_SIZE -= outgoing_nbytes;
1138 GPT += nchars;
1139 ZV += nchars;
1140 Z += nchars;
1141 GPT_BYTE += outgoing_nbytes;
1142 ZV_BYTE += outgoing_nbytes;
1143 Z_BYTE += outgoing_nbytes;
1144 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1146 eassert (GPT <= GPT_BYTE);
1148 /* The insert may have been in the unchanged region, so check again. */
1149 if (Z - GPT < END_UNCHANGED)
1150 END_UNCHANGED = Z - GPT;
1152 adjust_overlays_for_insert (PT, nchars);
1153 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1154 PT_BYTE + outgoing_nbytes,
1157 offset_intervals (current_buffer, PT, nchars);
1159 /* Get the intervals for the part of the string we are inserting. */
1160 intervals = buffer_intervals (buf);
1161 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1163 if (buf == current_buffer && PT <= from)
1164 from += nchars;
1165 intervals = copy_intervals (intervals, from, nchars);
1168 /* Insert those intervals. */
1169 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1171 adjust_point (nchars, outgoing_nbytes);
1174 /* Record undo information and adjust markers and position keepers for
1175 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1176 chars (LEN_BYTE bytes) which resides in the gap just after
1177 GPT_ADDR.
1179 PREV_TEXT nil means the new text was just inserted. */
1181 static void
1182 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1183 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1185 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1187 #ifdef BYTE_COMBINING_DEBUG
1188 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1189 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1190 emacs_abort ();
1191 #endif
1193 if (STRINGP (prev_text))
1195 nchars_del = SCHARS (prev_text);
1196 nbytes_del = SBYTES (prev_text);
1199 /* Update various buffer positions for the new text. */
1200 GAP_SIZE -= len_byte;
1201 ZV += len; Z+= len;
1202 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1203 GPT += len; GPT_BYTE += len_byte;
1204 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1206 if (nchars_del > 0)
1207 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1208 len, len_byte);
1209 else
1210 adjust_markers_for_insert (from, from_byte,
1211 from + len, from_byte + len_byte, 0);
1213 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1215 if (nchars_del > 0)
1216 record_delete (from, prev_text);
1217 record_insert (from, len);
1220 if (len > nchars_del)
1221 adjust_overlays_for_insert (from, len - nchars_del);
1222 else if (len < nchars_del)
1223 adjust_overlays_for_delete (from, nchars_del - len);
1225 offset_intervals (current_buffer, from, len - nchars_del);
1227 if (from < PT)
1228 adjust_point (len - nchars_del, len_byte - nbytes_del);
1230 /* As byte combining will decrease Z, we must check this again. */
1231 if (Z - GPT < END_UNCHANGED)
1232 END_UNCHANGED = Z - GPT;
1234 check_markers ();
1236 if (len == 0)
1237 evaporate_overlays (from);
1238 MODIFF++;
1239 CHARS_MODIFF = MODIFF;
1242 /* Record undo information, adjust markers and position keepers for an
1243 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1244 text already exists in the current buffer but character length (TO
1245 - FROM) may be incorrect, the correct length is NEWLEN. */
1247 void
1248 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1249 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1251 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1253 if (GPT != to)
1254 move_gap_both (to, to_byte);
1255 GAP_SIZE += len_byte;
1256 GPT -= len; GPT_BYTE -= len_byte;
1257 ZV -= len; ZV_BYTE -= len_byte;
1258 Z -= len; Z_BYTE -= len_byte;
1259 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1262 /* Replace the text from character positions FROM to TO with NEW,
1263 If PREPARE, call prepare_to_modify_buffer.
1264 If INHERIT, the newly inserted text should inherit text properties
1265 from the surrounding non-deleted text. */
1267 /* Note that this does not yet handle markers quite right.
1268 Also it needs to record a single undo-entry that does a replacement
1269 rather than a separate delete and insert.
1270 That way, undo will also handle markers properly.
1272 But if MARKERS is 0, don't relocate markers. */
1274 void
1275 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1276 bool prepare, bool inherit, bool markers)
1278 ptrdiff_t inschars = SCHARS (new);
1279 ptrdiff_t insbytes = SBYTES (new);
1280 ptrdiff_t from_byte, to_byte;
1281 ptrdiff_t nbytes_del, nchars_del;
1282 struct gcpro gcpro1;
1283 INTERVAL intervals;
1284 ptrdiff_t outgoing_insbytes = insbytes;
1285 Lisp_Object deletion;
1287 check_markers ();
1289 GCPRO1 (new);
1290 deletion = Qnil;
1292 if (prepare)
1294 ptrdiff_t range_length = to - from;
1295 prepare_to_modify_buffer (from, to, &from);
1296 to = from + range_length;
1299 UNGCPRO;
1301 /* Make args be valid. */
1302 if (from < BEGV)
1303 from = BEGV;
1304 if (to > ZV)
1305 to = ZV;
1307 from_byte = CHAR_TO_BYTE (from);
1308 to_byte = CHAR_TO_BYTE (to);
1310 nchars_del = to - from;
1311 nbytes_del = to_byte - from_byte;
1313 if (nbytes_del <= 0 && insbytes == 0)
1314 return;
1316 /* Make OUTGOING_INSBYTES describe the text
1317 as it will be inserted in this buffer. */
1319 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1320 outgoing_insbytes = inschars;
1321 else if (! STRING_MULTIBYTE (new))
1322 outgoing_insbytes
1323 = count_size_as_multibyte (SDATA (new), insbytes);
1325 GCPRO1 (new);
1327 /* Make sure the gap is somewhere in or next to what we are deleting. */
1328 if (from > GPT)
1329 gap_right (from, from_byte);
1330 if (to < GPT)
1331 gap_left (to, to_byte, 0);
1333 /* Even if we don't record for undo, we must keep the original text
1334 because we may have to recover it because of inappropriate byte
1335 combining. */
1336 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1337 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1339 GAP_SIZE += nbytes_del;
1340 ZV -= nchars_del;
1341 Z -= nchars_del;
1342 ZV_BYTE -= nbytes_del;
1343 Z_BYTE -= nbytes_del;
1344 GPT = from;
1345 GPT_BYTE = from_byte;
1346 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1348 eassert (GPT <= GPT_BYTE);
1350 if (GPT - BEG < BEG_UNCHANGED)
1351 BEG_UNCHANGED = GPT - BEG;
1352 if (Z - GPT < END_UNCHANGED)
1353 END_UNCHANGED = Z - GPT;
1355 if (GAP_SIZE < outgoing_insbytes)
1356 make_gap (outgoing_insbytes - GAP_SIZE);
1358 /* Copy the string text into the buffer, perhaps converting
1359 between single-byte and multibyte. */
1360 copy_text (SDATA (new), GPT_ADDR, insbytes,
1361 STRING_MULTIBYTE (new),
1362 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1364 #ifdef BYTE_COMBINING_DEBUG
1365 /* We have copied text into the gap, but we have not marked
1366 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1367 here, for both the previous text and the following text.
1368 Meanwhile, GPT_ADDR does point to
1369 the text that has been stored by copy_text. */
1370 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1371 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1372 emacs_abort ();
1373 #endif
1375 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1377 /* Record the insertion first, so that when we undo,
1378 the deletion will be undone first. Thus, undo
1379 will insert before deleting, and thus will keep
1380 the markers before and after this text separate. */
1381 record_insert (from + SCHARS (deletion), inschars);
1382 record_delete (from, deletion);
1385 GAP_SIZE -= outgoing_insbytes;
1386 GPT += inschars;
1387 ZV += inschars;
1388 Z += inschars;
1389 GPT_BYTE += outgoing_insbytes;
1390 ZV_BYTE += outgoing_insbytes;
1391 Z_BYTE += outgoing_insbytes;
1392 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1394 eassert (GPT <= GPT_BYTE);
1396 /* Adjust markers for the deletion and the insertion. */
1397 if (markers)
1398 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1399 inschars, outgoing_insbytes);
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 offset_intervals (current_buffer, from, inschars - nchars_del);
1408 /* Get the intervals for the part of the string we are inserting--
1409 not including the combined-before bytes. */
1410 intervals = string_intervals (new);
1411 /* Insert those intervals. */
1412 graft_intervals_into_buffer (intervals, from, inschars,
1413 current_buffer, inherit);
1415 /* Relocate point as if it were a marker. */
1416 if (from < PT)
1417 adjust_point ((from + inschars - (PT < to ? PT : to)),
1418 (from_byte + outgoing_insbytes
1419 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1421 if (outgoing_insbytes == 0)
1422 evaporate_overlays (from);
1424 check_markers ();
1426 MODIFF++;
1427 CHARS_MODIFF = MODIFF;
1428 UNGCPRO;
1430 signal_after_change (from, nchars_del, GPT - from);
1431 update_compositions (from, GPT, CHECK_BORDER);
1434 /* Replace the text from character positions FROM to TO with
1435 the text in INS of length INSCHARS.
1436 Keep the text properties that applied to the old characters
1437 (extending them to all the new chars if there are more new chars).
1439 Note that this does not yet handle markers quite right.
1441 If MARKERS, relocate markers.
1443 Unlike most functions at this level, never call
1444 prepare_to_modify_buffer and never call signal_after_change. */
1446 void
1447 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1448 ptrdiff_t to, ptrdiff_t to_byte,
1449 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1450 bool markers)
1452 ptrdiff_t nbytes_del, nchars_del;
1454 check_markers ();
1456 nchars_del = to - from;
1457 nbytes_del = to_byte - from_byte;
1459 if (nbytes_del <= 0 && insbytes == 0)
1460 return;
1462 /* Make sure the gap is somewhere in or next to what we are deleting. */
1463 if (from > GPT)
1464 gap_right (from, from_byte);
1465 if (to < GPT)
1466 gap_left (to, to_byte, 0);
1468 GAP_SIZE += nbytes_del;
1469 ZV -= nchars_del;
1470 Z -= nchars_del;
1471 ZV_BYTE -= nbytes_del;
1472 Z_BYTE -= nbytes_del;
1473 GPT = from;
1474 GPT_BYTE = from_byte;
1475 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1477 eassert (GPT <= GPT_BYTE);
1479 if (GPT - BEG < BEG_UNCHANGED)
1480 BEG_UNCHANGED = GPT - BEG;
1481 if (Z - GPT < END_UNCHANGED)
1482 END_UNCHANGED = Z - GPT;
1484 if (GAP_SIZE < insbytes)
1485 make_gap (insbytes - GAP_SIZE);
1487 /* Copy the replacement text into the buffer. */
1488 memcpy (GPT_ADDR, ins, insbytes);
1490 #ifdef BYTE_COMBINING_DEBUG
1491 /* We have copied text into the gap, but we have not marked
1492 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1493 here, for both the previous text and the following text.
1494 Meanwhile, GPT_ADDR does point to
1495 the text that has been stored by copy_text. */
1496 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1497 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1498 emacs_abort ();
1499 #endif
1501 GAP_SIZE -= insbytes;
1502 GPT += inschars;
1503 ZV += inschars;
1504 Z += inschars;
1505 GPT_BYTE += insbytes;
1506 ZV_BYTE += insbytes;
1507 Z_BYTE += insbytes;
1508 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1510 eassert (GPT <= GPT_BYTE);
1512 /* Adjust markers for the deletion and the insertion. */
1513 if (markers
1514 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1515 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1516 inschars, insbytes);
1518 /* Adjust the overlay center as needed. This must be done after
1519 adjusting the markers that bound the overlays. */
1520 if (nchars_del != inschars)
1522 adjust_overlays_for_insert (from, inschars);
1523 adjust_overlays_for_delete (from + inschars, nchars_del);
1526 offset_intervals (current_buffer, from, inschars - nchars_del);
1528 /* Relocate point as if it were a marker. */
1529 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1531 if (PT < to)
1532 /* PT was within the deleted text. Move it to FROM. */
1533 adjust_point (from - PT, from_byte - PT_BYTE);
1534 else
1535 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1538 if (insbytes == 0)
1539 evaporate_overlays (from);
1541 check_markers ();
1543 MODIFF++;
1544 CHARS_MODIFF = MODIFF;
1547 /* Delete characters in current buffer
1548 from FROM up to (but not including) TO.
1549 If TO comes before FROM, we delete nothing. */
1551 void
1552 del_range (ptrdiff_t from, ptrdiff_t to)
1554 del_range_1 (from, to, 1, 0);
1557 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1558 RET_STRING says to return the deleted text. */
1560 Lisp_Object
1561 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1563 ptrdiff_t from_byte, to_byte;
1564 Lisp_Object deletion;
1565 struct gcpro gcpro1;
1567 /* Make args be valid */
1568 if (from < BEGV)
1569 from = BEGV;
1570 if (to > ZV)
1571 to = ZV;
1573 if (to <= from)
1574 return Qnil;
1576 if (prepare)
1578 ptrdiff_t range_length = to - from;
1579 prepare_to_modify_buffer (from, to, &from);
1580 to = min (ZV, from + range_length);
1583 from_byte = CHAR_TO_BYTE (from);
1584 to_byte = CHAR_TO_BYTE (to);
1586 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1587 GCPRO1 (deletion);
1588 signal_after_change (from, to - from, 0);
1589 update_compositions (from, from, CHECK_HEAD);
1590 UNGCPRO;
1591 return deletion;
1594 /* Like del_range_1 but args are byte positions, not char positions. */
1596 void
1597 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
1599 ptrdiff_t from, to;
1601 /* Make args be valid */
1602 if (from_byte < BEGV_BYTE)
1603 from_byte = BEGV_BYTE;
1604 if (to_byte > ZV_BYTE)
1605 to_byte = ZV_BYTE;
1607 if (to_byte <= from_byte)
1608 return;
1610 from = BYTE_TO_CHAR (from_byte);
1611 to = BYTE_TO_CHAR (to_byte);
1613 if (prepare)
1615 ptrdiff_t old_from = from, old_to = Z - to;
1616 ptrdiff_t range_length = to - from;
1617 prepare_to_modify_buffer (from, to, &from);
1618 to = from + range_length;
1620 if (old_from != from)
1621 from_byte = CHAR_TO_BYTE (from);
1622 if (to > ZV)
1624 to = ZV;
1625 to_byte = ZV_BYTE;
1627 else if (old_to == Z - to)
1628 to_byte = CHAR_TO_BYTE (to);
1631 del_range_2 (from, from_byte, to, to_byte, 0);
1632 signal_after_change (from, to - from, 0);
1633 update_compositions (from, from, CHECK_HEAD);
1636 /* Like del_range_1, but positions are specified both as charpos
1637 and bytepos. */
1639 void
1640 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1641 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1643 /* Make args be valid */
1644 if (from_byte < BEGV_BYTE)
1645 from_byte = BEGV_BYTE;
1646 if (to_byte > ZV_BYTE)
1647 to_byte = ZV_BYTE;
1649 if (to_byte <= from_byte)
1650 return;
1652 if (from < BEGV)
1653 from = BEGV;
1654 if (to > ZV)
1655 to = ZV;
1657 if (prepare)
1659 ptrdiff_t old_from = from, old_to = Z - to;
1660 ptrdiff_t range_length = to - from;
1661 prepare_to_modify_buffer (from, to, &from);
1662 to = from + range_length;
1664 if (old_from != from)
1665 from_byte = CHAR_TO_BYTE (from);
1666 if (to > ZV)
1668 to = ZV;
1669 to_byte = ZV_BYTE;
1671 else if (old_to == Z - to)
1672 to_byte = CHAR_TO_BYTE (to);
1675 del_range_2 (from, from_byte, to, to_byte, 0);
1676 signal_after_change (from, to - from, 0);
1677 update_compositions (from, from, CHECK_HEAD);
1680 /* Delete a range of text, specified both as character positions
1681 and byte positions. FROM and TO are character positions,
1682 while FROM_BYTE and TO_BYTE are byte positions.
1683 If RET_STRING, the deleted area is returned as a string. */
1685 Lisp_Object
1686 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1687 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1689 ptrdiff_t nbytes_del, nchars_del;
1690 Lisp_Object deletion;
1692 check_markers ();
1694 nchars_del = to - from;
1695 nbytes_del = to_byte - from_byte;
1697 /* Make sure the gap is somewhere in or next to what we are deleting. */
1698 if (from > GPT)
1699 gap_right (from, from_byte);
1700 if (to < GPT)
1701 gap_left (to, to_byte, 0);
1703 #ifdef BYTE_COMBINING_DEBUG
1704 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1705 Z_BYTE - to_byte, from, from_byte))
1706 emacs_abort ();
1707 #endif
1709 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1710 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1711 else
1712 deletion = Qnil;
1714 /* Relocate all markers pointing into the new, larger gap
1715 to point at the end of the text before the gap.
1716 Do this before recording the deletion,
1717 so that undo handles this after reinserting the text. */
1718 adjust_markers_for_delete (from, from_byte, to, to_byte);
1720 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1721 record_delete (from, deletion);
1722 MODIFF++;
1723 CHARS_MODIFF = MODIFF;
1725 /* Relocate point as if it were a marker. */
1726 if (from < PT)
1727 adjust_point (from - (PT < to ? PT : to),
1728 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1730 offset_intervals (current_buffer, from, - nchars_del);
1732 /* Adjust the overlay center as needed. This must be done after
1733 adjusting the markers that bound the overlays. */
1734 adjust_overlays_for_delete (from, nchars_del);
1736 GAP_SIZE += nbytes_del;
1737 ZV_BYTE -= nbytes_del;
1738 Z_BYTE -= nbytes_del;
1739 ZV -= nchars_del;
1740 Z -= nchars_del;
1741 GPT = from;
1742 GPT_BYTE = from_byte;
1743 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1744 /* Put an anchor, unless called from decode_coding_object which
1745 needs to access the previous gap contents. */
1746 *(GPT_ADDR) = 0;
1748 eassert (GPT <= GPT_BYTE);
1750 if (GPT - BEG < BEG_UNCHANGED)
1751 BEG_UNCHANGED = GPT - BEG;
1752 if (Z - GPT < END_UNCHANGED)
1753 END_UNCHANGED = Z - GPT;
1755 check_markers ();
1757 evaporate_overlays (from);
1759 return deletion;
1762 /* Call this if you're about to change the region of current buffer
1763 from character positions START to END. This checks the read-only
1764 properties of the region, calls the necessary modification hooks,
1765 and warns the next redisplay that it should pay attention to that
1766 area.
1768 If PRESERVE_CHARS_MODIFF, do not update CHARS_MODIFF.
1769 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1771 void
1772 modify_region_1 (ptrdiff_t start, ptrdiff_t end, bool preserve_chars_modiff)
1774 prepare_to_modify_buffer (start, end, NULL);
1776 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1778 if (MODIFF <= SAVE_MODIFF)
1779 record_first_change ();
1780 MODIFF++;
1781 if (! preserve_chars_modiff)
1782 CHARS_MODIFF = MODIFF;
1784 bset_point_before_scroll (current_buffer, Qnil);
1787 /* Check that it is okay to modify the buffer between START and END,
1788 which are char positions.
1790 Run the before-change-function, if any. If intervals are in use,
1791 verify that the text to be modified is not read-only, and call
1792 any modification properties the text may have.
1794 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1795 by holding its value temporarily in a marker. */
1797 void
1798 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1799 ptrdiff_t *preserve_ptr)
1801 struct buffer *base_buffer;
1803 if (!NILP (BVAR (current_buffer, read_only)))
1804 Fbarf_if_buffer_read_only ();
1806 /* If we're modifying the buffer other than shown in a selected window,
1807 let redisplay consider other windows if this buffer is visible or
1808 hidden (although hidden buffers have zero window counts, their state
1809 may affect the display too, e.g. via mode lines of other buffers). */
1810 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer
1811 && (buffer_window_count (current_buffer)
1812 || BUFFER_HIDDEN_P (current_buffer)))
1813 ++windows_or_buffers_changed;
1815 if (buffer_intervals (current_buffer))
1817 if (preserve_ptr)
1819 Lisp_Object preserve_marker;
1820 struct gcpro gcpro1;
1821 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1822 GCPRO1 (preserve_marker);
1823 verify_interval_modification (current_buffer, start, end);
1824 *preserve_ptr = marker_position (preserve_marker);
1825 unchain_marker (XMARKER (preserve_marker));
1826 UNGCPRO;
1828 else
1829 verify_interval_modification (current_buffer, start, end);
1832 /* For indirect buffers, use the base buffer to check clashes. */
1833 if (current_buffer->base_buffer != 0)
1834 base_buffer = current_buffer->base_buffer;
1835 else
1836 base_buffer = current_buffer;
1838 #ifdef CLASH_DETECTION
1839 if (!NILP (BVAR (base_buffer, file_truename))
1840 /* Make binding buffer-file-name to nil effective. */
1841 && !NILP (BVAR (base_buffer, filename))
1842 && SAVE_MODIFF >= MODIFF)
1843 lock_file (BVAR (base_buffer, file_truename));
1844 #else
1845 /* At least warn if this file has changed on disk since it was visited. */
1846 if (!NILP (BVAR (base_buffer, filename))
1847 && SAVE_MODIFF >= MODIFF
1848 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1849 && !NILP (Ffile_exists_p (BVAR (base_buffer, filename))))
1850 call1 (intern ("ask-user-about-supersession-threat"),
1851 BVAR (base_buffer,filename));
1852 #endif /* not CLASH_DETECTION */
1854 /* If `select-active-regions' is non-nil, save the region text. */
1855 if (!NILP (BVAR (current_buffer, mark_active))
1856 && !inhibit_modification_hooks
1857 && XMARKER (BVAR (current_buffer, mark))->buffer
1858 && NILP (Vsaved_region_selection)
1859 && (EQ (Vselect_active_regions, Qonly)
1860 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1861 : (!NILP (Vselect_active_regions)
1862 && !NILP (Vtransient_mark_mode))))
1864 ptrdiff_t b = marker_position (BVAR (current_buffer, mark));
1865 ptrdiff_t e = PT;
1866 if (b < e)
1867 Vsaved_region_selection = make_buffer_string (b, e, 0);
1868 else if (b > e)
1869 Vsaved_region_selection = make_buffer_string (e, b, 0);
1872 signal_before_change (start, end, preserve_ptr);
1874 if (current_buffer->newline_cache)
1875 invalidate_region_cache (current_buffer,
1876 current_buffer->newline_cache,
1877 start - BEG, Z - end);
1878 if (current_buffer->width_run_cache)
1879 invalidate_region_cache (current_buffer,
1880 current_buffer->width_run_cache,
1881 start - BEG, Z - end);
1883 Vdeactivate_mark = Qt;
1886 /* These macros work with an argument named `preserve_ptr'
1887 and a local variable named `preserve_marker'. */
1889 #define PRESERVE_VALUE \
1890 if (preserve_ptr && NILP (preserve_marker)) \
1891 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1893 #define RESTORE_VALUE \
1894 if (! NILP (preserve_marker)) \
1896 *preserve_ptr = marker_position (preserve_marker); \
1897 unchain_marker (XMARKER (preserve_marker)); \
1900 #define PRESERVE_START_END \
1901 if (NILP (start_marker)) \
1902 start_marker = Fcopy_marker (start, Qnil); \
1903 if (NILP (end_marker)) \
1904 end_marker = Fcopy_marker (end, Qnil);
1906 #define FETCH_START \
1907 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1909 #define FETCH_END \
1910 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1912 /* Set a variable to nil if an error occurred.
1913 Don't change the variable if there was no error.
1914 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1915 VARIABLE is the variable to maybe set to nil.
1916 NO-ERROR-FLAG is nil if there was an error,
1917 anything else meaning no error (so this function does nothing). */
1918 static Lisp_Object
1919 reset_var_on_error (Lisp_Object val)
1921 if (NILP (XCDR (val)))
1922 Fset (XCAR (val), Qnil);
1923 return Qnil;
1926 /* Signal a change to the buffer immediately before it happens.
1927 START_INT and END_INT are the bounds of the text to be changed.
1929 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1930 by holding its value temporarily in a marker. */
1932 static void
1933 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1934 ptrdiff_t *preserve_ptr)
1936 Lisp_Object start, end;
1937 Lisp_Object start_marker, end_marker;
1938 Lisp_Object preserve_marker;
1939 struct gcpro gcpro1, gcpro2, gcpro3;
1940 ptrdiff_t count = SPECPDL_INDEX ();
1942 if (inhibit_modification_hooks)
1943 return;
1945 start = make_number (start_int);
1946 end = make_number (end_int);
1947 preserve_marker = Qnil;
1948 start_marker = Qnil;
1949 end_marker = Qnil;
1950 GCPRO3 (preserve_marker, start_marker, end_marker);
1952 specbind (Qinhibit_modification_hooks, Qt);
1954 /* If buffer is unmodified, run a special hook for that case. The
1955 check for Vfirst_change_hook is just a minor optimization. */
1956 if (SAVE_MODIFF >= MODIFF
1957 && !NILP (Vfirst_change_hook))
1959 PRESERVE_VALUE;
1960 PRESERVE_START_END;
1961 Frun_hooks (1, &Qfirst_change_hook);
1964 /* Now run the before-change-functions if any. */
1965 if (!NILP (Vbefore_change_functions))
1967 Lisp_Object args[3];
1968 Lisp_Object rvoe_arg = Fcons (Qbefore_change_functions, Qnil);
1970 PRESERVE_VALUE;
1971 PRESERVE_START_END;
1973 /* Mark before-change-functions to be reset to nil in case of error. */
1974 record_unwind_protect (reset_var_on_error, rvoe_arg);
1976 /* Actually run the hook functions. */
1977 args[0] = Qbefore_change_functions;
1978 args[1] = FETCH_START;
1979 args[2] = FETCH_END;
1980 Frun_hook_with_args (3, args);
1982 /* There was no error: unarm the reset_on_error. */
1983 XSETCDR (rvoe_arg, Qt);
1986 if (buffer_has_overlays ())
1988 PRESERVE_VALUE;
1989 report_overlay_modification (FETCH_START, FETCH_END, 0,
1990 FETCH_START, FETCH_END, Qnil);
1993 if (! NILP (start_marker))
1994 free_marker (start_marker);
1995 if (! NILP (end_marker))
1996 free_marker (end_marker);
1997 RESTORE_VALUE;
1998 UNGCPRO;
2000 unbind_to (count, Qnil);
2003 /* Signal a change immediately after it happens.
2004 CHARPOS is the character position of the start of the changed text.
2005 LENDEL is the number of characters of the text before the change.
2006 (Not the whole buffer; just the part that was changed.)
2007 LENINS is the number of characters in that part of the text
2008 after the change. */
2010 void
2011 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2013 ptrdiff_t count = SPECPDL_INDEX ();
2014 if (inhibit_modification_hooks)
2015 return;
2017 /* If we are deferring calls to the after-change functions
2018 and there are no before-change functions,
2019 just record the args that we were going to use. */
2020 if (! NILP (Vcombine_after_change_calls)
2021 && NILP (Vbefore_change_functions)
2022 && !buffer_has_overlays ())
2024 Lisp_Object elt;
2026 if (!NILP (combine_after_change_list)
2027 && current_buffer != XBUFFER (combine_after_change_buffer))
2028 Fcombine_after_change_execute ();
2030 elt = Fcons (make_number (charpos - BEG),
2031 Fcons (make_number (Z - (charpos - lendel + lenins)),
2032 Fcons (make_number (lenins - lendel), Qnil)));
2033 combine_after_change_list
2034 = Fcons (elt, combine_after_change_list);
2035 combine_after_change_buffer = Fcurrent_buffer ();
2037 return;
2040 if (!NILP (combine_after_change_list))
2041 Fcombine_after_change_execute ();
2043 specbind (Qinhibit_modification_hooks, Qt);
2045 if (!NILP (Vafter_change_functions))
2047 Lisp_Object args[4];
2048 Lisp_Object rvoe_arg = Fcons (Qafter_change_functions, Qnil);
2050 /* Mark after-change-functions to be reset to nil in case of error. */
2051 record_unwind_protect (reset_var_on_error, rvoe_arg);
2053 /* Actually run the hook functions. */
2054 args[0] = Qafter_change_functions;
2055 XSETFASTINT (args[1], charpos);
2056 XSETFASTINT (args[2], charpos + lenins);
2057 XSETFASTINT (args[3], lendel);
2058 Frun_hook_with_args (4, args);
2060 /* There was no error: unarm the reset_on_error. */
2061 XSETCDR (rvoe_arg, Qt);
2064 if (buffer_has_overlays ())
2065 report_overlay_modification (make_number (charpos),
2066 make_number (charpos + lenins),
2068 make_number (charpos),
2069 make_number (charpos + lenins),
2070 make_number (lendel));
2072 /* After an insertion, call the text properties
2073 insert-behind-hooks or insert-in-front-hooks. */
2074 if (lendel == 0)
2075 report_interval_modification (make_number (charpos),
2076 make_number (charpos + lenins));
2078 unbind_to (count, Qnil);
2081 static Lisp_Object
2082 Fcombine_after_change_execute_1 (Lisp_Object val)
2084 Vcombine_after_change_calls = val;
2085 return val;
2088 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2089 Scombine_after_change_execute, 0, 0, 0,
2090 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2091 (void)
2093 ptrdiff_t count = SPECPDL_INDEX ();
2094 ptrdiff_t beg, end, change;
2095 ptrdiff_t begpos, endpos;
2096 Lisp_Object tail;
2098 if (NILP (combine_after_change_list))
2099 return Qnil;
2101 /* It is rare for combine_after_change_buffer to be invalid, but
2102 possible. It can happen when combine-after-change-calls is
2103 non-nil, and insertion calls a file handler (e.g. through
2104 lock_file) which scribbles into a temp file -- cyd */
2105 if (!BUFFERP (combine_after_change_buffer)
2106 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2108 combine_after_change_list = Qnil;
2109 return Qnil;
2112 record_unwind_current_buffer ();
2114 Fset_buffer (combine_after_change_buffer);
2116 /* # chars unchanged at beginning of buffer. */
2117 beg = Z - BEG;
2118 /* # chars unchanged at end of buffer. */
2119 end = beg;
2120 /* Total amount of insertion (negative for deletion). */
2121 change = 0;
2123 /* Scan the various individual changes,
2124 accumulating the range info in BEG, END and CHANGE. */
2125 for (tail = combine_after_change_list; CONSP (tail);
2126 tail = XCDR (tail))
2128 Lisp_Object elt;
2129 ptrdiff_t thisbeg, thisend, thischange;
2131 /* Extract the info from the next element. */
2132 elt = XCAR (tail);
2133 if (! CONSP (elt))
2134 continue;
2135 thisbeg = XINT (XCAR (elt));
2137 elt = XCDR (elt);
2138 if (! CONSP (elt))
2139 continue;
2140 thisend = XINT (XCAR (elt));
2142 elt = XCDR (elt);
2143 if (! CONSP (elt))
2144 continue;
2145 thischange = XINT (XCAR (elt));
2147 /* Merge this range into the accumulated range. */
2148 change += thischange;
2149 if (thisbeg < beg)
2150 beg = thisbeg;
2151 if (thisend < end)
2152 end = thisend;
2155 /* Get the current start and end positions of the range
2156 that was changed. */
2157 begpos = BEG + beg;
2158 endpos = Z - end;
2160 /* We are about to handle these, so discard them. */
2161 combine_after_change_list = Qnil;
2163 /* Now run the after-change functions for real.
2164 Turn off the flag that defers them. */
2165 record_unwind_protect (Fcombine_after_change_execute_1,
2166 Vcombine_after_change_calls);
2167 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2168 update_compositions (begpos, endpos, CHECK_ALL);
2170 return unbind_to (count, Qnil);
2173 void
2174 syms_of_insdel (void)
2176 staticpro (&combine_after_change_list);
2177 staticpro (&combine_after_change_buffer);
2178 combine_after_change_list = Qnil;
2179 combine_after_change_buffer = Qnil;
2181 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2182 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2183 Vcombine_after_change_calls = Qnil;
2185 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2186 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2187 This affects `before-change-functions' and `after-change-functions',
2188 as well as hooks attached to text properties and overlays. */);
2189 inhibit_modification_hooks = 0;
2190 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2192 defsubr (&Scombine_after_change_execute);