Detect remote uid and gid in tramp-gvfs.el
[emacs.git] / src / insdel.c
blob4ad1074f5f79bf94e7c47f72f92b5aa0132bcd5e
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2016 Free Software
3 Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (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 prevent QUIT by setting 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 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 prevent QUIT by setting 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 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 ();
368 void
369 buffer_overflow (void)
371 error ("Maximum buffer size exceeded");
374 /* Make the gap NBYTES_ADDED bytes longer. */
376 static void
377 make_gap_larger (ptrdiff_t nbytes_added)
379 Lisp_Object tem;
380 ptrdiff_t real_gap_loc;
381 ptrdiff_t real_gap_loc_byte;
382 ptrdiff_t old_gap_size;
383 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
385 if (BUF_BYTES_MAX - current_size < nbytes_added)
386 buffer_overflow ();
388 /* If we have to get more space, get enough to last a while;
389 but do not exceed the maximum buffer size. */
390 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
391 BUF_BYTES_MAX - current_size);
393 enlarge_buffer_text (current_buffer, nbytes_added);
395 /* Prevent quitting in gap_left. We cannot allow a QUIT there,
396 because that would leave the buffer text in an inconsistent
397 state, with 2 gap holes instead of just one. */
398 tem = Vinhibit_quit;
399 Vinhibit_quit = Qt;
401 real_gap_loc = GPT;
402 real_gap_loc_byte = GPT_BYTE;
403 old_gap_size = GAP_SIZE;
405 /* Call the newly allocated space a gap at the end of the whole space. */
406 GPT = Z + GAP_SIZE;
407 GPT_BYTE = Z_BYTE + GAP_SIZE;
408 GAP_SIZE = nbytes_added;
410 /* Move the new gap down to be consecutive with the end of the old one. */
411 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
413 /* Now combine the two into one large gap. */
414 GAP_SIZE += old_gap_size;
415 GPT = real_gap_loc;
416 GPT_BYTE = real_gap_loc_byte;
418 /* Put an anchor. */
419 *(Z_ADDR) = 0;
421 Vinhibit_quit = tem;
424 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
426 /* Make the gap NBYTES_REMOVED bytes shorter. */
428 static void
429 make_gap_smaller (ptrdiff_t nbytes_removed)
431 Lisp_Object tem;
432 ptrdiff_t real_gap_loc;
433 ptrdiff_t real_gap_loc_byte;
434 ptrdiff_t real_Z;
435 ptrdiff_t real_Z_byte;
436 ptrdiff_t real_beg_unchanged;
437 ptrdiff_t new_gap_size;
439 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
440 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
441 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
443 /* Prevent quitting in gap_right. We cannot allow a QUIT there,
444 because that would leave the buffer text in an inconsistent
445 state, with 2 gap holes instead of just one. */
446 tem = Vinhibit_quit;
447 Vinhibit_quit = Qt;
449 real_gap_loc = GPT;
450 real_gap_loc_byte = GPT_BYTE;
451 new_gap_size = GAP_SIZE - nbytes_removed;
452 real_Z = Z;
453 real_Z_byte = Z_BYTE;
454 real_beg_unchanged = BEG_UNCHANGED;
456 /* Pretend that the last unwanted part of the gap is the entire gap,
457 and that the first desired part of the gap is part of the buffer
458 text. */
459 memset (GPT_ADDR, 0, new_gap_size);
460 GPT += new_gap_size;
461 GPT_BYTE += new_gap_size;
462 Z += new_gap_size;
463 Z_BYTE += new_gap_size;
464 GAP_SIZE = nbytes_removed;
466 /* Move the unwanted pretend gap to the end of the buffer. */
467 gap_right (Z, Z_BYTE);
469 enlarge_buffer_text (current_buffer, -nbytes_removed);
471 /* Now restore the desired gap. */
472 GAP_SIZE = new_gap_size;
473 GPT = real_gap_loc;
474 GPT_BYTE = real_gap_loc_byte;
475 Z = real_Z;
476 Z_BYTE = real_Z_byte;
477 BEG_UNCHANGED = real_beg_unchanged;
479 /* Put an anchor. */
480 *(Z_ADDR) = 0;
482 Vinhibit_quit = tem;
485 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
487 void
488 make_gap (ptrdiff_t nbytes_added)
490 if (nbytes_added >= 0)
491 make_gap_larger (nbytes_added);
492 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
493 else
494 make_gap_smaller (-nbytes_added);
495 #endif
498 /* Add NBYTES to B's gap. It's enough to temporarily
499 fake current_buffer and avoid real switch to B. */
501 void
502 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
504 struct buffer *oldb = current_buffer;
506 current_buffer = b;
507 make_gap (nbytes);
508 current_buffer = oldb;
511 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
512 FROM_MULTIBYTE says whether the incoming text is multibyte.
513 TO_MULTIBYTE says whether to store the text as multibyte.
514 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
516 Return the number of bytes stored at TO_ADDR. */
518 ptrdiff_t
519 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
520 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
522 if (from_multibyte == to_multibyte)
524 memcpy (to_addr, from_addr, nbytes);
525 return nbytes;
527 else if (from_multibyte)
529 ptrdiff_t nchars = 0;
530 ptrdiff_t bytes_left = nbytes;
532 while (bytes_left > 0)
534 int thislen, c;
535 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
536 if (! ASCII_CHAR_P (c))
537 c &= 0xFF;
538 *to_addr++ = c;
539 from_addr += thislen;
540 bytes_left -= thislen;
541 nchars++;
543 return nchars;
545 else
547 unsigned char *initial_to_addr = to_addr;
549 /* Convert single-byte to multibyte. */
550 while (nbytes > 0)
552 int c = *from_addr++;
554 if (!ASCII_CHAR_P (c))
556 c = BYTE8_TO_CHAR (c);
557 to_addr += CHAR_STRING (c, to_addr);
558 nbytes--;
560 else
561 /* Special case for speed. */
562 *to_addr++ = c, nbytes--;
564 return to_addr - initial_to_addr;
568 /* Insert a string of specified length before point.
569 This function judges multibyteness based on
570 enable_multibyte_characters in the current buffer;
571 it never converts between single-byte and multibyte.
573 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
574 prepare_to_modify_buffer could relocate the text. */
576 void
577 insert (const char *string, ptrdiff_t nbytes)
579 if (nbytes > 0)
581 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
582 insert_1_both (string, len, nbytes, 0, 1, 0);
583 opoint = PT - len;
584 signal_after_change (opoint, 0, len);
585 update_compositions (opoint, PT, CHECK_BORDER);
589 /* Likewise, but inherit text properties from neighboring characters. */
591 void
592 insert_and_inherit (const char *string, ptrdiff_t nbytes)
594 if (nbytes > 0)
596 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
597 insert_1_both (string, len, nbytes, 1, 1, 0);
598 opoint = PT - len;
599 signal_after_change (opoint, 0, len);
600 update_compositions (opoint, PT, CHECK_BORDER);
604 /* Insert the character C before point. Do not inherit text properties. */
606 void
607 insert_char (int c)
609 unsigned char str[MAX_MULTIBYTE_LENGTH];
610 int len;
612 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
613 len = CHAR_STRING (c, str);
614 else
616 len = 1;
617 str[0] = c;
620 insert ((char *) str, len);
623 /* Insert the null-terminated string S before point. */
625 void
626 insert_string (const char *s)
628 insert (s, strlen (s));
631 /* Like `insert' except that all markers pointing at the place where
632 the insertion happens are adjusted to point after it.
633 Don't use this function to insert part of a Lisp string,
634 since gc could happen and relocate it. */
636 void
637 insert_before_markers (const char *string, ptrdiff_t nbytes)
639 if (nbytes > 0)
641 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
642 insert_1_both (string, len, nbytes, 0, 1, 1);
643 opoint = PT - len;
644 signal_after_change (opoint, 0, len);
645 update_compositions (opoint, PT, CHECK_BORDER);
649 /* Likewise, but inherit text properties from neighboring characters. */
651 void
652 insert_before_markers_and_inherit (const char *string,
653 ptrdiff_t nbytes)
655 if (nbytes > 0)
657 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
658 insert_1_both (string, len, nbytes, 1, 1, 1);
659 opoint = PT - len;
660 signal_after_change (opoint, 0, len);
661 update_compositions (opoint, PT, CHECK_BORDER);
665 #ifdef BYTE_COMBINING_DEBUG
667 /* See if the bytes before POS/POS_BYTE combine with bytes
668 at the start of STRING to form a single character.
669 If so, return the number of bytes at the start of STRING
670 which combine in this way. Otherwise, return 0. */
673 count_combining_before (const unsigned char *string, ptrdiff_t length,
674 ptrdiff_t pos, ptrdiff_t pos_byte)
676 int len, combining_bytes;
677 const unsigned char *p;
679 if (NILP (current_buffer->enable_multibyte_characters))
680 return 0;
682 /* At first, we can exclude the following cases:
683 (1) STRING[0] can't be a following byte of multibyte sequence.
684 (2) POS is the start of the current buffer.
685 (3) A character before POS is not a multibyte character. */
686 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
687 return 0;
688 if (pos_byte == BEG_BYTE) /* case (2) */
689 return 0;
690 len = 1;
691 p = BYTE_POS_ADDR (pos_byte - 1);
692 while (! CHAR_HEAD_P (*p)) p--, len++;
693 if (! LEADING_CODE_P (*p)) /* case (3) */
694 return 0;
696 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
697 if (combining_bytes <= 0)
698 /* The character preceding POS is, complete and no room for
699 combining bytes (combining_bytes == 0), or an independent 8-bit
700 character (combining_bytes < 0). */
701 return 0;
703 /* We have a combination situation. Count the bytes at STRING that
704 may combine. */
705 p = string + 1;
706 while (!CHAR_HEAD_P (*p) && p < string + length)
707 p++;
709 return (combining_bytes < p - string ? combining_bytes : p - string);
712 /* See if the bytes after POS/POS_BYTE combine with bytes
713 at the end of STRING to form a single character.
714 If so, return the number of bytes after POS/POS_BYTE
715 which combine in this way. Otherwise, return 0. */
718 count_combining_after (const unsigned char *string,
719 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
721 ptrdiff_t opos_byte = pos_byte;
722 ptrdiff_t i;
723 ptrdiff_t bytes;
724 unsigned char *bufp;
726 if (NILP (current_buffer->enable_multibyte_characters))
727 return 0;
729 /* At first, we can exclude the following cases:
730 (1) The last byte of STRING is an ASCII.
731 (2) POS is the last of the current buffer.
732 (3) A character at POS can't be a following byte of multibyte
733 character. */
734 if (length > 0 && ASCII_CHAR_P (string[length - 1])) /* case (1) */
735 return 0;
736 if (pos_byte == Z_BYTE) /* case (2) */
737 return 0;
738 bufp = BYTE_POS_ADDR (pos_byte);
739 if (CHAR_HEAD_P (*bufp)) /* case (3) */
740 return 0;
742 i = length - 1;
743 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
745 i--;
747 if (i < 0)
749 /* All characters in STRING are not character head. We must
750 check also preceding bytes at POS. We are sure that the gap
751 is at POS. */
752 unsigned char *p = BEG_ADDR;
753 i = pos_byte - 2;
754 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
755 i--;
756 if (i < 0 || !LEADING_CODE_P (p[i]))
757 return 0;
759 bytes = BYTES_BY_CHAR_HEAD (p[i]);
760 return (bytes <= pos_byte - 1 - i + length
762 : bytes - (pos_byte - 1 - i + length));
764 if (!LEADING_CODE_P (string[i]))
765 return 0;
767 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
768 bufp++, pos_byte++;
769 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
771 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
774 #endif
777 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
778 starting at STRING. INHERIT non-zero means inherit the text
779 properties from neighboring characters; zero means inserted text
780 will have no text properties. PREPARE non-zero means call
781 prepare_to_modify_buffer, which checks that the region is not
782 read-only, and calls before-change-function and any modification
783 properties the text may have. BEFORE_MARKERS non-zero means adjust
784 all markers that point at the insertion place to point after it. */
786 void
787 insert_1_both (const char *string,
788 ptrdiff_t nchars, ptrdiff_t nbytes,
789 bool inherit, bool prepare, bool before_markers)
791 if (nchars == 0)
792 return;
794 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
795 nchars = nbytes;
797 if (prepare)
798 /* Do this before moving and increasing the gap,
799 because the before-change hooks might move the gap
800 or make it smaller. */
801 prepare_to_modify_buffer (PT, PT, NULL);
803 if (PT != GPT)
804 move_gap_both (PT, PT_BYTE);
805 if (GAP_SIZE < nbytes)
806 make_gap (nbytes - GAP_SIZE);
808 #ifdef BYTE_COMBINING_DEBUG
809 if (count_combining_before (string, nbytes, PT, PT_BYTE)
810 || count_combining_after (string, nbytes, PT, PT_BYTE))
811 emacs_abort ();
812 #endif
814 /* Record deletion of the surrounding text that combines with
815 the insertion. This, together with recording the insertion,
816 will add up to the right stuff in the undo list. */
817 record_insert (PT, nchars);
818 MODIFF++;
819 CHARS_MODIFF = MODIFF;
821 memcpy (GPT_ADDR, string, nbytes);
823 GAP_SIZE -= nbytes;
824 GPT += nchars;
825 ZV += nchars;
826 Z += nchars;
827 GPT_BYTE += nbytes;
828 ZV_BYTE += nbytes;
829 Z_BYTE += nbytes;
830 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
832 eassert (GPT <= GPT_BYTE);
834 /* The insert may have been in the unchanged region, so check again. */
835 if (Z - GPT < END_UNCHANGED)
836 END_UNCHANGED = Z - GPT;
838 adjust_overlays_for_insert (PT, nchars);
839 adjust_markers_for_insert (PT, PT_BYTE,
840 PT + nchars, PT_BYTE + nbytes,
841 before_markers);
843 offset_intervals (current_buffer, PT, nchars);
845 if (!inherit && buffer_intervals (current_buffer))
846 set_text_properties (make_number (PT), make_number (PT + nchars),
847 Qnil, Qnil, Qnil);
849 adjust_point (nchars, nbytes);
851 check_markers ();
854 /* Insert the part of the text of STRING, a Lisp object assumed to be
855 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
856 starting at position POS / POS_BYTE. If the text of STRING has properties,
857 copy them into the buffer.
859 It does not work to use `insert' for this, because a GC could happen
860 before we copy the stuff into the buffer, and relocate the string
861 without insert noticing. */
863 void
864 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
865 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
867 ptrdiff_t opoint = PT;
869 if (SCHARS (string) == 0)
870 return;
872 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
873 inherit, 0);
874 signal_after_change (opoint, 0, PT - opoint);
875 update_compositions (opoint, PT, CHECK_BORDER);
878 /* Like `insert_from_string' except that all markers pointing
879 at the place where the insertion happens are adjusted to point after it. */
881 void
882 insert_from_string_before_markers (Lisp_Object string,
883 ptrdiff_t pos, ptrdiff_t pos_byte,
884 ptrdiff_t length, ptrdiff_t length_byte,
885 bool inherit)
887 ptrdiff_t opoint = PT;
889 if (SCHARS (string) == 0)
890 return;
892 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
893 inherit, 1);
894 signal_after_change (opoint, 0, PT - opoint);
895 update_compositions (opoint, PT, CHECK_BORDER);
898 /* Subroutine of the insertion functions above. */
900 static void
901 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
902 ptrdiff_t nchars, ptrdiff_t nbytes,
903 bool inherit, bool before_markers)
905 ptrdiff_t outgoing_nbytes = nbytes;
906 INTERVAL intervals;
908 /* Make OUTGOING_NBYTES describe the text
909 as it will be inserted in this buffer. */
911 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
912 outgoing_nbytes = nchars;
913 else if (! STRING_MULTIBYTE (string))
914 outgoing_nbytes
915 = count_size_as_multibyte (SDATA (string) + pos_byte,
916 nbytes);
918 /* Do this before moving and increasing the gap,
919 because the before-change hooks might move the gap
920 or make it smaller. */
921 prepare_to_modify_buffer (PT, PT, NULL);
923 if (PT != GPT)
924 move_gap_both (PT, PT_BYTE);
925 if (GAP_SIZE < outgoing_nbytes)
926 make_gap (outgoing_nbytes - GAP_SIZE);
928 /* Copy the string text into the buffer, perhaps converting
929 between single-byte and multibyte. */
930 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
931 STRING_MULTIBYTE (string),
932 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
934 #ifdef BYTE_COMBINING_DEBUG
935 /* We have copied text into the gap, but we have not altered
936 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
937 to these functions and get the same results as we would
938 have got earlier on. Meanwhile, PT_ADDR does point to
939 the text that has been stored by copy_text. */
940 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
941 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
942 emacs_abort ();
943 #endif
945 record_insert (PT, nchars);
946 MODIFF++;
947 CHARS_MODIFF = MODIFF;
949 GAP_SIZE -= outgoing_nbytes;
950 GPT += nchars;
951 ZV += nchars;
952 Z += nchars;
953 GPT_BYTE += outgoing_nbytes;
954 ZV_BYTE += outgoing_nbytes;
955 Z_BYTE += outgoing_nbytes;
956 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
958 eassert (GPT <= GPT_BYTE);
960 /* The insert may have been in the unchanged region, so check again. */
961 if (Z - GPT < END_UNCHANGED)
962 END_UNCHANGED = Z - GPT;
964 adjust_overlays_for_insert (PT, nchars);
965 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
966 PT_BYTE + outgoing_nbytes,
967 before_markers);
969 offset_intervals (current_buffer, PT, nchars);
971 intervals = string_intervals (string);
972 /* Get the intervals for the part of the string we are inserting. */
973 if (nbytes < SBYTES (string))
974 intervals = copy_intervals (intervals, pos, nchars);
976 /* Insert those intervals. */
977 graft_intervals_into_buffer (intervals, PT, nchars,
978 current_buffer, inherit);
980 adjust_point (nchars, outgoing_nbytes);
982 check_markers ();
985 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
986 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
987 GPT_ADDR (if not text_at_gap_tail). */
989 void
990 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
992 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
994 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
995 nchars = nbytes;
997 /* No need to call prepare_to_modify_buffer, since this is called
998 from places that replace some region with a different text, so
999 prepare_to_modify_buffer was already called by the deletion part
1000 of this dance. */
1001 invalidate_buffer_caches (current_buffer, GPT, GPT);
1002 record_insert (GPT, nchars);
1003 MODIFF++;
1005 GAP_SIZE -= nbytes;
1006 if (! text_at_gap_tail)
1008 GPT += nchars;
1009 GPT_BYTE += nbytes;
1011 ZV += nchars;
1012 Z += nchars;
1013 ZV_BYTE += nbytes;
1014 Z_BYTE += nbytes;
1015 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1017 eassert (GPT <= GPT_BYTE);
1019 adjust_overlays_for_insert (ins_charpos, nchars);
1020 adjust_markers_for_insert (ins_charpos, ins_bytepos,
1021 ins_charpos + nchars, ins_bytepos + nbytes, 0);
1023 if (buffer_intervals (current_buffer))
1025 offset_intervals (current_buffer, ins_charpos, nchars);
1026 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1027 current_buffer, 0);
1030 if (ins_charpos < PT)
1031 adjust_point (nchars, nbytes);
1033 check_markers ();
1036 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1037 current buffer. If the text in BUF has properties, they are absorbed
1038 into the current buffer.
1040 It does not work to use `insert' for this, because a malloc could happen
1041 and relocate BUF's text before the copy happens. */
1043 void
1044 insert_from_buffer (struct buffer *buf,
1045 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1047 ptrdiff_t opoint = PT;
1049 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1050 signal_after_change (opoint, 0, PT - opoint);
1051 update_compositions (opoint, PT, CHECK_BORDER);
1054 static void
1055 insert_from_buffer_1 (struct buffer *buf,
1056 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1058 ptrdiff_t chunk, chunk_expanded;
1059 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1060 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1061 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1062 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1063 INTERVAL intervals;
1065 if (nchars == 0)
1066 return;
1068 /* Make OUTGOING_NBYTES describe the text
1069 as it will be inserted in this buffer. */
1071 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1072 outgoing_nbytes = nchars;
1073 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1075 ptrdiff_t outgoing_before_gap = 0;
1076 ptrdiff_t outgoing_after_gap = 0;
1078 if (from < BUF_GPT (buf))
1080 chunk = BUF_GPT_BYTE (buf) - from_byte;
1081 if (chunk > incoming_nbytes)
1082 chunk = incoming_nbytes;
1083 outgoing_before_gap
1084 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1085 chunk);
1087 else
1088 chunk = 0;
1090 if (chunk < incoming_nbytes)
1091 outgoing_after_gap
1092 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1093 from_byte + chunk),
1094 incoming_nbytes - chunk);
1096 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1099 /* Do this before moving and increasing the gap,
1100 because the before-change hooks might move the gap
1101 or make it smaller. */
1102 prepare_to_modify_buffer (PT, PT, NULL);
1104 if (PT != GPT)
1105 move_gap_both (PT, PT_BYTE);
1106 if (GAP_SIZE < outgoing_nbytes)
1107 make_gap (outgoing_nbytes - GAP_SIZE);
1109 if (from < BUF_GPT (buf))
1111 chunk = BUF_GPT_BYTE (buf) - from_byte;
1112 if (chunk > incoming_nbytes)
1113 chunk = incoming_nbytes;
1114 /* Record number of output bytes, so we know where
1115 to put the output from the second copy_text. */
1116 chunk_expanded
1117 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1118 GPT_ADDR, chunk,
1119 ! NILP (BVAR (buf, enable_multibyte_characters)),
1120 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1122 else
1123 chunk_expanded = chunk = 0;
1125 if (chunk < incoming_nbytes)
1126 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1127 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1128 ! NILP (BVAR (buf, enable_multibyte_characters)),
1129 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1131 #ifdef BYTE_COMBINING_DEBUG
1132 /* We have copied text into the gap, but we have not altered
1133 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1134 to these functions and get the same results as we would
1135 have got earlier on. Meanwhile, GPT_ADDR does point to
1136 the text that has been stored by copy_text. */
1137 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1138 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1139 emacs_abort ();
1140 #endif
1142 record_insert (PT, nchars);
1143 MODIFF++;
1144 CHARS_MODIFF = MODIFF;
1146 GAP_SIZE -= outgoing_nbytes;
1147 GPT += nchars;
1148 ZV += nchars;
1149 Z += nchars;
1150 GPT_BYTE += outgoing_nbytes;
1151 ZV_BYTE += outgoing_nbytes;
1152 Z_BYTE += outgoing_nbytes;
1153 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1155 eassert (GPT <= GPT_BYTE);
1157 /* The insert may have been in the unchanged region, so check again. */
1158 if (Z - GPT < END_UNCHANGED)
1159 END_UNCHANGED = Z - GPT;
1161 adjust_overlays_for_insert (PT, nchars);
1162 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1163 PT_BYTE + outgoing_nbytes,
1166 offset_intervals (current_buffer, PT, nchars);
1168 /* Get the intervals for the part of the string we are inserting. */
1169 intervals = buffer_intervals (buf);
1170 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1172 if (buf == current_buffer && PT <= from)
1173 from += nchars;
1174 intervals = copy_intervals (intervals, from, nchars);
1177 /* Insert those intervals. */
1178 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1180 adjust_point (nchars, outgoing_nbytes);
1183 /* Record undo information and adjust markers and position keepers for
1184 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1185 chars (LEN_BYTE bytes) which resides in the gap just after
1186 GPT_ADDR.
1188 PREV_TEXT nil means the new text was just inserted. */
1190 static void
1191 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1192 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1194 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1196 #ifdef BYTE_COMBINING_DEBUG
1197 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1198 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1199 emacs_abort ();
1200 #endif
1202 if (STRINGP (prev_text))
1204 nchars_del = SCHARS (prev_text);
1205 nbytes_del = SBYTES (prev_text);
1208 /* Update various buffer positions for the new text. */
1209 GAP_SIZE -= len_byte;
1210 ZV += len; Z += len;
1211 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1212 GPT += len; GPT_BYTE += len_byte;
1213 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1215 if (nchars_del > 0)
1216 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1217 len, len_byte);
1218 else
1219 adjust_markers_for_insert (from, from_byte,
1220 from + len, from_byte + len_byte, 0);
1222 if (nchars_del > 0)
1223 record_delete (from, prev_text, false);
1224 record_insert (from, len);
1226 if (len > nchars_del)
1227 adjust_overlays_for_insert (from, len - nchars_del);
1228 else if (len < nchars_del)
1229 adjust_overlays_for_delete (from, nchars_del - len);
1231 offset_intervals (current_buffer, from, len - nchars_del);
1233 if (from < PT)
1234 adjust_point (len - nchars_del, len_byte - nbytes_del);
1236 /* As byte combining will decrease Z, we must check this again. */
1237 if (Z - GPT < END_UNCHANGED)
1238 END_UNCHANGED = Z - GPT;
1240 check_markers ();
1242 if (len == 0)
1243 evaporate_overlays (from);
1244 MODIFF++;
1245 CHARS_MODIFF = MODIFF;
1248 /* Record undo information, adjust markers and position keepers for an
1249 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1250 text already exists in the current buffer but character length (TO
1251 - FROM) may be incorrect, the correct length is NEWLEN. */
1253 void
1254 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1255 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1257 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1259 if (GPT != to)
1260 move_gap_both (to, to_byte);
1261 GAP_SIZE += len_byte;
1262 GPT -= len; GPT_BYTE -= len_byte;
1263 ZV -= len; ZV_BYTE -= len_byte;
1264 Z -= len; Z_BYTE -= len_byte;
1265 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1268 /* Replace the text from character positions FROM to TO with NEW,
1269 If PREPARE, call prepare_to_modify_buffer.
1270 If INHERIT, the newly inserted text should inherit text properties
1271 from the surrounding non-deleted text. */
1273 /* Note that this does not yet handle markers quite right.
1274 Also it needs to record a single undo-entry that does a replacement
1275 rather than a separate delete and insert.
1276 That way, undo will also handle markers properly.
1278 But if MARKERS is 0, don't relocate markers. */
1280 void
1281 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1282 bool prepare, bool inherit, bool markers)
1284 ptrdiff_t inschars = SCHARS (new);
1285 ptrdiff_t insbytes = SBYTES (new);
1286 ptrdiff_t from_byte, to_byte;
1287 ptrdiff_t nbytes_del, nchars_del;
1288 INTERVAL intervals;
1289 ptrdiff_t outgoing_insbytes = insbytes;
1290 Lisp_Object deletion;
1292 check_markers ();
1294 deletion = Qnil;
1296 if (prepare)
1298 ptrdiff_t range_length = to - from;
1299 prepare_to_modify_buffer (from, to, &from);
1300 to = from + range_length;
1303 /* Make args be valid. */
1304 if (from < BEGV)
1305 from = BEGV;
1306 if (to > ZV)
1307 to = ZV;
1309 from_byte = CHAR_TO_BYTE (from);
1310 to_byte = CHAR_TO_BYTE (to);
1312 nchars_del = to - from;
1313 nbytes_del = to_byte - from_byte;
1315 if (nbytes_del <= 0 && insbytes == 0)
1316 return;
1318 /* Make OUTGOING_INSBYTES describe the text
1319 as it will be inserted in this buffer. */
1321 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1322 outgoing_insbytes = inschars;
1323 else if (! STRING_MULTIBYTE (new))
1324 outgoing_insbytes
1325 = count_size_as_multibyte (SDATA (new), insbytes);
1327 /* Make sure the gap is somewhere in or next to what we are deleting. */
1328 if (from > GPT)
1329 gap_right (from, from_byte);
1330 if (to < GPT)
1331 gap_left (to, to_byte, 0);
1333 /* Even if we don't record for undo, we must keep the original text
1334 because we may have to recover it because of inappropriate byte
1335 combining. */
1336 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1337 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1339 GAP_SIZE += nbytes_del;
1340 ZV -= nchars_del;
1341 Z -= nchars_del;
1342 ZV_BYTE -= nbytes_del;
1343 Z_BYTE -= nbytes_del;
1344 GPT = from;
1345 GPT_BYTE = from_byte;
1346 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1348 eassert (GPT <= GPT_BYTE);
1350 if (GPT - BEG < BEG_UNCHANGED)
1351 BEG_UNCHANGED = GPT - BEG;
1352 if (Z - GPT < END_UNCHANGED)
1353 END_UNCHANGED = Z - GPT;
1355 if (GAP_SIZE < outgoing_insbytes)
1356 make_gap (outgoing_insbytes - GAP_SIZE);
1358 /* Copy the string text into the buffer, perhaps converting
1359 between single-byte and multibyte. */
1360 copy_text (SDATA (new), GPT_ADDR, insbytes,
1361 STRING_MULTIBYTE (new),
1362 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1364 #ifdef BYTE_COMBINING_DEBUG
1365 /* We have copied text into the gap, but we have not marked
1366 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1367 here, for both the previous text and the following text.
1368 Meanwhile, GPT_ADDR does point to
1369 the text that has been stored by copy_text. */
1370 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1371 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1372 emacs_abort ();
1373 #endif
1375 /* Record the insertion first, so that when we undo,
1376 the deletion will be undone first. Thus, undo
1377 will insert before deleting, and thus will keep
1378 the markers before and after this text separate. */
1379 if (!NILP (deletion))
1381 record_insert (from + SCHARS (deletion), inschars);
1382 record_delete (from, deletion, false);
1385 GAP_SIZE -= outgoing_insbytes;
1386 GPT += inschars;
1387 ZV += inschars;
1388 Z += inschars;
1389 GPT_BYTE += outgoing_insbytes;
1390 ZV_BYTE += outgoing_insbytes;
1391 Z_BYTE += outgoing_insbytes;
1392 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1394 eassert (GPT <= GPT_BYTE);
1396 /* Adjust markers for the deletion and the insertion. */
1397 if (markers)
1398 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1399 inschars, outgoing_insbytes);
1401 /* Adjust the overlay center as needed. This must be done after
1402 adjusting the markers that bound the overlays. */
1403 adjust_overlays_for_delete (from, nchars_del);
1404 adjust_overlays_for_insert (from, inschars);
1406 offset_intervals (current_buffer, from, inschars - nchars_del);
1408 /* Get the intervals for the part of the string we are inserting--
1409 not including the combined-before bytes. */
1410 intervals = string_intervals (new);
1411 /* Insert those intervals. */
1412 graft_intervals_into_buffer (intervals, from, inschars,
1413 current_buffer, inherit);
1415 /* Relocate point as if it were a marker. */
1416 if (from < PT)
1417 adjust_point ((from + inschars - (PT < to ? PT : to)),
1418 (from_byte + outgoing_insbytes
1419 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1421 if (outgoing_insbytes == 0)
1422 evaporate_overlays (from);
1424 check_markers ();
1426 MODIFF++;
1427 CHARS_MODIFF = MODIFF;
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;
1565 /* Make args be valid */
1566 if (from < BEGV)
1567 from = BEGV;
1568 if (to > ZV)
1569 to = ZV;
1571 if (to <= from)
1572 return Qnil;
1574 if (prepare)
1576 ptrdiff_t range_length = to - from;
1577 prepare_to_modify_buffer (from, to, &from);
1578 to = min (ZV, from + range_length);
1581 from_byte = CHAR_TO_BYTE (from);
1582 to_byte = CHAR_TO_BYTE (to);
1584 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1585 signal_after_change (from, to - from, 0);
1586 update_compositions (from, from, CHECK_HEAD);
1587 return deletion;
1590 /* Like del_range_1 but args are byte positions, not char positions. */
1592 void
1593 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
1595 ptrdiff_t from, to;
1597 /* Make args be valid. */
1598 if (from_byte < BEGV_BYTE)
1599 from_byte = BEGV_BYTE;
1600 if (to_byte > ZV_BYTE)
1601 to_byte = ZV_BYTE;
1603 if (to_byte <= from_byte)
1604 return;
1606 from = BYTE_TO_CHAR (from_byte);
1607 to = BYTE_TO_CHAR (to_byte);
1609 if (prepare)
1611 ptrdiff_t old_from = from, old_to = Z - to;
1612 ptrdiff_t range_length = to - from;
1613 prepare_to_modify_buffer (from, to, &from);
1614 to = from + range_length;
1616 if (old_from != from)
1617 from_byte = CHAR_TO_BYTE (from);
1618 if (to > ZV)
1620 to = ZV;
1621 to_byte = ZV_BYTE;
1623 else if (old_to == Z - to)
1624 to_byte = CHAR_TO_BYTE (to);
1627 del_range_2 (from, from_byte, to, to_byte, 0);
1628 signal_after_change (from, to - from, 0);
1629 update_compositions (from, from, CHECK_HEAD);
1632 /* Like del_range_1, but positions are specified both as charpos
1633 and bytepos. */
1635 void
1636 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1637 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1639 /* Make args be valid */
1640 if (from_byte < BEGV_BYTE)
1641 from_byte = BEGV_BYTE;
1642 if (to_byte > ZV_BYTE)
1643 to_byte = ZV_BYTE;
1645 if (to_byte <= from_byte)
1646 return;
1648 if (from < BEGV)
1649 from = BEGV;
1650 if (to > ZV)
1651 to = ZV;
1653 if (prepare)
1655 ptrdiff_t old_from = from, old_to = Z - to;
1656 ptrdiff_t range_length = to - from;
1657 prepare_to_modify_buffer (from, to, &from);
1658 to = from + range_length;
1660 if (old_from != from)
1661 from_byte = CHAR_TO_BYTE (from);
1662 if (to > ZV)
1664 to = ZV;
1665 to_byte = ZV_BYTE;
1667 else if (old_to == Z - to)
1668 to_byte = CHAR_TO_BYTE (to);
1671 del_range_2 (from, from_byte, to, to_byte, 0);
1672 signal_after_change (from, to - from, 0);
1673 update_compositions (from, from, CHECK_HEAD);
1676 /* Delete a range of text, specified both as character positions
1677 and byte positions. FROM and TO are character positions,
1678 while FROM_BYTE and TO_BYTE are byte positions.
1679 If RET_STRING, the deleted area is returned as a string. */
1681 Lisp_Object
1682 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1683 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1685 ptrdiff_t nbytes_del, nchars_del;
1686 Lisp_Object deletion;
1688 check_markers ();
1690 nchars_del = to - from;
1691 nbytes_del = to_byte - from_byte;
1693 /* Make sure the gap is somewhere in or next to what we are deleting. */
1694 if (from > GPT)
1695 gap_right (from, from_byte);
1696 if (to < GPT)
1697 gap_left (to, to_byte, 0);
1699 #ifdef BYTE_COMBINING_DEBUG
1700 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1701 Z_BYTE - to_byte, from, from_byte))
1702 emacs_abort ();
1703 #endif
1705 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1706 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1707 else
1708 deletion = Qnil;
1710 /* Record marker adjustments, and text deletion into undo
1711 history. */
1712 record_delete (from, deletion, true);
1714 /* Relocate all markers pointing into the new, larger gap to point
1715 at the end of the text before the gap. */
1716 adjust_markers_for_delete (from, from_byte, to, to_byte);
1718 MODIFF++;
1719 CHARS_MODIFF = MODIFF;
1721 /* Relocate point as if it were a marker. */
1722 if (from < PT)
1723 adjust_point (from - (PT < to ? PT : to),
1724 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1726 offset_intervals (current_buffer, from, - nchars_del);
1728 /* Adjust the overlay center as needed. This must be done after
1729 adjusting the markers that bound the overlays. */
1730 adjust_overlays_for_delete (from, nchars_del);
1732 GAP_SIZE += nbytes_del;
1733 ZV_BYTE -= nbytes_del;
1734 Z_BYTE -= nbytes_del;
1735 ZV -= nchars_del;
1736 Z -= nchars_del;
1737 GPT = from;
1738 GPT_BYTE = from_byte;
1739 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1740 /* Put an anchor, unless called from decode_coding_object which
1741 needs to access the previous gap contents. */
1742 *(GPT_ADDR) = 0;
1744 eassert (GPT <= GPT_BYTE);
1746 if (GPT - BEG < BEG_UNCHANGED)
1747 BEG_UNCHANGED = GPT - BEG;
1748 if (Z - GPT < END_UNCHANGED)
1749 END_UNCHANGED = Z - GPT;
1751 check_markers ();
1753 evaporate_overlays (from);
1755 return deletion;
1758 /* Call this if you're about to change the text of current buffer
1759 from character positions START to END. This checks the read-only
1760 properties of the region, calls the necessary modification hooks,
1761 and warns the next redisplay that it should pay attention to that
1762 area. */
1764 void
1765 modify_text (ptrdiff_t start, ptrdiff_t end)
1767 prepare_to_modify_buffer (start, end, NULL);
1769 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1770 if (MODIFF <= SAVE_MODIFF)
1771 record_first_change ();
1772 MODIFF++;
1773 CHARS_MODIFF = MODIFF;
1775 bset_point_before_scroll (current_buffer, Qnil);
1778 /* Signal that we are about to make a change that may result in new
1779 undo information.
1781 static void
1782 run_undoable_change (void)
1784 if (EQ (BVAR (current_buffer, undo_list), Qt))
1785 return;
1787 call0 (Qundo_auto__undoable_change);
1790 /* Check that it is okay to modify the buffer between START and END,
1791 which are char positions.
1793 Run the before-change-function, if any. If intervals are in use,
1794 verify that the text to be modified is not read-only, and call
1795 any modification properties the text may have.
1797 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1798 by holding its value temporarily in a marker.
1800 This function runs Lisp, which means it can GC, which means it can
1801 compact buffers, including the current buffer being worked on here.
1802 So don't you dare calling this function while manipulating the gap,
1803 or during some other similar "critical section". */
1805 void
1806 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1807 ptrdiff_t *preserve_ptr)
1809 struct buffer *base_buffer;
1810 Lisp_Object temp;
1812 XSETFASTINT (temp, start);
1813 if (!NILP (BVAR (current_buffer, read_only)))
1814 Fbarf_if_buffer_read_only (temp);
1816 run_undoable_change();
1818 bset_redisplay (current_buffer);
1820 if (buffer_intervals (current_buffer))
1822 if (preserve_ptr)
1824 Lisp_Object preserve_marker;
1825 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1826 verify_interval_modification (current_buffer, start, end);
1827 *preserve_ptr = marker_position (preserve_marker);
1828 unchain_marker (XMARKER (preserve_marker));
1830 else
1831 verify_interval_modification (current_buffer, start, end);
1834 /* For indirect buffers, use the base buffer to check clashes. */
1835 if (current_buffer->base_buffer != 0)
1836 base_buffer = current_buffer->base_buffer;
1837 else
1838 base_buffer = current_buffer;
1840 if (inhibit_modification_hooks)
1841 return;
1843 if (!NILP (BVAR (base_buffer, file_truename))
1844 /* Make binding buffer-file-name to nil effective. */
1845 && !NILP (BVAR (base_buffer, filename))
1846 && SAVE_MODIFF >= MODIFF)
1847 lock_file (BVAR (base_buffer, file_truename));
1849 /* If `select-active-regions' is non-nil, save the region text. */
1850 /* FIXME: Move this to Elisp (via before-change-functions). */
1851 if (!NILP (BVAR (current_buffer, mark_active))
1852 && XMARKER (BVAR (current_buffer, mark))->buffer
1853 && NILP (Vsaved_region_selection)
1854 && (EQ (Vselect_active_regions, Qonly)
1855 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1856 : (!NILP (Vselect_active_regions)
1857 && !NILP (Vtransient_mark_mode))))
1858 Vsaved_region_selection
1859 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1861 signal_before_change (start, end, preserve_ptr);
1862 Fset (Qdeactivate_mark, Qt);
1865 /* Like above, but called when we know that the buffer text
1866 will be modified and region caches should be invalidated. */
1868 void
1869 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1870 ptrdiff_t *preserve_ptr)
1872 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1873 invalidate_buffer_caches (current_buffer, start, end);
1876 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1877 region between buffer positions START and END. */
1878 void
1879 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1881 /* Indirect buffers usually have their caches set to NULL, but we
1882 need to consider the caches of their base buffer. */
1883 if (buf->base_buffer)
1884 buf = buf->base_buffer;
1885 /* The bidi_paragraph_cache must be invalidated first, because doing
1886 so might need to use the newline_cache (via find_newline_no_quit,
1887 see below). */
1888 if (buf->bidi_paragraph_cache)
1890 if (start != end
1891 && start > BUF_BEG (buf))
1893 /* If we are deleting or replacing characters, we could
1894 create a paragraph start, because all of the characters
1895 from START to the beginning of START's line are
1896 whitespace. Therefore, we must extend the region to be
1897 invalidated up to the newline before START. */
1898 ptrdiff_t line_beg = start;
1899 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
1901 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
1903 struct buffer *old = current_buffer;
1905 set_buffer_internal (buf);
1907 line_beg = find_newline_no_quit (start, start_byte, -1,
1908 &start_byte);
1909 set_buffer_internal (old);
1911 start = line_beg - (line_beg > BUF_BEG (buf));
1913 invalidate_region_cache (buf,
1914 buf->bidi_paragraph_cache,
1915 start - BUF_BEG (buf), BUF_Z (buf) - end);
1917 if (buf->newline_cache)
1918 invalidate_region_cache (buf,
1919 buf->newline_cache,
1920 start - BUF_BEG (buf), BUF_Z (buf) - end);
1921 if (buf->width_run_cache)
1922 invalidate_region_cache (buf,
1923 buf->width_run_cache,
1924 start - BUF_BEG (buf), BUF_Z (buf) - end);
1927 /* These macros work with an argument named `preserve_ptr'
1928 and a local variable named `preserve_marker'. */
1930 #define PRESERVE_VALUE \
1931 if (preserve_ptr && NILP (preserve_marker)) \
1932 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1934 #define RESTORE_VALUE \
1935 if (! NILP (preserve_marker)) \
1937 *preserve_ptr = marker_position (preserve_marker); \
1938 unchain_marker (XMARKER (preserve_marker)); \
1941 #define PRESERVE_START_END \
1942 if (NILP (start_marker)) \
1943 start_marker = Fcopy_marker (start, Qnil); \
1944 if (NILP (end_marker)) \
1945 end_marker = Fcopy_marker (end, Qnil);
1947 #define FETCH_START \
1948 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1950 #define FETCH_END \
1951 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1953 /* Set a variable to nil if an error occurred.
1954 Don't change the variable if there was no error.
1955 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1956 VARIABLE is the variable to maybe set to nil.
1957 NO-ERROR-FLAG is nil if there was an error,
1958 anything else meaning no error (so this function does nothing). */
1959 struct rvoe_arg
1961 Lisp_Object *location;
1962 bool errorp;
1965 static void
1966 reset_var_on_error (void *ptr)
1968 struct rvoe_arg *p = ptr;
1969 if (p->errorp)
1970 *p->location = Qnil;
1973 /* Signal a change to the buffer immediately before it happens.
1974 START_INT and END_INT are the bounds of the text to be changed.
1976 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1977 by holding its value temporarily in a marker. */
1979 static void
1980 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1981 ptrdiff_t *preserve_ptr)
1983 Lisp_Object start, end;
1984 Lisp_Object start_marker, end_marker;
1985 Lisp_Object preserve_marker;
1986 ptrdiff_t count = SPECPDL_INDEX ();
1987 struct rvoe_arg rvoe_arg;
1989 start = make_number (start_int);
1990 end = make_number (end_int);
1991 preserve_marker = Qnil;
1992 start_marker = Qnil;
1993 end_marker = Qnil;
1995 specbind (Qinhibit_modification_hooks, Qt);
1997 /* If buffer is unmodified, run a special hook for that case. The
1998 check for Vfirst_change_hook is just a minor optimization. */
1999 if (SAVE_MODIFF >= MODIFF
2000 && !NILP (Vfirst_change_hook))
2002 PRESERVE_VALUE;
2003 PRESERVE_START_END;
2004 run_hook (Qfirst_change_hook);
2007 /* Now run the before-change-functions if any. */
2008 if (!NILP (Vbefore_change_functions))
2010 rvoe_arg.location = &Vbefore_change_functions;
2011 rvoe_arg.errorp = 1;
2013 PRESERVE_VALUE;
2014 PRESERVE_START_END;
2016 /* Mark before-change-functions to be reset to nil in case of error. */
2017 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2019 /* Actually run the hook functions. */
2020 CALLN (Frun_hook_with_args, Qbefore_change_functions,
2021 FETCH_START, FETCH_END);
2023 /* There was no error: unarm the reset_on_error. */
2024 rvoe_arg.errorp = 0;
2027 if (buffer_has_overlays ())
2029 PRESERVE_VALUE;
2030 report_overlay_modification (FETCH_START, FETCH_END, 0,
2031 FETCH_START, FETCH_END, Qnil);
2034 if (! NILP (start_marker))
2035 free_marker (start_marker);
2036 if (! NILP (end_marker))
2037 free_marker (end_marker);
2038 RESTORE_VALUE;
2040 unbind_to (count, Qnil);
2043 /* Signal a change immediately after it happens.
2044 CHARPOS is the character position of the start of the changed text.
2045 LENDEL is the number of characters of the text before the change.
2046 (Not the whole buffer; just the part that was changed.)
2047 LENINS is the number of characters in that part of the text
2048 after the change. */
2050 void
2051 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2053 ptrdiff_t count = SPECPDL_INDEX ();
2054 struct rvoe_arg rvoe_arg;
2056 if (inhibit_modification_hooks)
2057 return;
2059 /* If we are deferring calls to the after-change functions
2060 and there are no before-change functions,
2061 just record the args that we were going to use. */
2062 if (! NILP (Vcombine_after_change_calls)
2063 && NILP (Vbefore_change_functions)
2064 && !buffer_has_overlays ())
2066 Lisp_Object elt;
2068 if (!NILP (combine_after_change_list)
2069 && current_buffer != XBUFFER (combine_after_change_buffer))
2070 Fcombine_after_change_execute ();
2072 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2073 lenins - lendel);
2074 combine_after_change_list
2075 = Fcons (elt, combine_after_change_list);
2076 combine_after_change_buffer = Fcurrent_buffer ();
2078 return;
2081 if (!NILP (combine_after_change_list))
2082 Fcombine_after_change_execute ();
2084 specbind (Qinhibit_modification_hooks, Qt);
2086 if (!NILP (Vafter_change_functions))
2088 rvoe_arg.location = &Vafter_change_functions;
2089 rvoe_arg.errorp = 1;
2091 /* Mark after-change-functions to be reset to nil in case of error. */
2092 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2094 /* Actually run the hook functions. */
2095 CALLN (Frun_hook_with_args, Qafter_change_functions,
2096 make_number (charpos), make_number (charpos + lenins),
2097 make_number (lendel));
2099 /* There was no error: unarm the reset_on_error. */
2100 rvoe_arg.errorp = 0;
2103 if (buffer_has_overlays ())
2104 report_overlay_modification (make_number (charpos),
2105 make_number (charpos + lenins),
2107 make_number (charpos),
2108 make_number (charpos + lenins),
2109 make_number (lendel));
2111 /* After an insertion, call the text properties
2112 insert-behind-hooks or insert-in-front-hooks. */
2113 if (lendel == 0)
2114 report_interval_modification (make_number (charpos),
2115 make_number (charpos + lenins));
2117 unbind_to (count, Qnil);
2120 static void
2121 Fcombine_after_change_execute_1 (Lisp_Object val)
2123 Vcombine_after_change_calls = val;
2126 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2127 Scombine_after_change_execute, 0, 0, 0,
2128 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2129 (void)
2131 ptrdiff_t count = SPECPDL_INDEX ();
2132 ptrdiff_t beg, end, change;
2133 ptrdiff_t begpos, endpos;
2134 Lisp_Object tail;
2136 if (NILP (combine_after_change_list))
2137 return Qnil;
2139 /* It is rare for combine_after_change_buffer to be invalid, but
2140 possible. It can happen when combine-after-change-calls is
2141 non-nil, and insertion calls a file handler (e.g. through
2142 lock_file) which scribbles into a temp file -- cyd */
2143 if (!BUFFERP (combine_after_change_buffer)
2144 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2146 combine_after_change_list = Qnil;
2147 return Qnil;
2150 record_unwind_current_buffer ();
2152 Fset_buffer (combine_after_change_buffer);
2154 /* # chars unchanged at beginning of buffer. */
2155 beg = Z - BEG;
2156 /* # chars unchanged at end of buffer. */
2157 end = beg;
2158 /* Total amount of insertion (negative for deletion). */
2159 change = 0;
2161 /* Scan the various individual changes,
2162 accumulating the range info in BEG, END and CHANGE. */
2163 for (tail = combine_after_change_list; CONSP (tail);
2164 tail = XCDR (tail))
2166 Lisp_Object elt;
2167 ptrdiff_t thisbeg, thisend, thischange;
2169 /* Extract the info from the next element. */
2170 elt = XCAR (tail);
2171 if (! CONSP (elt))
2172 continue;
2173 thisbeg = XINT (XCAR (elt));
2175 elt = XCDR (elt);
2176 if (! CONSP (elt))
2177 continue;
2178 thisend = XINT (XCAR (elt));
2180 elt = XCDR (elt);
2181 if (! CONSP (elt))
2182 continue;
2183 thischange = XINT (XCAR (elt));
2185 /* Merge this range into the accumulated range. */
2186 change += thischange;
2187 if (thisbeg < beg)
2188 beg = thisbeg;
2189 if (thisend < end)
2190 end = thisend;
2193 /* Get the current start and end positions of the range
2194 that was changed. */
2195 begpos = BEG + beg;
2196 endpos = Z - end;
2198 /* We are about to handle these, so discard them. */
2199 combine_after_change_list = Qnil;
2201 /* Now run the after-change functions for real.
2202 Turn off the flag that defers them. */
2203 record_unwind_protect (Fcombine_after_change_execute_1,
2204 Vcombine_after_change_calls);
2205 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2206 update_compositions (begpos, endpos, CHECK_ALL);
2208 return unbind_to (count, Qnil);
2211 void
2212 syms_of_insdel (void)
2214 staticpro (&combine_after_change_list);
2215 staticpro (&combine_after_change_buffer);
2216 combine_after_change_list = Qnil;
2217 combine_after_change_buffer = Qnil;
2219 DEFSYM (Qundo_auto__undoable_change, "undo-auto--undoable-change");
2221 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2222 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2223 Vcombine_after_change_calls = Qnil;
2225 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2226 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2227 This affects `before-change-functions' and `after-change-functions',
2228 as well as hooks attached to text properties and overlays. */);
2229 inhibit_modification_hooks = 0;
2230 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2232 DEFSYM (Qregion_extract_function, "region-extract-function");
2234 defsubr (&Scombine_after_change_execute);