Allow to disable compaction of font caches
[emacs.git] / src / insdel.c
blobfc3f19fd581426a08150e34d3c903f61bb02f1c0
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2016 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 (at
10 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 "composite.h"
27 #include "intervals.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "window.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 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
56 /* Also used in marker.c to enable expensive marker checks. */
58 #ifdef MARKER_DEBUG
60 static void
61 check_markers (void)
63 struct Lisp_Marker *tail;
64 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
66 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
68 if (tail->buffer->text != current_buffer->text)
69 emacs_abort ();
70 if (tail->charpos > Z)
71 emacs_abort ();
72 if (tail->bytepos > Z_BYTE)
73 emacs_abort ();
74 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
75 emacs_abort ();
79 #else /* not MARKER_DEBUG */
81 #define check_markers() do { } while (0)
83 #endif /* MARKER_DEBUG */
85 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
86 Note that this can quit! */
88 void
89 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
91 eassert (charpos == BYTE_TO_CHAR (bytepos)
92 && bytepos == CHAR_TO_BYTE (charpos));
93 if (bytepos < GPT_BYTE)
94 gap_left (charpos, bytepos, 0);
95 else if (bytepos > GPT_BYTE)
96 gap_right (charpos, bytepos);
99 /* Move the gap to a position less than the current GPT.
100 BYTEPOS describes the new position as a byte position,
101 and CHARPOS is the corresponding char position.
102 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
104 static void
105 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
107 unsigned char *to, *from;
108 ptrdiff_t i;
109 ptrdiff_t new_s1;
111 if (!newgap)
112 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
114 i = GPT_BYTE;
115 to = GAP_END_ADDR;
116 from = GPT_ADDR;
117 new_s1 = GPT_BYTE;
119 /* Now copy the characters. To move the gap down,
120 copy characters up. */
122 while (1)
124 /* I gets number of characters left to copy. */
125 i = new_s1 - bytepos;
126 if (i == 0)
127 break;
128 /* If a quit is requested, stop copying now.
129 Change BYTEPOS to be where we have actually moved the gap to.
130 Note that this cannot happen when we are called to make the
131 gap larger or smaller, since make_gap_larger and
132 make_gap_smaller prevent QUIT by setting inhibit-quit. */
133 if (QUITP)
135 bytepos = new_s1;
136 charpos = BYTE_TO_CHAR (bytepos);
137 break;
139 /* Move at most 32000 chars before checking again for a quit. */
140 if (i > 32000)
141 i = 32000;
142 new_s1 -= i;
143 from -= i, to -= i;
144 memmove (to, from, i);
147 /* Adjust buffer data structure, to put the gap at BYTEPOS.
148 BYTEPOS is where the loop above stopped, which may be what
149 was specified or may be where a quit was detected. */
150 GPT_BYTE = bytepos;
151 GPT = charpos;
152 eassert (charpos <= bytepos);
153 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
154 QUIT;
157 /* Move the gap to a position greater than the current GPT.
158 BYTEPOS describes the new position as a byte position,
159 and CHARPOS is the corresponding char position. */
161 static void
162 gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
164 register unsigned char *to, *from;
165 register ptrdiff_t i;
166 ptrdiff_t new_s1;
168 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
170 i = GPT_BYTE;
171 from = GAP_END_ADDR;
172 to = GPT_ADDR;
173 new_s1 = GPT_BYTE;
175 /* Now copy the characters. To move the gap up,
176 copy characters down. */
178 while (1)
180 /* I gets number of characters left to copy. */
181 i = bytepos - new_s1;
182 if (i == 0)
183 break;
184 /* If a quit is requested, stop copying now.
185 Change BYTEPOS to be where we have actually moved the gap to.
186 Note that this cannot happen when we are called to make the
187 gap larger or smaller, since make_gap_larger and
188 make_gap_smaller prevent QUIT by setting inhibit-quit. */
189 if (QUITP)
191 bytepos = new_s1;
192 charpos = BYTE_TO_CHAR (bytepos);
193 break;
195 /* Move at most 32000 chars before checking again for a quit. */
196 if (i > 32000)
197 i = 32000;
198 new_s1 += i;
199 memmove (to, from, i);
200 from += i, to += i;
203 GPT = charpos;
204 GPT_BYTE = bytepos;
205 eassert (charpos <= bytepos);
206 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
207 QUIT;
210 /* If the selected window's old pointm is adjacent or covered by the
211 region from FROM to TO, unsuspend auto hscroll in that window. */
213 static void
214 adjust_suspend_auto_hscroll (ptrdiff_t from, ptrdiff_t to)
216 if (WINDOWP (selected_window))
218 struct window *w = XWINDOW (selected_window);
220 if (BUFFERP (w->contents)
221 && XBUFFER (w->contents) == current_buffer
222 && XMARKER (w->old_pointm)->charpos >= from
223 && XMARKER (w->old_pointm)->charpos <= to)
224 w->suspend_auto_hscroll = 0;
229 /* Adjust all markers for a deletion
230 whose range in bytes is FROM_BYTE to TO_BYTE.
231 The range in charpos is FROM to TO.
233 This function assumes that the gap is adjacent to
234 or inside of the range being deleted. */
236 void
237 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
238 ptrdiff_t to, ptrdiff_t to_byte)
240 struct Lisp_Marker *m;
241 ptrdiff_t charpos;
243 adjust_suspend_auto_hscroll (from, to);
244 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
246 charpos = m->charpos;
247 eassert (charpos <= Z);
249 /* If the marker is after the deletion,
250 relocate by number of chars / bytes deleted. */
251 if (charpos > to)
253 m->charpos -= to - from;
254 m->bytepos -= to_byte - from_byte;
256 /* Here's the case where a marker is inside text being deleted. */
257 else if (charpos > from)
259 m->charpos = from;
260 m->bytepos = from_byte;
266 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
267 to TO / TO_BYTE. We have to relocate the charpos of every marker
268 that points after the insertion (but not their bytepos).
270 When a marker points at the insertion point,
271 we advance it if either its insertion-type is t
272 or BEFORE_MARKERS is true. */
274 static void
275 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
276 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
278 struct Lisp_Marker *m;
279 bool adjusted = 0;
280 ptrdiff_t nchars = to - from;
281 ptrdiff_t nbytes = to_byte - from_byte;
283 adjust_suspend_auto_hscroll (from, to);
284 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
286 eassert (m->bytepos >= m->charpos
287 && m->bytepos - m->charpos <= Z_BYTE - Z);
289 if (m->bytepos == from_byte)
291 if (m->insertion_type || before_markers)
293 m->bytepos = to_byte;
294 m->charpos = to;
295 if (m->insertion_type)
296 adjusted = 1;
299 else if (m->bytepos > from_byte)
301 m->bytepos += nbytes;
302 m->charpos += nchars;
306 /* Adjusting only markers whose insertion-type is t may result in
307 - disordered start and end in overlays, and
308 - disordered overlays in the slot `overlays_before' of current_buffer. */
309 if (adjusted)
311 fix_start_end_in_overlays (from, to);
312 fix_overlays_before (current_buffer, from, to);
316 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
318 This is used only when the value of point changes due to an insert
319 or delete; it does not represent a conceptual change in point as a
320 marker. In particular, point is not crossing any interval
321 boundaries, so there's no need to use the usual SET_PT macro. In
322 fact it would be incorrect to do so, because either the old or the
323 new value of point is out of sync with the current set of
324 intervals. */
326 static void
327 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
329 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
330 /* In a single-byte buffer, the two positions must be equal. */
331 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
334 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
335 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
336 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
337 an insertion. */
339 static void
340 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
341 ptrdiff_t old_chars, ptrdiff_t old_bytes,
342 ptrdiff_t new_chars, ptrdiff_t new_bytes)
344 register struct Lisp_Marker *m;
345 ptrdiff_t prev_to_byte = from_byte + old_bytes;
346 ptrdiff_t diff_chars = new_chars - old_chars;
347 ptrdiff_t diff_bytes = new_bytes - old_bytes;
349 adjust_suspend_auto_hscroll (from, from + old_chars);
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 gap_left. We cannot allow a QUIT there,
396 because that would leave the buffer text in an inconsistent
397 state, with 2 gap holes instead of just one. */
398 tem = Vinhibit_quit;
399 Vinhibit_quit = Qt;
401 real_gap_loc = GPT;
402 real_gap_loc_byte = GPT_BYTE;
403 old_gap_size = GAP_SIZE;
405 /* Call the newly allocated space a gap at the end of the whole space. */
406 GPT = Z + GAP_SIZE;
407 GPT_BYTE = Z_BYTE + GAP_SIZE;
408 GAP_SIZE = nbytes_added;
410 /* Move the new gap down to be consecutive with the end of the old one. */
411 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
413 /* Now combine the two into one large gap. */
414 GAP_SIZE += old_gap_size;
415 GPT = real_gap_loc;
416 GPT_BYTE = real_gap_loc_byte;
418 /* Put an anchor. */
419 *(Z_ADDR) = 0;
421 Vinhibit_quit = tem;
424 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
426 /* Make the gap NBYTES_REMOVED bytes shorter. */
428 static void
429 make_gap_smaller (ptrdiff_t nbytes_removed)
431 Lisp_Object tem;
432 ptrdiff_t real_gap_loc;
433 ptrdiff_t real_gap_loc_byte;
434 ptrdiff_t real_Z;
435 ptrdiff_t real_Z_byte;
436 ptrdiff_t real_beg_unchanged;
437 ptrdiff_t new_gap_size;
439 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
440 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
441 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
443 /* Prevent quitting in gap_right. We cannot allow a QUIT there,
444 because that would leave the buffer text in an inconsistent
445 state, with 2 gap holes instead of just one. */
446 tem = Vinhibit_quit;
447 Vinhibit_quit = Qt;
449 real_gap_loc = GPT;
450 real_gap_loc_byte = GPT_BYTE;
451 new_gap_size = GAP_SIZE - nbytes_removed;
452 real_Z = Z;
453 real_Z_byte = Z_BYTE;
454 real_beg_unchanged = BEG_UNCHANGED;
456 /* Pretend that the last unwanted part of the gap is the entire gap,
457 and that the first desired part of the gap is part of the buffer
458 text. */
459 memset (GPT_ADDR, 0, new_gap_size);
460 GPT += new_gap_size;
461 GPT_BYTE += new_gap_size;
462 Z += new_gap_size;
463 Z_BYTE += new_gap_size;
464 GAP_SIZE = nbytes_removed;
466 /* Move the unwanted pretend gap to the end of the buffer. */
467 gap_right (Z, Z_BYTE);
469 enlarge_buffer_text (current_buffer, -nbytes_removed);
471 /* Now restore the desired gap. */
472 GAP_SIZE = new_gap_size;
473 GPT = real_gap_loc;
474 GPT_BYTE = real_gap_loc_byte;
475 Z = real_Z;
476 Z_BYTE = real_Z_byte;
477 BEG_UNCHANGED = real_beg_unchanged;
479 /* Put an anchor. */
480 *(Z_ADDR) = 0;
482 Vinhibit_quit = tem;
485 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
487 void
488 make_gap (ptrdiff_t nbytes_added)
490 if (nbytes_added >= 0)
491 make_gap_larger (nbytes_added);
492 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
493 else
494 make_gap_smaller (-nbytes_added);
495 #endif
498 /* Add NBYTES to B's gap. It's enough to temporarily
499 fake current_buffer and avoid real switch to B. */
501 void
502 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
504 struct buffer *oldb = current_buffer;
506 current_buffer = b;
507 make_gap (nbytes);
508 current_buffer = oldb;
511 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
512 FROM_MULTIBYTE says whether the incoming text is multibyte.
513 TO_MULTIBYTE says whether to store the text as multibyte.
514 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
516 Return the number of bytes stored at TO_ADDR. */
518 ptrdiff_t
519 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
520 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
522 if (from_multibyte == to_multibyte)
524 memcpy (to_addr, from_addr, nbytes);
525 return nbytes;
527 else if (from_multibyte)
529 ptrdiff_t nchars = 0;
530 ptrdiff_t bytes_left = nbytes;
532 while (bytes_left > 0)
534 int thislen, c;
535 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
536 if (! ASCII_CHAR_P (c))
537 c &= 0xFF;
538 *to_addr++ = c;
539 from_addr += thislen;
540 bytes_left -= thislen;
541 nchars++;
543 return nchars;
545 else
547 unsigned char *initial_to_addr = to_addr;
549 /* Convert single-byte to multibyte. */
550 while (nbytes > 0)
552 int c = *from_addr++;
554 if (!ASCII_CHAR_P (c))
556 c = BYTE8_TO_CHAR (c);
557 to_addr += CHAR_STRING (c, to_addr);
558 nbytes--;
560 else
561 /* Special case for speed. */
562 *to_addr++ = c, nbytes--;
564 return to_addr - initial_to_addr;
568 /* Insert a string of specified length before point.
569 This function judges multibyteness based on
570 enable_multibyte_characters in the current buffer;
571 it never converts between single-byte and multibyte.
573 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
574 prepare_to_modify_buffer could relocate the text. */
576 void
577 insert (const char *string, ptrdiff_t nbytes)
579 if (nbytes > 0)
581 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
582 insert_1_both (string, len, nbytes, 0, 1, 0);
583 opoint = PT - len;
584 signal_after_change (opoint, 0, len);
585 update_compositions (opoint, PT, CHECK_BORDER);
589 /* Likewise, but inherit text properties from neighboring characters. */
591 void
592 insert_and_inherit (const char *string, ptrdiff_t nbytes)
594 if (nbytes > 0)
596 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
597 insert_1_both (string, len, nbytes, 1, 1, 0);
598 opoint = PT - len;
599 signal_after_change (opoint, 0, len);
600 update_compositions (opoint, PT, CHECK_BORDER);
604 /* Insert the character C before point. Do not inherit text properties. */
606 void
607 insert_char (int c)
609 unsigned char str[MAX_MULTIBYTE_LENGTH];
610 int len;
612 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
613 len = CHAR_STRING (c, str);
614 else
616 len = 1;
617 str[0] = c;
620 insert ((char *) str, len);
623 /* Insert the null-terminated string S before point. */
625 void
626 insert_string (const char *s)
628 insert (s, strlen (s));
631 /* Like `insert' except that all markers pointing at the place where
632 the insertion happens are adjusted to point after it.
633 Don't use this function to insert part of a Lisp string,
634 since gc could happen and relocate it. */
636 void
637 insert_before_markers (const char *string, ptrdiff_t nbytes)
639 if (nbytes > 0)
641 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
642 insert_1_both (string, len, nbytes, 0, 1, 1);
643 opoint = PT - len;
644 signal_after_change (opoint, 0, len);
645 update_compositions (opoint, PT, CHECK_BORDER);
649 /* Likewise, but inherit text properties from neighboring characters. */
651 void
652 insert_before_markers_and_inherit (const char *string,
653 ptrdiff_t nbytes)
655 if (nbytes > 0)
657 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
658 insert_1_both (string, len, nbytes, 1, 1, 1);
659 opoint = PT - len;
660 signal_after_change (opoint, 0, len);
661 update_compositions (opoint, PT, CHECK_BORDER);
665 #ifdef BYTE_COMBINING_DEBUG
667 /* See if the bytes before POS/POS_BYTE combine with bytes
668 at the start of STRING to form a single character.
669 If so, return the number of bytes at the start of STRING
670 which combine in this way. Otherwise, return 0. */
673 count_combining_before (const unsigned char *string, ptrdiff_t length,
674 ptrdiff_t pos, ptrdiff_t pos_byte)
676 int len, combining_bytes;
677 const unsigned char *p;
679 if (NILP (current_buffer->enable_multibyte_characters))
680 return 0;
682 /* At first, we can exclude the following cases:
683 (1) STRING[0] can't be a following byte of multibyte sequence.
684 (2) POS is the start of the current buffer.
685 (3) A character before POS is not a multibyte character. */
686 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
687 return 0;
688 if (pos_byte == BEG_BYTE) /* case (2) */
689 return 0;
690 len = 1;
691 p = BYTE_POS_ADDR (pos_byte - 1);
692 while (! CHAR_HEAD_P (*p)) p--, len++;
693 if (! LEADING_CODE_P (*p)) /* case (3) */
694 return 0;
696 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
697 if (combining_bytes <= 0)
698 /* The character preceding POS is, complete and no room for
699 combining bytes (combining_bytes == 0), or an independent 8-bit
700 character (combining_bytes < 0). */
701 return 0;
703 /* We have a combination situation. Count the bytes at STRING that
704 may combine. */
705 p = string + 1;
706 while (!CHAR_HEAD_P (*p) && p < string + length)
707 p++;
709 return (combining_bytes < p - string ? combining_bytes : p - string);
712 /* See if the bytes after POS/POS_BYTE combine with bytes
713 at the end of STRING to form a single character.
714 If so, return the number of bytes after POS/POS_BYTE
715 which combine in this way. Otherwise, return 0. */
718 count_combining_after (const unsigned char *string,
719 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
721 ptrdiff_t opos_byte = pos_byte;
722 ptrdiff_t i;
723 ptrdiff_t bytes;
724 unsigned char *bufp;
726 if (NILP (current_buffer->enable_multibyte_characters))
727 return 0;
729 /* At first, we can exclude the following cases:
730 (1) The last byte of STRING is an ASCII.
731 (2) POS is the last of the current buffer.
732 (3) A character at POS can't be a following byte of multibyte
733 character. */
734 if (length > 0 && ASCII_CHAR_P (string[length - 1])) /* case (1) */
735 return 0;
736 if (pos_byte == Z_BYTE) /* case (2) */
737 return 0;
738 bufp = BYTE_POS_ADDR (pos_byte);
739 if (CHAR_HEAD_P (*bufp)) /* case (3) */
740 return 0;
742 i = length - 1;
743 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
745 i--;
747 if (i < 0)
749 /* All characters in STRING are not character head. We must
750 check also preceding bytes at POS. We are sure that the gap
751 is at POS. */
752 unsigned char *p = BEG_ADDR;
753 i = pos_byte - 2;
754 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
755 i--;
756 if (i < 0 || !LEADING_CODE_P (p[i]))
757 return 0;
759 bytes = BYTES_BY_CHAR_HEAD (p[i]);
760 return (bytes <= pos_byte - 1 - i + length
762 : bytes - (pos_byte - 1 - i + length));
764 if (!LEADING_CODE_P (string[i]))
765 return 0;
767 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
768 bufp++, pos_byte++;
769 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
771 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
774 #endif
777 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
778 starting at STRING. INHERIT non-zero means inherit the text
779 properties from neighboring characters; zero means inserted text
780 will have no text properties. PREPARE non-zero means call
781 prepare_to_modify_buffer, which checks that the region is not
782 read-only, and calls before-change-function and any modification
783 properties the text may have. BEFORE_MARKERS non-zero means adjust
784 all markers that point at the insertion place to point after it. */
786 void
787 insert_1_both (const char *string,
788 ptrdiff_t nchars, ptrdiff_t nbytes,
789 bool inherit, bool prepare, bool before_markers)
791 if (nchars == 0)
792 return;
794 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
795 nchars = nbytes;
797 if (prepare)
798 /* Do this before moving and increasing the gap,
799 because the before-change hooks might move the gap
800 or make it smaller. */
801 prepare_to_modify_buffer (PT, PT, NULL);
803 if (PT != GPT)
804 move_gap_both (PT, PT_BYTE);
805 if (GAP_SIZE < nbytes)
806 make_gap (nbytes - GAP_SIZE);
808 #ifdef BYTE_COMBINING_DEBUG
809 if (count_combining_before (string, nbytes, PT, PT_BYTE)
810 || count_combining_after (string, nbytes, PT, PT_BYTE))
811 emacs_abort ();
812 #endif
814 /* Record deletion of the surrounding text that combines with
815 the insertion. This, together with recording the insertion,
816 will add up to the right stuff in the undo list. */
817 record_insert (PT, nchars);
818 MODIFF++;
819 CHARS_MODIFF = MODIFF;
821 memcpy (GPT_ADDR, string, nbytes);
823 GAP_SIZE -= nbytes;
824 GPT += nchars;
825 ZV += nchars;
826 Z += nchars;
827 GPT_BYTE += nbytes;
828 ZV_BYTE += nbytes;
829 Z_BYTE += nbytes;
830 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
832 eassert (GPT <= GPT_BYTE);
834 /* The insert may have been in the unchanged region, so check again. */
835 if (Z - GPT < END_UNCHANGED)
836 END_UNCHANGED = Z - GPT;
838 adjust_overlays_for_insert (PT, nchars);
839 adjust_markers_for_insert (PT, PT_BYTE,
840 PT + nchars, PT_BYTE + nbytes,
841 before_markers);
843 offset_intervals (current_buffer, PT, nchars);
845 if (!inherit && buffer_intervals (current_buffer))
846 set_text_properties (make_number (PT), make_number (PT + nchars),
847 Qnil, Qnil, Qnil);
849 adjust_point (nchars, nbytes);
851 check_markers ();
854 /* Insert the part of the text of STRING, a Lisp object assumed to be
855 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
856 starting at position POS / POS_BYTE. If the text of STRING has properties,
857 copy them into the buffer.
859 It does not work to use `insert' for this, because a GC could happen
860 before we copy the stuff into the buffer, and relocate the string
861 without insert noticing. */
863 void
864 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
865 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
867 ptrdiff_t opoint = PT;
869 if (SCHARS (string) == 0)
870 return;
872 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
873 inherit, 0);
874 signal_after_change (opoint, 0, PT - opoint);
875 update_compositions (opoint, PT, CHECK_BORDER);
878 /* Like `insert_from_string' except that all markers pointing
879 at the place where the insertion happens are adjusted to point after it. */
881 void
882 insert_from_string_before_markers (Lisp_Object string,
883 ptrdiff_t pos, ptrdiff_t pos_byte,
884 ptrdiff_t length, ptrdiff_t length_byte,
885 bool inherit)
887 ptrdiff_t opoint = PT;
889 if (SCHARS (string) == 0)
890 return;
892 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
893 inherit, 1);
894 signal_after_change (opoint, 0, PT - opoint);
895 update_compositions (opoint, PT, CHECK_BORDER);
898 /* Subroutine of the insertion functions above. */
900 static void
901 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
902 ptrdiff_t nchars, ptrdiff_t nbytes,
903 bool inherit, bool before_markers)
905 ptrdiff_t outgoing_nbytes = nbytes;
906 INTERVAL intervals;
908 /* Make OUTGOING_NBYTES describe the text
909 as it will be inserted in this buffer. */
911 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
912 outgoing_nbytes = nchars;
913 else if (! STRING_MULTIBYTE (string))
914 outgoing_nbytes
915 = count_size_as_multibyte (SDATA (string) + pos_byte,
916 nbytes);
918 /* Do this before moving and increasing the gap,
919 because the before-change hooks might move the gap
920 or make it smaller. */
921 prepare_to_modify_buffer (PT, PT, NULL);
923 if (PT != GPT)
924 move_gap_both (PT, PT_BYTE);
925 if (GAP_SIZE < outgoing_nbytes)
926 make_gap (outgoing_nbytes - GAP_SIZE);
928 /* Copy the string text into the buffer, perhaps converting
929 between single-byte and multibyte. */
930 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
931 STRING_MULTIBYTE (string),
932 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
934 #ifdef BYTE_COMBINING_DEBUG
935 /* We have copied text into the gap, but we have not altered
936 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
937 to these functions and get the same results as we would
938 have got earlier on. Meanwhile, PT_ADDR does point to
939 the text that has been stored by copy_text. */
940 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
941 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
942 emacs_abort ();
943 #endif
945 record_insert (PT, nchars);
946 MODIFF++;
947 CHARS_MODIFF = MODIFF;
949 GAP_SIZE -= outgoing_nbytes;
950 GPT += nchars;
951 ZV += nchars;
952 Z += nchars;
953 GPT_BYTE += outgoing_nbytes;
954 ZV_BYTE += outgoing_nbytes;
955 Z_BYTE += outgoing_nbytes;
956 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
958 eassert (GPT <= GPT_BYTE);
960 /* The insert may have been in the unchanged region, so check again. */
961 if (Z - GPT < END_UNCHANGED)
962 END_UNCHANGED = Z - GPT;
964 adjust_overlays_for_insert (PT, nchars);
965 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
966 PT_BYTE + outgoing_nbytes,
967 before_markers);
969 offset_intervals (current_buffer, PT, nchars);
971 intervals = string_intervals (string);
972 /* Get the intervals for the part of the string we are inserting. */
973 if (nbytes < SBYTES (string))
974 intervals = copy_intervals (intervals, pos, nchars);
976 /* Insert those intervals. */
977 graft_intervals_into_buffer (intervals, PT, nchars,
978 current_buffer, inherit);
980 adjust_point (nchars, outgoing_nbytes);
982 check_markers ();
985 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
986 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
987 GPT_ADDR (if not text_at_gap_tail). */
989 void
990 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
992 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
994 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
995 nchars = nbytes;
997 /* No need to call prepare_to_modify_buffer, since this is called
998 from places that replace some region with a different text, so
999 prepare_to_modify_buffer was already called by the deletion part
1000 of this dance. */
1001 invalidate_buffer_caches (current_buffer, GPT, GPT);
1002 record_insert (GPT, nchars);
1003 MODIFF++;
1005 GAP_SIZE -= nbytes;
1006 if (! text_at_gap_tail)
1008 GPT += nchars;
1009 GPT_BYTE += nbytes;
1011 ZV += nchars;
1012 Z += nchars;
1013 ZV_BYTE += nbytes;
1014 Z_BYTE += nbytes;
1015 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1017 eassert (GPT <= GPT_BYTE);
1019 adjust_overlays_for_insert (ins_charpos, nchars);
1020 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1021 ins_charpos + nchars, ins_bytepos + nbytes, 0);
1023 if (buffer_intervals (current_buffer))
1025 offset_intervals (current_buffer, ins_charpos, nchars);
1026 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1027 current_buffer, 0);
1030 if (ins_charpos < PT)
1031 adjust_point (nchars, nbytes);
1033 check_markers ();
1036 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1037 current buffer. If the text in BUF has properties, they are absorbed
1038 into the current buffer.
1040 It does not work to use `insert' for this, because a malloc could happen
1041 and relocate BUF's text before the copy happens. */
1043 void
1044 insert_from_buffer (struct buffer *buf,
1045 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1047 ptrdiff_t opoint = PT;
1049 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1050 signal_after_change (opoint, 0, PT - opoint);
1051 update_compositions (opoint, PT, CHECK_BORDER);
1054 static void
1055 insert_from_buffer_1 (struct buffer *buf,
1056 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1058 ptrdiff_t chunk, chunk_expanded;
1059 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1060 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1061 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1062 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1063 INTERVAL intervals;
1065 if (nchars == 0)
1066 return;
1068 /* Make OUTGOING_NBYTES describe the text
1069 as it will be inserted in this buffer. */
1071 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1072 outgoing_nbytes = nchars;
1073 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1075 ptrdiff_t outgoing_before_gap = 0;
1076 ptrdiff_t outgoing_after_gap = 0;
1078 if (from < BUF_GPT (buf))
1080 chunk = BUF_GPT_BYTE (buf) - from_byte;
1081 if (chunk > incoming_nbytes)
1082 chunk = incoming_nbytes;
1083 outgoing_before_gap
1084 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1085 chunk);
1087 else
1088 chunk = 0;
1090 if (chunk < incoming_nbytes)
1091 outgoing_after_gap
1092 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1093 from_byte + chunk),
1094 incoming_nbytes - chunk);
1096 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1099 /* Do this before moving and increasing the gap,
1100 because the before-change hooks might move the gap
1101 or make it smaller. */
1102 prepare_to_modify_buffer (PT, PT, NULL);
1104 if (PT != GPT)
1105 move_gap_both (PT, PT_BYTE);
1106 if (GAP_SIZE < outgoing_nbytes)
1107 make_gap (outgoing_nbytes - GAP_SIZE);
1109 if (from < BUF_GPT (buf))
1111 chunk = BUF_GPT_BYTE (buf) - from_byte;
1112 if (chunk > incoming_nbytes)
1113 chunk = incoming_nbytes;
1114 /* Record number of output bytes, so we know where
1115 to put the output from the second copy_text. */
1116 chunk_expanded
1117 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1118 GPT_ADDR, chunk,
1119 ! NILP (BVAR (buf, enable_multibyte_characters)),
1120 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1122 else
1123 chunk_expanded = chunk = 0;
1125 if (chunk < incoming_nbytes)
1126 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1127 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1128 ! NILP (BVAR (buf, enable_multibyte_characters)),
1129 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1131 #ifdef BYTE_COMBINING_DEBUG
1132 /* We have copied text into the gap, but we have not altered
1133 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1134 to these functions and get the same results as we would
1135 have got earlier on. Meanwhile, GPT_ADDR does point to
1136 the text that has been stored by copy_text. */
1137 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1138 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1139 emacs_abort ();
1140 #endif
1142 record_insert (PT, nchars);
1143 MODIFF++;
1144 CHARS_MODIFF = MODIFF;
1146 GAP_SIZE -= outgoing_nbytes;
1147 GPT += nchars;
1148 ZV += nchars;
1149 Z += nchars;
1150 GPT_BYTE += outgoing_nbytes;
1151 ZV_BYTE += outgoing_nbytes;
1152 Z_BYTE += outgoing_nbytes;
1153 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1155 eassert (GPT <= GPT_BYTE);
1157 /* The insert may have been in the unchanged region, so check again. */
1158 if (Z - GPT < END_UNCHANGED)
1159 END_UNCHANGED = Z - GPT;
1161 adjust_overlays_for_insert (PT, nchars);
1162 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1163 PT_BYTE + outgoing_nbytes,
1166 offset_intervals (current_buffer, PT, nchars);
1168 /* Get the intervals for the part of the string we are inserting. */
1169 intervals = buffer_intervals (buf);
1170 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1172 if (buf == current_buffer && PT <= from)
1173 from += nchars;
1174 intervals = copy_intervals (intervals, from, nchars);
1177 /* Insert those intervals. */
1178 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1180 adjust_point (nchars, outgoing_nbytes);
1183 /* Record undo information and adjust markers and position keepers for
1184 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1185 chars (LEN_BYTE bytes) which resides in the gap just after
1186 GPT_ADDR.
1188 PREV_TEXT nil means the new text was just inserted. */
1190 static void
1191 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1192 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1194 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1196 #ifdef BYTE_COMBINING_DEBUG
1197 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1198 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1199 emacs_abort ();
1200 #endif
1202 if (STRINGP (prev_text))
1204 nchars_del = SCHARS (prev_text);
1205 nbytes_del = SBYTES (prev_text);
1208 /* Update various buffer positions for the new text. */
1209 GAP_SIZE -= len_byte;
1210 ZV += len; Z += len;
1211 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1212 GPT += len; GPT_BYTE += len_byte;
1213 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1215 if (nchars_del > 0)
1216 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1217 len, len_byte);
1218 else
1219 adjust_markers_for_insert (from, from_byte,
1220 from + len, from_byte + len_byte, 0);
1222 if (nchars_del > 0)
1223 record_delete (from, prev_text, false);
1224 record_insert (from, len);
1226 if (len > nchars_del)
1227 adjust_overlays_for_insert (from, len - nchars_del);
1228 else if (len < nchars_del)
1229 adjust_overlays_for_delete (from, nchars_del - len);
1231 offset_intervals (current_buffer, from, len - nchars_del);
1233 if (from < PT)
1234 adjust_point (len - nchars_del, len_byte - nbytes_del);
1236 /* As byte combining will decrease Z, we must check this again. */
1237 if (Z - GPT < END_UNCHANGED)
1238 END_UNCHANGED = Z - GPT;
1240 check_markers ();
1242 if (len == 0)
1243 evaporate_overlays (from);
1244 MODIFF++;
1245 CHARS_MODIFF = MODIFF;
1248 /* Record undo information, adjust markers and position keepers for an
1249 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1250 text already exists in the current buffer but character length (TO
1251 - FROM) may be incorrect, the correct length is NEWLEN. */
1253 void
1254 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1255 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1257 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1259 if (GPT != to)
1260 move_gap_both (to, to_byte);
1261 GAP_SIZE += len_byte;
1262 GPT -= len; GPT_BYTE -= len_byte;
1263 ZV -= len; ZV_BYTE -= len_byte;
1264 Z -= len; Z_BYTE -= len_byte;
1265 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1268 /* Replace the text from character positions FROM to TO with NEW,
1269 If PREPARE, call prepare_to_modify_buffer.
1270 If INHERIT, the newly inserted text should inherit text properties
1271 from the surrounding non-deleted text.
1272 If ADJUST_MATCH_DATA, then adjust the match data before calling
1273 signal_after_change. */
1275 /* Note that this does not yet handle markers quite right.
1276 Also it needs to record a single undo-entry that does a replacement
1277 rather than a separate delete and insert.
1278 That way, undo will also handle markers properly.
1280 But if MARKERS is 0, don't relocate markers. */
1282 void
1283 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1284 bool prepare, bool inherit, bool markers,
1285 bool adjust_match_data)
1287 ptrdiff_t inschars = SCHARS (new);
1288 ptrdiff_t insbytes = SBYTES (new);
1289 ptrdiff_t from_byte, to_byte;
1290 ptrdiff_t nbytes_del, nchars_del;
1291 INTERVAL intervals;
1292 ptrdiff_t outgoing_insbytes = insbytes;
1293 Lisp_Object deletion;
1295 check_markers ();
1297 deletion = Qnil;
1299 if (prepare)
1301 ptrdiff_t range_length = to - from;
1302 prepare_to_modify_buffer (from, to, &from);
1303 to = from + range_length;
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 /* Make sure the gap is somewhere in or next to what we are deleting. */
1331 if (from > GPT)
1332 gap_right (from, from_byte);
1333 if (to < GPT)
1334 gap_left (to, to_byte, 0);
1336 /* Even if we don't record for undo, we must keep the original text
1337 because we may have to recover it because of inappropriate byte
1338 combining. */
1339 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1340 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1342 GAP_SIZE += nbytes_del;
1343 ZV -= nchars_del;
1344 Z -= nchars_del;
1345 ZV_BYTE -= nbytes_del;
1346 Z_BYTE -= nbytes_del;
1347 GPT = from;
1348 GPT_BYTE = from_byte;
1349 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1351 eassert (GPT <= GPT_BYTE);
1353 if (GPT - BEG < BEG_UNCHANGED)
1354 BEG_UNCHANGED = GPT - BEG;
1355 if (Z - GPT < END_UNCHANGED)
1356 END_UNCHANGED = Z - GPT;
1358 if (GAP_SIZE < outgoing_insbytes)
1359 make_gap (outgoing_insbytes - GAP_SIZE);
1361 /* Copy the string text into the buffer, perhaps converting
1362 between single-byte and multibyte. */
1363 copy_text (SDATA (new), GPT_ADDR, insbytes,
1364 STRING_MULTIBYTE (new),
1365 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1367 #ifdef BYTE_COMBINING_DEBUG
1368 /* We have copied text into the gap, but we have not marked
1369 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1370 here, for both the previous text and the following text.
1371 Meanwhile, GPT_ADDR does point to
1372 the text that has been stored by copy_text. */
1373 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1374 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1375 emacs_abort ();
1376 #endif
1378 /* Record the insertion first, so that when we undo,
1379 the deletion will be undone first. Thus, undo
1380 will insert before deleting, and thus will keep
1381 the markers before and after this text separate. */
1382 if (!NILP (deletion))
1384 record_insert (from + SCHARS (deletion), inschars);
1385 record_delete (from, deletion, false);
1388 GAP_SIZE -= outgoing_insbytes;
1389 GPT += inschars;
1390 ZV += inschars;
1391 Z += inschars;
1392 GPT_BYTE += outgoing_insbytes;
1393 ZV_BYTE += outgoing_insbytes;
1394 Z_BYTE += outgoing_insbytes;
1395 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1397 eassert (GPT <= GPT_BYTE);
1399 /* Adjust markers for the deletion and the insertion. */
1400 if (markers)
1401 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1402 inschars, outgoing_insbytes);
1404 /* Adjust the overlay center as needed. This must be done after
1405 adjusting the markers that bound the overlays. */
1406 adjust_overlays_for_delete (from, nchars_del);
1407 adjust_overlays_for_insert (from, inschars);
1409 offset_intervals (current_buffer, from, inschars - nchars_del);
1411 /* Get the intervals for the part of the string we are inserting--
1412 not including the combined-before bytes. */
1413 intervals = string_intervals (new);
1414 /* Insert those intervals. */
1415 graft_intervals_into_buffer (intervals, from, inschars,
1416 current_buffer, inherit);
1418 /* Relocate point as if it were a marker. */
1419 if (from < PT)
1420 adjust_point ((from + inschars - (PT < to ? PT : to)),
1421 (from_byte + outgoing_insbytes
1422 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1424 if (outgoing_insbytes == 0)
1425 evaporate_overlays (from);
1427 check_markers ();
1429 MODIFF++;
1430 CHARS_MODIFF = MODIFF;
1432 if (adjust_match_data)
1433 update_search_regs (from, to, from + SCHARS (new));
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, 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 bool 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 emacs_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 markers for the deletion and the insertion. */
1518 if (markers
1519 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1520 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1521 inschars, insbytes);
1523 /* Adjust the overlay center as needed. This must be done after
1524 adjusting the markers that bound the overlays. */
1525 if (nchars_del != inschars)
1527 adjust_overlays_for_insert (from, inschars);
1528 adjust_overlays_for_delete (from + inschars, nchars_del);
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, bool prepare, bool ret_string)
1568 ptrdiff_t from_byte, to_byte;
1569 Lisp_Object deletion;
1571 /* Make args be valid */
1572 if (from < BEGV)
1573 from = BEGV;
1574 if (to > ZV)
1575 to = ZV;
1577 if (to <= from)
1578 return Qnil;
1580 if (prepare)
1582 ptrdiff_t range_length = to - from;
1583 prepare_to_modify_buffer (from, to, &from);
1584 to = min (ZV, from + range_length);
1587 from_byte = CHAR_TO_BYTE (from);
1588 to_byte = CHAR_TO_BYTE (to);
1590 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1591 signal_after_change (from, to - from, 0);
1592 update_compositions (from, from, CHECK_HEAD);
1593 return deletion;
1596 /* Like del_range_1 but args are byte positions, not char positions. */
1598 void
1599 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
1601 ptrdiff_t from, to;
1603 /* Make args be valid. */
1604 if (from_byte < BEGV_BYTE)
1605 from_byte = BEGV_BYTE;
1606 if (to_byte > ZV_BYTE)
1607 to_byte = ZV_BYTE;
1609 if (to_byte <= from_byte)
1610 return;
1612 from = BYTE_TO_CHAR (from_byte);
1613 to = BYTE_TO_CHAR (to_byte);
1615 if (prepare)
1617 ptrdiff_t old_from = from, old_to = Z - to;
1618 ptrdiff_t range_length = to - from;
1619 prepare_to_modify_buffer (from, to, &from);
1620 to = from + range_length;
1622 if (old_from != from)
1623 from_byte = CHAR_TO_BYTE (from);
1624 if (to > ZV)
1626 to = ZV;
1627 to_byte = ZV_BYTE;
1629 else if (old_to == Z - to)
1630 to_byte = CHAR_TO_BYTE (to);
1633 del_range_2 (from, from_byte, to, to_byte, 0);
1634 signal_after_change (from, to - from, 0);
1635 update_compositions (from, from, CHECK_HEAD);
1638 /* Like del_range_1, but positions are specified both as charpos
1639 and bytepos. */
1641 void
1642 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1643 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1645 /* Make args be valid */
1646 if (from_byte < BEGV_BYTE)
1647 from_byte = BEGV_BYTE;
1648 if (to_byte > ZV_BYTE)
1649 to_byte = ZV_BYTE;
1651 if (to_byte <= from_byte)
1652 return;
1654 if (from < BEGV)
1655 from = BEGV;
1656 if (to > ZV)
1657 to = ZV;
1659 if (prepare)
1661 ptrdiff_t old_from = from, old_to = Z - to;
1662 ptrdiff_t range_length = to - from;
1663 prepare_to_modify_buffer (from, to, &from);
1664 to = from + range_length;
1666 if (old_from != from)
1667 from_byte = CHAR_TO_BYTE (from);
1668 if (to > ZV)
1670 to = ZV;
1671 to_byte = ZV_BYTE;
1673 else if (old_to == Z - to)
1674 to_byte = CHAR_TO_BYTE (to);
1677 del_range_2 (from, from_byte, to, to_byte, 0);
1678 signal_after_change (from, to - from, 0);
1679 update_compositions (from, from, CHECK_HEAD);
1682 /* Delete a range of text, specified both as character positions
1683 and byte positions. FROM and TO are character positions,
1684 while FROM_BYTE and TO_BYTE are byte positions.
1685 If RET_STRING, the deleted area is returned as a string. */
1687 Lisp_Object
1688 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1689 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1691 ptrdiff_t nbytes_del, nchars_del;
1692 Lisp_Object deletion;
1694 check_markers ();
1696 nchars_del = to - from;
1697 nbytes_del = to_byte - from_byte;
1699 /* Make sure the gap is somewhere in or next to what we are deleting. */
1700 if (from > GPT)
1701 gap_right (from, from_byte);
1702 if (to < GPT)
1703 gap_left (to, to_byte, 0);
1705 #ifdef BYTE_COMBINING_DEBUG
1706 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1707 Z_BYTE - to_byte, from, from_byte))
1708 emacs_abort ();
1709 #endif
1711 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1712 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1713 else
1714 deletion = Qnil;
1716 /* Record marker adjustments, and text deletion into undo
1717 history. */
1718 record_delete (from, deletion, true);
1720 /* Relocate all markers pointing into the new, larger gap to point
1721 at the end of the text before the gap. */
1722 adjust_markers_for_delete (from, from_byte, to, to_byte);
1724 MODIFF++;
1725 CHARS_MODIFF = MODIFF;
1727 /* Relocate point as if it were a marker. */
1728 if (from < PT)
1729 adjust_point (from - (PT < to ? PT : to),
1730 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1732 offset_intervals (current_buffer, from, - nchars_del);
1734 /* Adjust the overlay center as needed. This must be done after
1735 adjusting the markers that bound the overlays. */
1736 adjust_overlays_for_delete (from, nchars_del);
1738 GAP_SIZE += nbytes_del;
1739 ZV_BYTE -= nbytes_del;
1740 Z_BYTE -= nbytes_del;
1741 ZV -= nchars_del;
1742 Z -= nchars_del;
1743 GPT = from;
1744 GPT_BYTE = from_byte;
1745 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1746 /* Put an anchor, unless called from decode_coding_object which
1747 needs to access the previous gap contents. */
1748 *(GPT_ADDR) = 0;
1750 eassert (GPT <= GPT_BYTE);
1752 if (GPT - BEG < BEG_UNCHANGED)
1753 BEG_UNCHANGED = GPT - BEG;
1754 if (Z - GPT < END_UNCHANGED)
1755 END_UNCHANGED = Z - GPT;
1757 check_markers ();
1759 evaporate_overlays (from);
1761 return deletion;
1764 /* Call this if you're about to change the text of current buffer
1765 from character positions START to END. This checks the read-only
1766 properties of the region, calls the necessary modification hooks,
1767 and warns the next redisplay that it should pay attention to that
1768 area. */
1770 void
1771 modify_text (ptrdiff_t start, ptrdiff_t end)
1773 prepare_to_modify_buffer (start, end, NULL);
1775 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1776 if (MODIFF <= SAVE_MODIFF)
1777 record_first_change ();
1778 MODIFF++;
1779 CHARS_MODIFF = MODIFF;
1781 bset_point_before_scroll (current_buffer, Qnil);
1784 /* Signal that we are about to make a change that may result in new
1785 undo information.
1787 static void
1788 run_undoable_change (void)
1790 if (EQ (BVAR (current_buffer, undo_list), Qt))
1791 return;
1793 call0 (Qundo_auto__undoable_change);
1796 /* Check that it is okay to modify the buffer between START and END,
1797 which are char positions.
1799 Run the before-change-function, if any. If intervals are in use,
1800 verify that the text to be modified is not read-only, and call
1801 any modification properties the text may have.
1803 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1804 by holding its value temporarily in a marker.
1806 This function runs Lisp, which means it can GC, which means it can
1807 compact buffers, including the current buffer being worked on here.
1808 So don't you dare calling this function while manipulating the gap,
1809 or during some other similar "critical section". */
1811 void
1812 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1813 ptrdiff_t *preserve_ptr)
1815 struct buffer *base_buffer;
1816 Lisp_Object temp;
1818 XSETFASTINT (temp, start);
1819 if (!NILP (BVAR (current_buffer, read_only)))
1820 Fbarf_if_buffer_read_only (temp);
1822 run_undoable_change();
1824 bset_redisplay (current_buffer);
1826 if (buffer_intervals (current_buffer))
1828 if (preserve_ptr)
1830 Lisp_Object preserve_marker;
1831 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1832 verify_interval_modification (current_buffer, start, end);
1833 *preserve_ptr = marker_position (preserve_marker);
1834 unchain_marker (XMARKER (preserve_marker));
1836 else
1837 verify_interval_modification (current_buffer, start, end);
1840 /* For indirect buffers, use the base buffer to check clashes. */
1841 if (current_buffer->base_buffer != 0)
1842 base_buffer = current_buffer->base_buffer;
1843 else
1844 base_buffer = current_buffer;
1846 if (inhibit_modification_hooks)
1847 return;
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));
1855 /* If `select-active-regions' is non-nil, save the region text. */
1856 /* FIXME: Move this to Elisp (via before-change-functions). */
1857 if (!NILP (BVAR (current_buffer, mark_active))
1858 && XMARKER (BVAR (current_buffer, mark))->buffer
1859 && NILP (Vsaved_region_selection)
1860 && (EQ (Vselect_active_regions, Qonly)
1861 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1862 : (!NILP (Vselect_active_regions)
1863 && !NILP (Vtransient_mark_mode))))
1864 Vsaved_region_selection
1865 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1867 signal_before_change (start, end, preserve_ptr);
1868 Fset (Qdeactivate_mark, Qt);
1871 /* Like above, but called when we know that the buffer text
1872 will be modified and region caches should be invalidated. */
1874 void
1875 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1876 ptrdiff_t *preserve_ptr)
1878 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1879 invalidate_buffer_caches (current_buffer, start, end);
1882 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1883 region between buffer positions START and END. */
1884 void
1885 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1887 /* Indirect buffers usually have their caches set to NULL, but we
1888 need to consider the caches of their base buffer. */
1889 if (buf->base_buffer)
1890 buf = buf->base_buffer;
1891 /* The bidi_paragraph_cache must be invalidated first, because doing
1892 so might need to use the newline_cache (via find_newline_no_quit,
1893 see below). */
1894 if (buf->bidi_paragraph_cache)
1896 if (start != end
1897 && start > BUF_BEG (buf))
1899 /* If we are deleting or replacing characters, we could
1900 create a paragraph start, because all of the characters
1901 from START to the beginning of START's line are
1902 whitespace. Therefore, we must extend the region to be
1903 invalidated up to the newline before START. */
1904 ptrdiff_t line_beg = start;
1905 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
1907 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
1909 struct buffer *old = current_buffer;
1911 set_buffer_internal (buf);
1913 line_beg = find_newline_no_quit (start, start_byte, -1,
1914 &start_byte);
1915 set_buffer_internal (old);
1917 start = line_beg - (line_beg > BUF_BEG (buf));
1919 invalidate_region_cache (buf,
1920 buf->bidi_paragraph_cache,
1921 start - BUF_BEG (buf), BUF_Z (buf) - end);
1923 if (buf->newline_cache)
1924 invalidate_region_cache (buf,
1925 buf->newline_cache,
1926 start - BUF_BEG (buf), BUF_Z (buf) - end);
1927 if (buf->width_run_cache)
1928 invalidate_region_cache (buf,
1929 buf->width_run_cache,
1930 start - BUF_BEG (buf), BUF_Z (buf) - end);
1933 /* These macros work with an argument named `preserve_ptr'
1934 and a local variable named `preserve_marker'. */
1936 #define PRESERVE_VALUE \
1937 if (preserve_ptr && NILP (preserve_marker)) \
1938 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1940 #define RESTORE_VALUE \
1941 if (! NILP (preserve_marker)) \
1943 *preserve_ptr = marker_position (preserve_marker); \
1944 unchain_marker (XMARKER (preserve_marker)); \
1947 #define PRESERVE_START_END \
1948 if (NILP (start_marker)) \
1949 start_marker = Fcopy_marker (start, Qnil); \
1950 if (NILP (end_marker)) \
1951 end_marker = Fcopy_marker (end, Qnil);
1953 #define FETCH_START \
1954 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1956 #define FETCH_END \
1957 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1959 /* Set a variable to nil if an error occurred.
1960 Don't change the variable if there was no error.
1961 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1962 VARIABLE is the variable to maybe set to nil.
1963 NO-ERROR-FLAG is nil if there was an error,
1964 anything else meaning no error (so this function does nothing). */
1965 struct rvoe_arg
1967 Lisp_Object *location;
1968 bool errorp;
1971 static void
1972 reset_var_on_error (void *ptr)
1974 struct rvoe_arg *p = ptr;
1975 if (p->errorp)
1976 *p->location = Qnil;
1979 /* Signal a change to the buffer immediately before it happens.
1980 START_INT and END_INT are the bounds of the text to be changed.
1982 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1983 by holding its value temporarily in a marker. */
1985 static void
1986 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1987 ptrdiff_t *preserve_ptr)
1989 Lisp_Object start, end;
1990 Lisp_Object start_marker, end_marker;
1991 Lisp_Object preserve_marker;
1992 ptrdiff_t count = SPECPDL_INDEX ();
1993 struct rvoe_arg rvoe_arg;
1995 start = make_number (start_int);
1996 end = make_number (end_int);
1997 preserve_marker = Qnil;
1998 start_marker = Qnil;
1999 end_marker = Qnil;
2001 specbind (Qinhibit_modification_hooks, Qt);
2003 /* If buffer is unmodified, run a special hook for that case. The
2004 check for Vfirst_change_hook is just a minor optimization. */
2005 if (SAVE_MODIFF >= MODIFF
2006 && !NILP (Vfirst_change_hook))
2008 PRESERVE_VALUE;
2009 PRESERVE_START_END;
2010 run_hook (Qfirst_change_hook);
2013 /* Now run the before-change-functions if any. */
2014 if (!NILP (Vbefore_change_functions))
2016 rvoe_arg.location = &Vbefore_change_functions;
2017 rvoe_arg.errorp = 1;
2019 PRESERVE_VALUE;
2020 PRESERVE_START_END;
2022 /* Mark before-change-functions to be reset to nil in case of error. */
2023 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2025 /* Actually run the hook functions. */
2026 CALLN (Frun_hook_with_args, Qbefore_change_functions,
2027 FETCH_START, FETCH_END);
2029 /* There was no error: unarm the reset_on_error. */
2030 rvoe_arg.errorp = 0;
2033 if (buffer_has_overlays ())
2035 PRESERVE_VALUE;
2036 report_overlay_modification (FETCH_START, FETCH_END, 0,
2037 FETCH_START, FETCH_END, Qnil);
2040 if (! NILP (start_marker))
2041 free_marker (start_marker);
2042 if (! NILP (end_marker))
2043 free_marker (end_marker);
2044 RESTORE_VALUE;
2046 unbind_to (count, Qnil);
2049 /* Signal a change immediately after it happens.
2050 CHARPOS is the character position of the start of the changed text.
2051 LENDEL is the number of characters of the text before the change.
2052 (Not the whole buffer; just the part that was changed.)
2053 LENINS is the number of characters in that part of the text
2054 after the change. */
2056 void
2057 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2059 ptrdiff_t count = SPECPDL_INDEX ();
2060 struct rvoe_arg rvoe_arg;
2062 if (inhibit_modification_hooks)
2063 return;
2065 /* If we are deferring calls to the after-change functions
2066 and there are no before-change functions,
2067 just record the args that we were going to use. */
2068 if (! NILP (Vcombine_after_change_calls)
2069 && NILP (Vbefore_change_functions)
2070 && !buffer_has_overlays ())
2072 Lisp_Object elt;
2074 if (!NILP (combine_after_change_list)
2075 && current_buffer != XBUFFER (combine_after_change_buffer))
2076 Fcombine_after_change_execute ();
2078 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2079 lenins - lendel);
2080 combine_after_change_list
2081 = Fcons (elt, combine_after_change_list);
2082 combine_after_change_buffer = Fcurrent_buffer ();
2084 return;
2087 if (!NILP (combine_after_change_list))
2088 Fcombine_after_change_execute ();
2090 specbind (Qinhibit_modification_hooks, Qt);
2092 if (!NILP (Vafter_change_functions))
2094 rvoe_arg.location = &Vafter_change_functions;
2095 rvoe_arg.errorp = 1;
2097 /* Mark after-change-functions to be reset to nil in case of error. */
2098 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2100 /* Actually run the hook functions. */
2101 CALLN (Frun_hook_with_args, Qafter_change_functions,
2102 make_number (charpos), make_number (charpos + lenins),
2103 make_number (lendel));
2105 /* There was no error: unarm the reset_on_error. */
2106 rvoe_arg.errorp = 0;
2109 if (buffer_has_overlays ())
2110 report_overlay_modification (make_number (charpos),
2111 make_number (charpos + lenins),
2113 make_number (charpos),
2114 make_number (charpos + lenins),
2115 make_number (lendel));
2117 /* After an insertion, call the text properties
2118 insert-behind-hooks or insert-in-front-hooks. */
2119 if (lendel == 0)
2120 report_interval_modification (make_number (charpos),
2121 make_number (charpos + lenins));
2123 unbind_to (count, Qnil);
2126 static void
2127 Fcombine_after_change_execute_1 (Lisp_Object val)
2129 Vcombine_after_change_calls = val;
2132 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2133 Scombine_after_change_execute, 0, 0, 0,
2134 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2135 (void)
2137 ptrdiff_t count = SPECPDL_INDEX ();
2138 ptrdiff_t beg, end, change;
2139 ptrdiff_t begpos, endpos;
2140 Lisp_Object tail;
2142 if (NILP (combine_after_change_list))
2143 return Qnil;
2145 /* It is rare for combine_after_change_buffer to be invalid, but
2146 possible. It can happen when combine-after-change-calls is
2147 non-nil, and insertion calls a file handler (e.g. through
2148 lock_file) which scribbles into a temp file -- cyd */
2149 if (!BUFFERP (combine_after_change_buffer)
2150 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2152 combine_after_change_list = Qnil;
2153 return Qnil;
2156 record_unwind_current_buffer ();
2158 Fset_buffer (combine_after_change_buffer);
2160 /* # chars unchanged at beginning of buffer. */
2161 beg = Z - BEG;
2162 /* # chars unchanged at end of buffer. */
2163 end = beg;
2164 /* Total amount of insertion (negative for deletion). */
2165 change = 0;
2167 /* Scan the various individual changes,
2168 accumulating the range info in BEG, END and CHANGE. */
2169 for (tail = combine_after_change_list; CONSP (tail);
2170 tail = XCDR (tail))
2172 Lisp_Object elt;
2173 ptrdiff_t thisbeg, thisend, thischange;
2175 /* Extract the info from the next element. */
2176 elt = XCAR (tail);
2177 if (! CONSP (elt))
2178 continue;
2179 thisbeg = XINT (XCAR (elt));
2181 elt = XCDR (elt);
2182 if (! CONSP (elt))
2183 continue;
2184 thisend = XINT (XCAR (elt));
2186 elt = XCDR (elt);
2187 if (! CONSP (elt))
2188 continue;
2189 thischange = XINT (XCAR (elt));
2191 /* Merge this range into the accumulated range. */
2192 change += thischange;
2193 if (thisbeg < beg)
2194 beg = thisbeg;
2195 if (thisend < end)
2196 end = thisend;
2199 /* Get the current start and end positions of the range
2200 that was changed. */
2201 begpos = BEG + beg;
2202 endpos = Z - end;
2204 /* We are about to handle these, so discard them. */
2205 combine_after_change_list = Qnil;
2207 /* Now run the after-change functions for real.
2208 Turn off the flag that defers them. */
2209 record_unwind_protect (Fcombine_after_change_execute_1,
2210 Vcombine_after_change_calls);
2211 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2212 update_compositions (begpos, endpos, CHECK_ALL);
2214 return unbind_to (count, Qnil);
2217 void
2218 syms_of_insdel (void)
2220 staticpro (&combine_after_change_list);
2221 staticpro (&combine_after_change_buffer);
2222 combine_after_change_list = Qnil;
2223 combine_after_change_buffer = Qnil;
2225 DEFSYM (Qundo_auto__undoable_change, "undo-auto--undoable-change");
2227 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2228 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2229 Vcombine_after_change_calls = Qnil;
2231 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2232 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2233 This affects `before-change-functions' and `after-change-functions',
2234 as well as hooks attached to text properties and overlays. */);
2235 inhibit_modification_hooks = 0;
2236 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2238 DEFSYM (Qregion_extract_function, "region-extract-function");
2240 defsubr (&Scombine_after_change_execute);