* lisp/emacs-lisp/easy-mmode.el (define-minor-mode): Use mode function
[emacs.git] / src / insdel.c
blob463392dcada70f31b269240ed22010218b8c2439
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
3 Copyright (C) 1985-1986, 1993-1995, 1997-2014 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <intprops.h>
25 #include "lisp.h"
26 #include "intervals.h"
27 #include "character.h"
28 #include "buffer.h"
29 #include "window.h"
30 #include "blockinput.h"
31 #include "region-cache.h"
33 static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35 static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
36 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37 static void gap_right (ptrdiff_t, ptrdiff_t);
39 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
41 was non-nil. We use this to decide how to call them
42 once the deferral ends.
44 In each element.
45 BEG-UNCHANGED is the number of chars before the changed range.
46 END-UNCHANGED is the number of chars after the changed range,
47 and CHANGE-AMOUNT is the number of characters inserted by the change
48 (negative for a deletion). */
49 static Lisp_Object combine_after_change_list;
51 /* Buffer which combine_after_change_list is about. */
52 static Lisp_Object combine_after_change_buffer;
54 Lisp_Object Qinhibit_modification_hooks;
56 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
58 /* Also used in marker.c to enable expensive marker checks. */
60 #ifdef MARKER_DEBUG
62 static void
63 check_markers (void)
65 struct Lisp_Marker *tail;
66 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
68 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
70 if (tail->buffer->text != current_buffer->text)
71 emacs_abort ();
72 if (tail->charpos > Z)
73 emacs_abort ();
74 if (tail->bytepos > Z_BYTE)
75 emacs_abort ();
76 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
77 emacs_abort ();
81 #else /* not MARKER_DEBUG */
83 #define check_markers() do { } while (0)
85 #endif /* MARKER_DEBUG */
87 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
88 Note that this can quit! */
90 void
91 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
93 eassert (charpos == BYTE_TO_CHAR (bytepos)
94 && bytepos == CHAR_TO_BYTE (charpos));
95 if (bytepos < GPT_BYTE)
96 gap_left (charpos, bytepos, 0);
97 else if (bytepos > GPT_BYTE)
98 gap_right (charpos, bytepos);
101 /* Move the gap to a position less than the current GPT.
102 BYTEPOS describes the new position as a byte position,
103 and CHARPOS is the corresponding char position.
104 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
106 static void
107 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
109 unsigned char *to, *from;
110 ptrdiff_t i;
111 ptrdiff_t new_s1;
113 if (!newgap)
114 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
116 i = GPT_BYTE;
117 to = GAP_END_ADDR;
118 from = GPT_ADDR;
119 new_s1 = GPT_BYTE;
121 /* Now copy the characters. To move the gap down,
122 copy characters up. */
124 while (1)
126 /* I gets number of characters left to copy. */
127 i = new_s1 - bytepos;
128 if (i == 0)
129 break;
130 /* If a quit is requested, stop copying now.
131 Change BYTEPOS to be where we have actually moved the gap to. */
132 if (QUITP)
134 bytepos = new_s1;
135 charpos = BYTE_TO_CHAR (bytepos);
136 break;
138 /* Move at most 32000 chars before checking again for a quit. */
139 if (i > 32000)
140 i = 32000;
141 new_s1 -= i;
142 from -= i, to -= i;
143 memmove (to, from, i);
146 /* Adjust buffer data structure, to put the gap at BYTEPOS.
147 BYTEPOS is where the loop above stopped, which may be what
148 was specified or may be where a quit was detected. */
149 GPT_BYTE = bytepos;
150 GPT = charpos;
151 eassert (charpos <= bytepos);
152 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
153 QUIT;
156 /* Move the gap to a position greater than the current GPT.
157 BYTEPOS describes the new position as a byte position,
158 and CHARPOS is the corresponding char position. */
160 static void
161 gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
163 register unsigned char *to, *from;
164 register ptrdiff_t i;
165 ptrdiff_t new_s1;
167 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
169 i = GPT_BYTE;
170 from = GAP_END_ADDR;
171 to = GPT_ADDR;
172 new_s1 = GPT_BYTE;
174 /* Now copy the characters. To move the gap up,
175 copy characters down. */
177 while (1)
179 /* I gets number of characters left to copy. */
180 i = bytepos - new_s1;
181 if (i == 0)
182 break;
183 /* If a quit is requested, stop copying now.
184 Change BYTEPOS to be where we have actually moved the gap to. */
185 if (QUITP)
187 bytepos = new_s1;
188 charpos = BYTE_TO_CHAR (bytepos);
189 break;
191 /* Move at most 32000 chars before checking again for a quit. */
192 if (i > 32000)
193 i = 32000;
194 new_s1 += i;
195 memmove (to, from, i);
196 from += i, to += i;
199 GPT = charpos;
200 GPT_BYTE = bytepos;
201 eassert (charpos <= bytepos);
202 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
203 QUIT;
206 /* If the selected window's old pointm is adjacent or covered by the
207 region from FROM to TO, unsuspend auto hscroll in that window. */
209 static void
210 adjust_suspend_auto_hscroll (ptrdiff_t from, ptrdiff_t to)
212 if (WINDOWP (selected_window))
214 struct window *w = XWINDOW (selected_window);
216 if (BUFFERP (w->contents)
217 && XBUFFER (w->contents) == current_buffer
218 && XMARKER (w->old_pointm)->charpos >= from
219 && XMARKER (w->old_pointm)->charpos <= to)
220 w->suspend_auto_hscroll = 0;
225 /* Adjust all markers for a deletion
226 whose range in bytes is FROM_BYTE to TO_BYTE.
227 The range in charpos is FROM to TO.
229 This function assumes that the gap is adjacent to
230 or inside of the range being deleted. */
232 void
233 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
234 ptrdiff_t to, ptrdiff_t to_byte)
236 struct Lisp_Marker *m;
237 ptrdiff_t charpos;
239 adjust_suspend_auto_hscroll (from, to);
240 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
242 charpos = m->charpos;
243 eassert (charpos <= Z);
245 /* If the marker is after the deletion,
246 relocate by number of chars / bytes deleted. */
247 if (charpos > to)
249 m->charpos -= to - from;
250 m->bytepos -= to_byte - from_byte;
252 /* Here's the case where a marker is inside text being deleted. */
253 else if (charpos > from)
255 m->charpos = from;
256 m->bytepos = from_byte;
262 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
263 to TO / TO_BYTE. We have to relocate the charpos of every marker
264 that points after the insertion (but not their bytepos).
266 When a marker points at the insertion point,
267 we advance it if either its insertion-type is t
268 or BEFORE_MARKERS is true. */
270 static void
271 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
272 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
274 struct Lisp_Marker *m;
275 bool adjusted = 0;
276 ptrdiff_t nchars = to - from;
277 ptrdiff_t nbytes = to_byte - from_byte;
279 adjust_suspend_auto_hscroll (from, to);
280 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
282 eassert (m->bytepos >= m->charpos
283 && m->bytepos - m->charpos <= Z_BYTE - Z);
285 if (m->bytepos == from_byte)
287 if (m->insertion_type || before_markers)
289 m->bytepos = to_byte;
290 m->charpos = to;
291 if (m->insertion_type)
292 adjusted = 1;
295 else if (m->bytepos > from_byte)
297 m->bytepos += nbytes;
298 m->charpos += nchars;
302 /* Adjusting only markers whose insertion-type is t may result in
303 - disordered start and end in overlays, and
304 - disordered overlays in the slot `overlays_before' of current_buffer. */
305 if (adjusted)
307 fix_start_end_in_overlays (from, to);
308 fix_overlays_before (current_buffer, from, to);
312 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
314 This is used only when the value of point changes due to an insert
315 or delete; it does not represent a conceptual change in point as a
316 marker. In particular, point is not crossing any interval
317 boundaries, so there's no need to use the usual SET_PT macro. In
318 fact it would be incorrect to do so, because either the old or the
319 new value of point is out of sync with the current set of
320 intervals. */
322 static void
323 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
325 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
326 /* In a single-byte buffer, the two positions must be equal. */
327 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
330 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
331 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
332 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
333 an insertion. */
335 static void
336 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
337 ptrdiff_t old_chars, ptrdiff_t old_bytes,
338 ptrdiff_t new_chars, ptrdiff_t new_bytes)
340 register struct Lisp_Marker *m;
341 ptrdiff_t prev_to_byte = from_byte + old_bytes;
342 ptrdiff_t diff_chars = new_chars - old_chars;
343 ptrdiff_t diff_bytes = new_bytes - old_bytes;
345 adjust_suspend_auto_hscroll (from, from + old_chars);
346 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
348 if (m->bytepos >= prev_to_byte)
350 m->charpos += diff_chars;
351 m->bytepos += diff_bytes;
353 else if (m->bytepos > from_byte)
355 m->charpos = from;
356 m->bytepos = from_byte;
360 check_markers ();
364 void
365 buffer_overflow (void)
367 error ("Maximum buffer size exceeded");
370 /* Make the gap NBYTES_ADDED bytes longer. */
372 static void
373 make_gap_larger (ptrdiff_t nbytes_added)
375 Lisp_Object tem;
376 ptrdiff_t real_gap_loc;
377 ptrdiff_t real_gap_loc_byte;
378 ptrdiff_t old_gap_size;
379 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
381 if (BUF_BYTES_MAX - current_size < nbytes_added)
382 buffer_overflow ();
384 /* If we have to get more space, get enough to last a while;
385 but do not exceed the maximum buffer size. */
386 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
387 BUF_BYTES_MAX - current_size);
389 enlarge_buffer_text (current_buffer, nbytes_added);
391 /* Prevent quitting in move_gap. */
392 tem = Vinhibit_quit;
393 Vinhibit_quit = Qt;
395 real_gap_loc = GPT;
396 real_gap_loc_byte = GPT_BYTE;
397 old_gap_size = GAP_SIZE;
399 /* Call the newly allocated space a gap at the end of the whole space. */
400 GPT = Z + GAP_SIZE;
401 GPT_BYTE = Z_BYTE + GAP_SIZE;
402 GAP_SIZE = nbytes_added;
404 /* Move the new gap down to be consecutive with the end of the old one. */
405 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
407 /* Now combine the two into one large gap. */
408 GAP_SIZE += old_gap_size;
409 GPT = real_gap_loc;
410 GPT_BYTE = real_gap_loc_byte;
412 /* Put an anchor. */
413 *(Z_ADDR) = 0;
415 Vinhibit_quit = tem;
418 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
420 /* Make the gap NBYTES_REMOVED bytes shorter. */
422 static void
423 make_gap_smaller (ptrdiff_t nbytes_removed)
425 Lisp_Object tem;
426 ptrdiff_t real_gap_loc;
427 ptrdiff_t real_gap_loc_byte;
428 ptrdiff_t real_Z;
429 ptrdiff_t real_Z_byte;
430 ptrdiff_t real_beg_unchanged;
431 ptrdiff_t new_gap_size;
433 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
434 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
435 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
437 /* Prevent quitting in move_gap. */
438 tem = Vinhibit_quit;
439 Vinhibit_quit = Qt;
441 real_gap_loc = GPT;
442 real_gap_loc_byte = GPT_BYTE;
443 new_gap_size = GAP_SIZE - nbytes_removed;
444 real_Z = Z;
445 real_Z_byte = Z_BYTE;
446 real_beg_unchanged = BEG_UNCHANGED;
448 /* Pretend that the last unwanted part of the gap is the entire gap,
449 and that the first desired part of the gap is part of the buffer
450 text. */
451 memset (GPT_ADDR, 0, new_gap_size);
452 GPT += new_gap_size;
453 GPT_BYTE += new_gap_size;
454 Z += new_gap_size;
455 Z_BYTE += new_gap_size;
456 GAP_SIZE = nbytes_removed;
458 /* Move the unwanted pretend gap to the end of the buffer. */
459 gap_right (Z, Z_BYTE);
461 enlarge_buffer_text (current_buffer, -nbytes_removed);
463 /* Now restore the desired gap. */
464 GAP_SIZE = new_gap_size;
465 GPT = real_gap_loc;
466 GPT_BYTE = real_gap_loc_byte;
467 Z = real_Z;
468 Z_BYTE = real_Z_byte;
469 BEG_UNCHANGED = real_beg_unchanged;
471 /* Put an anchor. */
472 *(Z_ADDR) = 0;
474 Vinhibit_quit = tem;
477 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
479 void
480 make_gap (ptrdiff_t nbytes_added)
482 if (nbytes_added >= 0)
483 make_gap_larger (nbytes_added);
484 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
485 else
486 make_gap_smaller (-nbytes_added);
487 #endif
490 /* Add NBYTES to B's gap. It's enough to temporarily
491 fake current_buffer and avoid real switch to B. */
493 void
494 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
496 struct buffer *oldb = current_buffer;
498 current_buffer = b;
499 make_gap (nbytes);
500 current_buffer = oldb;
503 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
504 FROM_MULTIBYTE says whether the incoming text is multibyte.
505 TO_MULTIBYTE says whether to store the text as multibyte.
506 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
508 Return the number of bytes stored at TO_ADDR. */
510 ptrdiff_t
511 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
512 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
514 if (from_multibyte == to_multibyte)
516 memcpy (to_addr, from_addr, nbytes);
517 return nbytes;
519 else if (from_multibyte)
521 ptrdiff_t nchars = 0;
522 ptrdiff_t bytes_left = nbytes;
524 while (bytes_left > 0)
526 int thislen, c;
527 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
528 if (! ASCII_CHAR_P (c))
529 c &= 0xFF;
530 *to_addr++ = c;
531 from_addr += thislen;
532 bytes_left -= thislen;
533 nchars++;
535 return nchars;
537 else
539 unsigned char *initial_to_addr = to_addr;
541 /* Convert single-byte to multibyte. */
542 while (nbytes > 0)
544 int c = *from_addr++;
546 if (!ASCII_CHAR_P (c))
548 c = BYTE8_TO_CHAR (c);
549 to_addr += CHAR_STRING (c, to_addr);
550 nbytes--;
552 else
553 /* Special case for speed. */
554 *to_addr++ = c, nbytes--;
556 return to_addr - initial_to_addr;
560 /* Insert a string of specified length before point.
561 This function judges multibyteness based on
562 enable_multibyte_characters in the current buffer;
563 it never converts between single-byte and multibyte.
565 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
566 prepare_to_modify_buffer could relocate the text. */
568 void
569 insert (const char *string, ptrdiff_t nbytes)
571 if (nbytes > 0)
573 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
574 insert_1_both (string, len, nbytes, 0, 1, 0);
575 opoint = PT - len;
576 signal_after_change (opoint, 0, len);
577 update_compositions (opoint, PT, CHECK_BORDER);
581 /* Likewise, but inherit text properties from neighboring characters. */
583 void
584 insert_and_inherit (const char *string, ptrdiff_t nbytes)
586 if (nbytes > 0)
588 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
589 insert_1_both (string, len, nbytes, 1, 1, 0);
590 opoint = PT - len;
591 signal_after_change (opoint, 0, len);
592 update_compositions (opoint, PT, CHECK_BORDER);
596 /* Insert the character C before point. Do not inherit text properties. */
598 void
599 insert_char (int c)
601 unsigned char str[MAX_MULTIBYTE_LENGTH];
602 int len;
604 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
605 len = CHAR_STRING (c, str);
606 else
608 len = 1;
609 str[0] = c;
612 insert ((char *) str, len);
615 /* Insert the null-terminated string S before point. */
617 void
618 insert_string (const char *s)
620 insert (s, strlen (s));
623 /* Like `insert' except that all markers pointing at the place where
624 the insertion happens are adjusted to point after it.
625 Don't use this function to insert part of a Lisp string,
626 since gc could happen and relocate it. */
628 void
629 insert_before_markers (const char *string, ptrdiff_t nbytes)
631 if (nbytes > 0)
633 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
634 insert_1_both (string, len, nbytes, 0, 1, 1);
635 opoint = PT - len;
636 signal_after_change (opoint, 0, len);
637 update_compositions (opoint, PT, CHECK_BORDER);
641 /* Likewise, but inherit text properties from neighboring characters. */
643 void
644 insert_before_markers_and_inherit (const char *string,
645 ptrdiff_t nbytes)
647 if (nbytes > 0)
649 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
650 insert_1_both (string, len, nbytes, 1, 1, 1);
651 opoint = PT - len;
652 signal_after_change (opoint, 0, len);
653 update_compositions (opoint, PT, CHECK_BORDER);
657 #ifdef BYTE_COMBINING_DEBUG
659 /* See if the bytes before POS/POS_BYTE combine with bytes
660 at the start of STRING to form a single character.
661 If so, return the number of bytes at the start of STRING
662 which combine in this way. Otherwise, return 0. */
665 count_combining_before (const unsigned char *string, ptrdiff_t length,
666 ptrdiff_t pos, ptrdiff_t pos_byte)
668 int len, combining_bytes;
669 const unsigned char *p;
671 if (NILP (current_buffer->enable_multibyte_characters))
672 return 0;
674 /* At first, we can exclude the following cases:
675 (1) STRING[0] can't be a following byte of multibyte sequence.
676 (2) POS is the start of the current buffer.
677 (3) A character before POS is not a multibyte character. */
678 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
679 return 0;
680 if (pos_byte == BEG_BYTE) /* case (2) */
681 return 0;
682 len = 1;
683 p = BYTE_POS_ADDR (pos_byte - 1);
684 while (! CHAR_HEAD_P (*p)) p--, len++;
685 if (! LEADING_CODE_P (*p)) /* case (3) */
686 return 0;
688 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
689 if (combining_bytes <= 0)
690 /* The character preceding POS is, complete and no room for
691 combining bytes (combining_bytes == 0), or an independent 8-bit
692 character (combining_bytes < 0). */
693 return 0;
695 /* We have a combination situation. Count the bytes at STRING that
696 may combine. */
697 p = string + 1;
698 while (!CHAR_HEAD_P (*p) && p < string + length)
699 p++;
701 return (combining_bytes < p - string ? combining_bytes : p - string);
704 /* See if the bytes after POS/POS_BYTE combine with bytes
705 at the end of STRING to form a single character.
706 If so, return the number of bytes after POS/POS_BYTE
707 which combine in this way. Otherwise, return 0. */
710 count_combining_after (const unsigned char *string,
711 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
713 ptrdiff_t opos_byte = pos_byte;
714 ptrdiff_t i;
715 ptrdiff_t bytes;
716 unsigned char *bufp;
718 if (NILP (current_buffer->enable_multibyte_characters))
719 return 0;
721 /* At first, we can exclude the following cases:
722 (1) The last byte of STRING is an ASCII.
723 (2) POS is the last of the current buffer.
724 (3) A character at POS can't be a following byte of multibyte
725 character. */
726 if (length > 0 && ASCII_CHAR_P (string[length - 1])) /* case (1) */
727 return 0;
728 if (pos_byte == Z_BYTE) /* case (2) */
729 return 0;
730 bufp = BYTE_POS_ADDR (pos_byte);
731 if (CHAR_HEAD_P (*bufp)) /* case (3) */
732 return 0;
734 i = length - 1;
735 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
737 i--;
739 if (i < 0)
741 /* All characters in STRING are not character head. We must
742 check also preceding bytes at POS. We are sure that the gap
743 is at POS. */
744 unsigned char *p = BEG_ADDR;
745 i = pos_byte - 2;
746 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
747 i--;
748 if (i < 0 || !LEADING_CODE_P (p[i]))
749 return 0;
751 bytes = BYTES_BY_CHAR_HEAD (p[i]);
752 return (bytes <= pos_byte - 1 - i + length
754 : bytes - (pos_byte - 1 - i + length));
756 if (!LEADING_CODE_P (string[i]))
757 return 0;
759 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
760 bufp++, pos_byte++;
761 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
763 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
766 #endif
769 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
770 starting at STRING. INHERIT non-zero means inherit the text
771 properties from neighboring characters; zero means inserted text
772 will have no text properties. PREPARE non-zero means call
773 prepare_to_modify_buffer, which checks that the region is not
774 read-only, and calls before-change-function and any modification
775 properties the text may have. BEFORE_MARKERS non-zero means adjust
776 all markers that point at the insertion place to point after it. */
778 void
779 insert_1_both (const char *string,
780 ptrdiff_t nchars, ptrdiff_t nbytes,
781 bool inherit, bool prepare, bool before_markers)
783 if (nchars == 0)
784 return;
786 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
787 nchars = nbytes;
789 if (prepare)
790 /* Do this before moving and increasing the gap,
791 because the before-change hooks might move the gap
792 or make it smaller. */
793 prepare_to_modify_buffer (PT, PT, NULL);
795 if (PT != GPT)
796 move_gap_both (PT, PT_BYTE);
797 if (GAP_SIZE < nbytes)
798 make_gap (nbytes - GAP_SIZE);
800 #ifdef BYTE_COMBINING_DEBUG
801 if (count_combining_before (string, nbytes, PT, PT_BYTE)
802 || count_combining_after (string, nbytes, PT, PT_BYTE))
803 emacs_abort ();
804 #endif
806 /* Record deletion of the surrounding text that combines with
807 the insertion. This, together with recording the insertion,
808 will add up to the right stuff in the undo list. */
809 record_insert (PT, nchars);
810 MODIFF++;
811 CHARS_MODIFF = MODIFF;
813 memcpy (GPT_ADDR, string, nbytes);
815 GAP_SIZE -= nbytes;
816 GPT += nchars;
817 ZV += nchars;
818 Z += nchars;
819 GPT_BYTE += nbytes;
820 ZV_BYTE += nbytes;
821 Z_BYTE += nbytes;
822 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
824 eassert (GPT <= GPT_BYTE);
826 /* The insert may have been in the unchanged region, so check again. */
827 if (Z - GPT < END_UNCHANGED)
828 END_UNCHANGED = Z - GPT;
830 adjust_overlays_for_insert (PT, nchars);
831 adjust_markers_for_insert (PT, PT_BYTE,
832 PT + nchars, PT_BYTE + nbytes,
833 before_markers);
835 offset_intervals (current_buffer, PT, nchars);
837 if (!inherit && buffer_intervals (current_buffer))
838 set_text_properties (make_number (PT), make_number (PT + nchars),
839 Qnil, Qnil, Qnil);
841 adjust_point (nchars, nbytes);
843 check_markers ();
846 /* Insert the part of the text of STRING, a Lisp object assumed to be
847 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
848 starting at position POS / POS_BYTE. If the text of STRING has properties,
849 copy them into the buffer.
851 It does not work to use `insert' for this, because a GC could happen
852 before we copy the stuff into the buffer, and relocate the string
853 without insert noticing. */
855 void
856 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
857 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
859 ptrdiff_t opoint = PT;
861 if (SCHARS (string) == 0)
862 return;
864 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
865 inherit, 0);
866 signal_after_change (opoint, 0, PT - opoint);
867 update_compositions (opoint, PT, CHECK_BORDER);
870 /* Like `insert_from_string' except that all markers pointing
871 at the place where the insertion happens are adjusted to point after it. */
873 void
874 insert_from_string_before_markers (Lisp_Object string,
875 ptrdiff_t pos, ptrdiff_t pos_byte,
876 ptrdiff_t length, ptrdiff_t length_byte,
877 bool inherit)
879 ptrdiff_t opoint = PT;
881 if (SCHARS (string) == 0)
882 return;
884 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
885 inherit, 1);
886 signal_after_change (opoint, 0, PT - opoint);
887 update_compositions (opoint, PT, CHECK_BORDER);
890 /* Subroutine of the insertion functions above. */
892 static void
893 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
894 ptrdiff_t nchars, ptrdiff_t nbytes,
895 bool inherit, bool before_markers)
897 struct gcpro gcpro1;
898 ptrdiff_t outgoing_nbytes = nbytes;
899 INTERVAL intervals;
901 /* Make OUTGOING_NBYTES describe the text
902 as it will be inserted in this buffer. */
904 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
905 outgoing_nbytes = nchars;
906 else if (! STRING_MULTIBYTE (string))
907 outgoing_nbytes
908 = count_size_as_multibyte (SDATA (string) + pos_byte,
909 nbytes);
911 GCPRO1 (string);
912 /* Do this before moving and increasing the gap,
913 because the before-change hooks might move the gap
914 or make it smaller. */
915 prepare_to_modify_buffer (PT, PT, NULL);
917 if (PT != GPT)
918 move_gap_both (PT, PT_BYTE);
919 if (GAP_SIZE < outgoing_nbytes)
920 make_gap (outgoing_nbytes - GAP_SIZE);
921 UNGCPRO;
923 /* Copy the string text into the buffer, perhaps converting
924 between single-byte and multibyte. */
925 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
926 STRING_MULTIBYTE (string),
927 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
929 #ifdef BYTE_COMBINING_DEBUG
930 /* We have copied text into the gap, but we have not altered
931 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
932 to these functions and get the same results as we would
933 have got earlier on. Meanwhile, PT_ADDR does point to
934 the text that has been stored by copy_text. */
935 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
936 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
937 emacs_abort ();
938 #endif
940 record_insert (PT, nchars);
941 MODIFF++;
942 CHARS_MODIFF = MODIFF;
944 GAP_SIZE -= outgoing_nbytes;
945 GPT += nchars;
946 ZV += nchars;
947 Z += nchars;
948 GPT_BYTE += outgoing_nbytes;
949 ZV_BYTE += outgoing_nbytes;
950 Z_BYTE += outgoing_nbytes;
951 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
953 eassert (GPT <= GPT_BYTE);
955 /* The insert may have been in the unchanged region, so check again. */
956 if (Z - GPT < END_UNCHANGED)
957 END_UNCHANGED = Z - GPT;
959 adjust_overlays_for_insert (PT, nchars);
960 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
961 PT_BYTE + outgoing_nbytes,
962 before_markers);
964 offset_intervals (current_buffer, PT, nchars);
966 intervals = string_intervals (string);
967 /* Get the intervals for the part of the string we are inserting. */
968 if (nbytes < SBYTES (string))
969 intervals = copy_intervals (intervals, pos, nchars);
971 /* Insert those intervals. */
972 graft_intervals_into_buffer (intervals, PT, nchars,
973 current_buffer, inherit);
975 adjust_point (nchars, outgoing_nbytes);
977 check_markers ();
980 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
981 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
982 GPT_ADDR (if not text_at_gap_tail). */
984 void
985 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
987 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
989 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
990 nchars = nbytes;
992 /* No need to call prepare_to_modify_buffer, since this is called
993 from places that replace some region with a different text, so
994 prepare_to_modify_buffer was already called by the deletion part
995 of this dance. */
996 invalidate_buffer_caches (current_buffer, GPT, GPT);
997 record_insert (GPT, nchars);
998 MODIFF++;
1000 GAP_SIZE -= nbytes;
1001 if (! text_at_gap_tail)
1003 GPT += nchars;
1004 GPT_BYTE += nbytes;
1006 ZV += nchars;
1007 Z += nchars;
1008 ZV_BYTE += nbytes;
1009 Z_BYTE += nbytes;
1010 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1012 eassert (GPT <= GPT_BYTE);
1014 adjust_overlays_for_insert (ins_charpos, nchars);
1015 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1016 ins_charpos + nchars, ins_bytepos + nbytes, 0);
1018 if (buffer_intervals (current_buffer))
1020 offset_intervals (current_buffer, ins_charpos, nchars);
1021 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1022 current_buffer, 0);
1025 if (ins_charpos < PT)
1026 adjust_point (nchars, nbytes);
1028 check_markers ();
1031 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1032 current buffer. If the text in BUF has properties, they are absorbed
1033 into the current buffer.
1035 It does not work to use `insert' for this, because a malloc could happen
1036 and relocate BUF's text before the copy happens. */
1038 void
1039 insert_from_buffer (struct buffer *buf,
1040 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1042 ptrdiff_t opoint = PT;
1044 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1045 signal_after_change (opoint, 0, PT - opoint);
1046 update_compositions (opoint, PT, CHECK_BORDER);
1049 static void
1050 insert_from_buffer_1 (struct buffer *buf,
1051 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1053 ptrdiff_t chunk, chunk_expanded;
1054 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1055 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1056 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1057 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1058 INTERVAL intervals;
1060 if (nchars == 0)
1061 return;
1063 /* Make OUTGOING_NBYTES describe the text
1064 as it will be inserted in this buffer. */
1066 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1067 outgoing_nbytes = nchars;
1068 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1070 ptrdiff_t outgoing_before_gap = 0;
1071 ptrdiff_t outgoing_after_gap = 0;
1073 if (from < BUF_GPT (buf))
1075 chunk = BUF_GPT_BYTE (buf) - from_byte;
1076 if (chunk > incoming_nbytes)
1077 chunk = incoming_nbytes;
1078 outgoing_before_gap
1079 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1080 chunk);
1082 else
1083 chunk = 0;
1085 if (chunk < incoming_nbytes)
1086 outgoing_after_gap
1087 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1088 from_byte + chunk),
1089 incoming_nbytes - chunk);
1091 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1094 /* Do this before moving and increasing the gap,
1095 because the before-change hooks might move the gap
1096 or make it smaller. */
1097 prepare_to_modify_buffer (PT, PT, NULL);
1099 if (PT != GPT)
1100 move_gap_both (PT, PT_BYTE);
1101 if (GAP_SIZE < outgoing_nbytes)
1102 make_gap (outgoing_nbytes - GAP_SIZE);
1104 if (from < BUF_GPT (buf))
1106 chunk = BUF_GPT_BYTE (buf) - from_byte;
1107 if (chunk > incoming_nbytes)
1108 chunk = incoming_nbytes;
1109 /* Record number of output bytes, so we know where
1110 to put the output from the second copy_text. */
1111 chunk_expanded
1112 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1113 GPT_ADDR, chunk,
1114 ! NILP (BVAR (buf, enable_multibyte_characters)),
1115 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1117 else
1118 chunk_expanded = chunk = 0;
1120 if (chunk < incoming_nbytes)
1121 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1122 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1123 ! NILP (BVAR (buf, enable_multibyte_characters)),
1124 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1126 #ifdef BYTE_COMBINING_DEBUG
1127 /* We have copied text into the gap, but we have not altered
1128 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1129 to these functions and get the same results as we would
1130 have got earlier on. Meanwhile, GPT_ADDR does point to
1131 the text that has been stored by copy_text. */
1132 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1133 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1134 emacs_abort ();
1135 #endif
1137 record_insert (PT, nchars);
1138 MODIFF++;
1139 CHARS_MODIFF = MODIFF;
1141 GAP_SIZE -= outgoing_nbytes;
1142 GPT += nchars;
1143 ZV += nchars;
1144 Z += nchars;
1145 GPT_BYTE += outgoing_nbytes;
1146 ZV_BYTE += outgoing_nbytes;
1147 Z_BYTE += outgoing_nbytes;
1148 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1150 eassert (GPT <= GPT_BYTE);
1152 /* The insert may have been in the unchanged region, so check again. */
1153 if (Z - GPT < END_UNCHANGED)
1154 END_UNCHANGED = Z - GPT;
1156 adjust_overlays_for_insert (PT, nchars);
1157 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1158 PT_BYTE + outgoing_nbytes,
1161 offset_intervals (current_buffer, PT, nchars);
1163 /* Get the intervals for the part of the string we are inserting. */
1164 intervals = buffer_intervals (buf);
1165 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1167 if (buf == current_buffer && PT <= from)
1168 from += nchars;
1169 intervals = copy_intervals (intervals, from, nchars);
1172 /* Insert those intervals. */
1173 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1175 adjust_point (nchars, outgoing_nbytes);
1178 /* Record undo information and adjust markers and position keepers for
1179 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1180 chars (LEN_BYTE bytes) which resides in the gap just after
1181 GPT_ADDR.
1183 PREV_TEXT nil means the new text was just inserted. */
1185 static void
1186 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1187 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1189 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1191 #ifdef BYTE_COMBINING_DEBUG
1192 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1193 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1194 emacs_abort ();
1195 #endif
1197 if (STRINGP (prev_text))
1199 nchars_del = SCHARS (prev_text);
1200 nbytes_del = SBYTES (prev_text);
1203 /* Update various buffer positions for the new text. */
1204 GAP_SIZE -= len_byte;
1205 ZV += len; Z+= len;
1206 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1207 GPT += len; GPT_BYTE += len_byte;
1208 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1210 if (nchars_del > 0)
1211 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1212 len, len_byte);
1213 else
1214 adjust_markers_for_insert (from, from_byte,
1215 from + len, from_byte + len_byte, 0);
1217 if (nchars_del > 0)
1218 record_delete (from, prev_text, false);
1219 record_insert (from, len);
1221 if (len > nchars_del)
1222 adjust_overlays_for_insert (from, len - nchars_del);
1223 else if (len < nchars_del)
1224 adjust_overlays_for_delete (from, nchars_del - len);
1226 offset_intervals (current_buffer, from, len - nchars_del);
1228 if (from < PT)
1229 adjust_point (len - nchars_del, len_byte - nbytes_del);
1231 /* As byte combining will decrease Z, we must check this again. */
1232 if (Z - GPT < END_UNCHANGED)
1233 END_UNCHANGED = Z - GPT;
1235 check_markers ();
1237 if (len == 0)
1238 evaporate_overlays (from);
1239 MODIFF++;
1240 CHARS_MODIFF = MODIFF;
1243 /* Record undo information, adjust markers and position keepers for an
1244 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1245 text already exists in the current buffer but character length (TO
1246 - FROM) may be incorrect, the correct length is NEWLEN. */
1248 void
1249 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1250 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1252 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1254 if (GPT != to)
1255 move_gap_both (to, to_byte);
1256 GAP_SIZE += len_byte;
1257 GPT -= len; GPT_BYTE -= len_byte;
1258 ZV -= len; ZV_BYTE -= len_byte;
1259 Z -= len; Z_BYTE -= len_byte;
1260 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1263 /* Replace the text from character positions FROM to TO with NEW,
1264 If PREPARE, call prepare_to_modify_buffer.
1265 If INHERIT, the newly inserted text should inherit text properties
1266 from the surrounding non-deleted text. */
1268 /* Note that this does not yet handle markers quite right.
1269 Also it needs to record a single undo-entry that does a replacement
1270 rather than a separate delete and insert.
1271 That way, undo will also handle markers properly.
1273 But if MARKERS is 0, don't relocate markers. */
1275 void
1276 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1277 bool prepare, bool inherit, bool markers)
1279 ptrdiff_t inschars = SCHARS (new);
1280 ptrdiff_t insbytes = SBYTES (new);
1281 ptrdiff_t from_byte, to_byte;
1282 ptrdiff_t nbytes_del, nchars_del;
1283 struct gcpro gcpro1;
1284 INTERVAL intervals;
1285 ptrdiff_t outgoing_insbytes = insbytes;
1286 Lisp_Object deletion;
1288 check_markers ();
1290 GCPRO1 (new);
1291 deletion = Qnil;
1293 if (prepare)
1295 ptrdiff_t range_length = to - from;
1296 prepare_to_modify_buffer (from, to, &from);
1297 to = from + range_length;
1300 UNGCPRO;
1302 /* Make args be valid. */
1303 if (from < BEGV)
1304 from = BEGV;
1305 if (to > ZV)
1306 to = ZV;
1308 from_byte = CHAR_TO_BYTE (from);
1309 to_byte = CHAR_TO_BYTE (to);
1311 nchars_del = to - from;
1312 nbytes_del = to_byte - from_byte;
1314 if (nbytes_del <= 0 && insbytes == 0)
1315 return;
1317 /* Make OUTGOING_INSBYTES describe the text
1318 as it will be inserted in this buffer. */
1320 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1321 outgoing_insbytes = inschars;
1322 else if (! STRING_MULTIBYTE (new))
1323 outgoing_insbytes
1324 = count_size_as_multibyte (SDATA (new), insbytes);
1326 GCPRO1 (new);
1328 /* Make sure the gap is somewhere in or next to what we are deleting. */
1329 if (from > GPT)
1330 gap_right (from, from_byte);
1331 if (to < GPT)
1332 gap_left (to, to_byte, 0);
1334 /* Even if we don't record for undo, we must keep the original text
1335 because we may have to recover it because of inappropriate byte
1336 combining. */
1337 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1338 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1340 GAP_SIZE += nbytes_del;
1341 ZV -= nchars_del;
1342 Z -= nchars_del;
1343 ZV_BYTE -= nbytes_del;
1344 Z_BYTE -= nbytes_del;
1345 GPT = from;
1346 GPT_BYTE = from_byte;
1347 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1349 eassert (GPT <= GPT_BYTE);
1351 if (GPT - BEG < BEG_UNCHANGED)
1352 BEG_UNCHANGED = GPT - BEG;
1353 if (Z - GPT < END_UNCHANGED)
1354 END_UNCHANGED = Z - GPT;
1356 if (GAP_SIZE < outgoing_insbytes)
1357 make_gap (outgoing_insbytes - GAP_SIZE);
1359 /* Copy the string text into the buffer, perhaps converting
1360 between single-byte and multibyte. */
1361 copy_text (SDATA (new), GPT_ADDR, insbytes,
1362 STRING_MULTIBYTE (new),
1363 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1365 #ifdef BYTE_COMBINING_DEBUG
1366 /* We have copied text into the gap, but we have not marked
1367 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1368 here, for both the previous text and the following text.
1369 Meanwhile, GPT_ADDR does point to
1370 the text that has been stored by copy_text. */
1371 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1372 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1373 emacs_abort ();
1374 #endif
1376 /* Record the insertion first, so that when we undo,
1377 the deletion will be undone first. Thus, undo
1378 will insert before deleting, and thus will keep
1379 the markers before and after this text separate. */
1380 if (!NILP (deletion))
1382 record_insert (from + SCHARS (deletion), inschars);
1383 record_delete (from, deletion, false);
1386 GAP_SIZE -= outgoing_insbytes;
1387 GPT += inschars;
1388 ZV += inschars;
1389 Z += inschars;
1390 GPT_BYTE += outgoing_insbytes;
1391 ZV_BYTE += outgoing_insbytes;
1392 Z_BYTE += outgoing_insbytes;
1393 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1395 eassert (GPT <= GPT_BYTE);
1397 /* Adjust markers for the deletion and the insertion. */
1398 if (markers)
1399 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1400 inschars, outgoing_insbytes);
1402 /* Adjust the overlay center as needed. This must be done after
1403 adjusting the markers that bound the overlays. */
1404 adjust_overlays_for_delete (from, nchars_del);
1405 adjust_overlays_for_insert (from, inschars);
1407 offset_intervals (current_buffer, from, inschars - nchars_del);
1409 /* Get the intervals for the part of the string we are inserting--
1410 not including the combined-before bytes. */
1411 intervals = string_intervals (new);
1412 /* Insert those intervals. */
1413 graft_intervals_into_buffer (intervals, from, inschars,
1414 current_buffer, inherit);
1416 /* Relocate point as if it were a marker. */
1417 if (from < PT)
1418 adjust_point ((from + inschars - (PT < to ? PT : to)),
1419 (from_byte + outgoing_insbytes
1420 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1422 if (outgoing_insbytes == 0)
1423 evaporate_overlays (from);
1425 check_markers ();
1427 MODIFF++;
1428 CHARS_MODIFF = MODIFF;
1429 UNGCPRO;
1431 signal_after_change (from, nchars_del, GPT - from);
1432 update_compositions (from, GPT, CHECK_BORDER);
1435 /* Replace the text from character positions FROM to TO with
1436 the text in INS of length INSCHARS.
1437 Keep the text properties that applied to the old characters
1438 (extending them to all the new chars if there are more new chars).
1440 Note that this does not yet handle markers quite right.
1442 If MARKERS, relocate markers.
1444 Unlike most functions at this level, never call
1445 prepare_to_modify_buffer and never call signal_after_change. */
1447 void
1448 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1449 ptrdiff_t to, ptrdiff_t to_byte,
1450 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1451 bool markers)
1453 ptrdiff_t nbytes_del, nchars_del;
1455 check_markers ();
1457 nchars_del = to - from;
1458 nbytes_del = to_byte - from_byte;
1460 if (nbytes_del <= 0 && insbytes == 0)
1461 return;
1463 /* Make sure the gap is somewhere in or next to what we are deleting. */
1464 if (from > GPT)
1465 gap_right (from, from_byte);
1466 if (to < GPT)
1467 gap_left (to, to_byte, 0);
1469 GAP_SIZE += nbytes_del;
1470 ZV -= nchars_del;
1471 Z -= nchars_del;
1472 ZV_BYTE -= nbytes_del;
1473 Z_BYTE -= nbytes_del;
1474 GPT = from;
1475 GPT_BYTE = from_byte;
1476 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1478 eassert (GPT <= GPT_BYTE);
1480 if (GPT - BEG < BEG_UNCHANGED)
1481 BEG_UNCHANGED = GPT - BEG;
1482 if (Z - GPT < END_UNCHANGED)
1483 END_UNCHANGED = Z - GPT;
1485 if (GAP_SIZE < insbytes)
1486 make_gap (insbytes - GAP_SIZE);
1488 /* Copy the replacement text into the buffer. */
1489 memcpy (GPT_ADDR, ins, insbytes);
1491 #ifdef BYTE_COMBINING_DEBUG
1492 /* We have copied text into the gap, but we have not marked
1493 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1494 here, for both the previous text and the following text.
1495 Meanwhile, GPT_ADDR does point to
1496 the text that has been stored by copy_text. */
1497 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1498 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1499 emacs_abort ();
1500 #endif
1502 GAP_SIZE -= insbytes;
1503 GPT += inschars;
1504 ZV += inschars;
1505 Z += inschars;
1506 GPT_BYTE += insbytes;
1507 ZV_BYTE += insbytes;
1508 Z_BYTE += insbytes;
1509 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1511 eassert (GPT <= GPT_BYTE);
1513 /* Adjust markers for the deletion and the insertion. */
1514 if (markers
1515 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1516 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1517 inschars, insbytes);
1519 /* Adjust the overlay center as needed. This must be done after
1520 adjusting the markers that bound the overlays. */
1521 if (nchars_del != inschars)
1523 adjust_overlays_for_insert (from, inschars);
1524 adjust_overlays_for_delete (from + inschars, nchars_del);
1527 offset_intervals (current_buffer, from, inschars - nchars_del);
1529 /* Relocate point as if it were a marker. */
1530 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1532 if (PT < to)
1533 /* PT was within the deleted text. Move it to FROM. */
1534 adjust_point (from - PT, from_byte - PT_BYTE);
1535 else
1536 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1539 if (insbytes == 0)
1540 evaporate_overlays (from);
1542 check_markers ();
1544 MODIFF++;
1545 CHARS_MODIFF = MODIFF;
1548 /* Delete characters in current buffer
1549 from FROM up to (but not including) TO.
1550 If TO comes before FROM, we delete nothing. */
1552 void
1553 del_range (ptrdiff_t from, ptrdiff_t to)
1555 del_range_1 (from, to, 1, 0);
1558 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1559 RET_STRING says to return the deleted text. */
1561 Lisp_Object
1562 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1564 ptrdiff_t from_byte, to_byte;
1565 Lisp_Object deletion;
1566 struct gcpro gcpro1;
1568 /* Make args be valid */
1569 if (from < BEGV)
1570 from = BEGV;
1571 if (to > ZV)
1572 to = ZV;
1574 if (to <= from)
1575 return Qnil;
1577 if (prepare)
1579 ptrdiff_t range_length = to - from;
1580 prepare_to_modify_buffer (from, to, &from);
1581 to = min (ZV, from + range_length);
1584 from_byte = CHAR_TO_BYTE (from);
1585 to_byte = CHAR_TO_BYTE (to);
1587 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1588 GCPRO1 (deletion);
1589 signal_after_change (from, to - from, 0);
1590 update_compositions (from, from, CHECK_HEAD);
1591 UNGCPRO;
1592 return deletion;
1595 /* Like del_range_1 but args are byte positions, not char positions. */
1597 void
1598 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
1600 ptrdiff_t from, to;
1602 /* Make args be valid */
1603 if (from_byte < BEGV_BYTE)
1604 from_byte = BEGV_BYTE;
1605 if (to_byte > ZV_BYTE)
1606 to_byte = ZV_BYTE;
1608 if (to_byte <= from_byte)
1609 return;
1611 from = BYTE_TO_CHAR (from_byte);
1612 to = BYTE_TO_CHAR (to_byte);
1614 if (prepare)
1616 ptrdiff_t old_from = from, old_to = Z - to;
1617 ptrdiff_t range_length = to - from;
1618 prepare_to_modify_buffer (from, to, &from);
1619 to = from + range_length;
1621 if (old_from != from)
1622 from_byte = CHAR_TO_BYTE (from);
1623 if (to > ZV)
1625 to = ZV;
1626 to_byte = ZV_BYTE;
1628 else if (old_to == Z - to)
1629 to_byte = CHAR_TO_BYTE (to);
1632 del_range_2 (from, from_byte, to, to_byte, 0);
1633 signal_after_change (from, to - from, 0);
1634 update_compositions (from, from, CHECK_HEAD);
1637 /* Like del_range_1, but positions are specified both as charpos
1638 and bytepos. */
1640 void
1641 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1642 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1644 /* Make args be valid */
1645 if (from_byte < BEGV_BYTE)
1646 from_byte = BEGV_BYTE;
1647 if (to_byte > ZV_BYTE)
1648 to_byte = ZV_BYTE;
1650 if (to_byte <= from_byte)
1651 return;
1653 if (from < BEGV)
1654 from = BEGV;
1655 if (to > ZV)
1656 to = ZV;
1658 if (prepare)
1660 ptrdiff_t old_from = from, old_to = Z - to;
1661 ptrdiff_t range_length = to - from;
1662 prepare_to_modify_buffer (from, to, &from);
1663 to = from + range_length;
1665 if (old_from != from)
1666 from_byte = CHAR_TO_BYTE (from);
1667 if (to > ZV)
1669 to = ZV;
1670 to_byte = ZV_BYTE;
1672 else if (old_to == Z - to)
1673 to_byte = CHAR_TO_BYTE (to);
1676 del_range_2 (from, from_byte, to, to_byte, 0);
1677 signal_after_change (from, to - from, 0);
1678 update_compositions (from, from, CHECK_HEAD);
1681 /* Delete a range of text, specified both as character positions
1682 and byte positions. FROM and TO are character positions,
1683 while FROM_BYTE and TO_BYTE are byte positions.
1684 If RET_STRING, the deleted area is returned as a string. */
1686 Lisp_Object
1687 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1688 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1690 ptrdiff_t nbytes_del, nchars_del;
1691 Lisp_Object deletion;
1693 check_markers ();
1695 nchars_del = to - from;
1696 nbytes_del = to_byte - from_byte;
1698 /* Make sure the gap is somewhere in or next to what we are deleting. */
1699 if (from > GPT)
1700 gap_right (from, from_byte);
1701 if (to < GPT)
1702 gap_left (to, to_byte, 0);
1704 #ifdef BYTE_COMBINING_DEBUG
1705 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1706 Z_BYTE - to_byte, from, from_byte))
1707 emacs_abort ();
1708 #endif
1710 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1711 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1712 else
1713 deletion = Qnil;
1715 /* Record marker adjustments, and text deletion into undo
1716 history. */
1717 record_delete (from, deletion, true);
1719 /* Relocate all markers pointing into the new, larger gap to point
1720 at the end of the text before the gap. */
1721 adjust_markers_for_delete (from, from_byte, to, to_byte);
1723 MODIFF++;
1724 CHARS_MODIFF = MODIFF;
1726 /* Relocate point as if it were a marker. */
1727 if (from < PT)
1728 adjust_point (from - (PT < to ? PT : to),
1729 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1731 offset_intervals (current_buffer, from, - nchars_del);
1733 /* Adjust the overlay center as needed. This must be done after
1734 adjusting the markers that bound the overlays. */
1735 adjust_overlays_for_delete (from, nchars_del);
1737 GAP_SIZE += nbytes_del;
1738 ZV_BYTE -= nbytes_del;
1739 Z_BYTE -= nbytes_del;
1740 ZV -= nchars_del;
1741 Z -= nchars_del;
1742 GPT = from;
1743 GPT_BYTE = from_byte;
1744 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1745 /* Put an anchor, unless called from decode_coding_object which
1746 needs to access the previous gap contents. */
1747 *(GPT_ADDR) = 0;
1749 eassert (GPT <= GPT_BYTE);
1751 if (GPT - BEG < BEG_UNCHANGED)
1752 BEG_UNCHANGED = GPT - BEG;
1753 if (Z - GPT < END_UNCHANGED)
1754 END_UNCHANGED = Z - GPT;
1756 check_markers ();
1758 evaporate_overlays (from);
1760 return deletion;
1763 /* Call this if you're about to change the text of current buffer
1764 from character positions START to END. This checks the read-only
1765 properties of the region, calls the necessary modification hooks,
1766 and warns the next redisplay that it should pay attention to that
1767 area. */
1769 void
1770 modify_text (ptrdiff_t start, ptrdiff_t end)
1772 prepare_to_modify_buffer (start, end, NULL);
1774 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1775 if (MODIFF <= SAVE_MODIFF)
1776 record_first_change ();
1777 MODIFF++;
1778 CHARS_MODIFF = MODIFF;
1780 bset_point_before_scroll (current_buffer, Qnil);
1783 Lisp_Object Qregion_extract_function;
1785 /* Check that it is okay to modify the buffer between START and END,
1786 which are char positions.
1788 Run the before-change-function, if any. If intervals are in use,
1789 verify that the text to be modified is not read-only, and call
1790 any modification properties the text may have.
1792 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1793 by holding its value temporarily in a marker. */
1795 void
1796 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1797 ptrdiff_t *preserve_ptr)
1799 struct buffer *base_buffer;
1801 if (!NILP (BVAR (current_buffer, read_only)))
1802 Fbarf_if_buffer_read_only ();
1804 bset_redisplay (current_buffer);
1806 if (buffer_intervals (current_buffer))
1808 if (preserve_ptr)
1810 Lisp_Object preserve_marker;
1811 struct gcpro gcpro1;
1812 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1813 GCPRO1 (preserve_marker);
1814 verify_interval_modification (current_buffer, start, end);
1815 *preserve_ptr = marker_position (preserve_marker);
1816 unchain_marker (XMARKER (preserve_marker));
1817 UNGCPRO;
1819 else
1820 verify_interval_modification (current_buffer, start, end);
1823 /* For indirect buffers, use the base buffer to check clashes. */
1824 if (current_buffer->base_buffer != 0)
1825 base_buffer = current_buffer->base_buffer;
1826 else
1827 base_buffer = current_buffer;
1829 if (inhibit_modification_hooks)
1830 return;
1832 if (!NILP (BVAR (base_buffer, file_truename))
1833 /* Make binding buffer-file-name to nil effective. */
1834 && !NILP (BVAR (base_buffer, filename))
1835 && SAVE_MODIFF >= MODIFF)
1836 lock_file (BVAR (base_buffer, file_truename));
1838 /* If `select-active-regions' is non-nil, save the region text. */
1839 /* FIXME: Move this to Elisp (via before-change-functions). */
1840 if (!NILP (BVAR (current_buffer, mark_active))
1841 && XMARKER (BVAR (current_buffer, mark))->buffer
1842 && NILP (Vsaved_region_selection)
1843 && (EQ (Vselect_active_regions, Qonly)
1844 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1845 : (!NILP (Vselect_active_regions)
1846 && !NILP (Vtransient_mark_mode))))
1847 Vsaved_region_selection
1848 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1850 signal_before_change (start, end, preserve_ptr);
1851 Vdeactivate_mark = Qt;
1854 /* Like above, but called when we know that the buffer text
1855 will be modified and region caches should be invalidated. */
1857 void
1858 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1859 ptrdiff_t *preserve_ptr)
1861 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1862 invalidate_buffer_caches (current_buffer, start, end);
1865 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1866 region between buffer positions START and END. */
1867 void
1868 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1870 /* Indirect buffers usually have their caches set to NULL, but we
1871 need to consider the caches of their base buffer. */
1872 if (buf->base_buffer)
1873 buf = buf->base_buffer;
1874 /* The bidi_paragraph_cache must be invalidated first, because doing
1875 so might need to use the newline_cache (via find_newline_no_quit,
1876 see below). */
1877 if (buf->bidi_paragraph_cache)
1879 if (start != end
1880 && start > BUF_BEG (buf))
1882 /* If we are deleting or replacing characters, we could
1883 create a paragraph start, because all of the characters
1884 from START to the beginning of START's line are
1885 whitespace. Therefore, we must extend the region to be
1886 invalidated up to the newline before START. */
1887 ptrdiff_t line_beg = start;
1888 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
1890 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
1892 struct buffer *old = current_buffer;
1894 set_buffer_internal (buf);
1896 line_beg = find_newline_no_quit (start, start_byte, -1,
1897 &start_byte);
1898 set_buffer_internal (old);
1900 start = line_beg - (line_beg > BUF_BEG (buf));
1902 invalidate_region_cache (buf,
1903 buf->bidi_paragraph_cache,
1904 start - BUF_BEG (buf), BUF_Z (buf) - end);
1906 if (buf->newline_cache)
1907 invalidate_region_cache (buf,
1908 buf->newline_cache,
1909 start - BUF_BEG (buf), BUF_Z (buf) - end);
1910 if (buf->width_run_cache)
1911 invalidate_region_cache (buf,
1912 buf->width_run_cache,
1913 start - BUF_BEG (buf), BUF_Z (buf) - end);
1916 /* These macros work with an argument named `preserve_ptr'
1917 and a local variable named `preserve_marker'. */
1919 #define PRESERVE_VALUE \
1920 if (preserve_ptr && NILP (preserve_marker)) \
1921 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1923 #define RESTORE_VALUE \
1924 if (! NILP (preserve_marker)) \
1926 *preserve_ptr = marker_position (preserve_marker); \
1927 unchain_marker (XMARKER (preserve_marker)); \
1930 #define PRESERVE_START_END \
1931 if (NILP (start_marker)) \
1932 start_marker = Fcopy_marker (start, Qnil); \
1933 if (NILP (end_marker)) \
1934 end_marker = Fcopy_marker (end, Qnil);
1936 #define FETCH_START \
1937 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1939 #define FETCH_END \
1940 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1942 /* Set a variable to nil if an error occurred.
1943 Don't change the variable if there was no error.
1944 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1945 VARIABLE is the variable to maybe set to nil.
1946 NO-ERROR-FLAG is nil if there was an error,
1947 anything else meaning no error (so this function does nothing). */
1948 struct rvoe_arg
1950 Lisp_Object *location;
1951 bool errorp;
1954 static void
1955 reset_var_on_error (void *ptr)
1957 struct rvoe_arg *p = ptr;
1958 if (p->errorp)
1959 *p->location = Qnil;
1962 /* Signal a change to the buffer immediately before it happens.
1963 START_INT and END_INT are the bounds of the text to be changed.
1965 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1966 by holding its value temporarily in a marker. */
1968 static void
1969 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1970 ptrdiff_t *preserve_ptr)
1972 Lisp_Object start, end;
1973 Lisp_Object start_marker, end_marker;
1974 Lisp_Object preserve_marker;
1975 struct gcpro gcpro1, gcpro2, gcpro3;
1976 ptrdiff_t count = SPECPDL_INDEX ();
1977 struct rvoe_arg rvoe_arg;
1979 start = make_number (start_int);
1980 end = make_number (end_int);
1981 preserve_marker = Qnil;
1982 start_marker = Qnil;
1983 end_marker = Qnil;
1984 GCPRO3 (preserve_marker, start_marker, end_marker);
1986 specbind (Qinhibit_modification_hooks, Qt);
1988 /* If buffer is unmodified, run a special hook for that case. The
1989 check for Vfirst_change_hook is just a minor optimization. */
1990 if (SAVE_MODIFF >= MODIFF
1991 && !NILP (Vfirst_change_hook))
1993 PRESERVE_VALUE;
1994 PRESERVE_START_END;
1995 Frun_hooks (1, &Qfirst_change_hook);
1998 /* Now run the before-change-functions if any. */
1999 if (!NILP (Vbefore_change_functions))
2001 Lisp_Object args[3];
2002 rvoe_arg.location = &Vbefore_change_functions;
2003 rvoe_arg.errorp = 1;
2005 PRESERVE_VALUE;
2006 PRESERVE_START_END;
2008 /* Mark before-change-functions to be reset to nil in case of error. */
2009 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2011 /* Actually run the hook functions. */
2012 args[0] = Qbefore_change_functions;
2013 args[1] = FETCH_START;
2014 args[2] = FETCH_END;
2015 Frun_hook_with_args (3, args);
2017 /* There was no error: unarm the reset_on_error. */
2018 rvoe_arg.errorp = 0;
2021 if (buffer_has_overlays ())
2023 PRESERVE_VALUE;
2024 report_overlay_modification (FETCH_START, FETCH_END, 0,
2025 FETCH_START, FETCH_END, Qnil);
2028 if (! NILP (start_marker))
2029 free_marker (start_marker);
2030 if (! NILP (end_marker))
2031 free_marker (end_marker);
2032 RESTORE_VALUE;
2033 UNGCPRO;
2035 unbind_to (count, Qnil);
2038 /* Signal a change immediately after it happens.
2039 CHARPOS is the character position of the start of the changed text.
2040 LENDEL is the number of characters of the text before the change.
2041 (Not the whole buffer; just the part that was changed.)
2042 LENINS is the number of characters in that part of the text
2043 after the change. */
2045 void
2046 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2048 ptrdiff_t count = SPECPDL_INDEX ();
2049 struct rvoe_arg rvoe_arg;
2051 if (inhibit_modification_hooks)
2052 return;
2054 /* If we are deferring calls to the after-change functions
2055 and there are no before-change functions,
2056 just record the args that we were going to use. */
2057 if (! NILP (Vcombine_after_change_calls)
2058 && NILP (Vbefore_change_functions)
2059 && !buffer_has_overlays ())
2061 Lisp_Object elt;
2063 if (!NILP (combine_after_change_list)
2064 && current_buffer != XBUFFER (combine_after_change_buffer))
2065 Fcombine_after_change_execute ();
2067 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2068 lenins - lendel);
2069 combine_after_change_list
2070 = Fcons (elt, combine_after_change_list);
2071 combine_after_change_buffer = Fcurrent_buffer ();
2073 return;
2076 if (!NILP (combine_after_change_list))
2077 Fcombine_after_change_execute ();
2079 specbind (Qinhibit_modification_hooks, Qt);
2081 if (!NILP (Vafter_change_functions))
2083 Lisp_Object args[4];
2084 rvoe_arg.location = &Vafter_change_functions;
2085 rvoe_arg.errorp = 1;
2087 /* Mark after-change-functions to be reset to nil in case of error. */
2088 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2090 /* Actually run the hook functions. */
2091 args[0] = Qafter_change_functions;
2092 XSETFASTINT (args[1], charpos);
2093 XSETFASTINT (args[2], charpos + lenins);
2094 XSETFASTINT (args[3], lendel);
2095 Frun_hook_with_args (4, args);
2097 /* There was no error: unarm the reset_on_error. */
2098 rvoe_arg.errorp = 0;
2101 if (buffer_has_overlays ())
2102 report_overlay_modification (make_number (charpos),
2103 make_number (charpos + lenins),
2105 make_number (charpos),
2106 make_number (charpos + lenins),
2107 make_number (lendel));
2109 /* After an insertion, call the text properties
2110 insert-behind-hooks or insert-in-front-hooks. */
2111 if (lendel == 0)
2112 report_interval_modification (make_number (charpos),
2113 make_number (charpos + lenins));
2115 unbind_to (count, Qnil);
2118 static void
2119 Fcombine_after_change_execute_1 (Lisp_Object val)
2121 Vcombine_after_change_calls = val;
2124 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2125 Scombine_after_change_execute, 0, 0, 0,
2126 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2127 (void)
2129 ptrdiff_t count = SPECPDL_INDEX ();
2130 ptrdiff_t beg, end, change;
2131 ptrdiff_t begpos, endpos;
2132 Lisp_Object tail;
2134 if (NILP (combine_after_change_list))
2135 return Qnil;
2137 /* It is rare for combine_after_change_buffer to be invalid, but
2138 possible. It can happen when combine-after-change-calls is
2139 non-nil, and insertion calls a file handler (e.g. through
2140 lock_file) which scribbles into a temp file -- cyd */
2141 if (!BUFFERP (combine_after_change_buffer)
2142 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2144 combine_after_change_list = Qnil;
2145 return Qnil;
2148 record_unwind_current_buffer ();
2150 Fset_buffer (combine_after_change_buffer);
2152 /* # chars unchanged at beginning of buffer. */
2153 beg = Z - BEG;
2154 /* # chars unchanged at end of buffer. */
2155 end = beg;
2156 /* Total amount of insertion (negative for deletion). */
2157 change = 0;
2159 /* Scan the various individual changes,
2160 accumulating the range info in BEG, END and CHANGE. */
2161 for (tail = combine_after_change_list; CONSP (tail);
2162 tail = XCDR (tail))
2164 Lisp_Object elt;
2165 ptrdiff_t thisbeg, thisend, thischange;
2167 /* Extract the info from the next element. */
2168 elt = XCAR (tail);
2169 if (! CONSP (elt))
2170 continue;
2171 thisbeg = XINT (XCAR (elt));
2173 elt = XCDR (elt);
2174 if (! CONSP (elt))
2175 continue;
2176 thisend = XINT (XCAR (elt));
2178 elt = XCDR (elt);
2179 if (! CONSP (elt))
2180 continue;
2181 thischange = XINT (XCAR (elt));
2183 /* Merge this range into the accumulated range. */
2184 change += thischange;
2185 if (thisbeg < beg)
2186 beg = thisbeg;
2187 if (thisend < end)
2188 end = thisend;
2191 /* Get the current start and end positions of the range
2192 that was changed. */
2193 begpos = BEG + beg;
2194 endpos = Z - end;
2196 /* We are about to handle these, so discard them. */
2197 combine_after_change_list = Qnil;
2199 /* Now run the after-change functions for real.
2200 Turn off the flag that defers them. */
2201 record_unwind_protect (Fcombine_after_change_execute_1,
2202 Vcombine_after_change_calls);
2203 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2204 update_compositions (begpos, endpos, CHECK_ALL);
2206 return unbind_to (count, Qnil);
2209 void
2210 syms_of_insdel (void)
2212 staticpro (&combine_after_change_list);
2213 staticpro (&combine_after_change_buffer);
2214 combine_after_change_list = Qnil;
2215 combine_after_change_buffer = Qnil;
2217 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2218 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2219 Vcombine_after_change_calls = Qnil;
2221 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2222 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2223 This affects `before-change-functions' and `after-change-functions',
2224 as well as hooks attached to text properties and overlays. */);
2225 inhibit_modification_hooks = 0;
2226 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2228 DEFSYM (Qregion_extract_function, "region-extract-function");
2230 defsubr (&Scombine_after_change_execute);