Update copyright year to 2015
[emacs.git] / src / insdel.c
bloba1bec4a9a6da952965f286d4f076fcb7b786fb08
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
3 Copyright (C) 1985-1986, 1993-1995, 1997-2015 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
24 #include <intprops.h>
26 #include "lisp.h"
27 #include "intervals.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "window.h"
31 #include "blockinput.h"
32 #include "region-cache.h"
34 static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
35 ptrdiff_t, bool, bool);
36 static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
37 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
38 static void gap_right (ptrdiff_t, ptrdiff_t);
40 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
41 describing changes which happened while combine_after_change_calls
42 was non-nil. We use this to decide how to call them
43 once the deferral ends.
45 In each element.
46 BEG-UNCHANGED is the number of chars before the changed range.
47 END-UNCHANGED is the number of chars after the changed range,
48 and CHANGE-AMOUNT is the number of characters inserted by the change
49 (negative for a deletion). */
50 static Lisp_Object combine_after_change_list;
52 /* Buffer which combine_after_change_list is about. */
53 static Lisp_Object combine_after_change_buffer;
55 Lisp_Object Qinhibit_modification_hooks;
57 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
59 /* Also used in marker.c to enable expensive marker checks. */
61 #ifdef MARKER_DEBUG
63 static void
64 check_markers (void)
66 struct Lisp_Marker *tail;
67 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
69 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
71 if (tail->buffer->text != current_buffer->text)
72 emacs_abort ();
73 if (tail->charpos > Z)
74 emacs_abort ();
75 if (tail->bytepos > Z_BYTE)
76 emacs_abort ();
77 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
78 emacs_abort ();
82 #else /* not MARKER_DEBUG */
84 #define check_markers() do { } while (0)
86 #endif /* MARKER_DEBUG */
88 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
89 Note that this can quit! */
91 void
92 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
94 eassert (charpos == BYTE_TO_CHAR (bytepos)
95 && bytepos == CHAR_TO_BYTE (charpos));
96 if (bytepos < GPT_BYTE)
97 gap_left (charpos, bytepos, 0);
98 else if (bytepos > GPT_BYTE)
99 gap_right (charpos, bytepos);
102 /* Move the gap to a position less than the current GPT.
103 BYTEPOS describes the new position as a byte position,
104 and CHARPOS is the corresponding char position.
105 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
107 static void
108 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
110 unsigned char *to, *from;
111 ptrdiff_t i;
112 ptrdiff_t new_s1;
114 if (!newgap)
115 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
117 i = GPT_BYTE;
118 to = GAP_END_ADDR;
119 from = GPT_ADDR;
120 new_s1 = GPT_BYTE;
122 /* Now copy the characters. To move the gap down,
123 copy characters up. */
125 while (1)
127 /* I gets number of characters left to copy. */
128 i = new_s1 - bytepos;
129 if (i == 0)
130 break;
131 /* If a quit is requested, stop copying now.
132 Change BYTEPOS to be where we have actually moved the gap to. */
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 if (QUITP)
188 bytepos = new_s1;
189 charpos = BYTE_TO_CHAR (bytepos);
190 break;
192 /* Move at most 32000 chars before checking again for a quit. */
193 if (i > 32000)
194 i = 32000;
195 new_s1 += i;
196 memmove (to, from, i);
197 from += i, to += i;
200 GPT = charpos;
201 GPT_BYTE = bytepos;
202 eassert (charpos <= bytepos);
203 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
204 QUIT;
207 /* If the selected window's old pointm is adjacent or covered by the
208 region from FROM to TO, unsuspend auto hscroll in that window. */
210 static void
211 adjust_suspend_auto_hscroll (ptrdiff_t from, ptrdiff_t to)
213 if (WINDOWP (selected_window))
215 struct window *w = XWINDOW (selected_window);
217 if (BUFFERP (w->contents)
218 && XBUFFER (w->contents) == current_buffer
219 && XMARKER (w->old_pointm)->charpos >= from
220 && XMARKER (w->old_pointm)->charpos <= to)
221 w->suspend_auto_hscroll = 0;
226 /* Adjust all markers for a deletion
227 whose range in bytes is FROM_BYTE to TO_BYTE.
228 The range in charpos is FROM to TO.
230 This function assumes that the gap is adjacent to
231 or inside of the range being deleted. */
233 void
234 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
235 ptrdiff_t to, ptrdiff_t to_byte)
237 struct Lisp_Marker *m;
238 ptrdiff_t charpos;
240 adjust_suspend_auto_hscroll (from, to);
241 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
243 charpos = m->charpos;
244 eassert (charpos <= Z);
246 /* If the marker is after the deletion,
247 relocate by number of chars / bytes deleted. */
248 if (charpos > to)
250 m->charpos -= to - from;
251 m->bytepos -= to_byte - from_byte;
253 /* Here's the case where a marker is inside text being deleted. */
254 else if (charpos > from)
256 m->charpos = from;
257 m->bytepos = from_byte;
263 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
264 to TO / TO_BYTE. We have to relocate the charpos of every marker
265 that points after the insertion (but not their bytepos).
267 When a marker points at the insertion point,
268 we advance it if either its insertion-type is t
269 or BEFORE_MARKERS is true. */
271 static void
272 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
273 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
275 struct Lisp_Marker *m;
276 bool adjusted = 0;
277 ptrdiff_t nchars = to - from;
278 ptrdiff_t nbytes = to_byte - from_byte;
280 adjust_suspend_auto_hscroll (from, to);
281 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
283 eassert (m->bytepos >= m->charpos
284 && m->bytepos - m->charpos <= Z_BYTE - Z);
286 if (m->bytepos == from_byte)
288 if (m->insertion_type || before_markers)
290 m->bytepos = to_byte;
291 m->charpos = to;
292 if (m->insertion_type)
293 adjusted = 1;
296 else if (m->bytepos > from_byte)
298 m->bytepos += nbytes;
299 m->charpos += nchars;
303 /* Adjusting only markers whose insertion-type is t may result in
304 - disordered start and end in overlays, and
305 - disordered overlays in the slot `overlays_before' of current_buffer. */
306 if (adjusted)
308 fix_start_end_in_overlays (from, to);
309 fix_overlays_before (current_buffer, from, to);
313 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
315 This is used only when the value of point changes due to an insert
316 or delete; it does not represent a conceptual change in point as a
317 marker. In particular, point is not crossing any interval
318 boundaries, so there's no need to use the usual SET_PT macro. In
319 fact it would be incorrect to do so, because either the old or the
320 new value of point is out of sync with the current set of
321 intervals. */
323 static void
324 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
326 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
327 /* In a single-byte buffer, the two positions must be equal. */
328 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
331 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
332 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
333 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
334 an insertion. */
336 static void
337 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
338 ptrdiff_t old_chars, ptrdiff_t old_bytes,
339 ptrdiff_t new_chars, ptrdiff_t new_bytes)
341 register struct Lisp_Marker *m;
342 ptrdiff_t prev_to_byte = from_byte + old_bytes;
343 ptrdiff_t diff_chars = new_chars - old_chars;
344 ptrdiff_t diff_bytes = new_bytes - old_bytes;
346 adjust_suspend_auto_hscroll (from, from + old_chars);
347 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
349 if (m->bytepos >= prev_to_byte)
351 m->charpos += diff_chars;
352 m->bytepos += diff_bytes;
354 else if (m->bytepos > from_byte)
356 m->charpos = from;
357 m->bytepos = from_byte;
361 check_markers ();
365 void
366 buffer_overflow (void)
368 error ("Maximum buffer size exceeded");
371 /* Make the gap NBYTES_ADDED bytes longer. */
373 static void
374 make_gap_larger (ptrdiff_t nbytes_added)
376 Lisp_Object tem;
377 ptrdiff_t real_gap_loc;
378 ptrdiff_t real_gap_loc_byte;
379 ptrdiff_t old_gap_size;
380 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
382 if (BUF_BYTES_MAX - current_size < nbytes_added)
383 buffer_overflow ();
385 /* If we have to get more space, get enough to last a while;
386 but do not exceed the maximum buffer size. */
387 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
388 BUF_BYTES_MAX - current_size);
390 enlarge_buffer_text (current_buffer, nbytes_added);
392 /* Prevent quitting in move_gap. */
393 tem = Vinhibit_quit;
394 Vinhibit_quit = Qt;
396 real_gap_loc = GPT;
397 real_gap_loc_byte = GPT_BYTE;
398 old_gap_size = GAP_SIZE;
400 /* Call the newly allocated space a gap at the end of the whole space. */
401 GPT = Z + GAP_SIZE;
402 GPT_BYTE = Z_BYTE + GAP_SIZE;
403 GAP_SIZE = nbytes_added;
405 /* Move the new gap down to be consecutive with the end of the old one. */
406 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
408 /* Now combine the two into one large gap. */
409 GAP_SIZE += old_gap_size;
410 GPT = real_gap_loc;
411 GPT_BYTE = real_gap_loc_byte;
413 /* Put an anchor. */
414 *(Z_ADDR) = 0;
416 Vinhibit_quit = tem;
419 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
421 /* Make the gap NBYTES_REMOVED bytes shorter. */
423 static void
424 make_gap_smaller (ptrdiff_t nbytes_removed)
426 Lisp_Object tem;
427 ptrdiff_t real_gap_loc;
428 ptrdiff_t real_gap_loc_byte;
429 ptrdiff_t real_Z;
430 ptrdiff_t real_Z_byte;
431 ptrdiff_t real_beg_unchanged;
432 ptrdiff_t new_gap_size;
434 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
435 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
436 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
438 /* Prevent quitting in move_gap. */
439 tem = Vinhibit_quit;
440 Vinhibit_quit = Qt;
442 real_gap_loc = GPT;
443 real_gap_loc_byte = GPT_BYTE;
444 new_gap_size = GAP_SIZE - nbytes_removed;
445 real_Z = Z;
446 real_Z_byte = Z_BYTE;
447 real_beg_unchanged = BEG_UNCHANGED;
449 /* Pretend that the last unwanted part of the gap is the entire gap,
450 and that the first desired part of the gap is part of the buffer
451 text. */
452 memset (GPT_ADDR, 0, new_gap_size);
453 GPT += new_gap_size;
454 GPT_BYTE += new_gap_size;
455 Z += new_gap_size;
456 Z_BYTE += new_gap_size;
457 GAP_SIZE = nbytes_removed;
459 /* Move the unwanted pretend gap to the end of the buffer. */
460 gap_right (Z, Z_BYTE);
462 enlarge_buffer_text (current_buffer, -nbytes_removed);
464 /* Now restore the desired gap. */
465 GAP_SIZE = new_gap_size;
466 GPT = real_gap_loc;
467 GPT_BYTE = real_gap_loc_byte;
468 Z = real_Z;
469 Z_BYTE = real_Z_byte;
470 BEG_UNCHANGED = real_beg_unchanged;
472 /* Put an anchor. */
473 *(Z_ADDR) = 0;
475 Vinhibit_quit = tem;
478 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
480 void
481 make_gap (ptrdiff_t nbytes_added)
483 if (nbytes_added >= 0)
484 make_gap_larger (nbytes_added);
485 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
486 else
487 make_gap_smaller (-nbytes_added);
488 #endif
491 /* Add NBYTES to B's gap. It's enough to temporarily
492 fake current_buffer and avoid real switch to B. */
494 void
495 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
497 struct buffer *oldb = current_buffer;
499 current_buffer = b;
500 make_gap (nbytes);
501 current_buffer = oldb;
504 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
505 FROM_MULTIBYTE says whether the incoming text is multibyte.
506 TO_MULTIBYTE says whether to store the text as multibyte.
507 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
509 Return the number of bytes stored at TO_ADDR. */
511 ptrdiff_t
512 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
513 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
515 if (from_multibyte == to_multibyte)
517 memcpy (to_addr, from_addr, nbytes);
518 return nbytes;
520 else if (from_multibyte)
522 ptrdiff_t nchars = 0;
523 ptrdiff_t bytes_left = nbytes;
525 while (bytes_left > 0)
527 int thislen, c;
528 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
529 if (! ASCII_CHAR_P (c))
530 c &= 0xFF;
531 *to_addr++ = c;
532 from_addr += thislen;
533 bytes_left -= thislen;
534 nchars++;
536 return nchars;
538 else
540 unsigned char *initial_to_addr = to_addr;
542 /* Convert single-byte to multibyte. */
543 while (nbytes > 0)
545 int c = *from_addr++;
547 if (!ASCII_CHAR_P (c))
549 c = BYTE8_TO_CHAR (c);
550 to_addr += CHAR_STRING (c, to_addr);
551 nbytes--;
553 else
554 /* Special case for speed. */
555 *to_addr++ = c, nbytes--;
557 return to_addr - initial_to_addr;
561 /* Insert a string of specified length before point.
562 This function judges multibyteness based on
563 enable_multibyte_characters in the current buffer;
564 it never converts between single-byte and multibyte.
566 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
567 prepare_to_modify_buffer could relocate the text. */
569 void
570 insert (const char *string, ptrdiff_t nbytes)
572 if (nbytes > 0)
574 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
575 insert_1_both (string, len, nbytes, 0, 1, 0);
576 opoint = PT - len;
577 signal_after_change (opoint, 0, len);
578 update_compositions (opoint, PT, CHECK_BORDER);
582 /* Likewise, but inherit text properties from neighboring characters. */
584 void
585 insert_and_inherit (const char *string, ptrdiff_t nbytes)
587 if (nbytes > 0)
589 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
590 insert_1_both (string, len, nbytes, 1, 1, 0);
591 opoint = PT - len;
592 signal_after_change (opoint, 0, len);
593 update_compositions (opoint, PT, CHECK_BORDER);
597 /* Insert the character C before point. Do not inherit text properties. */
599 void
600 insert_char (int c)
602 unsigned char str[MAX_MULTIBYTE_LENGTH];
603 int len;
605 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
606 len = CHAR_STRING (c, str);
607 else
609 len = 1;
610 str[0] = c;
613 insert ((char *) str, len);
616 /* Insert the null-terminated string S before point. */
618 void
619 insert_string (const char *s)
621 insert (s, strlen (s));
624 /* Like `insert' except that all markers pointing at the place where
625 the insertion happens are adjusted to point after it.
626 Don't use this function to insert part of a Lisp string,
627 since gc could happen and relocate it. */
629 void
630 insert_before_markers (const char *string, ptrdiff_t nbytes)
632 if (nbytes > 0)
634 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
635 insert_1_both (string, len, nbytes, 0, 1, 1);
636 opoint = PT - len;
637 signal_after_change (opoint, 0, len);
638 update_compositions (opoint, PT, CHECK_BORDER);
642 /* Likewise, but inherit text properties from neighboring characters. */
644 void
645 insert_before_markers_and_inherit (const char *string,
646 ptrdiff_t nbytes)
648 if (nbytes > 0)
650 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
651 insert_1_both (string, len, nbytes, 1, 1, 1);
652 opoint = PT - len;
653 signal_after_change (opoint, 0, len);
654 update_compositions (opoint, PT, CHECK_BORDER);
658 #ifdef BYTE_COMBINING_DEBUG
660 /* See if the bytes before POS/POS_BYTE combine with bytes
661 at the start of STRING to form a single character.
662 If so, return the number of bytes at the start of STRING
663 which combine in this way. Otherwise, return 0. */
666 count_combining_before (const unsigned char *string, ptrdiff_t length,
667 ptrdiff_t pos, ptrdiff_t pos_byte)
669 int len, combining_bytes;
670 const unsigned char *p;
672 if (NILP (current_buffer->enable_multibyte_characters))
673 return 0;
675 /* At first, we can exclude the following cases:
676 (1) STRING[0] can't be a following byte of multibyte sequence.
677 (2) POS is the start of the current buffer.
678 (3) A character before POS is not a multibyte character. */
679 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
680 return 0;
681 if (pos_byte == BEG_BYTE) /* case (2) */
682 return 0;
683 len = 1;
684 p = BYTE_POS_ADDR (pos_byte - 1);
685 while (! CHAR_HEAD_P (*p)) p--, len++;
686 if (! LEADING_CODE_P (*p)) /* case (3) */
687 return 0;
689 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
690 if (combining_bytes <= 0)
691 /* The character preceding POS is, complete and no room for
692 combining bytes (combining_bytes == 0), or an independent 8-bit
693 character (combining_bytes < 0). */
694 return 0;
696 /* We have a combination situation. Count the bytes at STRING that
697 may combine. */
698 p = string + 1;
699 while (!CHAR_HEAD_P (*p) && p < string + length)
700 p++;
702 return (combining_bytes < p - string ? combining_bytes : p - string);
705 /* See if the bytes after POS/POS_BYTE combine with bytes
706 at the end of STRING to form a single character.
707 If so, return the number of bytes after POS/POS_BYTE
708 which combine in this way. Otherwise, return 0. */
711 count_combining_after (const unsigned char *string,
712 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
714 ptrdiff_t opos_byte = pos_byte;
715 ptrdiff_t i;
716 ptrdiff_t bytes;
717 unsigned char *bufp;
719 if (NILP (current_buffer->enable_multibyte_characters))
720 return 0;
722 /* At first, we can exclude the following cases:
723 (1) The last byte of STRING is an ASCII.
724 (2) POS is the last of the current buffer.
725 (3) A character at POS can't be a following byte of multibyte
726 character. */
727 if (length > 0 && ASCII_CHAR_P (string[length - 1])) /* case (1) */
728 return 0;
729 if (pos_byte == Z_BYTE) /* case (2) */
730 return 0;
731 bufp = BYTE_POS_ADDR (pos_byte);
732 if (CHAR_HEAD_P (*bufp)) /* case (3) */
733 return 0;
735 i = length - 1;
736 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
738 i--;
740 if (i < 0)
742 /* All characters in STRING are not character head. We must
743 check also preceding bytes at POS. We are sure that the gap
744 is at POS. */
745 unsigned char *p = BEG_ADDR;
746 i = pos_byte - 2;
747 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
748 i--;
749 if (i < 0 || !LEADING_CODE_P (p[i]))
750 return 0;
752 bytes = BYTES_BY_CHAR_HEAD (p[i]);
753 return (bytes <= pos_byte - 1 - i + length
755 : bytes - (pos_byte - 1 - i + length));
757 if (!LEADING_CODE_P (string[i]))
758 return 0;
760 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
761 bufp++, pos_byte++;
762 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
764 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
767 #endif
770 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
771 starting at STRING. INHERIT non-zero means inherit the text
772 properties from neighboring characters; zero means inserted text
773 will have no text properties. PREPARE non-zero means call
774 prepare_to_modify_buffer, which checks that the region is not
775 read-only, and calls before-change-function and any modification
776 properties the text may have. BEFORE_MARKERS non-zero means adjust
777 all markers that point at the insertion place to point after it. */
779 void
780 insert_1_both (const char *string,
781 ptrdiff_t nchars, ptrdiff_t nbytes,
782 bool inherit, bool prepare, bool before_markers)
784 if (nchars == 0)
785 return;
787 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
788 nchars = nbytes;
790 if (prepare)
791 /* Do this before moving and increasing the gap,
792 because the before-change hooks might move the gap
793 or make it smaller. */
794 prepare_to_modify_buffer (PT, PT, NULL);
796 if (PT != GPT)
797 move_gap_both (PT, PT_BYTE);
798 if (GAP_SIZE < nbytes)
799 make_gap (nbytes - GAP_SIZE);
801 #ifdef BYTE_COMBINING_DEBUG
802 if (count_combining_before (string, nbytes, PT, PT_BYTE)
803 || count_combining_after (string, nbytes, PT, PT_BYTE))
804 emacs_abort ();
805 #endif
807 /* Record deletion of the surrounding text that combines with
808 the insertion. This, together with recording the insertion,
809 will add up to the right stuff in the undo list. */
810 record_insert (PT, nchars);
811 MODIFF++;
812 CHARS_MODIFF = MODIFF;
814 memcpy (GPT_ADDR, string, nbytes);
816 GAP_SIZE -= nbytes;
817 GPT += nchars;
818 ZV += nchars;
819 Z += nchars;
820 GPT_BYTE += nbytes;
821 ZV_BYTE += nbytes;
822 Z_BYTE += nbytes;
823 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
825 eassert (GPT <= GPT_BYTE);
827 /* The insert may have been in the unchanged region, so check again. */
828 if (Z - GPT < END_UNCHANGED)
829 END_UNCHANGED = Z - GPT;
831 adjust_overlays_for_insert (PT, nchars);
832 adjust_markers_for_insert (PT, PT_BYTE,
833 PT + nchars, PT_BYTE + nbytes,
834 before_markers);
836 offset_intervals (current_buffer, PT, nchars);
838 if (!inherit && buffer_intervals (current_buffer))
839 set_text_properties (make_number (PT), make_number (PT + nchars),
840 Qnil, Qnil, Qnil);
842 adjust_point (nchars, nbytes);
844 check_markers ();
847 /* Insert the part of the text of STRING, a Lisp object assumed to be
848 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
849 starting at position POS / POS_BYTE. If the text of STRING has properties,
850 copy them into the buffer.
852 It does not work to use `insert' for this, because a GC could happen
853 before we copy the stuff into the buffer, and relocate the string
854 without insert noticing. */
856 void
857 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
858 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
860 ptrdiff_t opoint = PT;
862 if (SCHARS (string) == 0)
863 return;
865 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
866 inherit, 0);
867 signal_after_change (opoint, 0, PT - opoint);
868 update_compositions (opoint, PT, CHECK_BORDER);
871 /* Like `insert_from_string' except that all markers pointing
872 at the place where the insertion happens are adjusted to point after it. */
874 void
875 insert_from_string_before_markers (Lisp_Object string,
876 ptrdiff_t pos, ptrdiff_t pos_byte,
877 ptrdiff_t length, ptrdiff_t length_byte,
878 bool inherit)
880 ptrdiff_t opoint = PT;
882 if (SCHARS (string) == 0)
883 return;
885 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
886 inherit, 1);
887 signal_after_change (opoint, 0, PT - opoint);
888 update_compositions (opoint, PT, CHECK_BORDER);
891 /* Subroutine of the insertion functions above. */
893 static void
894 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
895 ptrdiff_t nchars, ptrdiff_t nbytes,
896 bool inherit, bool before_markers)
898 struct gcpro gcpro1;
899 ptrdiff_t outgoing_nbytes = nbytes;
900 INTERVAL intervals;
902 /* Make OUTGOING_NBYTES describe the text
903 as it will be inserted in this buffer. */
905 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
906 outgoing_nbytes = nchars;
907 else if (! STRING_MULTIBYTE (string))
908 outgoing_nbytes
909 = count_size_as_multibyte (SDATA (string) + pos_byte,
910 nbytes);
912 GCPRO1 (string);
913 /* Do this before moving and increasing the gap,
914 because the before-change hooks might move the gap
915 or make it smaller. */
916 prepare_to_modify_buffer (PT, PT, NULL);
918 if (PT != GPT)
919 move_gap_both (PT, PT_BYTE);
920 if (GAP_SIZE < outgoing_nbytes)
921 make_gap (outgoing_nbytes - GAP_SIZE);
922 UNGCPRO;
924 /* Copy the string text into the buffer, perhaps converting
925 between single-byte and multibyte. */
926 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
927 STRING_MULTIBYTE (string),
928 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
930 #ifdef BYTE_COMBINING_DEBUG
931 /* We have copied text into the gap, but we have not altered
932 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
933 to these functions and get the same results as we would
934 have got earlier on. Meanwhile, PT_ADDR does point to
935 the text that has been stored by copy_text. */
936 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
937 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
938 emacs_abort ();
939 #endif
941 record_insert (PT, nchars);
942 MODIFF++;
943 CHARS_MODIFF = MODIFF;
945 GAP_SIZE -= outgoing_nbytes;
946 GPT += nchars;
947 ZV += nchars;
948 Z += nchars;
949 GPT_BYTE += outgoing_nbytes;
950 ZV_BYTE += outgoing_nbytes;
951 Z_BYTE += outgoing_nbytes;
952 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
954 eassert (GPT <= GPT_BYTE);
956 /* The insert may have been in the unchanged region, so check again. */
957 if (Z - GPT < END_UNCHANGED)
958 END_UNCHANGED = Z - GPT;
960 adjust_overlays_for_insert (PT, nchars);
961 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
962 PT_BYTE + outgoing_nbytes,
963 before_markers);
965 offset_intervals (current_buffer, PT, nchars);
967 intervals = string_intervals (string);
968 /* Get the intervals for the part of the string we are inserting. */
969 if (nbytes < SBYTES (string))
970 intervals = copy_intervals (intervals, pos, nchars);
972 /* Insert those intervals. */
973 graft_intervals_into_buffer (intervals, PT, nchars,
974 current_buffer, inherit);
976 adjust_point (nchars, outgoing_nbytes);
978 check_markers ();
981 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
982 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
983 GPT_ADDR (if not text_at_gap_tail). */
985 void
986 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
988 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
990 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
991 nchars = nbytes;
993 /* No need to call prepare_to_modify_buffer, since this is called
994 from places that replace some region with a different text, so
995 prepare_to_modify_buffer was already called by the deletion part
996 of this dance. */
997 invalidate_buffer_caches (current_buffer, GPT, GPT);
998 record_insert (GPT, nchars);
999 MODIFF++;
1001 GAP_SIZE -= nbytes;
1002 if (! text_at_gap_tail)
1004 GPT += nchars;
1005 GPT_BYTE += nbytes;
1007 ZV += nchars;
1008 Z += nchars;
1009 ZV_BYTE += nbytes;
1010 Z_BYTE += nbytes;
1011 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1013 eassert (GPT <= GPT_BYTE);
1015 adjust_overlays_for_insert (ins_charpos, nchars);
1016 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1017 ins_charpos + nchars, ins_bytepos + nbytes, 0);
1019 if (buffer_intervals (current_buffer))
1021 offset_intervals (current_buffer, ins_charpos, nchars);
1022 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1023 current_buffer, 0);
1026 if (ins_charpos < PT)
1027 adjust_point (nchars, nbytes);
1029 check_markers ();
1032 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1033 current buffer. If the text in BUF has properties, they are absorbed
1034 into the current buffer.
1036 It does not work to use `insert' for this, because a malloc could happen
1037 and relocate BUF's text before the copy happens. */
1039 void
1040 insert_from_buffer (struct buffer *buf,
1041 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1043 ptrdiff_t opoint = PT;
1045 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1046 signal_after_change (opoint, 0, PT - opoint);
1047 update_compositions (opoint, PT, CHECK_BORDER);
1050 static void
1051 insert_from_buffer_1 (struct buffer *buf,
1052 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1054 ptrdiff_t chunk, chunk_expanded;
1055 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1056 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1057 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1058 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1059 INTERVAL intervals;
1061 if (nchars == 0)
1062 return;
1064 /* Make OUTGOING_NBYTES describe the text
1065 as it will be inserted in this buffer. */
1067 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1068 outgoing_nbytes = nchars;
1069 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1071 ptrdiff_t outgoing_before_gap = 0;
1072 ptrdiff_t outgoing_after_gap = 0;
1074 if (from < BUF_GPT (buf))
1076 chunk = BUF_GPT_BYTE (buf) - from_byte;
1077 if (chunk > incoming_nbytes)
1078 chunk = incoming_nbytes;
1079 outgoing_before_gap
1080 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1081 chunk);
1083 else
1084 chunk = 0;
1086 if (chunk < incoming_nbytes)
1087 outgoing_after_gap
1088 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1089 from_byte + chunk),
1090 incoming_nbytes - chunk);
1092 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1095 /* Do this before moving and increasing the gap,
1096 because the before-change hooks might move the gap
1097 or make it smaller. */
1098 prepare_to_modify_buffer (PT, PT, NULL);
1100 if (PT != GPT)
1101 move_gap_both (PT, PT_BYTE);
1102 if (GAP_SIZE < outgoing_nbytes)
1103 make_gap (outgoing_nbytes - GAP_SIZE);
1105 if (from < BUF_GPT (buf))
1107 chunk = BUF_GPT_BYTE (buf) - from_byte;
1108 if (chunk > incoming_nbytes)
1109 chunk = incoming_nbytes;
1110 /* Record number of output bytes, so we know where
1111 to put the output from the second copy_text. */
1112 chunk_expanded
1113 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1114 GPT_ADDR, chunk,
1115 ! NILP (BVAR (buf, enable_multibyte_characters)),
1116 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1118 else
1119 chunk_expanded = chunk = 0;
1121 if (chunk < incoming_nbytes)
1122 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1123 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1124 ! NILP (BVAR (buf, enable_multibyte_characters)),
1125 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1127 #ifdef BYTE_COMBINING_DEBUG
1128 /* We have copied text into the gap, but we have not altered
1129 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1130 to these functions and get the same results as we would
1131 have got earlier on. Meanwhile, GPT_ADDR does point to
1132 the text that has been stored by copy_text. */
1133 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1134 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1135 emacs_abort ();
1136 #endif
1138 record_insert (PT, nchars);
1139 MODIFF++;
1140 CHARS_MODIFF = MODIFF;
1142 GAP_SIZE -= outgoing_nbytes;
1143 GPT += nchars;
1144 ZV += nchars;
1145 Z += nchars;
1146 GPT_BYTE += outgoing_nbytes;
1147 ZV_BYTE += outgoing_nbytes;
1148 Z_BYTE += outgoing_nbytes;
1149 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1151 eassert (GPT <= GPT_BYTE);
1153 /* The insert may have been in the unchanged region, so check again. */
1154 if (Z - GPT < END_UNCHANGED)
1155 END_UNCHANGED = Z - GPT;
1157 adjust_overlays_for_insert (PT, nchars);
1158 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1159 PT_BYTE + outgoing_nbytes,
1162 offset_intervals (current_buffer, PT, nchars);
1164 /* Get the intervals for the part of the string we are inserting. */
1165 intervals = buffer_intervals (buf);
1166 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1168 if (buf == current_buffer && PT <= from)
1169 from += nchars;
1170 intervals = copy_intervals (intervals, from, nchars);
1173 /* Insert those intervals. */
1174 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1176 adjust_point (nchars, outgoing_nbytes);
1179 /* Record undo information and adjust markers and position keepers for
1180 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1181 chars (LEN_BYTE bytes) which resides in the gap just after
1182 GPT_ADDR.
1184 PREV_TEXT nil means the new text was just inserted. */
1186 static void
1187 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1188 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1190 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1192 #ifdef BYTE_COMBINING_DEBUG
1193 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1194 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1195 emacs_abort ();
1196 #endif
1198 if (STRINGP (prev_text))
1200 nchars_del = SCHARS (prev_text);
1201 nbytes_del = SBYTES (prev_text);
1204 /* Update various buffer positions for the new text. */
1205 GAP_SIZE -= len_byte;
1206 ZV += len; Z += len;
1207 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1208 GPT += len; GPT_BYTE += len_byte;
1209 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1211 if (nchars_del > 0)
1212 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1213 len, len_byte);
1214 else
1215 adjust_markers_for_insert (from, from_byte,
1216 from + len, from_byte + len_byte, 0);
1218 if (nchars_del > 0)
1219 record_delete (from, prev_text, false);
1220 record_insert (from, len);
1222 if (len > nchars_del)
1223 adjust_overlays_for_insert (from, len - nchars_del);
1224 else if (len < nchars_del)
1225 adjust_overlays_for_delete (from, nchars_del - len);
1227 offset_intervals (current_buffer, from, len - nchars_del);
1229 if (from < PT)
1230 adjust_point (len - nchars_del, len_byte - nbytes_del);
1232 /* As byte combining will decrease Z, we must check this again. */
1233 if (Z - GPT < END_UNCHANGED)
1234 END_UNCHANGED = Z - GPT;
1236 check_markers ();
1238 if (len == 0)
1239 evaporate_overlays (from);
1240 MODIFF++;
1241 CHARS_MODIFF = MODIFF;
1244 /* Record undo information, adjust markers and position keepers for an
1245 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1246 text already exists in the current buffer but character length (TO
1247 - FROM) may be incorrect, the correct length is NEWLEN. */
1249 void
1250 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1251 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1253 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1255 if (GPT != to)
1256 move_gap_both (to, to_byte);
1257 GAP_SIZE += len_byte;
1258 GPT -= len; GPT_BYTE -= len_byte;
1259 ZV -= len; ZV_BYTE -= len_byte;
1260 Z -= len; Z_BYTE -= len_byte;
1261 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1264 /* Replace the text from character positions FROM to TO with NEW,
1265 If PREPARE, call prepare_to_modify_buffer.
1266 If INHERIT, the newly inserted text should inherit text properties
1267 from the surrounding non-deleted text. */
1269 /* Note that this does not yet handle markers quite right.
1270 Also it needs to record a single undo-entry that does a replacement
1271 rather than a separate delete and insert.
1272 That way, undo will also handle markers properly.
1274 But if MARKERS is 0, don't relocate markers. */
1276 void
1277 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1278 bool prepare, bool inherit, bool markers)
1280 ptrdiff_t inschars = SCHARS (new);
1281 ptrdiff_t insbytes = SBYTES (new);
1282 ptrdiff_t from_byte, to_byte;
1283 ptrdiff_t nbytes_del, nchars_del;
1284 struct gcpro gcpro1;
1285 INTERVAL intervals;
1286 ptrdiff_t outgoing_insbytes = insbytes;
1287 Lisp_Object deletion;
1289 check_markers ();
1291 GCPRO1 (new);
1292 deletion = Qnil;
1294 if (prepare)
1296 ptrdiff_t range_length = to - from;
1297 prepare_to_modify_buffer (from, to, &from);
1298 to = from + range_length;
1301 UNGCPRO;
1303 /* Make args be valid. */
1304 if (from < BEGV)
1305 from = BEGV;
1306 if (to > ZV)
1307 to = ZV;
1309 from_byte = CHAR_TO_BYTE (from);
1310 to_byte = CHAR_TO_BYTE (to);
1312 nchars_del = to - from;
1313 nbytes_del = to_byte - from_byte;
1315 if (nbytes_del <= 0 && insbytes == 0)
1316 return;
1318 /* Make OUTGOING_INSBYTES describe the text
1319 as it will be inserted in this buffer. */
1321 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1322 outgoing_insbytes = inschars;
1323 else if (! STRING_MULTIBYTE (new))
1324 outgoing_insbytes
1325 = count_size_as_multibyte (SDATA (new), insbytes);
1327 GCPRO1 (new);
1329 /* Make sure the gap is somewhere in or next to what we are deleting. */
1330 if (from > GPT)
1331 gap_right (from, from_byte);
1332 if (to < GPT)
1333 gap_left (to, to_byte, 0);
1335 /* Even if we don't record for undo, we must keep the original text
1336 because we may have to recover it because of inappropriate byte
1337 combining. */
1338 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1339 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1341 GAP_SIZE += nbytes_del;
1342 ZV -= nchars_del;
1343 Z -= nchars_del;
1344 ZV_BYTE -= nbytes_del;
1345 Z_BYTE -= nbytes_del;
1346 GPT = from;
1347 GPT_BYTE = from_byte;
1348 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1350 eassert (GPT <= GPT_BYTE);
1352 if (GPT - BEG < BEG_UNCHANGED)
1353 BEG_UNCHANGED = GPT - BEG;
1354 if (Z - GPT < END_UNCHANGED)
1355 END_UNCHANGED = Z - GPT;
1357 if (GAP_SIZE < outgoing_insbytes)
1358 make_gap (outgoing_insbytes - GAP_SIZE);
1360 /* Copy the string text into the buffer, perhaps converting
1361 between single-byte and multibyte. */
1362 copy_text (SDATA (new), GPT_ADDR, insbytes,
1363 STRING_MULTIBYTE (new),
1364 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1366 #ifdef BYTE_COMBINING_DEBUG
1367 /* We have copied text into the gap, but we have not marked
1368 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1369 here, for both the previous text and the following text.
1370 Meanwhile, GPT_ADDR does point to
1371 the text that has been stored by copy_text. */
1372 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1373 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1374 emacs_abort ();
1375 #endif
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 if (!NILP (deletion))
1383 record_insert (from + SCHARS (deletion), inschars);
1384 record_delete (from, deletion, false);
1387 GAP_SIZE -= outgoing_insbytes;
1388 GPT += inschars;
1389 ZV += inschars;
1390 Z += inschars;
1391 GPT_BYTE += outgoing_insbytes;
1392 ZV_BYTE += outgoing_insbytes;
1393 Z_BYTE += outgoing_insbytes;
1394 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1396 eassert (GPT <= GPT_BYTE);
1398 /* Adjust markers for the deletion and the insertion. */
1399 if (markers)
1400 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1401 inschars, outgoing_insbytes);
1403 /* Adjust the overlay center as needed. This must be done after
1404 adjusting the markers that bound the overlays. */
1405 adjust_overlays_for_delete (from, nchars_del);
1406 adjust_overlays_for_insert (from, inschars);
1408 offset_intervals (current_buffer, from, inschars - nchars_del);
1410 /* Get the intervals for the part of the string we are inserting--
1411 not including the combined-before bytes. */
1412 intervals = string_intervals (new);
1413 /* Insert those intervals. */
1414 graft_intervals_into_buffer (intervals, from, inschars,
1415 current_buffer, inherit);
1417 /* Relocate point as if it were a marker. */
1418 if (from < PT)
1419 adjust_point ((from + inschars - (PT < to ? PT : to)),
1420 (from_byte + outgoing_insbytes
1421 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1423 if (outgoing_insbytes == 0)
1424 evaporate_overlays (from);
1426 check_markers ();
1428 MODIFF++;
1429 CHARS_MODIFF = MODIFF;
1430 UNGCPRO;
1432 signal_after_change (from, nchars_del, GPT - from);
1433 update_compositions (from, GPT, CHECK_BORDER);
1436 /* Replace the text from character positions FROM to TO with
1437 the text in INS of length INSCHARS.
1438 Keep the text properties that applied to the old characters
1439 (extending them to all the new chars if there are more new chars).
1441 Note that this does not yet handle markers quite right.
1443 If MARKERS, relocate markers.
1445 Unlike most functions at this level, never call
1446 prepare_to_modify_buffer and never call signal_after_change. */
1448 void
1449 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1450 ptrdiff_t to, ptrdiff_t to_byte,
1451 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1452 bool markers)
1454 ptrdiff_t nbytes_del, nchars_del;
1456 check_markers ();
1458 nchars_del = to - from;
1459 nbytes_del = to_byte - from_byte;
1461 if (nbytes_del <= 0 && insbytes == 0)
1462 return;
1464 /* Make sure the gap is somewhere in or next to what we are deleting. */
1465 if (from > GPT)
1466 gap_right (from, from_byte);
1467 if (to < GPT)
1468 gap_left (to, to_byte, 0);
1470 GAP_SIZE += nbytes_del;
1471 ZV -= nchars_del;
1472 Z -= nchars_del;
1473 ZV_BYTE -= nbytes_del;
1474 Z_BYTE -= nbytes_del;
1475 GPT = from;
1476 GPT_BYTE = from_byte;
1477 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1479 eassert (GPT <= GPT_BYTE);
1481 if (GPT - BEG < BEG_UNCHANGED)
1482 BEG_UNCHANGED = GPT - BEG;
1483 if (Z - GPT < END_UNCHANGED)
1484 END_UNCHANGED = Z - GPT;
1486 if (GAP_SIZE < insbytes)
1487 make_gap (insbytes - GAP_SIZE);
1489 /* Copy the replacement text into the buffer. */
1490 memcpy (GPT_ADDR, ins, insbytes);
1492 #ifdef BYTE_COMBINING_DEBUG
1493 /* We have copied text into the gap, but we have not marked
1494 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1495 here, for both the previous text and the following text.
1496 Meanwhile, GPT_ADDR does point to
1497 the text that has been stored by copy_text. */
1498 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1499 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1500 emacs_abort ();
1501 #endif
1503 GAP_SIZE -= insbytes;
1504 GPT += inschars;
1505 ZV += inschars;
1506 Z += inschars;
1507 GPT_BYTE += insbytes;
1508 ZV_BYTE += insbytes;
1509 Z_BYTE += insbytes;
1510 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1512 eassert (GPT <= GPT_BYTE);
1514 /* Adjust markers for the deletion and the insertion. */
1515 if (markers
1516 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1517 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1518 inschars, insbytes);
1520 /* Adjust the overlay center as needed. This must be done after
1521 adjusting the markers that bound the overlays. */
1522 if (nchars_del != inschars)
1524 adjust_overlays_for_insert (from, inschars);
1525 adjust_overlays_for_delete (from + inschars, nchars_del);
1528 offset_intervals (current_buffer, from, inschars - nchars_del);
1530 /* Relocate point as if it were a marker. */
1531 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1533 if (PT < to)
1534 /* PT was within the deleted text. Move it to FROM. */
1535 adjust_point (from - PT, from_byte - PT_BYTE);
1536 else
1537 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1540 if (insbytes == 0)
1541 evaporate_overlays (from);
1543 check_markers ();
1545 MODIFF++;
1546 CHARS_MODIFF = MODIFF;
1549 /* Delete characters in current buffer
1550 from FROM up to (but not including) TO.
1551 If TO comes before FROM, we delete nothing. */
1553 void
1554 del_range (ptrdiff_t from, ptrdiff_t to)
1556 del_range_1 (from, to, 1, 0);
1559 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1560 RET_STRING says to return the deleted text. */
1562 Lisp_Object
1563 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1565 ptrdiff_t from_byte, to_byte;
1566 Lisp_Object deletion;
1567 struct gcpro gcpro1;
1569 /* Make args be valid */
1570 if (from < BEGV)
1571 from = BEGV;
1572 if (to > ZV)
1573 to = ZV;
1575 if (to <= from)
1576 return Qnil;
1578 if (prepare)
1580 ptrdiff_t range_length = to - from;
1581 prepare_to_modify_buffer (from, to, &from);
1582 to = min (ZV, from + range_length);
1585 from_byte = CHAR_TO_BYTE (from);
1586 to_byte = CHAR_TO_BYTE (to);
1588 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1589 GCPRO1 (deletion);
1590 signal_after_change (from, to - from, 0);
1591 update_compositions (from, from, CHECK_HEAD);
1592 UNGCPRO;
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 Lisp_Object Qregion_extract_function;
1786 /* Check that it is okay to modify the buffer between START and END,
1787 which are char positions.
1789 Run the before-change-function, if any. If intervals are in use,
1790 verify that the text to be modified is not read-only, and call
1791 any modification properties the text may have.
1793 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1794 by holding its value temporarily in a marker. */
1796 void
1797 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1798 ptrdiff_t *preserve_ptr)
1800 struct buffer *base_buffer;
1801 Lisp_Object temp;
1803 XSETFASTINT (temp, start);
1804 if (!NILP (BVAR (current_buffer, read_only)))
1805 Fbarf_if_buffer_read_only (temp);
1807 bset_redisplay (current_buffer);
1809 if (buffer_intervals (current_buffer))
1811 if (preserve_ptr)
1813 Lisp_Object preserve_marker;
1814 struct gcpro gcpro1;
1815 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1816 GCPRO1 (preserve_marker);
1817 verify_interval_modification (current_buffer, start, end);
1818 *preserve_ptr = marker_position (preserve_marker);
1819 unchain_marker (XMARKER (preserve_marker));
1820 UNGCPRO;
1822 else
1823 verify_interval_modification (current_buffer, start, end);
1826 /* For indirect buffers, use the base buffer to check clashes. */
1827 if (current_buffer->base_buffer != 0)
1828 base_buffer = current_buffer->base_buffer;
1829 else
1830 base_buffer = current_buffer;
1832 if (inhibit_modification_hooks)
1833 return;
1835 if (!NILP (BVAR (base_buffer, file_truename))
1836 /* Make binding buffer-file-name to nil effective. */
1837 && !NILP (BVAR (base_buffer, filename))
1838 && SAVE_MODIFF >= MODIFF)
1839 lock_file (BVAR (base_buffer, file_truename));
1841 /* If `select-active-regions' is non-nil, save the region text. */
1842 /* FIXME: Move this to Elisp (via before-change-functions). */
1843 if (!NILP (BVAR (current_buffer, mark_active))
1844 && XMARKER (BVAR (current_buffer, mark))->buffer
1845 && NILP (Vsaved_region_selection)
1846 && (EQ (Vselect_active_regions, Qonly)
1847 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1848 : (!NILP (Vselect_active_regions)
1849 && !NILP (Vtransient_mark_mode))))
1850 Vsaved_region_selection
1851 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1853 signal_before_change (start, end, preserve_ptr);
1854 Vdeactivate_mark = Qt;
1857 /* Like above, but called when we know that the buffer text
1858 will be modified and region caches should be invalidated. */
1860 void
1861 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1862 ptrdiff_t *preserve_ptr)
1864 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1865 invalidate_buffer_caches (current_buffer, start, end);
1868 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1869 region between buffer positions START and END. */
1870 void
1871 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1873 /* Indirect buffers usually have their caches set to NULL, but we
1874 need to consider the caches of their base buffer. */
1875 if (buf->base_buffer)
1876 buf = buf->base_buffer;
1877 /* The bidi_paragraph_cache must be invalidated first, because doing
1878 so might need to use the newline_cache (via find_newline_no_quit,
1879 see below). */
1880 if (buf->bidi_paragraph_cache)
1882 if (start != end
1883 && start > BUF_BEG (buf))
1885 /* If we are deleting or replacing characters, we could
1886 create a paragraph start, because all of the characters
1887 from START to the beginning of START's line are
1888 whitespace. Therefore, we must extend the region to be
1889 invalidated up to the newline before START. */
1890 ptrdiff_t line_beg = start;
1891 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
1893 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
1895 struct buffer *old = current_buffer;
1897 set_buffer_internal (buf);
1899 line_beg = find_newline_no_quit (start, start_byte, -1,
1900 &start_byte);
1901 set_buffer_internal (old);
1903 start = line_beg - (line_beg > BUF_BEG (buf));
1905 invalidate_region_cache (buf,
1906 buf->bidi_paragraph_cache,
1907 start - BUF_BEG (buf), BUF_Z (buf) - end);
1909 if (buf->newline_cache)
1910 invalidate_region_cache (buf,
1911 buf->newline_cache,
1912 start - BUF_BEG (buf), BUF_Z (buf) - end);
1913 if (buf->width_run_cache)
1914 invalidate_region_cache (buf,
1915 buf->width_run_cache,
1916 start - BUF_BEG (buf), BUF_Z (buf) - end);
1919 /* These macros work with an argument named `preserve_ptr'
1920 and a local variable named `preserve_marker'. */
1922 #define PRESERVE_VALUE \
1923 if (preserve_ptr && NILP (preserve_marker)) \
1924 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1926 #define RESTORE_VALUE \
1927 if (! NILP (preserve_marker)) \
1929 *preserve_ptr = marker_position (preserve_marker); \
1930 unchain_marker (XMARKER (preserve_marker)); \
1933 #define PRESERVE_START_END \
1934 if (NILP (start_marker)) \
1935 start_marker = Fcopy_marker (start, Qnil); \
1936 if (NILP (end_marker)) \
1937 end_marker = Fcopy_marker (end, Qnil);
1939 #define FETCH_START \
1940 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1942 #define FETCH_END \
1943 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1945 /* Set a variable to nil if an error occurred.
1946 Don't change the variable if there was no error.
1947 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1948 VARIABLE is the variable to maybe set to nil.
1949 NO-ERROR-FLAG is nil if there was an error,
1950 anything else meaning no error (so this function does nothing). */
1951 struct rvoe_arg
1953 Lisp_Object *location;
1954 bool errorp;
1957 static void
1958 reset_var_on_error (void *ptr)
1960 struct rvoe_arg *p = ptr;
1961 if (p->errorp)
1962 *p->location = Qnil;
1965 /* Signal a change to the buffer immediately before it happens.
1966 START_INT and END_INT are the bounds of the text to be changed.
1968 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1969 by holding its value temporarily in a marker. */
1971 static void
1972 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1973 ptrdiff_t *preserve_ptr)
1975 Lisp_Object start, end;
1976 Lisp_Object start_marker, end_marker;
1977 Lisp_Object preserve_marker;
1978 struct gcpro gcpro1, gcpro2, gcpro3;
1979 ptrdiff_t count = SPECPDL_INDEX ();
1980 struct rvoe_arg rvoe_arg;
1982 start = make_number (start_int);
1983 end = make_number (end_int);
1984 preserve_marker = Qnil;
1985 start_marker = Qnil;
1986 end_marker = Qnil;
1987 GCPRO3 (preserve_marker, start_marker, end_marker);
1989 specbind (Qinhibit_modification_hooks, Qt);
1991 /* If buffer is unmodified, run a special hook for that case. The
1992 check for Vfirst_change_hook is just a minor optimization. */
1993 if (SAVE_MODIFF >= MODIFF
1994 && !NILP (Vfirst_change_hook))
1996 PRESERVE_VALUE;
1997 PRESERVE_START_END;
1998 Frun_hooks (1, &Qfirst_change_hook);
2001 /* Now run the before-change-functions if any. */
2002 if (!NILP (Vbefore_change_functions))
2004 Lisp_Object args[3];
2005 rvoe_arg.location = &Vbefore_change_functions;
2006 rvoe_arg.errorp = 1;
2008 PRESERVE_VALUE;
2009 PRESERVE_START_END;
2011 /* Mark before-change-functions to be reset to nil in case of error. */
2012 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2014 /* Actually run the hook functions. */
2015 args[0] = Qbefore_change_functions;
2016 args[1] = FETCH_START;
2017 args[2] = FETCH_END;
2018 Frun_hook_with_args (3, args);
2020 /* There was no error: unarm the reset_on_error. */
2021 rvoe_arg.errorp = 0;
2024 if (buffer_has_overlays ())
2026 PRESERVE_VALUE;
2027 report_overlay_modification (FETCH_START, FETCH_END, 0,
2028 FETCH_START, FETCH_END, Qnil);
2031 if (! NILP (start_marker))
2032 free_marker (start_marker);
2033 if (! NILP (end_marker))
2034 free_marker (end_marker);
2035 RESTORE_VALUE;
2036 UNGCPRO;
2038 unbind_to (count, Qnil);
2041 /* Signal a change immediately after it happens.
2042 CHARPOS is the character position of the start of the changed text.
2043 LENDEL is the number of characters of the text before the change.
2044 (Not the whole buffer; just the part that was changed.)
2045 LENINS is the number of characters in that part of the text
2046 after the change. */
2048 void
2049 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2051 ptrdiff_t count = SPECPDL_INDEX ();
2052 struct rvoe_arg rvoe_arg;
2054 if (inhibit_modification_hooks)
2055 return;
2057 /* If we are deferring calls to the after-change functions
2058 and there are no before-change functions,
2059 just record the args that we were going to use. */
2060 if (! NILP (Vcombine_after_change_calls)
2061 && NILP (Vbefore_change_functions)
2062 && !buffer_has_overlays ())
2064 Lisp_Object elt;
2066 if (!NILP (combine_after_change_list)
2067 && current_buffer != XBUFFER (combine_after_change_buffer))
2068 Fcombine_after_change_execute ();
2070 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2071 lenins - lendel);
2072 combine_after_change_list
2073 = Fcons (elt, combine_after_change_list);
2074 combine_after_change_buffer = Fcurrent_buffer ();
2076 return;
2079 if (!NILP (combine_after_change_list))
2080 Fcombine_after_change_execute ();
2082 specbind (Qinhibit_modification_hooks, Qt);
2084 if (!NILP (Vafter_change_functions))
2086 Lisp_Object args[4];
2087 rvoe_arg.location = &Vafter_change_functions;
2088 rvoe_arg.errorp = 1;
2090 /* Mark after-change-functions to be reset to nil in case of error. */
2091 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2093 /* Actually run the hook functions. */
2094 args[0] = Qafter_change_functions;
2095 XSETFASTINT (args[1], charpos);
2096 XSETFASTINT (args[2], charpos + lenins);
2097 XSETFASTINT (args[3], lendel);
2098 Frun_hook_with_args (4, args);
2100 /* There was no error: unarm the reset_on_error. */
2101 rvoe_arg.errorp = 0;
2104 if (buffer_has_overlays ())
2105 report_overlay_modification (make_number (charpos),
2106 make_number (charpos + lenins),
2108 make_number (charpos),
2109 make_number (charpos + lenins),
2110 make_number (lendel));
2112 /* After an insertion, call the text properties
2113 insert-behind-hooks or insert-in-front-hooks. */
2114 if (lendel == 0)
2115 report_interval_modification (make_number (charpos),
2116 make_number (charpos + lenins));
2118 unbind_to (count, Qnil);
2121 static void
2122 Fcombine_after_change_execute_1 (Lisp_Object val)
2124 Vcombine_after_change_calls = val;
2127 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2128 Scombine_after_change_execute, 0, 0, 0,
2129 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2130 (void)
2132 ptrdiff_t count = SPECPDL_INDEX ();
2133 ptrdiff_t beg, end, change;
2134 ptrdiff_t begpos, endpos;
2135 Lisp_Object tail;
2137 if (NILP (combine_after_change_list))
2138 return Qnil;
2140 /* It is rare for combine_after_change_buffer to be invalid, but
2141 possible. It can happen when combine-after-change-calls is
2142 non-nil, and insertion calls a file handler (e.g. through
2143 lock_file) which scribbles into a temp file -- cyd */
2144 if (!BUFFERP (combine_after_change_buffer)
2145 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2147 combine_after_change_list = Qnil;
2148 return Qnil;
2151 record_unwind_current_buffer ();
2153 Fset_buffer (combine_after_change_buffer);
2155 /* # chars unchanged at beginning of buffer. */
2156 beg = Z - BEG;
2157 /* # chars unchanged at end of buffer. */
2158 end = beg;
2159 /* Total amount of insertion (negative for deletion). */
2160 change = 0;
2162 /* Scan the various individual changes,
2163 accumulating the range info in BEG, END and CHANGE. */
2164 for (tail = combine_after_change_list; CONSP (tail);
2165 tail = XCDR (tail))
2167 Lisp_Object elt;
2168 ptrdiff_t thisbeg, thisend, thischange;
2170 /* Extract the info from the next element. */
2171 elt = XCAR (tail);
2172 if (! CONSP (elt))
2173 continue;
2174 thisbeg = XINT (XCAR (elt));
2176 elt = XCDR (elt);
2177 if (! CONSP (elt))
2178 continue;
2179 thisend = XINT (XCAR (elt));
2181 elt = XCDR (elt);
2182 if (! CONSP (elt))
2183 continue;
2184 thischange = XINT (XCAR (elt));
2186 /* Merge this range into the accumulated range. */
2187 change += thischange;
2188 if (thisbeg < beg)
2189 beg = thisbeg;
2190 if (thisend < end)
2191 end = thisend;
2194 /* Get the current start and end positions of the range
2195 that was changed. */
2196 begpos = BEG + beg;
2197 endpos = Z - end;
2199 /* We are about to handle these, so discard them. */
2200 combine_after_change_list = Qnil;
2202 /* Now run the after-change functions for real.
2203 Turn off the flag that defers them. */
2204 record_unwind_protect (Fcombine_after_change_execute_1,
2205 Vcombine_after_change_calls);
2206 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2207 update_compositions (begpos, endpos, CHECK_ALL);
2209 return unbind_to (count, Qnil);
2212 void
2213 syms_of_insdel (void)
2215 staticpro (&combine_after_change_list);
2216 staticpro (&combine_after_change_buffer);
2217 combine_after_change_list = Qnil;
2218 combine_after_change_buffer = Qnil;
2220 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2221 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2222 Vcombine_after_change_calls = Qnil;
2224 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2225 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2226 This affects `before-change-functions' and `after-change-functions',
2227 as well as hooks attached to text properties and overlays. */);
2228 inhibit_modification_hooks = 0;
2229 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2231 DEFSYM (Qregion_extract_function, "region-extract-function");
2233 defsubr (&Scombine_after_change_execute);