* src/insdel.c (make_gap): Increase enough to avoid O(N^2) behavior.
[emacs.git] / src / insdel.c
blob8b684fd278039a00c6ff0a0fca8f120849b69651
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2017 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <intprops.h>
25 #include "lisp.h"
26 #include "composite.h"
27 #include "intervals.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "window.h"
31 #include "region-cache.h"
33 static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35 static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
36 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37 static void gap_right (ptrdiff_t, ptrdiff_t);
39 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
41 was non-nil. We use this to decide how to call them
42 once the deferral ends.
44 In each element.
45 BEG-UNCHANGED is the number of chars before the changed range.
46 END-UNCHANGED is the number of chars after the changed range,
47 and CHANGE-AMOUNT is the number of characters inserted by the change
48 (negative for a deletion). */
49 static Lisp_Object combine_after_change_list;
51 /* Buffer which combine_after_change_list is about. */
52 static Lisp_Object combine_after_change_buffer;
54 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
56 /* Also used in marker.c to enable expensive marker checks. */
58 #ifdef MARKER_DEBUG
60 static void
61 check_markers (void)
63 struct Lisp_Marker *tail;
64 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
66 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
68 if (tail->buffer->text != current_buffer->text)
69 emacs_abort ();
70 if (tail->charpos > Z)
71 emacs_abort ();
72 if (tail->bytepos > Z_BYTE)
73 emacs_abort ();
74 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
75 emacs_abort ();
79 #else /* not MARKER_DEBUG */
81 #define check_markers() do { } while (0)
83 #endif /* MARKER_DEBUG */
85 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
86 Note that this can quit! */
88 void
89 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
91 eassert (charpos == BYTE_TO_CHAR (bytepos)
92 && bytepos == CHAR_TO_BYTE (charpos));
93 if (bytepos < GPT_BYTE)
94 gap_left (charpos, bytepos, 0);
95 else if (bytepos > GPT_BYTE)
96 gap_right (charpos, bytepos);
99 /* Move the gap to a position less than the current GPT.
100 BYTEPOS describes the new position as a byte position,
101 and CHARPOS is the corresponding char position.
102 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
104 static void
105 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
107 unsigned char *to, *from;
108 ptrdiff_t i;
109 ptrdiff_t new_s1;
111 if (!newgap)
112 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
114 i = GPT_BYTE;
115 to = GAP_END_ADDR;
116 from = GPT_ADDR;
117 new_s1 = GPT_BYTE;
119 /* Now copy the characters. To move the gap down,
120 copy characters up. */
122 while (1)
124 /* I gets number of characters left to copy. */
125 i = new_s1 - bytepos;
126 if (i == 0)
127 break;
128 /* If a quit is requested, stop copying now.
129 Change BYTEPOS to be where we have actually moved the gap to.
130 Note that this cannot happen when we are called to make the
131 gap larger or smaller, since make_gap_larger and
132 make_gap_smaller set inhibit-quit. */
133 if (QUITP)
135 bytepos = new_s1;
136 charpos = BYTE_TO_CHAR (bytepos);
137 break;
139 /* Move at most 32000 chars before checking again for a quit. */
140 if (i > 32000)
141 i = 32000;
142 new_s1 -= i;
143 from -= i, to -= i;
144 memmove (to, from, i);
147 /* Adjust buffer data structure, to put the gap at BYTEPOS.
148 BYTEPOS is where the loop above stopped, which may be what
149 was specified or may be where a quit was detected. */
150 GPT_BYTE = bytepos;
151 GPT = charpos;
152 eassert (charpos <= bytepos);
153 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
154 maybe_quit ();
157 /* Move the gap to a position greater than the current GPT.
158 BYTEPOS describes the new position as a byte position,
159 and CHARPOS is the corresponding char position. */
161 static void
162 gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
164 register unsigned char *to, *from;
165 register ptrdiff_t i;
166 ptrdiff_t new_s1;
168 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
170 i = GPT_BYTE;
171 from = GAP_END_ADDR;
172 to = GPT_ADDR;
173 new_s1 = GPT_BYTE;
175 /* Now copy the characters. To move the gap up,
176 copy characters down. */
178 while (1)
180 /* I gets number of characters left to copy. */
181 i = bytepos - new_s1;
182 if (i == 0)
183 break;
184 /* If a quit is requested, stop copying now.
185 Change BYTEPOS to be where we have actually moved the gap to.
186 Note that this cannot happen when we are called to make the
187 gap larger or smaller, since make_gap_larger and
188 make_gap_smaller set inhibit-quit. */
189 if (QUITP)
191 bytepos = new_s1;
192 charpos = BYTE_TO_CHAR (bytepos);
193 break;
195 /* Move at most 32000 chars before checking again for a quit. */
196 if (i > 32000)
197 i = 32000;
198 new_s1 += i;
199 memmove (to, from, i);
200 from += i, to += i;
203 GPT = charpos;
204 GPT_BYTE = bytepos;
205 eassert (charpos <= bytepos);
206 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
207 maybe_quit ();
210 /* If the selected window's old pointm is adjacent or covered by the
211 region from FROM to TO, unsuspend auto hscroll in that window. */
213 static void
214 adjust_suspend_auto_hscroll (ptrdiff_t from, ptrdiff_t to)
216 if (WINDOWP (selected_window))
218 struct window *w = XWINDOW (selected_window);
220 if (BUFFERP (w->contents)
221 && XBUFFER (w->contents) == current_buffer
222 && XMARKER (w->old_pointm)->charpos >= from
223 && XMARKER (w->old_pointm)->charpos <= to)
224 w->suspend_auto_hscroll = 0;
229 /* Adjust all markers for a deletion
230 whose range in bytes is FROM_BYTE to TO_BYTE.
231 The range in charpos is FROM to TO.
233 This function assumes that the gap is adjacent to
234 or inside of the range being deleted. */
236 void
237 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
238 ptrdiff_t to, ptrdiff_t to_byte)
240 struct Lisp_Marker *m;
241 ptrdiff_t charpos;
243 adjust_suspend_auto_hscroll (from, to);
244 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
246 charpos = m->charpos;
247 eassert (charpos <= Z);
249 /* If the marker is after the deletion,
250 relocate by number of chars / bytes deleted. */
251 if (charpos > to)
253 m->charpos -= to - from;
254 m->bytepos -= to_byte - from_byte;
256 /* Here's the case where a marker is inside text being deleted. */
257 else if (charpos > from)
259 m->charpos = from;
260 m->bytepos = from_byte;
266 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
267 to TO / TO_BYTE. We have to relocate the charpos of every marker
268 that points after the insertion (but not their bytepos).
270 When a marker points at the insertion point,
271 we advance it if either its insertion-type is t
272 or BEFORE_MARKERS is true. */
274 static void
275 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
276 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
278 struct Lisp_Marker *m;
279 bool adjusted = 0;
280 ptrdiff_t nchars = to - from;
281 ptrdiff_t nbytes = to_byte - from_byte;
283 adjust_suspend_auto_hscroll (from, to);
284 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
286 eassert (m->bytepos >= m->charpos
287 && m->bytepos - m->charpos <= Z_BYTE - Z);
289 if (m->bytepos == from_byte)
291 if (m->insertion_type || before_markers)
293 m->bytepos = to_byte;
294 m->charpos = to;
295 if (m->insertion_type)
296 adjusted = 1;
299 else if (m->bytepos > from_byte)
301 m->bytepos += nbytes;
302 m->charpos += nchars;
306 /* Adjusting only markers whose insertion-type is t may result in
307 - disordered start and end in overlays, and
308 - disordered overlays in the slot `overlays_before' of current_buffer. */
309 if (adjusted)
311 fix_start_end_in_overlays (from, to);
312 fix_overlays_before (current_buffer, from, to);
316 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
318 This is used only when the value of point changes due to an insert
319 or delete; it does not represent a conceptual change in point as a
320 marker. In particular, point is not crossing any interval
321 boundaries, so there's no need to use the usual SET_PT macro. In
322 fact it would be incorrect to do so, because either the old or the
323 new value of point is out of sync with the current set of
324 intervals. */
326 static void
327 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
329 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
330 /* In a single-byte buffer, the two positions must be equal. */
331 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
334 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
335 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
336 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
337 an insertion. */
339 static void
340 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
341 ptrdiff_t old_chars, ptrdiff_t old_bytes,
342 ptrdiff_t new_chars, ptrdiff_t new_bytes)
344 register struct Lisp_Marker *m;
345 ptrdiff_t prev_to_byte = from_byte + old_bytes;
346 ptrdiff_t diff_chars = new_chars - old_chars;
347 ptrdiff_t diff_bytes = new_bytes - old_bytes;
349 adjust_suspend_auto_hscroll (from, from + old_chars);
350 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
352 if (m->bytepos >= prev_to_byte)
354 m->charpos += diff_chars;
355 m->bytepos += diff_bytes;
357 else if (m->bytepos > from_byte)
359 m->charpos = from;
360 m->bytepos = from_byte;
364 check_markers ();
367 /* Starting at POS (BYTEPOS), find the byte position corresponding to
368 ENDPOS, which could be either before or after POS. */
369 static ptrdiff_t
370 count_bytes (ptrdiff_t pos, ptrdiff_t bytepos, ptrdiff_t endpos)
372 eassert (BEG_BYTE <= bytepos && bytepos <= Z_BYTE
373 && BEG <= endpos && endpos <= Z);
375 if (pos <= endpos)
376 for ( ; pos < endpos; pos++)
377 INC_POS (bytepos);
378 else
379 for ( ; pos > endpos; pos--)
380 DEC_POS (bytepos);
382 return bytepos;
385 /* Adjust byte positions of markers when their character positions
386 didn't change. This is used in several places that replace text,
387 but keep the character positions of the markers unchanged -- the
388 byte positions could still change due to different numbers of bytes
389 in the new text.
391 FROM (FROM_BYTE) and TO (TO_BYTE) specify the region of text where
392 changes have been done. TO_Z, if non-zero, means all the markers
393 whose positions are after TO should also be adjusted. */
394 void
395 adjust_markers_bytepos (ptrdiff_t from, ptrdiff_t from_byte,
396 ptrdiff_t to, ptrdiff_t to_byte, int to_z)
398 register struct Lisp_Marker *m;
399 ptrdiff_t beg = from, begbyte = from_byte;
401 adjust_suspend_auto_hscroll (from, to);
403 if (Z == Z_BYTE || (!to_z && to == to_byte))
405 /* Make sure each affected marker's bytepos is equal to
406 its charpos. */
407 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
409 if (m->bytepos > from_byte
410 && (to_z || m->bytepos <= to_byte))
411 m->bytepos = m->charpos;
414 else
416 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
418 /* Recompute each affected marker's bytepos. */
419 if (m->bytepos > from_byte
420 && (to_z || m->bytepos <= to_byte))
422 if (m->charpos < beg
423 && beg - m->charpos > m->charpos - from)
425 beg = from;
426 begbyte = from_byte;
428 m->bytepos = count_bytes (beg, begbyte, m->charpos);
429 beg = m->charpos;
430 begbyte = m->bytepos;
435 /* Make sure cached charpos/bytepos is invalid. */
436 clear_charpos_cache (current_buffer);
440 void
441 buffer_overflow (void)
443 error ("Maximum buffer size exceeded");
446 /* Make the gap NBYTES_ADDED bytes longer. */
448 static void
449 make_gap_larger (ptrdiff_t nbytes_added)
451 Lisp_Object tem;
452 ptrdiff_t real_gap_loc;
453 ptrdiff_t real_gap_loc_byte;
454 ptrdiff_t old_gap_size;
455 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
457 if (BUF_BYTES_MAX - current_size < nbytes_added)
458 buffer_overflow ();
460 /* If we have to get more space, get enough to last a while;
461 but do not exceed the maximum buffer size. */
462 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
463 BUF_BYTES_MAX - current_size);
465 enlarge_buffer_text (current_buffer, nbytes_added);
467 /* Prevent quitting in gap_left. We cannot allow a quit there,
468 because that would leave the buffer text in an inconsistent
469 state, with 2 gap holes instead of just one. */
470 tem = Vinhibit_quit;
471 Vinhibit_quit = Qt;
473 real_gap_loc = GPT;
474 real_gap_loc_byte = GPT_BYTE;
475 old_gap_size = GAP_SIZE;
477 /* Call the newly allocated space a gap at the end of the whole space. */
478 GPT = Z + GAP_SIZE;
479 GPT_BYTE = Z_BYTE + GAP_SIZE;
480 GAP_SIZE = nbytes_added;
482 /* Move the new gap down to be consecutive with the end of the old one. */
483 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
485 /* Now combine the two into one large gap. */
486 GAP_SIZE += old_gap_size;
487 GPT = real_gap_loc;
488 GPT_BYTE = real_gap_loc_byte;
490 /* Put an anchor. */
491 *(Z_ADDR) = 0;
493 Vinhibit_quit = tem;
496 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
498 /* Make the gap NBYTES_REMOVED bytes shorter. */
500 static void
501 make_gap_smaller (ptrdiff_t nbytes_removed)
503 Lisp_Object tem;
504 ptrdiff_t real_gap_loc;
505 ptrdiff_t real_gap_loc_byte;
506 ptrdiff_t real_Z;
507 ptrdiff_t real_Z_byte;
508 ptrdiff_t real_beg_unchanged;
509 ptrdiff_t new_gap_size;
511 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
512 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
513 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
515 /* Prevent quitting in gap_right. We cannot allow a quit there,
516 because that would leave the buffer text in an inconsistent
517 state, with 2 gap holes instead of just one. */
518 tem = Vinhibit_quit;
519 Vinhibit_quit = Qt;
521 real_gap_loc = GPT;
522 real_gap_loc_byte = GPT_BYTE;
523 new_gap_size = GAP_SIZE - nbytes_removed;
524 real_Z = Z;
525 real_Z_byte = Z_BYTE;
526 real_beg_unchanged = BEG_UNCHANGED;
528 /* Pretend that the last unwanted part of the gap is the entire gap,
529 and that the first desired part of the gap is part of the buffer
530 text. */
531 memset (GPT_ADDR, 0, new_gap_size);
532 GPT += new_gap_size;
533 GPT_BYTE += new_gap_size;
534 Z += new_gap_size;
535 Z_BYTE += new_gap_size;
536 GAP_SIZE = nbytes_removed;
538 /* Move the unwanted pretend gap to the end of the buffer. */
539 gap_right (Z, Z_BYTE);
541 enlarge_buffer_text (current_buffer, -nbytes_removed);
543 /* Now restore the desired gap. */
544 GAP_SIZE = new_gap_size;
545 GPT = real_gap_loc;
546 GPT_BYTE = real_gap_loc_byte;
547 Z = real_Z;
548 Z_BYTE = real_Z_byte;
549 BEG_UNCHANGED = real_beg_unchanged;
551 /* Put an anchor. */
552 *(Z_ADDR) = 0;
554 Vinhibit_quit = tem;
557 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
559 void
560 make_gap (ptrdiff_t nbytes_added)
562 if (nbytes_added >= 0)
563 /* With set-buffer-multibyte on a large buffer, we can end up growing the
564 * buffer *many* times. Avoid an O(N^2) behavior by increasing by an
565 * amount at least proportional to the size of the buffer.
566 * On my test (a 223.9MB zip file on a Thinkpad T61):
567 * With /5 => 24s
568 * With /32 => 25s
569 * With /64 => 26s
570 * With /128 => 28s
571 * With /1024 => 51s
572 * With /4096 => 131s
573 * With /∞ => gave up after 858s
574 * Of couse, ideally we should never call set-buffer-multibyte on
575 * a non-empty buffer (e.g. use buffer-swa-text instead). */
576 make_gap_larger (max (nbytes_added, (Z - BEG) / 64));
577 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
578 else
579 make_gap_smaller (-nbytes_added);
580 #endif
583 /* Add NBYTES to B's gap. It's enough to temporarily
584 fake current_buffer and avoid real switch to B. */
586 void
587 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
589 struct buffer *oldb = current_buffer;
591 current_buffer = b;
592 make_gap (nbytes);
593 current_buffer = oldb;
596 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
597 FROM_MULTIBYTE says whether the incoming text is multibyte.
598 TO_MULTIBYTE says whether to store the text as multibyte.
599 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
601 Return the number of bytes stored at TO_ADDR. */
603 ptrdiff_t
604 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
605 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
607 if (from_multibyte == to_multibyte)
609 memcpy (to_addr, from_addr, nbytes);
610 return nbytes;
612 else if (from_multibyte)
614 ptrdiff_t nchars = 0;
615 ptrdiff_t bytes_left = nbytes;
617 while (bytes_left > 0)
619 int thislen, c;
620 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
621 if (! ASCII_CHAR_P (c))
622 c &= 0xFF;
623 *to_addr++ = c;
624 from_addr += thislen;
625 bytes_left -= thislen;
626 nchars++;
628 return nchars;
630 else
632 unsigned char *initial_to_addr = to_addr;
634 /* Convert single-byte to multibyte. */
635 while (nbytes > 0)
637 int c = *from_addr++;
639 if (!ASCII_CHAR_P (c))
641 c = BYTE8_TO_CHAR (c);
642 to_addr += CHAR_STRING (c, to_addr);
643 nbytes--;
645 else
646 /* Special case for speed. */
647 *to_addr++ = c, nbytes--;
649 return to_addr - initial_to_addr;
653 /* Insert a string of specified length before point.
654 This function judges multibyteness based on
655 enable_multibyte_characters in the current buffer;
656 it never converts between single-byte and multibyte.
658 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
659 prepare_to_modify_buffer could relocate the text. */
661 void
662 insert (const char *string, ptrdiff_t nbytes)
664 if (nbytes > 0)
666 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
667 insert_1_both (string, len, nbytes, 0, 1, 0);
668 opoint = PT - len;
669 signal_after_change (opoint, 0, len);
670 update_compositions (opoint, PT, CHECK_BORDER);
674 /* Likewise, but inherit text properties from neighboring characters. */
676 void
677 insert_and_inherit (const char *string, ptrdiff_t nbytes)
679 if (nbytes > 0)
681 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
682 insert_1_both (string, len, nbytes, 1, 1, 0);
683 opoint = PT - len;
684 signal_after_change (opoint, 0, len);
685 update_compositions (opoint, PT, CHECK_BORDER);
689 /* Insert the character C before point. Do not inherit text properties. */
691 void
692 insert_char (int c)
694 unsigned char str[MAX_MULTIBYTE_LENGTH];
695 int len;
697 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
698 len = CHAR_STRING (c, str);
699 else
701 len = 1;
702 str[0] = c;
705 insert ((char *) str, len);
708 /* Insert the null-terminated string S before point. */
710 void
711 insert_string (const char *s)
713 insert (s, strlen (s));
716 /* Like `insert' except that all markers pointing at the place where
717 the insertion happens are adjusted to point after it.
718 Don't use this function to insert part of a Lisp string,
719 since gc could happen and relocate it. */
721 void
722 insert_before_markers (const char *string, ptrdiff_t nbytes)
724 if (nbytes > 0)
726 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
727 insert_1_both (string, len, nbytes, 0, 1, 1);
728 opoint = PT - len;
729 signal_after_change (opoint, 0, len);
730 update_compositions (opoint, PT, CHECK_BORDER);
734 /* Likewise, but inherit text properties from neighboring characters. */
736 void
737 insert_before_markers_and_inherit (const char *string,
738 ptrdiff_t nbytes)
740 if (nbytes > 0)
742 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
743 insert_1_both (string, len, nbytes, 1, 1, 1);
744 opoint = PT - len;
745 signal_after_change (opoint, 0, len);
746 update_compositions (opoint, PT, CHECK_BORDER);
750 #ifdef BYTE_COMBINING_DEBUG
752 /* See if the bytes before POS/POS_BYTE combine with bytes
753 at the start of STRING to form a single character.
754 If so, return the number of bytes at the start of STRING
755 which combine in this way. Otherwise, return 0. */
758 count_combining_before (const unsigned char *string, ptrdiff_t length,
759 ptrdiff_t pos, ptrdiff_t pos_byte)
761 int len, combining_bytes;
762 const unsigned char *p;
764 if (NILP (current_buffer->enable_multibyte_characters))
765 return 0;
767 /* At first, we can exclude the following cases:
768 (1) STRING[0] can't be a following byte of multibyte sequence.
769 (2) POS is the start of the current buffer.
770 (3) A character before POS is not a multibyte character. */
771 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
772 return 0;
773 if (pos_byte == BEG_BYTE) /* case (2) */
774 return 0;
775 len = 1;
776 p = BYTE_POS_ADDR (pos_byte - 1);
777 while (! CHAR_HEAD_P (*p)) p--, len++;
778 if (! LEADING_CODE_P (*p)) /* case (3) */
779 return 0;
781 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
782 if (combining_bytes <= 0)
783 /* The character preceding POS is, complete and no room for
784 combining bytes (combining_bytes == 0), or an independent 8-bit
785 character (combining_bytes < 0). */
786 return 0;
788 /* We have a combination situation. Count the bytes at STRING that
789 may combine. */
790 p = string + 1;
791 while (!CHAR_HEAD_P (*p) && p < string + length)
792 p++;
794 return (combining_bytes < p - string ? combining_bytes : p - string);
797 /* See if the bytes after POS/POS_BYTE combine with bytes
798 at the end of STRING to form a single character.
799 If so, return the number of bytes after POS/POS_BYTE
800 which combine in this way. Otherwise, return 0. */
803 count_combining_after (const unsigned char *string,
804 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
806 ptrdiff_t opos_byte = pos_byte;
807 ptrdiff_t i;
808 ptrdiff_t bytes;
809 unsigned char *bufp;
811 if (NILP (current_buffer->enable_multibyte_characters))
812 return 0;
814 /* At first, we can exclude the following cases:
815 (1) The last byte of STRING is an ASCII.
816 (2) POS is the last of the current buffer.
817 (3) A character at POS can't be a following byte of multibyte
818 character. */
819 if (length > 0 && ASCII_CHAR_P (string[length - 1])) /* case (1) */
820 return 0;
821 if (pos_byte == Z_BYTE) /* case (2) */
822 return 0;
823 bufp = BYTE_POS_ADDR (pos_byte);
824 if (CHAR_HEAD_P (*bufp)) /* case (3) */
825 return 0;
827 i = length - 1;
828 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
830 i--;
832 if (i < 0)
834 /* All characters in STRING are not character head. We must
835 check also preceding bytes at POS. We are sure that the gap
836 is at POS. */
837 unsigned char *p = BEG_ADDR;
838 i = pos_byte - 2;
839 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
840 i--;
841 if (i < 0 || !LEADING_CODE_P (p[i]))
842 return 0;
844 bytes = BYTES_BY_CHAR_HEAD (p[i]);
845 return (bytes <= pos_byte - 1 - i + length
847 : bytes - (pos_byte - 1 - i + length));
849 if (!LEADING_CODE_P (string[i]))
850 return 0;
852 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
853 bufp++, pos_byte++;
854 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
856 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
859 #endif
862 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
863 starting at STRING. INHERIT non-zero means inherit the text
864 properties from neighboring characters; zero means inserted text
865 will have no text properties. PREPARE non-zero means call
866 prepare_to_modify_buffer, which checks that the region is not
867 read-only, and calls before-change-function and any modification
868 properties the text may have. BEFORE_MARKERS non-zero means adjust
869 all markers that point at the insertion place to point after it. */
871 void
872 insert_1_both (const char *string,
873 ptrdiff_t nchars, ptrdiff_t nbytes,
874 bool inherit, bool prepare, bool before_markers)
876 if (nchars == 0)
877 return;
879 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
880 nchars = nbytes;
882 if (prepare)
883 /* Do this before moving and increasing the gap,
884 because the before-change hooks might move the gap
885 or make it smaller. */
886 prepare_to_modify_buffer (PT, PT, NULL);
888 if (PT != GPT)
889 move_gap_both (PT, PT_BYTE);
890 if (GAP_SIZE < nbytes)
891 make_gap (nbytes - GAP_SIZE);
893 #ifdef BYTE_COMBINING_DEBUG
894 if (count_combining_before (string, nbytes, PT, PT_BYTE)
895 || count_combining_after (string, nbytes, PT, PT_BYTE))
896 emacs_abort ();
897 #endif
899 /* Record deletion of the surrounding text that combines with
900 the insertion. This, together with recording the insertion,
901 will add up to the right stuff in the undo list. */
902 record_insert (PT, nchars);
903 MODIFF++;
904 CHARS_MODIFF = MODIFF;
906 memcpy (GPT_ADDR, string, nbytes);
908 GAP_SIZE -= nbytes;
909 GPT += nchars;
910 ZV += nchars;
911 Z += nchars;
912 GPT_BYTE += nbytes;
913 ZV_BYTE += nbytes;
914 Z_BYTE += nbytes;
915 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
917 eassert (GPT <= GPT_BYTE);
919 /* The insert may have been in the unchanged region, so check again. */
920 if (Z - GPT < END_UNCHANGED)
921 END_UNCHANGED = Z - GPT;
923 adjust_overlays_for_insert (PT, nchars);
924 adjust_markers_for_insert (PT, PT_BYTE,
925 PT + nchars, PT_BYTE + nbytes,
926 before_markers);
928 offset_intervals (current_buffer, PT, nchars);
930 if (!inherit && buffer_intervals (current_buffer))
931 set_text_properties (make_number (PT), make_number (PT + nchars),
932 Qnil, Qnil, Qnil);
934 adjust_point (nchars, nbytes);
936 check_markers ();
939 /* Insert the part of the text of STRING, a Lisp object assumed to be
940 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
941 starting at position POS / POS_BYTE. If the text of STRING has properties,
942 copy them into the buffer.
944 It does not work to use `insert' for this, because a GC could happen
945 before we copy the stuff into the buffer, and relocate the string
946 without insert noticing. */
948 void
949 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
950 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
952 ptrdiff_t opoint = PT;
954 if (SCHARS (string) == 0)
955 return;
957 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
958 inherit, 0);
959 signal_after_change (opoint, 0, PT - opoint);
960 update_compositions (opoint, PT, CHECK_BORDER);
963 /* Like `insert_from_string' except that all markers pointing
964 at the place where the insertion happens are adjusted to point after it. */
966 void
967 insert_from_string_before_markers (Lisp_Object string,
968 ptrdiff_t pos, ptrdiff_t pos_byte,
969 ptrdiff_t length, ptrdiff_t length_byte,
970 bool inherit)
972 ptrdiff_t opoint = PT;
974 if (SCHARS (string) == 0)
975 return;
977 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
978 inherit, 1);
979 signal_after_change (opoint, 0, PT - opoint);
980 update_compositions (opoint, PT, CHECK_BORDER);
983 /* Subroutine of the insertion functions above. */
985 static void
986 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
987 ptrdiff_t nchars, ptrdiff_t nbytes,
988 bool inherit, bool before_markers)
990 ptrdiff_t outgoing_nbytes = nbytes;
991 INTERVAL intervals;
993 /* Make OUTGOING_NBYTES describe the text
994 as it will be inserted in this buffer. */
996 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
997 outgoing_nbytes = nchars;
998 else if (! STRING_MULTIBYTE (string))
999 outgoing_nbytes
1000 = count_size_as_multibyte (SDATA (string) + pos_byte,
1001 nbytes);
1003 /* Do this before moving and increasing the gap,
1004 because the before-change hooks might move the gap
1005 or make it smaller. */
1006 prepare_to_modify_buffer (PT, PT, NULL);
1008 if (PT != GPT)
1009 move_gap_both (PT, PT_BYTE);
1010 if (GAP_SIZE < outgoing_nbytes)
1011 make_gap (outgoing_nbytes - GAP_SIZE);
1013 /* Copy the string text into the buffer, perhaps converting
1014 between single-byte and multibyte. */
1015 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
1016 STRING_MULTIBYTE (string),
1017 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1019 #ifdef BYTE_COMBINING_DEBUG
1020 /* We have copied text into the gap, but we have not altered
1021 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1022 to these functions and get the same results as we would
1023 have got earlier on. Meanwhile, PT_ADDR does point to
1024 the text that has been stored by copy_text. */
1025 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1026 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1027 emacs_abort ();
1028 #endif
1030 record_insert (PT, nchars);
1031 MODIFF++;
1032 CHARS_MODIFF = MODIFF;
1034 GAP_SIZE -= outgoing_nbytes;
1035 GPT += nchars;
1036 ZV += nchars;
1037 Z += nchars;
1038 GPT_BYTE += outgoing_nbytes;
1039 ZV_BYTE += outgoing_nbytes;
1040 Z_BYTE += outgoing_nbytes;
1041 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1043 eassert (GPT <= GPT_BYTE);
1045 /* The insert may have been in the unchanged region, so check again. */
1046 if (Z - GPT < END_UNCHANGED)
1047 END_UNCHANGED = Z - GPT;
1049 adjust_overlays_for_insert (PT, nchars);
1050 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1051 PT_BYTE + outgoing_nbytes,
1052 before_markers);
1054 offset_intervals (current_buffer, PT, nchars);
1056 intervals = string_intervals (string);
1057 /* Get the intervals for the part of the string we are inserting. */
1058 if (nbytes < SBYTES (string))
1059 intervals = copy_intervals (intervals, pos, nchars);
1061 /* Insert those intervals. */
1062 graft_intervals_into_buffer (intervals, PT, nchars,
1063 current_buffer, inherit);
1065 adjust_point (nchars, outgoing_nbytes);
1067 check_markers ();
1070 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1071 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
1072 GPT_ADDR (if not text_at_gap_tail). */
1074 void
1075 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
1077 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
1079 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1080 nchars = nbytes;
1082 /* No need to call prepare_to_modify_buffer, since this is called
1083 from places that replace some region with a different text, so
1084 prepare_to_modify_buffer was already called by the deletion part
1085 of this dance. */
1086 invalidate_buffer_caches (current_buffer, GPT, GPT);
1087 record_insert (GPT, nchars);
1088 MODIFF++;
1090 GAP_SIZE -= nbytes;
1091 if (! text_at_gap_tail)
1093 GPT += nchars;
1094 GPT_BYTE += nbytes;
1096 ZV += nchars;
1097 Z += nchars;
1098 ZV_BYTE += nbytes;
1099 Z_BYTE += nbytes;
1100 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1102 eassert (GPT <= GPT_BYTE);
1104 adjust_overlays_for_insert (ins_charpos, nchars);
1105 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1106 ins_charpos + nchars, ins_bytepos + nbytes, 0);
1108 if (buffer_intervals (current_buffer))
1110 offset_intervals (current_buffer, ins_charpos, nchars);
1111 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1112 current_buffer, 0);
1115 if (ins_charpos < PT)
1116 adjust_point (nchars, nbytes);
1118 check_markers ();
1121 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1122 current buffer. If the text in BUF has properties, they are absorbed
1123 into the current buffer.
1125 It does not work to use `insert' for this, because a malloc could happen
1126 and relocate BUF's text before the copy happens. */
1128 void
1129 insert_from_buffer (struct buffer *buf,
1130 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1132 ptrdiff_t opoint = PT;
1134 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1135 signal_after_change (opoint, 0, PT - opoint);
1136 update_compositions (opoint, PT, CHECK_BORDER);
1139 static void
1140 insert_from_buffer_1 (struct buffer *buf,
1141 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1143 ptrdiff_t chunk, chunk_expanded;
1144 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1145 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1146 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1147 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1148 INTERVAL intervals;
1150 if (nchars == 0)
1151 return;
1153 /* Make OUTGOING_NBYTES describe the text
1154 as it will be inserted in this buffer. */
1156 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1157 outgoing_nbytes = nchars;
1158 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1160 ptrdiff_t outgoing_before_gap = 0;
1161 ptrdiff_t outgoing_after_gap = 0;
1163 if (from < BUF_GPT (buf))
1165 chunk = BUF_GPT_BYTE (buf) - from_byte;
1166 if (chunk > incoming_nbytes)
1167 chunk = incoming_nbytes;
1168 outgoing_before_gap
1169 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1170 chunk);
1172 else
1173 chunk = 0;
1175 if (chunk < incoming_nbytes)
1176 outgoing_after_gap
1177 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1178 from_byte + chunk),
1179 incoming_nbytes - chunk);
1181 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1184 /* Do this before moving and increasing the gap,
1185 because the before-change hooks might move the gap
1186 or make it smaller. */
1187 prepare_to_modify_buffer (PT, PT, NULL);
1189 if (PT != GPT)
1190 move_gap_both (PT, PT_BYTE);
1191 if (GAP_SIZE < outgoing_nbytes)
1192 make_gap (outgoing_nbytes - GAP_SIZE);
1194 if (from < BUF_GPT (buf))
1196 chunk = BUF_GPT_BYTE (buf) - from_byte;
1197 if (chunk > incoming_nbytes)
1198 chunk = incoming_nbytes;
1199 /* Record number of output bytes, so we know where
1200 to put the output from the second copy_text. */
1201 chunk_expanded
1202 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1203 GPT_ADDR, chunk,
1204 ! NILP (BVAR (buf, enable_multibyte_characters)),
1205 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1207 else
1208 chunk_expanded = chunk = 0;
1210 if (chunk < incoming_nbytes)
1211 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1212 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1213 ! NILP (BVAR (buf, enable_multibyte_characters)),
1214 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1216 #ifdef BYTE_COMBINING_DEBUG
1217 /* We have copied text into the gap, but we have not altered
1218 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1219 to these functions and get the same results as we would
1220 have got earlier on. Meanwhile, GPT_ADDR does point to
1221 the text that has been stored by copy_text. */
1222 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1223 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1224 emacs_abort ();
1225 #endif
1227 record_insert (PT, nchars);
1228 MODIFF++;
1229 CHARS_MODIFF = MODIFF;
1231 GAP_SIZE -= outgoing_nbytes;
1232 GPT += nchars;
1233 ZV += nchars;
1234 Z += nchars;
1235 GPT_BYTE += outgoing_nbytes;
1236 ZV_BYTE += outgoing_nbytes;
1237 Z_BYTE += outgoing_nbytes;
1238 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1240 eassert (GPT <= GPT_BYTE);
1242 /* The insert may have been in the unchanged region, so check again. */
1243 if (Z - GPT < END_UNCHANGED)
1244 END_UNCHANGED = Z - GPT;
1246 adjust_overlays_for_insert (PT, nchars);
1247 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1248 PT_BYTE + outgoing_nbytes,
1251 offset_intervals (current_buffer, PT, nchars);
1253 /* Get the intervals for the part of the string we are inserting. */
1254 intervals = buffer_intervals (buf);
1255 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1257 if (buf == current_buffer && PT <= from)
1258 from += nchars;
1259 intervals = copy_intervals (intervals, from, nchars);
1262 /* Insert those intervals. */
1263 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1265 adjust_point (nchars, outgoing_nbytes);
1268 /* Record undo information and adjust markers and position keepers for
1269 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1270 chars (LEN_BYTE bytes) which resides in the gap just after
1271 GPT_ADDR.
1273 PREV_TEXT nil means the new text was just inserted. */
1275 static void
1276 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1277 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1279 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1281 #ifdef BYTE_COMBINING_DEBUG
1282 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1283 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1284 emacs_abort ();
1285 #endif
1287 if (STRINGP (prev_text))
1289 nchars_del = SCHARS (prev_text);
1290 nbytes_del = SBYTES (prev_text);
1293 /* Update various buffer positions for the new text. */
1294 GAP_SIZE -= len_byte;
1295 ZV += len; Z += len;
1296 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1297 GPT += len; GPT_BYTE += len_byte;
1298 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1300 if (nchars_del > 0)
1301 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1302 len, len_byte);
1303 else
1304 adjust_markers_for_insert (from, from_byte,
1305 from + len, from_byte + len_byte, 0);
1307 if (nchars_del > 0)
1308 record_delete (from, prev_text, false);
1309 record_insert (from, len);
1311 if (len > nchars_del)
1312 adjust_overlays_for_insert (from, len - nchars_del);
1313 else if (len < nchars_del)
1314 adjust_overlays_for_delete (from, nchars_del - len);
1316 offset_intervals (current_buffer, from, len - nchars_del);
1318 if (from < PT)
1319 adjust_point (len - nchars_del, len_byte - nbytes_del);
1321 /* As byte combining will decrease Z, we must check this again. */
1322 if (Z - GPT < END_UNCHANGED)
1323 END_UNCHANGED = Z - GPT;
1325 check_markers ();
1327 if (len == 0)
1328 evaporate_overlays (from);
1329 MODIFF++;
1330 CHARS_MODIFF = MODIFF;
1333 /* Record undo information, adjust markers and position keepers for an
1334 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1335 text already exists in the current buffer but character length (TO
1336 - FROM) may be incorrect, the correct length is NEWLEN. */
1338 void
1339 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1340 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1342 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1344 if (GPT != to)
1345 move_gap_both (to, to_byte);
1346 GAP_SIZE += len_byte;
1347 GPT -= len; GPT_BYTE -= len_byte;
1348 ZV -= len; ZV_BYTE -= len_byte;
1349 Z -= len; Z_BYTE -= len_byte;
1350 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1353 /* Replace the text from character positions FROM to TO with NEW,
1354 If PREPARE, call prepare_to_modify_buffer.
1355 If INHERIT, the newly inserted text should inherit text properties
1356 from the surrounding non-deleted text.
1357 If ADJUST_MATCH_DATA, then adjust the match data before calling
1358 signal_after_change. */
1360 /* Note that this does not yet handle markers quite right.
1361 Also it needs to record a single undo-entry that does a replacement
1362 rather than a separate delete and insert.
1363 That way, undo will also handle markers properly.
1365 But if MARKERS is 0, don't relocate markers. */
1367 void
1368 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1369 bool prepare, bool inherit, bool markers,
1370 bool adjust_match_data)
1372 ptrdiff_t inschars = SCHARS (new);
1373 ptrdiff_t insbytes = SBYTES (new);
1374 ptrdiff_t from_byte, to_byte;
1375 ptrdiff_t nbytes_del, nchars_del;
1376 INTERVAL intervals;
1377 ptrdiff_t outgoing_insbytes = insbytes;
1378 Lisp_Object deletion;
1380 check_markers ();
1382 deletion = Qnil;
1384 if (prepare)
1386 ptrdiff_t range_length = to - from;
1387 prepare_to_modify_buffer (from, to, &from);
1388 to = from + range_length;
1391 /* Make args be valid. */
1392 if (from < BEGV)
1393 from = BEGV;
1394 if (to > ZV)
1395 to = ZV;
1397 from_byte = CHAR_TO_BYTE (from);
1398 to_byte = CHAR_TO_BYTE (to);
1400 nchars_del = to - from;
1401 nbytes_del = to_byte - from_byte;
1403 if (nbytes_del <= 0 && insbytes == 0)
1404 return;
1406 /* Make OUTGOING_INSBYTES describe the text
1407 as it will be inserted in this buffer. */
1409 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1410 outgoing_insbytes = inschars;
1411 else if (! STRING_MULTIBYTE (new))
1412 outgoing_insbytes
1413 = count_size_as_multibyte (SDATA (new), insbytes);
1415 /* Make sure the gap is somewhere in or next to what we are deleting. */
1416 if (from > GPT)
1417 gap_right (from, from_byte);
1418 if (to < GPT)
1419 gap_left (to, to_byte, 0);
1421 /* Even if we don't record for undo, we must keep the original text
1422 because we may have to recover it because of inappropriate byte
1423 combining. */
1424 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1425 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1427 GAP_SIZE += nbytes_del;
1428 ZV -= nchars_del;
1429 Z -= nchars_del;
1430 ZV_BYTE -= nbytes_del;
1431 Z_BYTE -= nbytes_del;
1432 GPT = from;
1433 GPT_BYTE = from_byte;
1434 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1436 eassert (GPT <= GPT_BYTE);
1438 if (GPT - BEG < BEG_UNCHANGED)
1439 BEG_UNCHANGED = GPT - BEG;
1440 if (Z - GPT < END_UNCHANGED)
1441 END_UNCHANGED = Z - GPT;
1443 if (GAP_SIZE < outgoing_insbytes)
1444 make_gap (outgoing_insbytes - GAP_SIZE);
1446 /* Copy the string text into the buffer, perhaps converting
1447 between single-byte and multibyte. */
1448 copy_text (SDATA (new), GPT_ADDR, insbytes,
1449 STRING_MULTIBYTE (new),
1450 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1452 #ifdef BYTE_COMBINING_DEBUG
1453 /* We have copied text into the gap, but we have not marked
1454 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1455 here, for both the previous text and the following text.
1456 Meanwhile, GPT_ADDR does point to
1457 the text that has been stored by copy_text. */
1458 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1459 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1460 emacs_abort ();
1461 #endif
1463 /* Record the insertion first, so that when we undo,
1464 the deletion will be undone first. Thus, undo
1465 will insert before deleting, and thus will keep
1466 the markers before and after this text separate. */
1467 if (!NILP (deletion))
1469 record_insert (from + SCHARS (deletion), inschars);
1470 record_delete (from, deletion, false);
1473 GAP_SIZE -= outgoing_insbytes;
1474 GPT += inschars;
1475 ZV += inschars;
1476 Z += inschars;
1477 GPT_BYTE += outgoing_insbytes;
1478 ZV_BYTE += outgoing_insbytes;
1479 Z_BYTE += outgoing_insbytes;
1480 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1482 eassert (GPT <= GPT_BYTE);
1484 /* Adjust markers for the deletion and the insertion. */
1485 if (markers)
1486 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1487 inschars, outgoing_insbytes);
1488 else
1490 /* The character positions of the markers remain intact, but we
1491 still need to update their byte positions, because the
1492 deleted and the inserted text might have multibyte sequences
1493 which make the original byte positions of the markers
1494 invalid. */
1495 adjust_markers_bytepos (from, from_byte, from + inschars,
1496 from_byte + outgoing_insbytes, 1);
1499 /* Adjust the overlay center as needed. This must be done after
1500 adjusting the markers that bound the overlays. */
1501 adjust_overlays_for_delete (from, nchars_del);
1502 adjust_overlays_for_insert (from, inschars);
1504 offset_intervals (current_buffer, from, inschars - nchars_del);
1506 /* Get the intervals for the part of the string we are inserting--
1507 not including the combined-before bytes. */
1508 intervals = string_intervals (new);
1509 /* Insert those intervals. */
1510 graft_intervals_into_buffer (intervals, from, inschars,
1511 current_buffer, inherit);
1513 /* Relocate point as if it were a marker. */
1514 if (from < PT)
1515 adjust_point ((from + inschars - (PT < to ? PT : to)),
1516 (from_byte + outgoing_insbytes
1517 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1519 if (outgoing_insbytes == 0)
1520 evaporate_overlays (from);
1522 check_markers ();
1524 MODIFF++;
1525 CHARS_MODIFF = MODIFF;
1527 if (adjust_match_data)
1528 update_search_regs (from, to, from + SCHARS (new));
1530 signal_after_change (from, nchars_del, GPT - from);
1531 update_compositions (from, GPT, CHECK_BORDER);
1534 /* Replace the text from character positions FROM to TO with
1535 the text in INS of length INSCHARS.
1536 Keep the text properties that applied to the old characters
1537 (extending them to all the new chars if there are more new chars).
1539 Note that this does not yet handle markers quite right.
1541 If MARKERS, relocate markers.
1543 Unlike most functions at this level, never call
1544 prepare_to_modify_buffer and never call signal_after_change. */
1546 void
1547 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1548 ptrdiff_t to, ptrdiff_t to_byte,
1549 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1550 bool markers)
1552 ptrdiff_t nbytes_del, nchars_del;
1554 check_markers ();
1556 nchars_del = to - from;
1557 nbytes_del = to_byte - from_byte;
1559 if (nbytes_del <= 0 && insbytes == 0)
1560 return;
1562 /* Make sure the gap is somewhere in or next to what we are deleting. */
1563 if (from > GPT)
1564 gap_right (from, from_byte);
1565 if (to < GPT)
1566 gap_left (to, to_byte, 0);
1568 GAP_SIZE += nbytes_del;
1569 ZV -= nchars_del;
1570 Z -= nchars_del;
1571 ZV_BYTE -= nbytes_del;
1572 Z_BYTE -= nbytes_del;
1573 GPT = from;
1574 GPT_BYTE = from_byte;
1575 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1577 eassert (GPT <= GPT_BYTE);
1579 if (GPT - BEG < BEG_UNCHANGED)
1580 BEG_UNCHANGED = GPT - BEG;
1581 if (Z - GPT < END_UNCHANGED)
1582 END_UNCHANGED = Z - GPT;
1584 if (GAP_SIZE < insbytes)
1585 make_gap (insbytes - GAP_SIZE);
1587 /* Copy the replacement text into the buffer. */
1588 memcpy (GPT_ADDR, ins, insbytes);
1590 #ifdef BYTE_COMBINING_DEBUG
1591 /* We have copied text into the gap, but we have not marked
1592 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1593 here, for both the previous text and the following text.
1594 Meanwhile, GPT_ADDR does point to
1595 the text that has been stored by copy_text. */
1596 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1597 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1598 emacs_abort ();
1599 #endif
1601 GAP_SIZE -= insbytes;
1602 GPT += inschars;
1603 ZV += inschars;
1604 Z += inschars;
1605 GPT_BYTE += insbytes;
1606 ZV_BYTE += insbytes;
1607 Z_BYTE += insbytes;
1608 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1610 eassert (GPT <= GPT_BYTE);
1612 /* Adjust markers for the deletion and the insertion. */
1613 if (! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1615 if (markers)
1616 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1617 inschars, insbytes);
1618 else
1620 /* The character positions of the markers remain intact, but
1621 we still need to update their byte positions, because the
1622 deleted and the inserted text might have multibyte
1623 sequences which make the original byte positions of the
1624 markers invalid. */
1625 adjust_markers_bytepos (from, from_byte, from + inschars,
1626 from_byte + insbytes, 1);
1630 /* Adjust the overlay center as needed. This must be done after
1631 adjusting the markers that bound the overlays. */
1632 if (nchars_del != inschars)
1634 adjust_overlays_for_insert (from, inschars);
1635 adjust_overlays_for_delete (from + inschars, nchars_del);
1638 offset_intervals (current_buffer, from, inschars - nchars_del);
1640 /* Relocate point as if it were a marker. */
1641 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1643 if (PT < to)
1644 /* PT was within the deleted text. Move it to FROM. */
1645 adjust_point (from - PT, from_byte - PT_BYTE);
1646 else
1647 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1650 if (insbytes == 0)
1651 evaporate_overlays (from);
1653 check_markers ();
1655 MODIFF++;
1656 CHARS_MODIFF = MODIFF;
1659 /* Delete characters in current buffer
1660 from FROM up to (but not including) TO.
1661 If TO comes before FROM, we delete nothing. */
1663 void
1664 del_range (ptrdiff_t from, ptrdiff_t to)
1666 del_range_1 (from, to, 1, 0);
1669 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1670 RET_STRING says to return the deleted text. */
1672 Lisp_Object
1673 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1675 ptrdiff_t from_byte, to_byte;
1676 Lisp_Object deletion;
1678 /* Make args be valid */
1679 if (from < BEGV)
1680 from = BEGV;
1681 if (to > ZV)
1682 to = ZV;
1684 if (to <= from)
1685 return Qnil;
1687 if (prepare)
1689 ptrdiff_t range_length = to - from;
1690 prepare_to_modify_buffer (from, to, &from);
1691 to = min (ZV, from + range_length);
1694 from_byte = CHAR_TO_BYTE (from);
1695 to_byte = CHAR_TO_BYTE (to);
1697 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1698 signal_after_change (from, to - from, 0);
1699 update_compositions (from, from, CHECK_HEAD);
1700 return deletion;
1703 /* Like del_range_1 but args are byte positions, not char positions. */
1705 void
1706 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte)
1708 ptrdiff_t from, to;
1710 /* Make args be valid. */
1711 if (from_byte < BEGV_BYTE)
1712 from_byte = BEGV_BYTE;
1713 if (to_byte > ZV_BYTE)
1714 to_byte = ZV_BYTE;
1716 if (to_byte <= from_byte)
1717 return;
1719 from = BYTE_TO_CHAR (from_byte);
1720 to = BYTE_TO_CHAR (to_byte);
1723 ptrdiff_t old_from = from, old_to = Z - to;
1724 ptrdiff_t range_length = to - from;
1725 prepare_to_modify_buffer (from, to, &from);
1726 to = from + range_length;
1728 if (old_from != from)
1729 from_byte = CHAR_TO_BYTE (from);
1730 if (to > ZV)
1732 to = ZV;
1733 to_byte = ZV_BYTE;
1735 else if (old_to == Z - to)
1736 to_byte = CHAR_TO_BYTE (to);
1739 del_range_2 (from, from_byte, to, to_byte, 0);
1740 signal_after_change (from, to - from, 0);
1741 update_compositions (from, from, CHECK_HEAD);
1744 /* Like del_range_1, but positions are specified both as charpos
1745 and bytepos. */
1747 void
1748 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1749 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1751 /* Make args be valid */
1752 if (from_byte < BEGV_BYTE)
1753 from_byte = BEGV_BYTE;
1754 if (to_byte > ZV_BYTE)
1755 to_byte = ZV_BYTE;
1757 if (to_byte <= from_byte)
1758 return;
1760 if (from < BEGV)
1761 from = BEGV;
1762 if (to > ZV)
1763 to = ZV;
1765 if (prepare)
1767 ptrdiff_t old_from = from, old_to = Z - to;
1768 ptrdiff_t range_length = to - from;
1769 prepare_to_modify_buffer (from, to, &from);
1770 to = from + range_length;
1772 if (old_from != from)
1773 from_byte = CHAR_TO_BYTE (from);
1774 if (to > ZV)
1776 to = ZV;
1777 to_byte = ZV_BYTE;
1779 else if (old_to == Z - to)
1780 to_byte = CHAR_TO_BYTE (to);
1783 del_range_2 (from, from_byte, to, to_byte, 0);
1784 signal_after_change (from, to - from, 0);
1785 update_compositions (from, from, CHECK_HEAD);
1788 /* Delete a range of text, specified both as character positions
1789 and byte positions. FROM and TO are character positions,
1790 while FROM_BYTE and TO_BYTE are byte positions.
1791 If RET_STRING, the deleted area is returned as a string. */
1793 Lisp_Object
1794 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1795 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1797 ptrdiff_t nbytes_del, nchars_del;
1798 Lisp_Object deletion;
1800 check_markers ();
1802 nchars_del = to - from;
1803 nbytes_del = to_byte - from_byte;
1805 /* Make sure the gap is somewhere in or next to what we are deleting. */
1806 if (from > GPT)
1807 gap_right (from, from_byte);
1808 if (to < GPT)
1809 gap_left (to, to_byte, 0);
1811 #ifdef BYTE_COMBINING_DEBUG
1812 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1813 Z_BYTE - to_byte, from, from_byte))
1814 emacs_abort ();
1815 #endif
1817 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1818 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1819 else
1820 deletion = Qnil;
1822 /* Record marker adjustments, and text deletion into undo
1823 history. */
1824 record_delete (from, deletion, true);
1826 /* Relocate all markers pointing into the new, larger gap to point
1827 at the end of the text before the gap. */
1828 adjust_markers_for_delete (from, from_byte, to, to_byte);
1830 MODIFF++;
1831 CHARS_MODIFF = MODIFF;
1833 /* Relocate point as if it were a marker. */
1834 if (from < PT)
1835 adjust_point (from - (PT < to ? PT : to),
1836 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1838 offset_intervals (current_buffer, from, - nchars_del);
1840 /* Adjust the overlay center as needed. This must be done after
1841 adjusting the markers that bound the overlays. */
1842 adjust_overlays_for_delete (from, nchars_del);
1844 GAP_SIZE += nbytes_del;
1845 ZV_BYTE -= nbytes_del;
1846 Z_BYTE -= nbytes_del;
1847 ZV -= nchars_del;
1848 Z -= nchars_del;
1849 GPT = from;
1850 GPT_BYTE = from_byte;
1851 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1852 /* Put an anchor, unless called from decode_coding_object which
1853 needs to access the previous gap contents. */
1854 *(GPT_ADDR) = 0;
1856 eassert (GPT <= GPT_BYTE);
1858 if (GPT - BEG < BEG_UNCHANGED)
1859 BEG_UNCHANGED = GPT - BEG;
1860 if (Z - GPT < END_UNCHANGED)
1861 END_UNCHANGED = Z - GPT;
1863 check_markers ();
1865 evaporate_overlays (from);
1867 return deletion;
1870 /* Call this if you're about to change the text of current buffer
1871 from character positions START to END. This checks the read-only
1872 properties of the region, calls the necessary modification hooks,
1873 and warns the next redisplay that it should pay attention to that
1874 area. */
1876 void
1877 modify_text (ptrdiff_t start, ptrdiff_t end)
1879 prepare_to_modify_buffer (start, end, NULL);
1881 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1882 if (MODIFF <= SAVE_MODIFF)
1883 record_first_change ();
1884 MODIFF++;
1885 CHARS_MODIFF = MODIFF;
1887 bset_point_before_scroll (current_buffer, Qnil);
1890 /* Signal that we are about to make a change that may result in new
1891 undo information.
1893 static void
1894 run_undoable_change (void)
1896 if (EQ (BVAR (current_buffer, undo_list), Qt))
1897 return;
1899 call0 (Qundo_auto__undoable_change);
1902 /* Check that it is okay to modify the buffer between START and END,
1903 which are char positions.
1905 Run the before-change-function, if any. If intervals are in use,
1906 verify that the text to be modified is not read-only, and call
1907 any modification properties the text may have.
1909 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1910 by holding its value temporarily in a marker.
1912 This function runs Lisp, which means it can GC, which means it can
1913 compact buffers, including the current buffer being worked on here.
1914 So don't you dare calling this function while manipulating the gap,
1915 or during some other similar "critical section". */
1917 void
1918 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1919 ptrdiff_t *preserve_ptr)
1921 struct buffer *base_buffer;
1922 Lisp_Object temp;
1924 XSETFASTINT (temp, start);
1925 if (!NILP (BVAR (current_buffer, read_only)))
1926 Fbarf_if_buffer_read_only (temp);
1928 run_undoable_change();
1930 bset_redisplay (current_buffer);
1932 if (buffer_intervals (current_buffer))
1934 if (preserve_ptr)
1936 Lisp_Object preserve_marker;
1937 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1938 verify_interval_modification (current_buffer, start, end);
1939 *preserve_ptr = marker_position (preserve_marker);
1940 unchain_marker (XMARKER (preserve_marker));
1942 else
1943 verify_interval_modification (current_buffer, start, end);
1946 /* For indirect buffers, use the base buffer to check clashes. */
1947 if (current_buffer->base_buffer != 0)
1948 base_buffer = current_buffer->base_buffer;
1949 else
1950 base_buffer = current_buffer;
1952 if (inhibit_modification_hooks)
1953 return;
1955 if (!NILP (BVAR (base_buffer, file_truename))
1956 /* Make binding buffer-file-name to nil effective. */
1957 && !NILP (BVAR (base_buffer, filename))
1958 && SAVE_MODIFF >= MODIFF)
1959 lock_file (BVAR (base_buffer, file_truename));
1961 /* If `select-active-regions' is non-nil, save the region text. */
1962 /* FIXME: Move this to Elisp (via before-change-functions). */
1963 if (!NILP (BVAR (current_buffer, mark_active))
1964 && XMARKER (BVAR (current_buffer, mark))->buffer
1965 && NILP (Vsaved_region_selection)
1966 && (EQ (Vselect_active_regions, Qonly)
1967 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1968 : (!NILP (Vselect_active_regions)
1969 && !NILP (Vtransient_mark_mode))))
1970 Vsaved_region_selection
1971 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1973 signal_before_change (start, end, preserve_ptr);
1974 Fset (Qdeactivate_mark, Qt);
1977 /* Like above, but called when we know that the buffer text
1978 will be modified and region caches should be invalidated. */
1980 void
1981 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1982 ptrdiff_t *preserve_ptr)
1984 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1985 invalidate_buffer_caches (current_buffer, start, end);
1988 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1989 region between buffer positions START and END. */
1990 void
1991 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1993 /* Indirect buffers usually have their caches set to NULL, but we
1994 need to consider the caches of their base buffer. */
1995 if (buf->base_buffer)
1996 buf = buf->base_buffer;
1997 /* The bidi_paragraph_cache must be invalidated first, because doing
1998 so might need to use the newline_cache (via find_newline_no_quit,
1999 see below). */
2000 if (buf->bidi_paragraph_cache)
2002 if (start != end
2003 && start > BUF_BEG (buf))
2005 /* If we are deleting or replacing characters, we could
2006 create a paragraph start, because all of the characters
2007 from START to the beginning of START's line are
2008 whitespace. Therefore, we must extend the region to be
2009 invalidated up to the newline before START. */
2010 ptrdiff_t line_beg = start;
2011 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
2013 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
2015 struct buffer *old = current_buffer;
2017 set_buffer_internal (buf);
2019 line_beg = find_newline_no_quit (start, start_byte, -1,
2020 &start_byte);
2021 set_buffer_internal (old);
2023 start = line_beg - (line_beg > BUF_BEG (buf));
2025 invalidate_region_cache (buf,
2026 buf->bidi_paragraph_cache,
2027 start - BUF_BEG (buf), BUF_Z (buf) - end);
2029 if (buf->newline_cache)
2030 invalidate_region_cache (buf,
2031 buf->newline_cache,
2032 start - BUF_BEG (buf), BUF_Z (buf) - end);
2033 if (buf->width_run_cache)
2034 invalidate_region_cache (buf,
2035 buf->width_run_cache,
2036 start - BUF_BEG (buf), BUF_Z (buf) - end);
2039 /* These macros work with an argument named `preserve_ptr'
2040 and a local variable named `preserve_marker'. */
2042 #define PRESERVE_VALUE \
2043 if (preserve_ptr && NILP (preserve_marker)) \
2044 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2046 #define RESTORE_VALUE \
2047 if (! NILP (preserve_marker)) \
2049 *preserve_ptr = marker_position (preserve_marker); \
2050 unchain_marker (XMARKER (preserve_marker)); \
2053 #define PRESERVE_START_END \
2054 if (NILP (start_marker)) \
2055 start_marker = Fcopy_marker (start, Qnil); \
2056 if (NILP (end_marker)) \
2057 end_marker = Fcopy_marker (end, Qnil);
2059 #define FETCH_START \
2060 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2062 #define FETCH_END \
2063 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2065 /* Set a variable to nil if an error occurred.
2066 Don't change the variable if there was no error.
2067 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2068 VARIABLE is the variable to maybe set to nil.
2069 NO-ERROR-FLAG is nil if there was an error,
2070 anything else meaning no error (so this function does nothing). */
2071 struct rvoe_arg
2073 Lisp_Object *location;
2074 bool errorp;
2077 static void
2078 reset_var_on_error (void *ptr)
2080 struct rvoe_arg *p = ptr;
2081 if (p->errorp)
2082 *p->location = Qnil;
2085 /* Signal a change to the buffer immediately before it happens.
2086 START_INT and END_INT are the bounds of the text to be changed.
2088 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2089 by holding its value temporarily in a marker. */
2091 static void
2092 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
2093 ptrdiff_t *preserve_ptr)
2095 Lisp_Object start, end;
2096 Lisp_Object start_marker, end_marker;
2097 Lisp_Object preserve_marker;
2098 ptrdiff_t count = SPECPDL_INDEX ();
2099 struct rvoe_arg rvoe_arg;
2101 start = make_number (start_int);
2102 end = make_number (end_int);
2103 preserve_marker = Qnil;
2104 start_marker = Qnil;
2105 end_marker = Qnil;
2107 specbind (Qinhibit_modification_hooks, Qt);
2109 /* If buffer is unmodified, run a special hook for that case. The
2110 check for Vfirst_change_hook is just a minor optimization. */
2111 if (SAVE_MODIFF >= MODIFF
2112 && !NILP (Vfirst_change_hook))
2114 PRESERVE_VALUE;
2115 PRESERVE_START_END;
2116 run_hook (Qfirst_change_hook);
2119 /* Now run the before-change-functions if any. */
2120 if (!NILP (Vbefore_change_functions))
2122 rvoe_arg.location = &Vbefore_change_functions;
2123 rvoe_arg.errorp = 1;
2125 PRESERVE_VALUE;
2126 PRESERVE_START_END;
2128 /* Mark before-change-functions to be reset to nil in case of error. */
2129 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2131 /* Actually run the hook functions. */
2132 CALLN (Frun_hook_with_args, Qbefore_change_functions,
2133 FETCH_START, FETCH_END);
2135 /* There was no error: unarm the reset_on_error. */
2136 rvoe_arg.errorp = 0;
2139 if (buffer_has_overlays ())
2141 PRESERVE_VALUE;
2142 report_overlay_modification (FETCH_START, FETCH_END, 0,
2143 FETCH_START, FETCH_END, Qnil);
2146 if (! NILP (start_marker))
2147 free_marker (start_marker);
2148 if (! NILP (end_marker))
2149 free_marker (end_marker);
2150 RESTORE_VALUE;
2152 unbind_to (count, Qnil);
2155 /* Signal a change immediately after it happens.
2156 CHARPOS is the character position of the start of the changed text.
2157 LENDEL is the number of characters of the text before the change.
2158 (Not the whole buffer; just the part that was changed.)
2159 LENINS is the number of characters in that part of the text
2160 after the change. */
2162 void
2163 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2165 ptrdiff_t count = SPECPDL_INDEX ();
2166 struct rvoe_arg rvoe_arg;
2168 if (inhibit_modification_hooks)
2169 return;
2171 /* If we are deferring calls to the after-change functions
2172 and there are no before-change functions,
2173 just record the args that we were going to use. */
2174 if (! NILP (Vcombine_after_change_calls)
2175 && NILP (Vbefore_change_functions)
2176 && !buffer_has_overlays ())
2178 Lisp_Object elt;
2180 if (!NILP (combine_after_change_list)
2181 && current_buffer != XBUFFER (combine_after_change_buffer))
2182 Fcombine_after_change_execute ();
2184 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2185 lenins - lendel);
2186 combine_after_change_list
2187 = Fcons (elt, combine_after_change_list);
2188 combine_after_change_buffer = Fcurrent_buffer ();
2190 return;
2193 if (!NILP (combine_after_change_list))
2194 Fcombine_after_change_execute ();
2196 specbind (Qinhibit_modification_hooks, Qt);
2198 if (!NILP (Vafter_change_functions))
2200 rvoe_arg.location = &Vafter_change_functions;
2201 rvoe_arg.errorp = 1;
2203 /* Mark after-change-functions to be reset to nil in case of error. */
2204 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2206 /* Actually run the hook functions. */
2207 CALLN (Frun_hook_with_args, Qafter_change_functions,
2208 make_number (charpos), make_number (charpos + lenins),
2209 make_number (lendel));
2211 /* There was no error: unarm the reset_on_error. */
2212 rvoe_arg.errorp = 0;
2215 if (buffer_has_overlays ())
2216 report_overlay_modification (make_number (charpos),
2217 make_number (charpos + lenins),
2219 make_number (charpos),
2220 make_number (charpos + lenins),
2221 make_number (lendel));
2223 /* After an insertion, call the text properties
2224 insert-behind-hooks or insert-in-front-hooks. */
2225 if (lendel == 0)
2226 report_interval_modification (make_number (charpos),
2227 make_number (charpos + lenins));
2229 unbind_to (count, Qnil);
2232 static void
2233 Fcombine_after_change_execute_1 (Lisp_Object val)
2235 Vcombine_after_change_calls = val;
2238 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2239 Scombine_after_change_execute, 0, 0, 0,
2240 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2241 (void)
2243 ptrdiff_t count = SPECPDL_INDEX ();
2244 ptrdiff_t beg, end, change;
2245 ptrdiff_t begpos, endpos;
2246 Lisp_Object tail;
2248 if (NILP (combine_after_change_list))
2249 return Qnil;
2251 /* It is rare for combine_after_change_buffer to be invalid, but
2252 possible. It can happen when combine-after-change-calls is
2253 non-nil, and insertion calls a file handler (e.g. through
2254 lock_file) which scribbles into a temp file -- cyd */
2255 if (!BUFFERP (combine_after_change_buffer)
2256 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2258 combine_after_change_list = Qnil;
2259 return Qnil;
2262 record_unwind_current_buffer ();
2264 Fset_buffer (combine_after_change_buffer);
2266 /* # chars unchanged at beginning of buffer. */
2267 beg = Z - BEG;
2268 /* # chars unchanged at end of buffer. */
2269 end = beg;
2270 /* Total amount of insertion (negative for deletion). */
2271 change = 0;
2273 /* Scan the various individual changes,
2274 accumulating the range info in BEG, END and CHANGE. */
2275 for (tail = combine_after_change_list; CONSP (tail);
2276 tail = XCDR (tail))
2278 Lisp_Object elt;
2279 ptrdiff_t thisbeg, thisend, thischange;
2281 /* Extract the info from the next element. */
2282 elt = XCAR (tail);
2283 if (! CONSP (elt))
2284 continue;
2285 thisbeg = XINT (XCAR (elt));
2287 elt = XCDR (elt);
2288 if (! CONSP (elt))
2289 continue;
2290 thisend = XINT (XCAR (elt));
2292 elt = XCDR (elt);
2293 if (! CONSP (elt))
2294 continue;
2295 thischange = XINT (XCAR (elt));
2297 /* Merge this range into the accumulated range. */
2298 change += thischange;
2299 if (thisbeg < beg)
2300 beg = thisbeg;
2301 if (thisend < end)
2302 end = thisend;
2305 /* Get the current start and end positions of the range
2306 that was changed. */
2307 begpos = BEG + beg;
2308 endpos = Z - end;
2310 /* We are about to handle these, so discard them. */
2311 combine_after_change_list = Qnil;
2313 /* Now run the after-change functions for real.
2314 Turn off the flag that defers them. */
2315 record_unwind_protect (Fcombine_after_change_execute_1,
2316 Vcombine_after_change_calls);
2317 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2318 update_compositions (begpos, endpos, CHECK_ALL);
2320 return unbind_to (count, Qnil);
2323 void
2324 syms_of_insdel (void)
2326 staticpro (&combine_after_change_list);
2327 staticpro (&combine_after_change_buffer);
2328 combine_after_change_list = Qnil;
2329 combine_after_change_buffer = Qnil;
2331 DEFSYM (Qundo_auto__undoable_change, "undo-auto--undoable-change");
2333 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2334 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2335 Vcombine_after_change_calls = Qnil;
2337 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2338 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2339 This affects `before-change-functions' and `after-change-functions',
2340 as well as hooks attached to text properties and overlays. */);
2341 inhibit_modification_hooks = 0;
2342 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2344 DEFSYM (Qregion_extract_function, "region-extract-function");
2346 defsubr (&Scombine_after_change_execute);