Mention web bugs
[emacs.git] / src / insdel.c
blobf0a4dcd784e7d8827632c59b9040cbcf5e25fb50
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2016 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
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 "composite.h"
27 #include "intervals.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "window.h"
31 #include "region-cache.h"
33 static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35 static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
36 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37 static void gap_right (ptrdiff_t, ptrdiff_t);
39 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
41 was non-nil. We use this to decide how to call them
42 once the deferral ends.
44 In each element.
45 BEG-UNCHANGED is the number of chars before the changed range.
46 END-UNCHANGED is the number of chars after the changed range,
47 and CHANGE-AMOUNT is the number of characters inserted by the change
48 (negative for a deletion). */
49 static Lisp_Object combine_after_change_list;
51 /* Buffer which combine_after_change_list is about. */
52 static Lisp_Object combine_after_change_buffer;
54 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
56 /* Also used in marker.c to enable expensive marker checks. */
58 #ifdef MARKER_DEBUG
60 static void
61 check_markers (void)
63 struct Lisp_Marker *tail;
64 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
66 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
68 if (tail->buffer->text != current_buffer->text)
69 emacs_abort ();
70 if (tail->charpos > Z)
71 emacs_abort ();
72 if (tail->bytepos > Z_BYTE)
73 emacs_abort ();
74 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
75 emacs_abort ();
79 #else /* not MARKER_DEBUG */
81 #define check_markers() do { } while (0)
83 #endif /* MARKER_DEBUG */
85 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
86 Note that this can quit! */
88 void
89 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
91 eassert (charpos == BYTE_TO_CHAR (bytepos)
92 && bytepos == CHAR_TO_BYTE (charpos));
93 if (bytepos < GPT_BYTE)
94 gap_left (charpos, bytepos, 0);
95 else if (bytepos > GPT_BYTE)
96 gap_right (charpos, bytepos);
99 /* Move the gap to a position less than the current GPT.
100 BYTEPOS describes the new position as a byte position,
101 and CHARPOS is the corresponding char position.
102 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
104 static void
105 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
107 unsigned char *to, *from;
108 ptrdiff_t i;
109 ptrdiff_t new_s1;
111 if (!newgap)
112 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
114 i = GPT_BYTE;
115 to = GAP_END_ADDR;
116 from = GPT_ADDR;
117 new_s1 = GPT_BYTE;
119 /* Now copy the characters. To move the gap down,
120 copy characters up. */
122 while (1)
124 /* I gets number of characters left to copy. */
125 i = new_s1 - bytepos;
126 if (i == 0)
127 break;
128 /* If a quit is requested, stop copying now.
129 Change BYTEPOS to be where we have actually moved the gap to. */
130 if (QUITP)
132 bytepos = new_s1;
133 charpos = BYTE_TO_CHAR (bytepos);
134 break;
136 /* Move at most 32000 chars before checking again for a quit. */
137 if (i > 32000)
138 i = 32000;
139 new_s1 -= i;
140 from -= i, to -= i;
141 memmove (to, from, i);
144 /* Adjust buffer data structure, to put the gap at BYTEPOS.
145 BYTEPOS is where the loop above stopped, which may be what
146 was specified or may be where a quit was detected. */
147 GPT_BYTE = bytepos;
148 GPT = charpos;
149 eassert (charpos <= bytepos);
150 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
151 QUIT;
154 /* Move the gap to a position greater than the current GPT.
155 BYTEPOS describes the new position as a byte position,
156 and CHARPOS is the corresponding char position. */
158 static void
159 gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
161 register unsigned char *to, *from;
162 register ptrdiff_t i;
163 ptrdiff_t new_s1;
165 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
167 i = GPT_BYTE;
168 from = GAP_END_ADDR;
169 to = GPT_ADDR;
170 new_s1 = GPT_BYTE;
172 /* Now copy the characters. To move the gap up,
173 copy characters down. */
175 while (1)
177 /* I gets number of characters left to copy. */
178 i = bytepos - new_s1;
179 if (i == 0)
180 break;
181 /* If a quit is requested, stop copying now.
182 Change BYTEPOS to be where we have actually moved the gap to. */
183 if (QUITP)
185 bytepos = new_s1;
186 charpos = BYTE_TO_CHAR (bytepos);
187 break;
189 /* Move at most 32000 chars before checking again for a quit. */
190 if (i > 32000)
191 i = 32000;
192 new_s1 += i;
193 memmove (to, from, i);
194 from += i, to += i;
197 GPT = charpos;
198 GPT_BYTE = bytepos;
199 eassert (charpos <= bytepos);
200 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
201 QUIT;
204 /* If the selected window's old pointm is adjacent or covered by the
205 region from FROM to TO, unsuspend auto hscroll in that window. */
207 static void
208 adjust_suspend_auto_hscroll (ptrdiff_t from, ptrdiff_t to)
210 if (WINDOWP (selected_window))
212 struct window *w = XWINDOW (selected_window);
214 if (BUFFERP (w->contents)
215 && XBUFFER (w->contents) == current_buffer
216 && XMARKER (w->old_pointm)->charpos >= from
217 && XMARKER (w->old_pointm)->charpos <= to)
218 w->suspend_auto_hscroll = 0;
223 /* Adjust all markers for a deletion
224 whose range in bytes is FROM_BYTE to TO_BYTE.
225 The range in charpos is FROM to TO.
227 This function assumes that the gap is adjacent to
228 or inside of the range being deleted. */
230 void
231 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
232 ptrdiff_t to, ptrdiff_t to_byte)
234 struct Lisp_Marker *m;
235 ptrdiff_t charpos;
237 adjust_suspend_auto_hscroll (from, to);
238 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
240 charpos = m->charpos;
241 eassert (charpos <= Z);
243 /* If the marker is after the deletion,
244 relocate by number of chars / bytes deleted. */
245 if (charpos > to)
247 m->charpos -= to - from;
248 m->bytepos -= to_byte - from_byte;
250 /* Here's the case where a marker is inside text being deleted. */
251 else if (charpos > from)
253 m->charpos = from;
254 m->bytepos = from_byte;
260 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
261 to TO / TO_BYTE. We have to relocate the charpos of every marker
262 that points after the insertion (but not their bytepos).
264 When a marker points at the insertion point,
265 we advance it if either its insertion-type is t
266 or BEFORE_MARKERS is true. */
268 static void
269 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
270 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
272 struct Lisp_Marker *m;
273 bool adjusted = 0;
274 ptrdiff_t nchars = to - from;
275 ptrdiff_t nbytes = to_byte - from_byte;
277 adjust_suspend_auto_hscroll (from, to);
278 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
280 eassert (m->bytepos >= m->charpos
281 && m->bytepos - m->charpos <= Z_BYTE - Z);
283 if (m->bytepos == from_byte)
285 if (m->insertion_type || before_markers)
287 m->bytepos = to_byte;
288 m->charpos = to;
289 if (m->insertion_type)
290 adjusted = 1;
293 else if (m->bytepos > from_byte)
295 m->bytepos += nbytes;
296 m->charpos += nchars;
300 /* Adjusting only markers whose insertion-type is t may result in
301 - disordered start and end in overlays, and
302 - disordered overlays in the slot `overlays_before' of current_buffer. */
303 if (adjusted)
305 fix_start_end_in_overlays (from, to);
306 fix_overlays_before (current_buffer, from, to);
310 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
312 This is used only when the value of point changes due to an insert
313 or delete; it does not represent a conceptual change in point as a
314 marker. In particular, point is not crossing any interval
315 boundaries, so there's no need to use the usual SET_PT macro. In
316 fact it would be incorrect to do so, because either the old or the
317 new value of point is out of sync with the current set of
318 intervals. */
320 static void
321 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
323 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
324 /* In a single-byte buffer, the two positions must be equal. */
325 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
328 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
329 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
330 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
331 an insertion. */
333 static void
334 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
335 ptrdiff_t old_chars, ptrdiff_t old_bytes,
336 ptrdiff_t new_chars, ptrdiff_t new_bytes)
338 register struct Lisp_Marker *m;
339 ptrdiff_t prev_to_byte = from_byte + old_bytes;
340 ptrdiff_t diff_chars = new_chars - old_chars;
341 ptrdiff_t diff_bytes = new_bytes - old_bytes;
343 adjust_suspend_auto_hscroll (from, from + old_chars);
344 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
346 if (m->bytepos >= prev_to_byte)
348 m->charpos += diff_chars;
349 m->bytepos += diff_bytes;
351 else if (m->bytepos > from_byte)
353 m->charpos = from;
354 m->bytepos = from_byte;
358 check_markers ();
362 void
363 buffer_overflow (void)
365 error ("Maximum buffer size exceeded");
368 /* Make the gap NBYTES_ADDED bytes longer. */
370 static void
371 make_gap_larger (ptrdiff_t nbytes_added)
373 Lisp_Object tem;
374 ptrdiff_t real_gap_loc;
375 ptrdiff_t real_gap_loc_byte;
376 ptrdiff_t old_gap_size;
377 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
379 if (BUF_BYTES_MAX - current_size < nbytes_added)
380 buffer_overflow ();
382 /* If we have to get more space, get enough to last a while;
383 but do not exceed the maximum buffer size. */
384 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
385 BUF_BYTES_MAX - current_size);
387 enlarge_buffer_text (current_buffer, nbytes_added);
389 /* Prevent quitting in move_gap. */
390 tem = Vinhibit_quit;
391 Vinhibit_quit = Qt;
393 real_gap_loc = GPT;
394 real_gap_loc_byte = GPT_BYTE;
395 old_gap_size = GAP_SIZE;
397 /* Call the newly allocated space a gap at the end of the whole space. */
398 GPT = Z + GAP_SIZE;
399 GPT_BYTE = Z_BYTE + GAP_SIZE;
400 GAP_SIZE = nbytes_added;
402 /* Move the new gap down to be consecutive with the end of the old one. */
403 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
405 /* Now combine the two into one large gap. */
406 GAP_SIZE += old_gap_size;
407 GPT = real_gap_loc;
408 GPT_BYTE = real_gap_loc_byte;
410 /* Put an anchor. */
411 *(Z_ADDR) = 0;
413 Vinhibit_quit = tem;
416 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
418 /* Make the gap NBYTES_REMOVED bytes shorter. */
420 static void
421 make_gap_smaller (ptrdiff_t nbytes_removed)
423 Lisp_Object tem;
424 ptrdiff_t real_gap_loc;
425 ptrdiff_t real_gap_loc_byte;
426 ptrdiff_t real_Z;
427 ptrdiff_t real_Z_byte;
428 ptrdiff_t real_beg_unchanged;
429 ptrdiff_t new_gap_size;
431 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
432 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
433 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
435 /* Prevent quitting in move_gap. */
436 tem = Vinhibit_quit;
437 Vinhibit_quit = Qt;
439 real_gap_loc = GPT;
440 real_gap_loc_byte = GPT_BYTE;
441 new_gap_size = GAP_SIZE - nbytes_removed;
442 real_Z = Z;
443 real_Z_byte = Z_BYTE;
444 real_beg_unchanged = BEG_UNCHANGED;
446 /* Pretend that the last unwanted part of the gap is the entire gap,
447 and that the first desired part of the gap is part of the buffer
448 text. */
449 memset (GPT_ADDR, 0, new_gap_size);
450 GPT += new_gap_size;
451 GPT_BYTE += new_gap_size;
452 Z += new_gap_size;
453 Z_BYTE += new_gap_size;
454 GAP_SIZE = nbytes_removed;
456 /* Move the unwanted pretend gap to the end of the buffer. */
457 gap_right (Z, Z_BYTE);
459 enlarge_buffer_text (current_buffer, -nbytes_removed);
461 /* Now restore the desired gap. */
462 GAP_SIZE = new_gap_size;
463 GPT = real_gap_loc;
464 GPT_BYTE = real_gap_loc_byte;
465 Z = real_Z;
466 Z_BYTE = real_Z_byte;
467 BEG_UNCHANGED = real_beg_unchanged;
469 /* Put an anchor. */
470 *(Z_ADDR) = 0;
472 Vinhibit_quit = tem;
475 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
477 void
478 make_gap (ptrdiff_t nbytes_added)
480 if (nbytes_added >= 0)
481 make_gap_larger (nbytes_added);
482 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
483 else
484 make_gap_smaller (-nbytes_added);
485 #endif
488 /* Add NBYTES to B's gap. It's enough to temporarily
489 fake current_buffer and avoid real switch to B. */
491 void
492 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
494 struct buffer *oldb = current_buffer;
496 current_buffer = b;
497 make_gap (nbytes);
498 current_buffer = oldb;
501 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
502 FROM_MULTIBYTE says whether the incoming text is multibyte.
503 TO_MULTIBYTE says whether to store the text as multibyte.
504 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
506 Return the number of bytes stored at TO_ADDR. */
508 ptrdiff_t
509 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
510 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
512 if (from_multibyte == to_multibyte)
514 memcpy (to_addr, from_addr, nbytes);
515 return nbytes;
517 else if (from_multibyte)
519 ptrdiff_t nchars = 0;
520 ptrdiff_t bytes_left = nbytes;
522 while (bytes_left > 0)
524 int thislen, c;
525 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
526 if (! ASCII_CHAR_P (c))
527 c &= 0xFF;
528 *to_addr++ = c;
529 from_addr += thislen;
530 bytes_left -= thislen;
531 nchars++;
533 return nchars;
535 else
537 unsigned char *initial_to_addr = to_addr;
539 /* Convert single-byte to multibyte. */
540 while (nbytes > 0)
542 int c = *from_addr++;
544 if (!ASCII_CHAR_P (c))
546 c = BYTE8_TO_CHAR (c);
547 to_addr += CHAR_STRING (c, to_addr);
548 nbytes--;
550 else
551 /* Special case for speed. */
552 *to_addr++ = c, nbytes--;
554 return to_addr - initial_to_addr;
558 /* Insert a string of specified length before point.
559 This function judges multibyteness based on
560 enable_multibyte_characters in the current buffer;
561 it never converts between single-byte and multibyte.
563 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
564 prepare_to_modify_buffer could relocate the text. */
566 void
567 insert (const char *string, ptrdiff_t nbytes)
569 if (nbytes > 0)
571 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
572 insert_1_both (string, len, nbytes, 0, 1, 0);
573 opoint = PT - len;
574 signal_after_change (opoint, 0, len);
575 update_compositions (opoint, PT, CHECK_BORDER);
579 /* Likewise, but inherit text properties from neighboring characters. */
581 void
582 insert_and_inherit (const char *string, ptrdiff_t nbytes)
584 if (nbytes > 0)
586 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
587 insert_1_both (string, len, nbytes, 1, 1, 0);
588 opoint = PT - len;
589 signal_after_change (opoint, 0, len);
590 update_compositions (opoint, PT, CHECK_BORDER);
594 /* Insert the character C before point. Do not inherit text properties. */
596 void
597 insert_char (int c)
599 unsigned char str[MAX_MULTIBYTE_LENGTH];
600 int len;
602 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
603 len = CHAR_STRING (c, str);
604 else
606 len = 1;
607 str[0] = c;
610 insert ((char *) str, len);
613 /* Insert the null-terminated string S before point. */
615 void
616 insert_string (const char *s)
618 insert (s, strlen (s));
621 /* Like `insert' except that all markers pointing at the place where
622 the insertion happens are adjusted to point after it.
623 Don't use this function to insert part of a Lisp string,
624 since gc could happen and relocate it. */
626 void
627 insert_before_markers (const char *string, ptrdiff_t nbytes)
629 if (nbytes > 0)
631 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
632 insert_1_both (string, len, nbytes, 0, 1, 1);
633 opoint = PT - len;
634 signal_after_change (opoint, 0, len);
635 update_compositions (opoint, PT, CHECK_BORDER);
639 /* Likewise, but inherit text properties from neighboring characters. */
641 void
642 insert_before_markers_and_inherit (const char *string,
643 ptrdiff_t nbytes)
645 if (nbytes > 0)
647 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
648 insert_1_both (string, len, nbytes, 1, 1, 1);
649 opoint = PT - len;
650 signal_after_change (opoint, 0, len);
651 update_compositions (opoint, PT, CHECK_BORDER);
655 #ifdef BYTE_COMBINING_DEBUG
657 /* See if the bytes before POS/POS_BYTE combine with bytes
658 at the start of STRING to form a single character.
659 If so, return the number of bytes at the start of STRING
660 which combine in this way. Otherwise, return 0. */
663 count_combining_before (const unsigned char *string, ptrdiff_t length,
664 ptrdiff_t pos, ptrdiff_t pos_byte)
666 int len, combining_bytes;
667 const unsigned char *p;
669 if (NILP (current_buffer->enable_multibyte_characters))
670 return 0;
672 /* At first, we can exclude the following cases:
673 (1) STRING[0] can't be a following byte of multibyte sequence.
674 (2) POS is the start of the current buffer.
675 (3) A character before POS is not a multibyte character. */
676 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
677 return 0;
678 if (pos_byte == BEG_BYTE) /* case (2) */
679 return 0;
680 len = 1;
681 p = BYTE_POS_ADDR (pos_byte - 1);
682 while (! CHAR_HEAD_P (*p)) p--, len++;
683 if (! LEADING_CODE_P (*p)) /* case (3) */
684 return 0;
686 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
687 if (combining_bytes <= 0)
688 /* The character preceding POS is, complete and no room for
689 combining bytes (combining_bytes == 0), or an independent 8-bit
690 character (combining_bytes < 0). */
691 return 0;
693 /* We have a combination situation. Count the bytes at STRING that
694 may combine. */
695 p = string + 1;
696 while (!CHAR_HEAD_P (*p) && p < string + length)
697 p++;
699 return (combining_bytes < p - string ? combining_bytes : p - string);
702 /* See if the bytes after POS/POS_BYTE combine with bytes
703 at the end of STRING to form a single character.
704 If so, return the number of bytes after POS/POS_BYTE
705 which combine in this way. Otherwise, return 0. */
708 count_combining_after (const unsigned char *string,
709 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
711 ptrdiff_t opos_byte = pos_byte;
712 ptrdiff_t i;
713 ptrdiff_t bytes;
714 unsigned char *bufp;
716 if (NILP (current_buffer->enable_multibyte_characters))
717 return 0;
719 /* At first, we can exclude the following cases:
720 (1) The last byte of STRING is an ASCII.
721 (2) POS is the last of the current buffer.
722 (3) A character at POS can't be a following byte of multibyte
723 character. */
724 if (length > 0 && ASCII_CHAR_P (string[length - 1])) /* case (1) */
725 return 0;
726 if (pos_byte == Z_BYTE) /* case (2) */
727 return 0;
728 bufp = BYTE_POS_ADDR (pos_byte);
729 if (CHAR_HEAD_P (*bufp)) /* case (3) */
730 return 0;
732 i = length - 1;
733 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
735 i--;
737 if (i < 0)
739 /* All characters in STRING are not character head. We must
740 check also preceding bytes at POS. We are sure that the gap
741 is at POS. */
742 unsigned char *p = BEG_ADDR;
743 i = pos_byte - 2;
744 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
745 i--;
746 if (i < 0 || !LEADING_CODE_P (p[i]))
747 return 0;
749 bytes = BYTES_BY_CHAR_HEAD (p[i]);
750 return (bytes <= pos_byte - 1 - i + length
752 : bytes - (pos_byte - 1 - i + length));
754 if (!LEADING_CODE_P (string[i]))
755 return 0;
757 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
758 bufp++, pos_byte++;
759 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
761 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
764 #endif
767 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
768 starting at STRING. INHERIT non-zero means inherit the text
769 properties from neighboring characters; zero means inserted text
770 will have no text properties. PREPARE non-zero means call
771 prepare_to_modify_buffer, which checks that the region is not
772 read-only, and calls before-change-function and any modification
773 properties the text may have. BEFORE_MARKERS non-zero means adjust
774 all markers that point at the insertion place to point after it. */
776 void
777 insert_1_both (const char *string,
778 ptrdiff_t nchars, ptrdiff_t nbytes,
779 bool inherit, bool prepare, bool before_markers)
781 if (nchars == 0)
782 return;
784 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
785 nchars = nbytes;
787 if (prepare)
788 /* Do this before moving and increasing the gap,
789 because the before-change hooks might move the gap
790 or make it smaller. */
791 prepare_to_modify_buffer (PT, PT, NULL);
793 if (PT != GPT)
794 move_gap_both (PT, PT_BYTE);
795 if (GAP_SIZE < nbytes)
796 make_gap (nbytes - GAP_SIZE);
798 #ifdef BYTE_COMBINING_DEBUG
799 if (count_combining_before (string, nbytes, PT, PT_BYTE)
800 || count_combining_after (string, nbytes, PT, PT_BYTE))
801 emacs_abort ();
802 #endif
804 /* Record deletion of the surrounding text that combines with
805 the insertion. This, together with recording the insertion,
806 will add up to the right stuff in the undo list. */
807 record_insert (PT, nchars);
808 MODIFF++;
809 CHARS_MODIFF = MODIFF;
811 memcpy (GPT_ADDR, string, nbytes);
813 GAP_SIZE -= nbytes;
814 GPT += nchars;
815 ZV += nchars;
816 Z += nchars;
817 GPT_BYTE += nbytes;
818 ZV_BYTE += nbytes;
819 Z_BYTE += nbytes;
820 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
822 eassert (GPT <= GPT_BYTE);
824 /* The insert may have been in the unchanged region, so check again. */
825 if (Z - GPT < END_UNCHANGED)
826 END_UNCHANGED = Z - GPT;
828 adjust_overlays_for_insert (PT, nchars);
829 adjust_markers_for_insert (PT, PT_BYTE,
830 PT + nchars, PT_BYTE + nbytes,
831 before_markers);
833 offset_intervals (current_buffer, PT, nchars);
835 if (!inherit && buffer_intervals (current_buffer))
836 set_text_properties (make_number (PT), make_number (PT + nchars),
837 Qnil, Qnil, Qnil);
839 adjust_point (nchars, nbytes);
841 check_markers ();
844 /* Insert the part of the text of STRING, a Lisp object assumed to be
845 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
846 starting at position POS / POS_BYTE. If the text of STRING has properties,
847 copy them into the buffer.
849 It does not work to use `insert' for this, because a GC could happen
850 before we copy the stuff into the buffer, and relocate the string
851 without insert noticing. */
853 void
854 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
855 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
857 ptrdiff_t opoint = PT;
859 if (SCHARS (string) == 0)
860 return;
862 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
863 inherit, 0);
864 signal_after_change (opoint, 0, PT - opoint);
865 update_compositions (opoint, PT, CHECK_BORDER);
868 /* Like `insert_from_string' except that all markers pointing
869 at the place where the insertion happens are adjusted to point after it. */
871 void
872 insert_from_string_before_markers (Lisp_Object string,
873 ptrdiff_t pos, ptrdiff_t pos_byte,
874 ptrdiff_t length, ptrdiff_t length_byte,
875 bool inherit)
877 ptrdiff_t opoint = PT;
879 if (SCHARS (string) == 0)
880 return;
882 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
883 inherit, 1);
884 signal_after_change (opoint, 0, PT - opoint);
885 update_compositions (opoint, PT, CHECK_BORDER);
888 /* Subroutine of the insertion functions above. */
890 static void
891 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
892 ptrdiff_t nchars, ptrdiff_t nbytes,
893 bool inherit, bool before_markers)
895 ptrdiff_t outgoing_nbytes = nbytes;
896 INTERVAL intervals;
898 /* Make OUTGOING_NBYTES describe the text
899 as it will be inserted in this buffer. */
901 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
902 outgoing_nbytes = nchars;
903 else if (! STRING_MULTIBYTE (string))
904 outgoing_nbytes
905 = count_size_as_multibyte (SDATA (string) + pos_byte,
906 nbytes);
908 /* Do this before moving and increasing the gap,
909 because the before-change hooks might move the gap
910 or make it smaller. */
911 prepare_to_modify_buffer (PT, PT, NULL);
913 if (PT != GPT)
914 move_gap_both (PT, PT_BYTE);
915 if (GAP_SIZE < outgoing_nbytes)
916 make_gap (outgoing_nbytes - GAP_SIZE);
918 /* Copy the string text into the buffer, perhaps converting
919 between single-byte and multibyte. */
920 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
921 STRING_MULTIBYTE (string),
922 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
924 #ifdef BYTE_COMBINING_DEBUG
925 /* We have copied text into the gap, but we have not altered
926 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
927 to these functions and get the same results as we would
928 have got earlier on. Meanwhile, PT_ADDR does point to
929 the text that has been stored by copy_text. */
930 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
931 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
932 emacs_abort ();
933 #endif
935 record_insert (PT, nchars);
936 MODIFF++;
937 CHARS_MODIFF = MODIFF;
939 GAP_SIZE -= outgoing_nbytes;
940 GPT += nchars;
941 ZV += nchars;
942 Z += nchars;
943 GPT_BYTE += outgoing_nbytes;
944 ZV_BYTE += outgoing_nbytes;
945 Z_BYTE += outgoing_nbytes;
946 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
948 eassert (GPT <= GPT_BYTE);
950 /* The insert may have been in the unchanged region, so check again. */
951 if (Z - GPT < END_UNCHANGED)
952 END_UNCHANGED = Z - GPT;
954 adjust_overlays_for_insert (PT, nchars);
955 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
956 PT_BYTE + outgoing_nbytes,
957 before_markers);
959 offset_intervals (current_buffer, PT, nchars);
961 intervals = string_intervals (string);
962 /* Get the intervals for the part of the string we are inserting. */
963 if (nbytes < SBYTES (string))
964 intervals = copy_intervals (intervals, pos, nchars);
966 /* Insert those intervals. */
967 graft_intervals_into_buffer (intervals, PT, nchars,
968 current_buffer, inherit);
970 adjust_point (nchars, outgoing_nbytes);
972 check_markers ();
975 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
976 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
977 GPT_ADDR (if not text_at_gap_tail). */
979 void
980 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
982 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
984 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
985 nchars = nbytes;
987 /* No need to call prepare_to_modify_buffer, since this is called
988 from places that replace some region with a different text, so
989 prepare_to_modify_buffer was already called by the deletion part
990 of this dance. */
991 invalidate_buffer_caches (current_buffer, GPT, GPT);
992 record_insert (GPT, nchars);
993 MODIFF++;
995 GAP_SIZE -= nbytes;
996 if (! text_at_gap_tail)
998 GPT += nchars;
999 GPT_BYTE += nbytes;
1001 ZV += nchars;
1002 Z += nchars;
1003 ZV_BYTE += nbytes;
1004 Z_BYTE += nbytes;
1005 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1007 eassert (GPT <= GPT_BYTE);
1009 adjust_overlays_for_insert (ins_charpos, nchars);
1010 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1011 ins_charpos + nchars, ins_bytepos + nbytes, 0);
1013 if (buffer_intervals (current_buffer))
1015 offset_intervals (current_buffer, ins_charpos, nchars);
1016 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1017 current_buffer, 0);
1020 if (ins_charpos < PT)
1021 adjust_point (nchars, nbytes);
1023 check_markers ();
1026 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1027 current buffer. If the text in BUF has properties, they are absorbed
1028 into the current buffer.
1030 It does not work to use `insert' for this, because a malloc could happen
1031 and relocate BUF's text before the copy happens. */
1033 void
1034 insert_from_buffer (struct buffer *buf,
1035 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1037 ptrdiff_t opoint = PT;
1039 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1040 signal_after_change (opoint, 0, PT - opoint);
1041 update_compositions (opoint, PT, CHECK_BORDER);
1044 static void
1045 insert_from_buffer_1 (struct buffer *buf,
1046 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1048 ptrdiff_t chunk, chunk_expanded;
1049 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1050 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1051 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1052 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1053 INTERVAL intervals;
1055 if (nchars == 0)
1056 return;
1058 /* Make OUTGOING_NBYTES describe the text
1059 as it will be inserted in this buffer. */
1061 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1062 outgoing_nbytes = nchars;
1063 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1065 ptrdiff_t outgoing_before_gap = 0;
1066 ptrdiff_t outgoing_after_gap = 0;
1068 if (from < BUF_GPT (buf))
1070 chunk = BUF_GPT_BYTE (buf) - from_byte;
1071 if (chunk > incoming_nbytes)
1072 chunk = incoming_nbytes;
1073 outgoing_before_gap
1074 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1075 chunk);
1077 else
1078 chunk = 0;
1080 if (chunk < incoming_nbytes)
1081 outgoing_after_gap
1082 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1083 from_byte + chunk),
1084 incoming_nbytes - chunk);
1086 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1089 /* Do this before moving and increasing the gap,
1090 because the before-change hooks might move the gap
1091 or make it smaller. */
1092 prepare_to_modify_buffer (PT, PT, NULL);
1094 if (PT != GPT)
1095 move_gap_both (PT, PT_BYTE);
1096 if (GAP_SIZE < outgoing_nbytes)
1097 make_gap (outgoing_nbytes - GAP_SIZE);
1099 if (from < BUF_GPT (buf))
1101 chunk = BUF_GPT_BYTE (buf) - from_byte;
1102 if (chunk > incoming_nbytes)
1103 chunk = incoming_nbytes;
1104 /* Record number of output bytes, so we know where
1105 to put the output from the second copy_text. */
1106 chunk_expanded
1107 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1108 GPT_ADDR, chunk,
1109 ! NILP (BVAR (buf, enable_multibyte_characters)),
1110 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1112 else
1113 chunk_expanded = chunk = 0;
1115 if (chunk < incoming_nbytes)
1116 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1117 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1118 ! NILP (BVAR (buf, enable_multibyte_characters)),
1119 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1121 #ifdef BYTE_COMBINING_DEBUG
1122 /* We have copied text into the gap, but we have not altered
1123 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1124 to these functions and get the same results as we would
1125 have got earlier on. Meanwhile, GPT_ADDR does point to
1126 the text that has been stored by copy_text. */
1127 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1128 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1129 emacs_abort ();
1130 #endif
1132 record_insert (PT, nchars);
1133 MODIFF++;
1134 CHARS_MODIFF = MODIFF;
1136 GAP_SIZE -= outgoing_nbytes;
1137 GPT += nchars;
1138 ZV += nchars;
1139 Z += nchars;
1140 GPT_BYTE += outgoing_nbytes;
1141 ZV_BYTE += outgoing_nbytes;
1142 Z_BYTE += outgoing_nbytes;
1143 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1145 eassert (GPT <= GPT_BYTE);
1147 /* The insert may have been in the unchanged region, so check again. */
1148 if (Z - GPT < END_UNCHANGED)
1149 END_UNCHANGED = Z - GPT;
1151 adjust_overlays_for_insert (PT, nchars);
1152 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1153 PT_BYTE + outgoing_nbytes,
1156 offset_intervals (current_buffer, PT, nchars);
1158 /* Get the intervals for the part of the string we are inserting. */
1159 intervals = buffer_intervals (buf);
1160 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1162 if (buf == current_buffer && PT <= from)
1163 from += nchars;
1164 intervals = copy_intervals (intervals, from, nchars);
1167 /* Insert those intervals. */
1168 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1170 adjust_point (nchars, outgoing_nbytes);
1173 /* Record undo information and adjust markers and position keepers for
1174 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1175 chars (LEN_BYTE bytes) which resides in the gap just after
1176 GPT_ADDR.
1178 PREV_TEXT nil means the new text was just inserted. */
1180 static void
1181 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1182 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1184 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1186 #ifdef BYTE_COMBINING_DEBUG
1187 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1188 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1189 emacs_abort ();
1190 #endif
1192 if (STRINGP (prev_text))
1194 nchars_del = SCHARS (prev_text);
1195 nbytes_del = SBYTES (prev_text);
1198 /* Update various buffer positions for the new text. */
1199 GAP_SIZE -= len_byte;
1200 ZV += len; Z += len;
1201 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1202 GPT += len; GPT_BYTE += len_byte;
1203 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1205 if (nchars_del > 0)
1206 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1207 len, len_byte);
1208 else
1209 adjust_markers_for_insert (from, from_byte,
1210 from + len, from_byte + len_byte, 0);
1212 if (nchars_del > 0)
1213 record_delete (from, prev_text, false);
1214 record_insert (from, len);
1216 if (len > nchars_del)
1217 adjust_overlays_for_insert (from, len - nchars_del);
1218 else if (len < nchars_del)
1219 adjust_overlays_for_delete (from, nchars_del - len);
1221 offset_intervals (current_buffer, from, len - nchars_del);
1223 if (from < PT)
1224 adjust_point (len - nchars_del, len_byte - nbytes_del);
1226 /* As byte combining will decrease Z, we must check this again. */
1227 if (Z - GPT < END_UNCHANGED)
1228 END_UNCHANGED = Z - GPT;
1230 check_markers ();
1232 if (len == 0)
1233 evaporate_overlays (from);
1234 MODIFF++;
1235 CHARS_MODIFF = MODIFF;
1238 /* Record undo information, adjust markers and position keepers for an
1239 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1240 text already exists in the current buffer but character length (TO
1241 - FROM) may be incorrect, the correct length is NEWLEN. */
1243 void
1244 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1245 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1247 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1249 if (GPT != to)
1250 move_gap_both (to, to_byte);
1251 GAP_SIZE += len_byte;
1252 GPT -= len; GPT_BYTE -= len_byte;
1253 ZV -= len; ZV_BYTE -= len_byte;
1254 Z -= len; Z_BYTE -= len_byte;
1255 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1258 /* Replace the text from character positions FROM to TO with NEW,
1259 If PREPARE, call prepare_to_modify_buffer.
1260 If INHERIT, the newly inserted text should inherit text properties
1261 from the surrounding non-deleted text. */
1263 /* Note that this does not yet handle markers quite right.
1264 Also it needs to record a single undo-entry that does a replacement
1265 rather than a separate delete and insert.
1266 That way, undo will also handle markers properly.
1268 But if MARKERS is 0, don't relocate markers. */
1270 void
1271 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1272 bool prepare, bool inherit, bool markers)
1274 ptrdiff_t inschars = SCHARS (new);
1275 ptrdiff_t insbytes = SBYTES (new);
1276 ptrdiff_t from_byte, to_byte;
1277 ptrdiff_t nbytes_del, nchars_del;
1278 INTERVAL intervals;
1279 ptrdiff_t outgoing_insbytes = insbytes;
1280 Lisp_Object deletion;
1282 check_markers ();
1284 deletion = Qnil;
1286 if (prepare)
1288 ptrdiff_t range_length = to - from;
1289 prepare_to_modify_buffer (from, to, &from);
1290 to = from + range_length;
1293 /* Make args be valid. */
1294 if (from < BEGV)
1295 from = BEGV;
1296 if (to > ZV)
1297 to = ZV;
1299 from_byte = CHAR_TO_BYTE (from);
1300 to_byte = CHAR_TO_BYTE (to);
1302 nchars_del = to - from;
1303 nbytes_del = to_byte - from_byte;
1305 if (nbytes_del <= 0 && insbytes == 0)
1306 return;
1308 /* Make OUTGOING_INSBYTES describe the text
1309 as it will be inserted in this buffer. */
1311 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1312 outgoing_insbytes = inschars;
1313 else if (! STRING_MULTIBYTE (new))
1314 outgoing_insbytes
1315 = count_size_as_multibyte (SDATA (new), insbytes);
1317 /* Make sure the gap is somewhere in or next to what we are deleting. */
1318 if (from > GPT)
1319 gap_right (from, from_byte);
1320 if (to < GPT)
1321 gap_left (to, to_byte, 0);
1323 /* Even if we don't record for undo, we must keep the original text
1324 because we may have to recover it because of inappropriate byte
1325 combining. */
1326 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1327 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1329 GAP_SIZE += nbytes_del;
1330 ZV -= nchars_del;
1331 Z -= nchars_del;
1332 ZV_BYTE -= nbytes_del;
1333 Z_BYTE -= nbytes_del;
1334 GPT = from;
1335 GPT_BYTE = from_byte;
1336 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1338 eassert (GPT <= GPT_BYTE);
1340 if (GPT - BEG < BEG_UNCHANGED)
1341 BEG_UNCHANGED = GPT - BEG;
1342 if (Z - GPT < END_UNCHANGED)
1343 END_UNCHANGED = Z - GPT;
1345 if (GAP_SIZE < outgoing_insbytes)
1346 make_gap (outgoing_insbytes - GAP_SIZE);
1348 /* Copy the string text into the buffer, perhaps converting
1349 between single-byte and multibyte. */
1350 copy_text (SDATA (new), GPT_ADDR, insbytes,
1351 STRING_MULTIBYTE (new),
1352 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1354 #ifdef BYTE_COMBINING_DEBUG
1355 /* We have copied text into the gap, but we have not marked
1356 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1357 here, for both the previous text and the following text.
1358 Meanwhile, GPT_ADDR does point to
1359 the text that has been stored by copy_text. */
1360 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1361 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1362 emacs_abort ();
1363 #endif
1365 /* Record the insertion first, so that when we undo,
1366 the deletion will be undone first. Thus, undo
1367 will insert before deleting, and thus will keep
1368 the markers before and after this text separate. */
1369 if (!NILP (deletion))
1371 record_insert (from + SCHARS (deletion), inschars);
1372 record_delete (from, deletion, false);
1375 GAP_SIZE -= outgoing_insbytes;
1376 GPT += inschars;
1377 ZV += inschars;
1378 Z += inschars;
1379 GPT_BYTE += outgoing_insbytes;
1380 ZV_BYTE += outgoing_insbytes;
1381 Z_BYTE += outgoing_insbytes;
1382 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1384 eassert (GPT <= GPT_BYTE);
1386 /* Adjust markers for the deletion and the insertion. */
1387 if (markers)
1388 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1389 inschars, outgoing_insbytes);
1391 /* Adjust the overlay center as needed. This must be done after
1392 adjusting the markers that bound the overlays. */
1393 adjust_overlays_for_delete (from, nchars_del);
1394 adjust_overlays_for_insert (from, inschars);
1396 offset_intervals (current_buffer, from, inschars - nchars_del);
1398 /* Get the intervals for the part of the string we are inserting--
1399 not including the combined-before bytes. */
1400 intervals = string_intervals (new);
1401 /* Insert those intervals. */
1402 graft_intervals_into_buffer (intervals, from, inschars,
1403 current_buffer, inherit);
1405 /* Relocate point as if it were a marker. */
1406 if (from < PT)
1407 adjust_point ((from + inschars - (PT < to ? PT : to)),
1408 (from_byte + outgoing_insbytes
1409 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1411 if (outgoing_insbytes == 0)
1412 evaporate_overlays (from);
1414 check_markers ();
1416 MODIFF++;
1417 CHARS_MODIFF = MODIFF;
1419 signal_after_change (from, nchars_del, GPT - from);
1420 update_compositions (from, GPT, CHECK_BORDER);
1423 /* Replace the text from character positions FROM to TO with
1424 the text in INS of length INSCHARS.
1425 Keep the text properties that applied to the old characters
1426 (extending them to all the new chars if there are more new chars).
1428 Note that this does not yet handle markers quite right.
1430 If MARKERS, relocate markers.
1432 Unlike most functions at this level, never call
1433 prepare_to_modify_buffer and never call signal_after_change. */
1435 void
1436 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1437 ptrdiff_t to, ptrdiff_t to_byte,
1438 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1439 bool markers)
1441 ptrdiff_t nbytes_del, nchars_del;
1443 check_markers ();
1445 nchars_del = to - from;
1446 nbytes_del = to_byte - from_byte;
1448 if (nbytes_del <= 0 && insbytes == 0)
1449 return;
1451 /* Make sure the gap is somewhere in or next to what we are deleting. */
1452 if (from > GPT)
1453 gap_right (from, from_byte);
1454 if (to < GPT)
1455 gap_left (to, to_byte, 0);
1457 GAP_SIZE += nbytes_del;
1458 ZV -= nchars_del;
1459 Z -= nchars_del;
1460 ZV_BYTE -= nbytes_del;
1461 Z_BYTE -= nbytes_del;
1462 GPT = from;
1463 GPT_BYTE = from_byte;
1464 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1466 eassert (GPT <= GPT_BYTE);
1468 if (GPT - BEG < BEG_UNCHANGED)
1469 BEG_UNCHANGED = GPT - BEG;
1470 if (Z - GPT < END_UNCHANGED)
1471 END_UNCHANGED = Z - GPT;
1473 if (GAP_SIZE < insbytes)
1474 make_gap (insbytes - GAP_SIZE);
1476 /* Copy the replacement text into the buffer. */
1477 memcpy (GPT_ADDR, ins, insbytes);
1479 #ifdef BYTE_COMBINING_DEBUG
1480 /* We have copied text into the gap, but we have not marked
1481 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1482 here, for both the previous text and the following text.
1483 Meanwhile, GPT_ADDR does point to
1484 the text that has been stored by copy_text. */
1485 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1486 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1487 emacs_abort ();
1488 #endif
1490 GAP_SIZE -= insbytes;
1491 GPT += inschars;
1492 ZV += inschars;
1493 Z += inschars;
1494 GPT_BYTE += insbytes;
1495 ZV_BYTE += insbytes;
1496 Z_BYTE += insbytes;
1497 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1499 eassert (GPT <= GPT_BYTE);
1501 /* Adjust markers for the deletion and the insertion. */
1502 if (markers
1503 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1504 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1505 inschars, insbytes);
1507 /* Adjust the overlay center as needed. This must be done after
1508 adjusting the markers that bound the overlays. */
1509 if (nchars_del != inschars)
1511 adjust_overlays_for_insert (from, inschars);
1512 adjust_overlays_for_delete (from + inschars, nchars_del);
1515 offset_intervals (current_buffer, from, inschars - nchars_del);
1517 /* Relocate point as if it were a marker. */
1518 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1520 if (PT < to)
1521 /* PT was within the deleted text. Move it to FROM. */
1522 adjust_point (from - PT, from_byte - PT_BYTE);
1523 else
1524 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1527 if (insbytes == 0)
1528 evaporate_overlays (from);
1530 check_markers ();
1532 MODIFF++;
1533 CHARS_MODIFF = MODIFF;
1536 /* Delete characters in current buffer
1537 from FROM up to (but not including) TO.
1538 If TO comes before FROM, we delete nothing. */
1540 void
1541 del_range (ptrdiff_t from, ptrdiff_t to)
1543 del_range_1 (from, to, 1, 0);
1546 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1547 RET_STRING says to return the deleted text. */
1549 Lisp_Object
1550 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1552 ptrdiff_t from_byte, to_byte;
1553 Lisp_Object deletion;
1555 /* Make args be valid */
1556 if (from < BEGV)
1557 from = BEGV;
1558 if (to > ZV)
1559 to = ZV;
1561 if (to <= from)
1562 return Qnil;
1564 if (prepare)
1566 ptrdiff_t range_length = to - from;
1567 prepare_to_modify_buffer (from, to, &from);
1568 to = min (ZV, from + range_length);
1571 from_byte = CHAR_TO_BYTE (from);
1572 to_byte = CHAR_TO_BYTE (to);
1574 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1575 signal_after_change (from, to - from, 0);
1576 update_compositions (from, from, CHECK_HEAD);
1577 return deletion;
1580 /* Like del_range_1 but args are byte positions, not char positions. */
1582 void
1583 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
1585 ptrdiff_t from, to;
1587 /* Make args be valid. */
1588 if (from_byte < BEGV_BYTE)
1589 from_byte = BEGV_BYTE;
1590 if (to_byte > ZV_BYTE)
1591 to_byte = ZV_BYTE;
1593 if (to_byte <= from_byte)
1594 return;
1596 from = BYTE_TO_CHAR (from_byte);
1597 to = BYTE_TO_CHAR (to_byte);
1599 if (prepare)
1601 ptrdiff_t old_from = from, old_to = Z - to;
1602 ptrdiff_t range_length = to - from;
1603 prepare_to_modify_buffer (from, to, &from);
1604 to = from + range_length;
1606 if (old_from != from)
1607 from_byte = CHAR_TO_BYTE (from);
1608 if (to > ZV)
1610 to = ZV;
1611 to_byte = ZV_BYTE;
1613 else if (old_to == Z - to)
1614 to_byte = CHAR_TO_BYTE (to);
1617 del_range_2 (from, from_byte, to, to_byte, 0);
1618 signal_after_change (from, to - from, 0);
1619 update_compositions (from, from, CHECK_HEAD);
1622 /* Like del_range_1, but positions are specified both as charpos
1623 and bytepos. */
1625 void
1626 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1627 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1629 /* Make args be valid */
1630 if (from_byte < BEGV_BYTE)
1631 from_byte = BEGV_BYTE;
1632 if (to_byte > ZV_BYTE)
1633 to_byte = ZV_BYTE;
1635 if (to_byte <= from_byte)
1636 return;
1638 if (from < BEGV)
1639 from = BEGV;
1640 if (to > ZV)
1641 to = ZV;
1643 if (prepare)
1645 ptrdiff_t old_from = from, old_to = Z - to;
1646 ptrdiff_t range_length = to - from;
1647 prepare_to_modify_buffer (from, to, &from);
1648 to = from + range_length;
1650 if (old_from != from)
1651 from_byte = CHAR_TO_BYTE (from);
1652 if (to > ZV)
1654 to = ZV;
1655 to_byte = ZV_BYTE;
1657 else if (old_to == Z - to)
1658 to_byte = CHAR_TO_BYTE (to);
1661 del_range_2 (from, from_byte, to, to_byte, 0);
1662 signal_after_change (from, to - from, 0);
1663 update_compositions (from, from, CHECK_HEAD);
1666 /* Delete a range of text, specified both as character positions
1667 and byte positions. FROM and TO are character positions,
1668 while FROM_BYTE and TO_BYTE are byte positions.
1669 If RET_STRING, the deleted area is returned as a string. */
1671 Lisp_Object
1672 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1673 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1675 ptrdiff_t nbytes_del, nchars_del;
1676 Lisp_Object deletion;
1678 check_markers ();
1680 nchars_del = to - from;
1681 nbytes_del = to_byte - from_byte;
1683 /* Make sure the gap is somewhere in or next to what we are deleting. */
1684 if (from > GPT)
1685 gap_right (from, from_byte);
1686 if (to < GPT)
1687 gap_left (to, to_byte, 0);
1689 #ifdef BYTE_COMBINING_DEBUG
1690 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1691 Z_BYTE - to_byte, from, from_byte))
1692 emacs_abort ();
1693 #endif
1695 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1696 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1697 else
1698 deletion = Qnil;
1700 /* Record marker adjustments, and text deletion into undo
1701 history. */
1702 record_delete (from, deletion, true);
1704 /* Relocate all markers pointing into the new, larger gap to point
1705 at the end of the text before the gap. */
1706 adjust_markers_for_delete (from, from_byte, to, to_byte);
1708 MODIFF++;
1709 CHARS_MODIFF = MODIFF;
1711 /* Relocate point as if it were a marker. */
1712 if (from < PT)
1713 adjust_point (from - (PT < to ? PT : to),
1714 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1716 offset_intervals (current_buffer, from, - nchars_del);
1718 /* Adjust the overlay center as needed. This must be done after
1719 adjusting the markers that bound the overlays. */
1720 adjust_overlays_for_delete (from, nchars_del);
1722 GAP_SIZE += nbytes_del;
1723 ZV_BYTE -= nbytes_del;
1724 Z_BYTE -= nbytes_del;
1725 ZV -= nchars_del;
1726 Z -= nchars_del;
1727 GPT = from;
1728 GPT_BYTE = from_byte;
1729 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1730 /* Put an anchor, unless called from decode_coding_object which
1731 needs to access the previous gap contents. */
1732 *(GPT_ADDR) = 0;
1734 eassert (GPT <= GPT_BYTE);
1736 if (GPT - BEG < BEG_UNCHANGED)
1737 BEG_UNCHANGED = GPT - BEG;
1738 if (Z - GPT < END_UNCHANGED)
1739 END_UNCHANGED = Z - GPT;
1741 check_markers ();
1743 evaporate_overlays (from);
1745 return deletion;
1748 /* Call this if you're about to change the text of current buffer
1749 from character positions START to END. This checks the read-only
1750 properties of the region, calls the necessary modification hooks,
1751 and warns the next redisplay that it should pay attention to that
1752 area. */
1754 void
1755 modify_text (ptrdiff_t start, ptrdiff_t end)
1757 prepare_to_modify_buffer (start, end, NULL);
1759 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1760 if (MODIFF <= SAVE_MODIFF)
1761 record_first_change ();
1762 MODIFF++;
1763 CHARS_MODIFF = MODIFF;
1765 bset_point_before_scroll (current_buffer, Qnil);
1768 /* Signal that we are about to make a change that may result in new
1769 undo information.
1771 static void
1772 run_undoable_change (void)
1774 if (EQ (BVAR (current_buffer, undo_list), Qt))
1775 return;
1777 call0 (Qundo_auto__undoable_change);
1780 /* Check that it is okay to modify the buffer between START and END,
1781 which are char positions.
1783 Run the before-change-function, if any. If intervals are in use,
1784 verify that the text to be modified is not read-only, and call
1785 any modification properties the text may have.
1787 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1788 by holding its value temporarily in a marker.
1790 This function runs Lisp, which means it can GC, which means it can
1791 compact buffers, including the current buffer being worked on here.
1792 So don't you dare calling this function while manipulating the gap,
1793 or during some other similar "critical section". */
1795 void
1796 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1797 ptrdiff_t *preserve_ptr)
1799 struct buffer *base_buffer;
1800 Lisp_Object temp;
1802 XSETFASTINT (temp, start);
1803 if (!NILP (BVAR (current_buffer, read_only)))
1804 Fbarf_if_buffer_read_only (temp);
1806 run_undoable_change();
1808 bset_redisplay (current_buffer);
1810 if (buffer_intervals (current_buffer))
1812 if (preserve_ptr)
1814 Lisp_Object preserve_marker;
1815 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1816 verify_interval_modification (current_buffer, start, end);
1817 *preserve_ptr = marker_position (preserve_marker);
1818 unchain_marker (XMARKER (preserve_marker));
1820 else
1821 verify_interval_modification (current_buffer, start, end);
1824 /* For indirect buffers, use the base buffer to check clashes. */
1825 if (current_buffer->base_buffer != 0)
1826 base_buffer = current_buffer->base_buffer;
1827 else
1828 base_buffer = current_buffer;
1830 if (inhibit_modification_hooks)
1831 return;
1833 if (!NILP (BVAR (base_buffer, file_truename))
1834 /* Make binding buffer-file-name to nil effective. */
1835 && !NILP (BVAR (base_buffer, filename))
1836 && SAVE_MODIFF >= MODIFF)
1837 lock_file (BVAR (base_buffer, file_truename));
1839 /* If `select-active-regions' is non-nil, save the region text. */
1840 /* FIXME: Move this to Elisp (via before-change-functions). */
1841 if (!NILP (BVAR (current_buffer, mark_active))
1842 && XMARKER (BVAR (current_buffer, mark))->buffer
1843 && NILP (Vsaved_region_selection)
1844 && (EQ (Vselect_active_regions, Qonly)
1845 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1846 : (!NILP (Vselect_active_regions)
1847 && !NILP (Vtransient_mark_mode))))
1848 Vsaved_region_selection
1849 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1851 signal_before_change (start, end, preserve_ptr);
1852 Fset (Qdeactivate_mark, Qt);
1855 /* Like above, but called when we know that the buffer text
1856 will be modified and region caches should be invalidated. */
1858 void
1859 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1860 ptrdiff_t *preserve_ptr)
1862 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1863 invalidate_buffer_caches (current_buffer, start, end);
1866 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1867 region between buffer positions START and END. */
1868 void
1869 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1871 /* Indirect buffers usually have their caches set to NULL, but we
1872 need to consider the caches of their base buffer. */
1873 if (buf->base_buffer)
1874 buf = buf->base_buffer;
1875 /* The bidi_paragraph_cache must be invalidated first, because doing
1876 so might need to use the newline_cache (via find_newline_no_quit,
1877 see below). */
1878 if (buf->bidi_paragraph_cache)
1880 if (start != end
1881 && start > BUF_BEG (buf))
1883 /* If we are deleting or replacing characters, we could
1884 create a paragraph start, because all of the characters
1885 from START to the beginning of START's line are
1886 whitespace. Therefore, we must extend the region to be
1887 invalidated up to the newline before START. */
1888 ptrdiff_t line_beg = start;
1889 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
1891 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
1893 struct buffer *old = current_buffer;
1895 set_buffer_internal (buf);
1897 line_beg = find_newline_no_quit (start, start_byte, -1,
1898 &start_byte);
1899 set_buffer_internal (old);
1901 start = line_beg - (line_beg > BUF_BEG (buf));
1903 invalidate_region_cache (buf,
1904 buf->bidi_paragraph_cache,
1905 start - BUF_BEG (buf), BUF_Z (buf) - end);
1907 if (buf->newline_cache)
1908 invalidate_region_cache (buf,
1909 buf->newline_cache,
1910 start - BUF_BEG (buf), BUF_Z (buf) - end);
1911 if (buf->width_run_cache)
1912 invalidate_region_cache (buf,
1913 buf->width_run_cache,
1914 start - BUF_BEG (buf), BUF_Z (buf) - end);
1917 /* These macros work with an argument named `preserve_ptr'
1918 and a local variable named `preserve_marker'. */
1920 #define PRESERVE_VALUE \
1921 if (preserve_ptr && NILP (preserve_marker)) \
1922 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1924 #define RESTORE_VALUE \
1925 if (! NILP (preserve_marker)) \
1927 *preserve_ptr = marker_position (preserve_marker); \
1928 unchain_marker (XMARKER (preserve_marker)); \
1931 #define PRESERVE_START_END \
1932 if (NILP (start_marker)) \
1933 start_marker = Fcopy_marker (start, Qnil); \
1934 if (NILP (end_marker)) \
1935 end_marker = Fcopy_marker (end, Qnil);
1937 #define FETCH_START \
1938 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1940 #define FETCH_END \
1941 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1943 /* Set a variable to nil if an error occurred.
1944 Don't change the variable if there was no error.
1945 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1946 VARIABLE is the variable to maybe set to nil.
1947 NO-ERROR-FLAG is nil if there was an error,
1948 anything else meaning no error (so this function does nothing). */
1949 struct rvoe_arg
1951 Lisp_Object *location;
1952 bool errorp;
1955 static void
1956 reset_var_on_error (void *ptr)
1958 struct rvoe_arg *p = ptr;
1959 if (p->errorp)
1960 *p->location = Qnil;
1963 /* Signal a change to the buffer immediately before it happens.
1964 START_INT and END_INT are the bounds of the text to be changed.
1966 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1967 by holding its value temporarily in a marker. */
1969 static void
1970 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1971 ptrdiff_t *preserve_ptr)
1973 Lisp_Object start, end;
1974 Lisp_Object start_marker, end_marker;
1975 Lisp_Object preserve_marker;
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;
1985 specbind (Qinhibit_modification_hooks, Qt);
1987 /* If buffer is unmodified, run a special hook for that case. The
1988 check for Vfirst_change_hook is just a minor optimization. */
1989 if (SAVE_MODIFF >= MODIFF
1990 && !NILP (Vfirst_change_hook))
1992 PRESERVE_VALUE;
1993 PRESERVE_START_END;
1994 run_hook (Qfirst_change_hook);
1997 /* Now run the before-change-functions if any. */
1998 if (!NILP (Vbefore_change_functions))
2000 rvoe_arg.location = &Vbefore_change_functions;
2001 rvoe_arg.errorp = 1;
2003 PRESERVE_VALUE;
2004 PRESERVE_START_END;
2006 /* Mark before-change-functions to be reset to nil in case of error. */
2007 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2009 /* Actually run the hook functions. */
2010 CALLN (Frun_hook_with_args, Qbefore_change_functions,
2011 FETCH_START, FETCH_END);
2013 /* There was no error: unarm the reset_on_error. */
2014 rvoe_arg.errorp = 0;
2017 if (buffer_has_overlays ())
2019 PRESERVE_VALUE;
2020 report_overlay_modification (FETCH_START, FETCH_END, 0,
2021 FETCH_START, FETCH_END, Qnil);
2024 if (! NILP (start_marker))
2025 free_marker (start_marker);
2026 if (! NILP (end_marker))
2027 free_marker (end_marker);
2028 RESTORE_VALUE;
2030 unbind_to (count, Qnil);
2033 /* Signal a change immediately after it happens.
2034 CHARPOS is the character position of the start of the changed text.
2035 LENDEL is the number of characters of the text before the change.
2036 (Not the whole buffer; just the part that was changed.)
2037 LENINS is the number of characters in that part of the text
2038 after the change. */
2040 void
2041 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2043 ptrdiff_t count = SPECPDL_INDEX ();
2044 struct rvoe_arg rvoe_arg;
2046 if (inhibit_modification_hooks)
2047 return;
2049 /* If we are deferring calls to the after-change functions
2050 and there are no before-change functions,
2051 just record the args that we were going to use. */
2052 if (! NILP (Vcombine_after_change_calls)
2053 && NILP (Vbefore_change_functions)
2054 && !buffer_has_overlays ())
2056 Lisp_Object elt;
2058 if (!NILP (combine_after_change_list)
2059 && current_buffer != XBUFFER (combine_after_change_buffer))
2060 Fcombine_after_change_execute ();
2062 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2063 lenins - lendel);
2064 combine_after_change_list
2065 = Fcons (elt, combine_after_change_list);
2066 combine_after_change_buffer = Fcurrent_buffer ();
2068 return;
2071 if (!NILP (combine_after_change_list))
2072 Fcombine_after_change_execute ();
2074 specbind (Qinhibit_modification_hooks, Qt);
2076 if (!NILP (Vafter_change_functions))
2078 rvoe_arg.location = &Vafter_change_functions;
2079 rvoe_arg.errorp = 1;
2081 /* Mark after-change-functions to be reset to nil in case of error. */
2082 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2084 /* Actually run the hook functions. */
2085 CALLN (Frun_hook_with_args, Qafter_change_functions,
2086 make_number (charpos), make_number (charpos + lenins),
2087 make_number (lendel));
2089 /* There was no error: unarm the reset_on_error. */
2090 rvoe_arg.errorp = 0;
2093 if (buffer_has_overlays ())
2094 report_overlay_modification (make_number (charpos),
2095 make_number (charpos + lenins),
2097 make_number (charpos),
2098 make_number (charpos + lenins),
2099 make_number (lendel));
2101 /* After an insertion, call the text properties
2102 insert-behind-hooks or insert-in-front-hooks. */
2103 if (lendel == 0)
2104 report_interval_modification (make_number (charpos),
2105 make_number (charpos + lenins));
2107 unbind_to (count, Qnil);
2110 static void
2111 Fcombine_after_change_execute_1 (Lisp_Object val)
2113 Vcombine_after_change_calls = val;
2116 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2117 Scombine_after_change_execute, 0, 0, 0,
2118 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2119 (void)
2121 ptrdiff_t count = SPECPDL_INDEX ();
2122 ptrdiff_t beg, end, change;
2123 ptrdiff_t begpos, endpos;
2124 Lisp_Object tail;
2126 if (NILP (combine_after_change_list))
2127 return Qnil;
2129 /* It is rare for combine_after_change_buffer to be invalid, but
2130 possible. It can happen when combine-after-change-calls is
2131 non-nil, and insertion calls a file handler (e.g. through
2132 lock_file) which scribbles into a temp file -- cyd */
2133 if (!BUFFERP (combine_after_change_buffer)
2134 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2136 combine_after_change_list = Qnil;
2137 return Qnil;
2140 record_unwind_current_buffer ();
2142 Fset_buffer (combine_after_change_buffer);
2144 /* # chars unchanged at beginning of buffer. */
2145 beg = Z - BEG;
2146 /* # chars unchanged at end of buffer. */
2147 end = beg;
2148 /* Total amount of insertion (negative for deletion). */
2149 change = 0;
2151 /* Scan the various individual changes,
2152 accumulating the range info in BEG, END and CHANGE. */
2153 for (tail = combine_after_change_list; CONSP (tail);
2154 tail = XCDR (tail))
2156 Lisp_Object elt;
2157 ptrdiff_t thisbeg, thisend, thischange;
2159 /* Extract the info from the next element. */
2160 elt = XCAR (tail);
2161 if (! CONSP (elt))
2162 continue;
2163 thisbeg = XINT (XCAR (elt));
2165 elt = XCDR (elt);
2166 if (! CONSP (elt))
2167 continue;
2168 thisend = XINT (XCAR (elt));
2170 elt = XCDR (elt);
2171 if (! CONSP (elt))
2172 continue;
2173 thischange = XINT (XCAR (elt));
2175 /* Merge this range into the accumulated range. */
2176 change += thischange;
2177 if (thisbeg < beg)
2178 beg = thisbeg;
2179 if (thisend < end)
2180 end = thisend;
2183 /* Get the current start and end positions of the range
2184 that was changed. */
2185 begpos = BEG + beg;
2186 endpos = Z - end;
2188 /* We are about to handle these, so discard them. */
2189 combine_after_change_list = Qnil;
2191 /* Now run the after-change functions for real.
2192 Turn off the flag that defers them. */
2193 record_unwind_protect (Fcombine_after_change_execute_1,
2194 Vcombine_after_change_calls);
2195 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2196 update_compositions (begpos, endpos, CHECK_ALL);
2198 return unbind_to (count, Qnil);
2201 void
2202 syms_of_insdel (void)
2204 staticpro (&combine_after_change_list);
2205 staticpro (&combine_after_change_buffer);
2206 combine_after_change_list = Qnil;
2207 combine_after_change_buffer = Qnil;
2209 DEFSYM (Qundo_auto__undoable_change, "undo-auto--undoable-change");
2211 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2212 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2213 Vcombine_after_change_calls = Qnil;
2215 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2216 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2217 This affects `before-change-functions' and `after-change-functions',
2218 as well as hooks attached to text properties and overlays. */);
2219 inhibit_modification_hooks = 0;
2220 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2222 DEFSYM (Qregion_extract_function, "region-extract-function");
2224 defsubr (&Scombine_after_change_execute);