Add a second argument to project-ignores
[emacs.git] / src / insdel.c
blob22c2bcccb82e476688fa671614d1d03674c6b7b2
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2015 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <intprops.h>
25 #include "lisp.h"
26 #include "intervals.h"
27 #include "character.h"
28 #include "buffer.h"
29 #include "window.h"
30 #include "blockinput.h"
31 #include "region-cache.h"
33 static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35 static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
36 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37 static void gap_right (ptrdiff_t, ptrdiff_t);
39 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
41 was non-nil. We use this to decide how to call them
42 once the deferral ends.
44 In each element.
45 BEG-UNCHANGED is the number of chars before the changed range.
46 END-UNCHANGED is the number of chars after the changed range,
47 and CHANGE-AMOUNT is the number of characters inserted by the change
48 (negative for a deletion). */
49 static Lisp_Object combine_after_change_list;
51 /* Buffer which combine_after_change_list is about. */
52 static Lisp_Object combine_after_change_buffer;
54 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 struct gcpro gcpro1;
896 ptrdiff_t outgoing_nbytes = nbytes;
897 INTERVAL intervals;
899 /* Make OUTGOING_NBYTES describe the text
900 as it will be inserted in this buffer. */
902 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
903 outgoing_nbytes = nchars;
904 else if (! STRING_MULTIBYTE (string))
905 outgoing_nbytes
906 = count_size_as_multibyte (SDATA (string) + pos_byte,
907 nbytes);
909 GCPRO1 (string);
910 /* Do this before moving and increasing the gap,
911 because the before-change hooks might move the gap
912 or make it smaller. */
913 prepare_to_modify_buffer (PT, PT, NULL);
915 if (PT != GPT)
916 move_gap_both (PT, PT_BYTE);
917 if (GAP_SIZE < outgoing_nbytes)
918 make_gap (outgoing_nbytes - GAP_SIZE);
919 UNGCPRO;
921 /* Copy the string text into the buffer, perhaps converting
922 between single-byte and multibyte. */
923 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
924 STRING_MULTIBYTE (string),
925 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
927 #ifdef BYTE_COMBINING_DEBUG
928 /* We have copied text into the gap, but we have not altered
929 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
930 to these functions and get the same results as we would
931 have got earlier on. Meanwhile, PT_ADDR does point to
932 the text that has been stored by copy_text. */
933 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
934 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
935 emacs_abort ();
936 #endif
938 record_insert (PT, nchars);
939 MODIFF++;
940 CHARS_MODIFF = MODIFF;
942 GAP_SIZE -= outgoing_nbytes;
943 GPT += nchars;
944 ZV += nchars;
945 Z += nchars;
946 GPT_BYTE += outgoing_nbytes;
947 ZV_BYTE += outgoing_nbytes;
948 Z_BYTE += outgoing_nbytes;
949 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
951 eassert (GPT <= GPT_BYTE);
953 /* The insert may have been in the unchanged region, so check again. */
954 if (Z - GPT < END_UNCHANGED)
955 END_UNCHANGED = Z - GPT;
957 adjust_overlays_for_insert (PT, nchars);
958 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
959 PT_BYTE + outgoing_nbytes,
960 before_markers);
962 offset_intervals (current_buffer, PT, nchars);
964 intervals = string_intervals (string);
965 /* Get the intervals for the part of the string we are inserting. */
966 if (nbytes < SBYTES (string))
967 intervals = copy_intervals (intervals, pos, nchars);
969 /* Insert those intervals. */
970 graft_intervals_into_buffer (intervals, PT, nchars,
971 current_buffer, inherit);
973 adjust_point (nchars, outgoing_nbytes);
975 check_markers ();
978 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
979 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
980 GPT_ADDR (if not text_at_gap_tail). */
982 void
983 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
985 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
987 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
988 nchars = nbytes;
990 /* No need to call prepare_to_modify_buffer, since this is called
991 from places that replace some region with a different text, so
992 prepare_to_modify_buffer was already called by the deletion part
993 of this dance. */
994 invalidate_buffer_caches (current_buffer, GPT, GPT);
995 record_insert (GPT, nchars);
996 MODIFF++;
998 GAP_SIZE -= nbytes;
999 if (! text_at_gap_tail)
1001 GPT += nchars;
1002 GPT_BYTE += nbytes;
1004 ZV += nchars;
1005 Z += nchars;
1006 ZV_BYTE += nbytes;
1007 Z_BYTE += nbytes;
1008 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1010 eassert (GPT <= GPT_BYTE);
1012 adjust_overlays_for_insert (ins_charpos, nchars);
1013 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1014 ins_charpos + nchars, ins_bytepos + nbytes, 0);
1016 if (buffer_intervals (current_buffer))
1018 offset_intervals (current_buffer, ins_charpos, nchars);
1019 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1020 current_buffer, 0);
1023 if (ins_charpos < PT)
1024 adjust_point (nchars, nbytes);
1026 check_markers ();
1029 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1030 current buffer. If the text in BUF has properties, they are absorbed
1031 into the current buffer.
1033 It does not work to use `insert' for this, because a malloc could happen
1034 and relocate BUF's text before the copy happens. */
1036 void
1037 insert_from_buffer (struct buffer *buf,
1038 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1040 ptrdiff_t opoint = PT;
1042 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1043 signal_after_change (opoint, 0, PT - opoint);
1044 update_compositions (opoint, PT, CHECK_BORDER);
1047 static void
1048 insert_from_buffer_1 (struct buffer *buf,
1049 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1051 ptrdiff_t chunk, chunk_expanded;
1052 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1053 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1054 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1055 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1056 INTERVAL intervals;
1058 if (nchars == 0)
1059 return;
1061 /* Make OUTGOING_NBYTES describe the text
1062 as it will be inserted in this buffer. */
1064 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1065 outgoing_nbytes = nchars;
1066 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1068 ptrdiff_t outgoing_before_gap = 0;
1069 ptrdiff_t outgoing_after_gap = 0;
1071 if (from < BUF_GPT (buf))
1073 chunk = BUF_GPT_BYTE (buf) - from_byte;
1074 if (chunk > incoming_nbytes)
1075 chunk = incoming_nbytes;
1076 outgoing_before_gap
1077 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1078 chunk);
1080 else
1081 chunk = 0;
1083 if (chunk < incoming_nbytes)
1084 outgoing_after_gap
1085 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1086 from_byte + chunk),
1087 incoming_nbytes - chunk);
1089 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1092 /* Do this before moving and increasing the gap,
1093 because the before-change hooks might move the gap
1094 or make it smaller. */
1095 prepare_to_modify_buffer (PT, PT, NULL);
1097 if (PT != GPT)
1098 move_gap_both (PT, PT_BYTE);
1099 if (GAP_SIZE < outgoing_nbytes)
1100 make_gap (outgoing_nbytes - GAP_SIZE);
1102 if (from < BUF_GPT (buf))
1104 chunk = BUF_GPT_BYTE (buf) - from_byte;
1105 if (chunk > incoming_nbytes)
1106 chunk = incoming_nbytes;
1107 /* Record number of output bytes, so we know where
1108 to put the output from the second copy_text. */
1109 chunk_expanded
1110 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1111 GPT_ADDR, chunk,
1112 ! NILP (BVAR (buf, enable_multibyte_characters)),
1113 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1115 else
1116 chunk_expanded = chunk = 0;
1118 if (chunk < incoming_nbytes)
1119 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1120 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1121 ! NILP (BVAR (buf, enable_multibyte_characters)),
1122 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1124 #ifdef BYTE_COMBINING_DEBUG
1125 /* We have copied text into the gap, but we have not altered
1126 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1127 to these functions and get the same results as we would
1128 have got earlier on. Meanwhile, GPT_ADDR does point to
1129 the text that has been stored by copy_text. */
1130 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1131 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1132 emacs_abort ();
1133 #endif
1135 record_insert (PT, nchars);
1136 MODIFF++;
1137 CHARS_MODIFF = MODIFF;
1139 GAP_SIZE -= outgoing_nbytes;
1140 GPT += nchars;
1141 ZV += nchars;
1142 Z += nchars;
1143 GPT_BYTE += outgoing_nbytes;
1144 ZV_BYTE += outgoing_nbytes;
1145 Z_BYTE += outgoing_nbytes;
1146 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1148 eassert (GPT <= GPT_BYTE);
1150 /* The insert may have been in the unchanged region, so check again. */
1151 if (Z - GPT < END_UNCHANGED)
1152 END_UNCHANGED = Z - GPT;
1154 adjust_overlays_for_insert (PT, nchars);
1155 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1156 PT_BYTE + outgoing_nbytes,
1159 offset_intervals (current_buffer, PT, nchars);
1161 /* Get the intervals for the part of the string we are inserting. */
1162 intervals = buffer_intervals (buf);
1163 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1165 if (buf == current_buffer && PT <= from)
1166 from += nchars;
1167 intervals = copy_intervals (intervals, from, nchars);
1170 /* Insert those intervals. */
1171 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1173 adjust_point (nchars, outgoing_nbytes);
1176 /* Record undo information and adjust markers and position keepers for
1177 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1178 chars (LEN_BYTE bytes) which resides in the gap just after
1179 GPT_ADDR.
1181 PREV_TEXT nil means the new text was just inserted. */
1183 static void
1184 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1185 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1187 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1189 #ifdef BYTE_COMBINING_DEBUG
1190 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1191 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1192 emacs_abort ();
1193 #endif
1195 if (STRINGP (prev_text))
1197 nchars_del = SCHARS (prev_text);
1198 nbytes_del = SBYTES (prev_text);
1201 /* Update various buffer positions for the new text. */
1202 GAP_SIZE -= len_byte;
1203 ZV += len; Z += len;
1204 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1205 GPT += len; GPT_BYTE += len_byte;
1206 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1208 if (nchars_del > 0)
1209 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1210 len, len_byte);
1211 else
1212 adjust_markers_for_insert (from, from_byte,
1213 from + len, from_byte + len_byte, 0);
1215 if (nchars_del > 0)
1216 record_delete (from, prev_text, false);
1217 record_insert (from, len);
1219 if (len > nchars_del)
1220 adjust_overlays_for_insert (from, len - nchars_del);
1221 else if (len < nchars_del)
1222 adjust_overlays_for_delete (from, nchars_del - len);
1224 offset_intervals (current_buffer, from, len - nchars_del);
1226 if (from < PT)
1227 adjust_point (len - nchars_del, len_byte - nbytes_del);
1229 /* As byte combining will decrease Z, we must check this again. */
1230 if (Z - GPT < END_UNCHANGED)
1231 END_UNCHANGED = Z - GPT;
1233 check_markers ();
1235 if (len == 0)
1236 evaporate_overlays (from);
1237 MODIFF++;
1238 CHARS_MODIFF = MODIFF;
1241 /* Record undo information, adjust markers and position keepers for an
1242 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1243 text already exists in the current buffer but character length (TO
1244 - FROM) may be incorrect, the correct length is NEWLEN. */
1246 void
1247 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1248 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1250 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1252 if (GPT != to)
1253 move_gap_both (to, to_byte);
1254 GAP_SIZE += len_byte;
1255 GPT -= len; GPT_BYTE -= len_byte;
1256 ZV -= len; ZV_BYTE -= len_byte;
1257 Z -= len; Z_BYTE -= len_byte;
1258 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1261 /* Replace the text from character positions FROM to TO with NEW,
1262 If PREPARE, call prepare_to_modify_buffer.
1263 If INHERIT, the newly inserted text should inherit text properties
1264 from the surrounding non-deleted text. */
1266 /* Note that this does not yet handle markers quite right.
1267 Also it needs to record a single undo-entry that does a replacement
1268 rather than a separate delete and insert.
1269 That way, undo will also handle markers properly.
1271 But if MARKERS is 0, don't relocate markers. */
1273 void
1274 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1275 bool prepare, bool inherit, bool markers)
1277 ptrdiff_t inschars = SCHARS (new);
1278 ptrdiff_t insbytes = SBYTES (new);
1279 ptrdiff_t from_byte, to_byte;
1280 ptrdiff_t nbytes_del, nchars_del;
1281 struct gcpro gcpro1;
1282 INTERVAL intervals;
1283 ptrdiff_t outgoing_insbytes = insbytes;
1284 Lisp_Object deletion;
1286 check_markers ();
1288 GCPRO1 (new);
1289 deletion = Qnil;
1291 if (prepare)
1293 ptrdiff_t range_length = to - from;
1294 prepare_to_modify_buffer (from, to, &from);
1295 to = from + range_length;
1298 UNGCPRO;
1300 /* Make args be valid. */
1301 if (from < BEGV)
1302 from = BEGV;
1303 if (to > ZV)
1304 to = ZV;
1306 from_byte = CHAR_TO_BYTE (from);
1307 to_byte = CHAR_TO_BYTE (to);
1309 nchars_del = to - from;
1310 nbytes_del = to_byte - from_byte;
1312 if (nbytes_del <= 0 && insbytes == 0)
1313 return;
1315 /* Make OUTGOING_INSBYTES describe the text
1316 as it will be inserted in this buffer. */
1318 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1319 outgoing_insbytes = inschars;
1320 else if (! STRING_MULTIBYTE (new))
1321 outgoing_insbytes
1322 = count_size_as_multibyte (SDATA (new), insbytes);
1324 GCPRO1 (new);
1326 /* Make sure the gap is somewhere in or next to what we are deleting. */
1327 if (from > GPT)
1328 gap_right (from, from_byte);
1329 if (to < GPT)
1330 gap_left (to, to_byte, 0);
1332 /* Even if we don't record for undo, we must keep the original text
1333 because we may have to recover it because of inappropriate byte
1334 combining. */
1335 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1336 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1338 GAP_SIZE += nbytes_del;
1339 ZV -= nchars_del;
1340 Z -= nchars_del;
1341 ZV_BYTE -= nbytes_del;
1342 Z_BYTE -= nbytes_del;
1343 GPT = from;
1344 GPT_BYTE = from_byte;
1345 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1347 eassert (GPT <= GPT_BYTE);
1349 if (GPT - BEG < BEG_UNCHANGED)
1350 BEG_UNCHANGED = GPT - BEG;
1351 if (Z - GPT < END_UNCHANGED)
1352 END_UNCHANGED = Z - GPT;
1354 if (GAP_SIZE < outgoing_insbytes)
1355 make_gap (outgoing_insbytes - GAP_SIZE);
1357 /* Copy the string text into the buffer, perhaps converting
1358 between single-byte and multibyte. */
1359 copy_text (SDATA (new), GPT_ADDR, insbytes,
1360 STRING_MULTIBYTE (new),
1361 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1363 #ifdef BYTE_COMBINING_DEBUG
1364 /* We have copied text into the gap, but we have not marked
1365 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1366 here, for both the previous text and the following text.
1367 Meanwhile, GPT_ADDR does point to
1368 the text that has been stored by copy_text. */
1369 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1370 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1371 emacs_abort ();
1372 #endif
1374 /* Record the insertion first, so that when we undo,
1375 the deletion will be undone first. Thus, undo
1376 will insert before deleting, and thus will keep
1377 the markers before and after this text separate. */
1378 if (!NILP (deletion))
1380 record_insert (from + SCHARS (deletion), inschars);
1381 record_delete (from, deletion, false);
1384 GAP_SIZE -= outgoing_insbytes;
1385 GPT += inschars;
1386 ZV += inschars;
1387 Z += inschars;
1388 GPT_BYTE += outgoing_insbytes;
1389 ZV_BYTE += outgoing_insbytes;
1390 Z_BYTE += outgoing_insbytes;
1391 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1393 eassert (GPT <= GPT_BYTE);
1395 /* Adjust markers for the deletion and the insertion. */
1396 if (markers)
1397 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1398 inschars, outgoing_insbytes);
1400 /* Adjust the overlay center as needed. This must be done after
1401 adjusting the markers that bound the overlays. */
1402 adjust_overlays_for_delete (from, nchars_del);
1403 adjust_overlays_for_insert (from, inschars);
1405 offset_intervals (current_buffer, from, inschars - nchars_del);
1407 /* Get the intervals for the part of the string we are inserting--
1408 not including the combined-before bytes. */
1409 intervals = string_intervals (new);
1410 /* Insert those intervals. */
1411 graft_intervals_into_buffer (intervals, from, inschars,
1412 current_buffer, inherit);
1414 /* Relocate point as if it were a marker. */
1415 if (from < PT)
1416 adjust_point ((from + inschars - (PT < to ? PT : to)),
1417 (from_byte + outgoing_insbytes
1418 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1420 if (outgoing_insbytes == 0)
1421 evaporate_overlays (from);
1423 check_markers ();
1425 MODIFF++;
1426 CHARS_MODIFF = MODIFF;
1427 UNGCPRO;
1429 signal_after_change (from, nchars_del, GPT - from);
1430 update_compositions (from, GPT, CHECK_BORDER);
1433 /* Replace the text from character positions FROM to TO with
1434 the text in INS of length INSCHARS.
1435 Keep the text properties that applied to the old characters
1436 (extending them to all the new chars if there are more new chars).
1438 Note that this does not yet handle markers quite right.
1440 If MARKERS, relocate markers.
1442 Unlike most functions at this level, never call
1443 prepare_to_modify_buffer and never call signal_after_change. */
1445 void
1446 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1447 ptrdiff_t to, ptrdiff_t to_byte,
1448 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1449 bool markers)
1451 ptrdiff_t nbytes_del, nchars_del;
1453 check_markers ();
1455 nchars_del = to - from;
1456 nbytes_del = to_byte - from_byte;
1458 if (nbytes_del <= 0 && insbytes == 0)
1459 return;
1461 /* Make sure the gap is somewhere in or next to what we are deleting. */
1462 if (from > GPT)
1463 gap_right (from, from_byte);
1464 if (to < GPT)
1465 gap_left (to, to_byte, 0);
1467 GAP_SIZE += nbytes_del;
1468 ZV -= nchars_del;
1469 Z -= nchars_del;
1470 ZV_BYTE -= nbytes_del;
1471 Z_BYTE -= nbytes_del;
1472 GPT = from;
1473 GPT_BYTE = from_byte;
1474 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1476 eassert (GPT <= GPT_BYTE);
1478 if (GPT - BEG < BEG_UNCHANGED)
1479 BEG_UNCHANGED = GPT - BEG;
1480 if (Z - GPT < END_UNCHANGED)
1481 END_UNCHANGED = Z - GPT;
1483 if (GAP_SIZE < insbytes)
1484 make_gap (insbytes - GAP_SIZE);
1486 /* Copy the replacement text into the buffer. */
1487 memcpy (GPT_ADDR, ins, insbytes);
1489 #ifdef BYTE_COMBINING_DEBUG
1490 /* We have copied text into the gap, but we have not marked
1491 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1492 here, for both the previous text and the following text.
1493 Meanwhile, GPT_ADDR does point to
1494 the text that has been stored by copy_text. */
1495 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1496 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1497 emacs_abort ();
1498 #endif
1500 GAP_SIZE -= insbytes;
1501 GPT += inschars;
1502 ZV += inschars;
1503 Z += inschars;
1504 GPT_BYTE += insbytes;
1505 ZV_BYTE += insbytes;
1506 Z_BYTE += insbytes;
1507 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1509 eassert (GPT <= GPT_BYTE);
1511 /* Adjust markers for the deletion and the insertion. */
1512 if (markers
1513 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1514 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1515 inschars, insbytes);
1517 /* Adjust the overlay center as needed. This must be done after
1518 adjusting the markers that bound the overlays. */
1519 if (nchars_del != inschars)
1521 adjust_overlays_for_insert (from, inschars);
1522 adjust_overlays_for_delete (from + inschars, nchars_del);
1525 offset_intervals (current_buffer, from, inschars - nchars_del);
1527 /* Relocate point as if it were a marker. */
1528 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1530 if (PT < to)
1531 /* PT was within the deleted text. Move it to FROM. */
1532 adjust_point (from - PT, from_byte - PT_BYTE);
1533 else
1534 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1537 if (insbytes == 0)
1538 evaporate_overlays (from);
1540 check_markers ();
1542 MODIFF++;
1543 CHARS_MODIFF = MODIFF;
1546 /* Delete characters in current buffer
1547 from FROM up to (but not including) TO.
1548 If TO comes before FROM, we delete nothing. */
1550 void
1551 del_range (ptrdiff_t from, ptrdiff_t to)
1553 del_range_1 (from, to, 1, 0);
1556 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1557 RET_STRING says to return the deleted text. */
1559 Lisp_Object
1560 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1562 ptrdiff_t from_byte, to_byte;
1563 Lisp_Object deletion;
1564 struct gcpro gcpro1;
1566 /* Make args be valid */
1567 if (from < BEGV)
1568 from = BEGV;
1569 if (to > ZV)
1570 to = ZV;
1572 if (to <= from)
1573 return Qnil;
1575 if (prepare)
1577 ptrdiff_t range_length = to - from;
1578 prepare_to_modify_buffer (from, to, &from);
1579 to = min (ZV, from + range_length);
1582 from_byte = CHAR_TO_BYTE (from);
1583 to_byte = CHAR_TO_BYTE (to);
1585 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1586 GCPRO1 (deletion);
1587 signal_after_change (from, to - from, 0);
1588 update_compositions (from, from, CHECK_HEAD);
1589 UNGCPRO;
1590 return deletion;
1593 /* Like del_range_1 but args are byte positions, not char positions. */
1595 void
1596 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
1598 ptrdiff_t from, to;
1600 /* Make args be valid. */
1601 if (from_byte < BEGV_BYTE)
1602 from_byte = BEGV_BYTE;
1603 if (to_byte > ZV_BYTE)
1604 to_byte = ZV_BYTE;
1606 if (to_byte <= from_byte)
1607 return;
1609 from = BYTE_TO_CHAR (from_byte);
1610 to = BYTE_TO_CHAR (to_byte);
1612 if (prepare)
1614 ptrdiff_t old_from = from, old_to = Z - to;
1615 ptrdiff_t range_length = to - from;
1616 prepare_to_modify_buffer (from, to, &from);
1617 to = from + range_length;
1619 if (old_from != from)
1620 from_byte = CHAR_TO_BYTE (from);
1621 if (to > ZV)
1623 to = ZV;
1624 to_byte = ZV_BYTE;
1626 else if (old_to == Z - to)
1627 to_byte = CHAR_TO_BYTE (to);
1630 del_range_2 (from, from_byte, to, to_byte, 0);
1631 signal_after_change (from, to - from, 0);
1632 update_compositions (from, from, CHECK_HEAD);
1635 /* Like del_range_1, but positions are specified both as charpos
1636 and bytepos. */
1638 void
1639 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1640 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1642 /* Make args be valid */
1643 if (from_byte < BEGV_BYTE)
1644 from_byte = BEGV_BYTE;
1645 if (to_byte > ZV_BYTE)
1646 to_byte = ZV_BYTE;
1648 if (to_byte <= from_byte)
1649 return;
1651 if (from < BEGV)
1652 from = BEGV;
1653 if (to > ZV)
1654 to = ZV;
1656 if (prepare)
1658 ptrdiff_t old_from = from, old_to = Z - to;
1659 ptrdiff_t range_length = to - from;
1660 prepare_to_modify_buffer (from, to, &from);
1661 to = from + range_length;
1663 if (old_from != from)
1664 from_byte = CHAR_TO_BYTE (from);
1665 if (to > ZV)
1667 to = ZV;
1668 to_byte = ZV_BYTE;
1670 else if (old_to == Z - to)
1671 to_byte = CHAR_TO_BYTE (to);
1674 del_range_2 (from, from_byte, to, to_byte, 0);
1675 signal_after_change (from, to - from, 0);
1676 update_compositions (from, from, CHECK_HEAD);
1679 /* Delete a range of text, specified both as character positions
1680 and byte positions. FROM and TO are character positions,
1681 while FROM_BYTE and TO_BYTE are byte positions.
1682 If RET_STRING, the deleted area is returned as a string. */
1684 Lisp_Object
1685 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1686 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1688 ptrdiff_t nbytes_del, nchars_del;
1689 Lisp_Object deletion;
1691 check_markers ();
1693 nchars_del = to - from;
1694 nbytes_del = to_byte - from_byte;
1696 /* Make sure the gap is somewhere in or next to what we are deleting. */
1697 if (from > GPT)
1698 gap_right (from, from_byte);
1699 if (to < GPT)
1700 gap_left (to, to_byte, 0);
1702 #ifdef BYTE_COMBINING_DEBUG
1703 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1704 Z_BYTE - to_byte, from, from_byte))
1705 emacs_abort ();
1706 #endif
1708 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1709 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1710 else
1711 deletion = Qnil;
1713 /* Record marker adjustments, and text deletion into undo
1714 history. */
1715 record_delete (from, deletion, true);
1717 /* Relocate all markers pointing into the new, larger gap to point
1718 at the end of the text before the gap. */
1719 adjust_markers_for_delete (from, from_byte, to, to_byte);
1721 MODIFF++;
1722 CHARS_MODIFF = MODIFF;
1724 /* Relocate point as if it were a marker. */
1725 if (from < PT)
1726 adjust_point (from - (PT < to ? PT : to),
1727 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1729 offset_intervals (current_buffer, from, - nchars_del);
1731 /* Adjust the overlay center as needed. This must be done after
1732 adjusting the markers that bound the overlays. */
1733 adjust_overlays_for_delete (from, nchars_del);
1735 GAP_SIZE += nbytes_del;
1736 ZV_BYTE -= nbytes_del;
1737 Z_BYTE -= nbytes_del;
1738 ZV -= nchars_del;
1739 Z -= nchars_del;
1740 GPT = from;
1741 GPT_BYTE = from_byte;
1742 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1743 /* Put an anchor, unless called from decode_coding_object which
1744 needs to access the previous gap contents. */
1745 *(GPT_ADDR) = 0;
1747 eassert (GPT <= GPT_BYTE);
1749 if (GPT - BEG < BEG_UNCHANGED)
1750 BEG_UNCHANGED = GPT - BEG;
1751 if (Z - GPT < END_UNCHANGED)
1752 END_UNCHANGED = Z - GPT;
1754 check_markers ();
1756 evaporate_overlays (from);
1758 return deletion;
1761 /* Call this if you're about to change the text of current buffer
1762 from character positions START to END. This checks the read-only
1763 properties of the region, calls the necessary modification hooks,
1764 and warns the next redisplay that it should pay attention to that
1765 area. */
1767 void
1768 modify_text (ptrdiff_t start, ptrdiff_t end)
1770 prepare_to_modify_buffer (start, end, NULL);
1772 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1773 if (MODIFF <= SAVE_MODIFF)
1774 record_first_change ();
1775 MODIFF++;
1776 CHARS_MODIFF = MODIFF;
1778 bset_point_before_scroll (current_buffer, Qnil);
1781 /* Check that it is okay to modify the buffer between START and END,
1782 which are char positions.
1784 Run the before-change-function, if any. If intervals are in use,
1785 verify that the text to be modified is not read-only, and call
1786 any modification properties the text may have.
1788 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1789 by holding its value temporarily in a marker. */
1791 void
1792 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1793 ptrdiff_t *preserve_ptr)
1795 struct buffer *base_buffer;
1796 Lisp_Object temp;
1798 XSETFASTINT (temp, start);
1799 if (!NILP (BVAR (current_buffer, read_only)))
1800 Fbarf_if_buffer_read_only (temp);
1802 bset_redisplay (current_buffer);
1804 if (buffer_intervals (current_buffer))
1806 if (preserve_ptr)
1808 Lisp_Object preserve_marker;
1809 struct gcpro gcpro1;
1810 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1811 GCPRO1 (preserve_marker);
1812 verify_interval_modification (current_buffer, start, end);
1813 *preserve_ptr = marker_position (preserve_marker);
1814 unchain_marker (XMARKER (preserve_marker));
1815 UNGCPRO;
1817 else
1818 verify_interval_modification (current_buffer, start, end);
1821 /* For indirect buffers, use the base buffer to check clashes. */
1822 if (current_buffer->base_buffer != 0)
1823 base_buffer = current_buffer->base_buffer;
1824 else
1825 base_buffer = current_buffer;
1827 if (inhibit_modification_hooks)
1828 return;
1830 if (!NILP (BVAR (base_buffer, file_truename))
1831 /* Make binding buffer-file-name to nil effective. */
1832 && !NILP (BVAR (base_buffer, filename))
1833 && SAVE_MODIFF >= MODIFF)
1834 lock_file (BVAR (base_buffer, file_truename));
1836 /* If `select-active-regions' is non-nil, save the region text. */
1837 /* FIXME: Move this to Elisp (via before-change-functions). */
1838 if (!NILP (BVAR (current_buffer, mark_active))
1839 && XMARKER (BVAR (current_buffer, mark))->buffer
1840 && NILP (Vsaved_region_selection)
1841 && (EQ (Vselect_active_regions, Qonly)
1842 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1843 : (!NILP (Vselect_active_regions)
1844 && !NILP (Vtransient_mark_mode))))
1845 Vsaved_region_selection
1846 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1848 signal_before_change (start, end, preserve_ptr);
1849 Fset (Qdeactivate_mark, Qt);
1852 /* Like above, but called when we know that the buffer text
1853 will be modified and region caches should be invalidated. */
1855 void
1856 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1857 ptrdiff_t *preserve_ptr)
1859 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1860 invalidate_buffer_caches (current_buffer, start, end);
1863 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1864 region between buffer positions START and END. */
1865 void
1866 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1868 /* Indirect buffers usually have their caches set to NULL, but we
1869 need to consider the caches of their base buffer. */
1870 if (buf->base_buffer)
1871 buf = buf->base_buffer;
1872 /* The bidi_paragraph_cache must be invalidated first, because doing
1873 so might need to use the newline_cache (via find_newline_no_quit,
1874 see below). */
1875 if (buf->bidi_paragraph_cache)
1877 if (start != end
1878 && start > BUF_BEG (buf))
1880 /* If we are deleting or replacing characters, we could
1881 create a paragraph start, because all of the characters
1882 from START to the beginning of START's line are
1883 whitespace. Therefore, we must extend the region to be
1884 invalidated up to the newline before START. */
1885 ptrdiff_t line_beg = start;
1886 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
1888 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
1890 struct buffer *old = current_buffer;
1892 set_buffer_internal (buf);
1894 line_beg = find_newline_no_quit (start, start_byte, -1,
1895 &start_byte);
1896 set_buffer_internal (old);
1898 start = line_beg - (line_beg > BUF_BEG (buf));
1900 invalidate_region_cache (buf,
1901 buf->bidi_paragraph_cache,
1902 start - BUF_BEG (buf), BUF_Z (buf) - end);
1904 if (buf->newline_cache)
1905 invalidate_region_cache (buf,
1906 buf->newline_cache,
1907 start - BUF_BEG (buf), BUF_Z (buf) - end);
1908 if (buf->width_run_cache)
1909 invalidate_region_cache (buf,
1910 buf->width_run_cache,
1911 start - BUF_BEG (buf), BUF_Z (buf) - end);
1914 /* These macros work with an argument named `preserve_ptr'
1915 and a local variable named `preserve_marker'. */
1917 #define PRESERVE_VALUE \
1918 if (preserve_ptr && NILP (preserve_marker)) \
1919 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1921 #define RESTORE_VALUE \
1922 if (! NILP (preserve_marker)) \
1924 *preserve_ptr = marker_position (preserve_marker); \
1925 unchain_marker (XMARKER (preserve_marker)); \
1928 #define PRESERVE_START_END \
1929 if (NILP (start_marker)) \
1930 start_marker = Fcopy_marker (start, Qnil); \
1931 if (NILP (end_marker)) \
1932 end_marker = Fcopy_marker (end, Qnil);
1934 #define FETCH_START \
1935 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1937 #define FETCH_END \
1938 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1940 /* Set a variable to nil if an error occurred.
1941 Don't change the variable if there was no error.
1942 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1943 VARIABLE is the variable to maybe set to nil.
1944 NO-ERROR-FLAG is nil if there was an error,
1945 anything else meaning no error (so this function does nothing). */
1946 struct rvoe_arg
1948 Lisp_Object *location;
1949 bool errorp;
1952 static void
1953 reset_var_on_error (void *ptr)
1955 struct rvoe_arg *p = ptr;
1956 if (p->errorp)
1957 *p->location = Qnil;
1960 /* Signal a change to the buffer immediately before it happens.
1961 START_INT and END_INT are the bounds of the text to be changed.
1963 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1964 by holding its value temporarily in a marker. */
1966 static void
1967 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1968 ptrdiff_t *preserve_ptr)
1970 Lisp_Object start, end;
1971 Lisp_Object start_marker, end_marker;
1972 Lisp_Object preserve_marker;
1973 struct gcpro gcpro1, gcpro2, gcpro3;
1974 ptrdiff_t count = SPECPDL_INDEX ();
1975 struct rvoe_arg rvoe_arg;
1977 start = make_number (start_int);
1978 end = make_number (end_int);
1979 preserve_marker = Qnil;
1980 start_marker = Qnil;
1981 end_marker = Qnil;
1982 GCPRO3 (preserve_marker, start_marker, end_marker);
1984 specbind (Qinhibit_modification_hooks, Qt);
1986 /* If buffer is unmodified, run a special hook for that case. The
1987 check for Vfirst_change_hook is just a minor optimization. */
1988 if (SAVE_MODIFF >= MODIFF
1989 && !NILP (Vfirst_change_hook))
1991 PRESERVE_VALUE;
1992 PRESERVE_START_END;
1993 run_hook (Qfirst_change_hook);
1996 /* Now run the before-change-functions if any. */
1997 if (!NILP (Vbefore_change_functions))
1999 rvoe_arg.location = &Vbefore_change_functions;
2000 rvoe_arg.errorp = 1;
2002 PRESERVE_VALUE;
2003 PRESERVE_START_END;
2005 /* Mark before-change-functions to be reset to nil in case of error. */
2006 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2008 /* Actually run the hook functions. */
2009 CALLN (Frun_hook_with_args, Qbefore_change_functions,
2010 FETCH_START, FETCH_END);
2012 /* There was no error: unarm the reset_on_error. */
2013 rvoe_arg.errorp = 0;
2016 if (buffer_has_overlays ())
2018 PRESERVE_VALUE;
2019 report_overlay_modification (FETCH_START, FETCH_END, 0,
2020 FETCH_START, FETCH_END, Qnil);
2023 if (! NILP (start_marker))
2024 free_marker (start_marker);
2025 if (! NILP (end_marker))
2026 free_marker (end_marker);
2027 RESTORE_VALUE;
2028 UNGCPRO;
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 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2210 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2211 Vcombine_after_change_calls = Qnil;
2213 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2214 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2215 This affects `before-change-functions' and `after-change-functions',
2216 as well as hooks attached to text properties and overlays. */);
2217 inhibit_modification_hooks = 0;
2218 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2220 DEFSYM (Qregion_extract_function, "region-extract-function");
2222 defsubr (&Scombine_after_change_execute);