Indentation, punctuation, and other nitpicks.
[emacs.git] / src / insdel.c
blob876e2869978a345c873090287623919df1fb9161
1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
3 Copyright (C) 1985-1986, 1993-1995, 1997-2014 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <intprops.h>
25 #include "lisp.h"
26 #include "intervals.h"
27 #include "character.h"
28 #include "buffer.h"
29 #include "window.h"
30 #include "blockinput.h"
31 #include "region-cache.h"
33 static void insert_from_string_1 (Lisp_Object, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35 static void insert_from_buffer_1 (struct buffer *, ptrdiff_t, ptrdiff_t, bool);
36 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37 static void gap_right (ptrdiff_t, ptrdiff_t);
39 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
41 was non-nil. We use this to decide how to call them
42 once the deferral ends.
44 In each element.
45 BEG-UNCHANGED is the number of chars before the changed range.
46 END-UNCHANGED is the number of chars after the changed range,
47 and CHANGE-AMOUNT is the number of characters inserted by the change
48 (negative for a deletion). */
49 static Lisp_Object combine_after_change_list;
51 /* Buffer which combine_after_change_list is about. */
52 static Lisp_Object combine_after_change_buffer;
54 Lisp_Object Qinhibit_modification_hooks;
56 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
58 /* Also used in marker.c to enable expensive marker checks. */
60 #ifdef MARKER_DEBUG
62 static void
63 check_markers (void)
65 struct Lisp_Marker *tail;
66 bool multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
68 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
70 if (tail->buffer->text != current_buffer->text)
71 emacs_abort ();
72 if (tail->charpos > Z)
73 emacs_abort ();
74 if (tail->bytepos > Z_BYTE)
75 emacs_abort ();
76 if (multibyte && ! CHAR_HEAD_P (FETCH_BYTE (tail->bytepos)))
77 emacs_abort ();
81 #else /* not MARKER_DEBUG */
83 #define check_markers() do { } while (0)
85 #endif /* MARKER_DEBUG */
87 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
88 Note that this can quit! */
90 void
91 move_gap_both (ptrdiff_t charpos, ptrdiff_t bytepos)
93 eassert (charpos == BYTE_TO_CHAR (bytepos)
94 && bytepos == CHAR_TO_BYTE (charpos));
95 if (bytepos < GPT_BYTE)
96 gap_left (charpos, bytepos, 0);
97 else if (bytepos > GPT_BYTE)
98 gap_right (charpos, bytepos);
101 /* Move the gap to a position less than the current GPT.
102 BYTEPOS describes the new position as a byte position,
103 and CHARPOS is the corresponding char position.
104 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
106 static void
107 gap_left (ptrdiff_t charpos, ptrdiff_t bytepos, bool newgap)
109 unsigned char *to, *from;
110 ptrdiff_t i;
111 ptrdiff_t new_s1;
113 if (!newgap)
114 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
116 i = GPT_BYTE;
117 to = GAP_END_ADDR;
118 from = GPT_ADDR;
119 new_s1 = GPT_BYTE;
121 /* Now copy the characters. To move the gap down,
122 copy characters up. */
124 while (1)
126 /* I gets number of characters left to copy. */
127 i = new_s1 - bytepos;
128 if (i == 0)
129 break;
130 /* If a quit is requested, stop copying now.
131 Change BYTEPOS to be where we have actually moved the gap to. */
132 if (QUITP)
134 bytepos = new_s1;
135 charpos = BYTE_TO_CHAR (bytepos);
136 break;
138 /* Move at most 32000 chars before checking again for a quit. */
139 if (i > 32000)
140 i = 32000;
141 new_s1 -= i;
142 from -= i, to -= i;
143 memmove (to, from, i);
146 /* Adjust buffer data structure, to put the gap at BYTEPOS.
147 BYTEPOS is where the loop above stopped, which may be what
148 was specified or may be where a quit was detected. */
149 GPT_BYTE = bytepos;
150 GPT = charpos;
151 eassert (charpos <= bytepos);
152 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
153 QUIT;
156 /* Move the gap to a position greater than the current GPT.
157 BYTEPOS describes the new position as a byte position,
158 and CHARPOS is the corresponding char position. */
160 static void
161 gap_right (ptrdiff_t charpos, ptrdiff_t bytepos)
163 register unsigned char *to, *from;
164 register ptrdiff_t i;
165 ptrdiff_t new_s1;
167 BUF_COMPUTE_UNCHANGED (current_buffer, charpos, GPT);
169 i = GPT_BYTE;
170 from = GAP_END_ADDR;
171 to = GPT_ADDR;
172 new_s1 = GPT_BYTE;
174 /* Now copy the characters. To move the gap up,
175 copy characters down. */
177 while (1)
179 /* I gets number of characters left to copy. */
180 i = bytepos - new_s1;
181 if (i == 0)
182 break;
183 /* If a quit is requested, stop copying now.
184 Change BYTEPOS to be where we have actually moved the gap to. */
185 if (QUITP)
187 bytepos = new_s1;
188 charpos = BYTE_TO_CHAR (bytepos);
189 break;
191 /* Move at most 32000 chars before checking again for a quit. */
192 if (i > 32000)
193 i = 32000;
194 new_s1 += i;
195 memmove (to, from, i);
196 from += i, to += i;
199 GPT = charpos;
200 GPT_BYTE = bytepos;
201 eassert (charpos <= bytepos);
202 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
203 QUIT;
206 /* Adjust all markers for a deletion
207 whose range in bytes is FROM_BYTE to TO_BYTE.
208 The range in charpos is FROM to TO.
210 This function assumes that the gap is adjacent to
211 or inside of the range being deleted. */
213 void
214 adjust_markers_for_delete (ptrdiff_t from, ptrdiff_t from_byte,
215 ptrdiff_t to, ptrdiff_t to_byte)
217 struct Lisp_Marker *m;
218 ptrdiff_t charpos;
220 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
222 charpos = m->charpos;
223 eassert (charpos <= Z);
225 /* If the marker is after the deletion,
226 relocate by number of chars / bytes deleted. */
227 if (charpos > to)
229 m->charpos -= to - from;
230 m->bytepos -= to_byte - from_byte;
232 /* Here's the case where a marker is inside text being deleted. */
233 else if (charpos > from)
235 m->charpos = from;
236 m->bytepos = from_byte;
242 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
243 to TO / TO_BYTE. We have to relocate the charpos of every marker
244 that points after the insertion (but not their bytepos).
246 When a marker points at the insertion point,
247 we advance it if either its insertion-type is t
248 or BEFORE_MARKERS is true. */
250 static void
251 adjust_markers_for_insert (ptrdiff_t from, ptrdiff_t from_byte,
252 ptrdiff_t to, ptrdiff_t to_byte, bool before_markers)
254 struct Lisp_Marker *m;
255 bool adjusted = 0;
256 ptrdiff_t nchars = to - from;
257 ptrdiff_t nbytes = to_byte - from_byte;
259 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
261 eassert (m->bytepos >= m->charpos
262 && m->bytepos - m->charpos <= Z_BYTE - Z);
264 if (m->bytepos == from_byte)
266 if (m->insertion_type || before_markers)
268 m->bytepos = to_byte;
269 m->charpos = to;
270 if (m->insertion_type)
271 adjusted = 1;
274 else if (m->bytepos > from_byte)
276 m->bytepos += nbytes;
277 m->charpos += nchars;
281 /* Adjusting only markers whose insertion-type is t may result in
282 - disordered start and end in overlays, and
283 - disordered overlays in the slot `overlays_before' of current_buffer. */
284 if (adjusted)
286 fix_start_end_in_overlays (from, to);
287 fix_overlays_before (current_buffer, from, to);
291 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
293 This is used only when the value of point changes due to an insert
294 or delete; it does not represent a conceptual change in point as a
295 marker. In particular, point is not crossing any interval
296 boundaries, so there's no need to use the usual SET_PT macro. In
297 fact it would be incorrect to do so, because either the old or the
298 new value of point is out of sync with the current set of
299 intervals. */
301 static void
302 adjust_point (ptrdiff_t nchars, ptrdiff_t nbytes)
304 SET_BUF_PT_BOTH (current_buffer, PT + nchars, PT_BYTE + nbytes);
305 /* In a single-byte buffer, the two positions must be equal. */
306 eassert (PT_BYTE >= PT && PT_BYTE - PT <= ZV_BYTE - ZV);
309 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
310 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
311 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
312 an insertion. */
314 static void
315 adjust_markers_for_replace (ptrdiff_t from, ptrdiff_t from_byte,
316 ptrdiff_t old_chars, ptrdiff_t old_bytes,
317 ptrdiff_t new_chars, ptrdiff_t new_bytes)
319 register struct Lisp_Marker *m;
320 ptrdiff_t prev_to_byte = from_byte + old_bytes;
321 ptrdiff_t diff_chars = new_chars - old_chars;
322 ptrdiff_t diff_bytes = new_bytes - old_bytes;
324 for (m = BUF_MARKERS (current_buffer); m; m = m->next)
326 if (m->bytepos >= prev_to_byte)
328 m->charpos += diff_chars;
329 m->bytepos += diff_bytes;
331 else if (m->bytepos > from_byte)
333 m->charpos = from;
334 m->bytepos = from_byte;
338 check_markers ();
342 void
343 buffer_overflow (void)
345 error ("Maximum buffer size exceeded");
348 /* Make the gap NBYTES_ADDED bytes longer. */
350 static void
351 make_gap_larger (ptrdiff_t nbytes_added)
353 Lisp_Object tem;
354 ptrdiff_t real_gap_loc;
355 ptrdiff_t real_gap_loc_byte;
356 ptrdiff_t old_gap_size;
357 ptrdiff_t current_size = Z_BYTE - BEG_BYTE + GAP_SIZE;
359 if (BUF_BYTES_MAX - current_size < nbytes_added)
360 buffer_overflow ();
362 /* If we have to get more space, get enough to last a while;
363 but do not exceed the maximum buffer size. */
364 nbytes_added = min (nbytes_added + GAP_BYTES_DFL,
365 BUF_BYTES_MAX - current_size);
367 enlarge_buffer_text (current_buffer, nbytes_added);
369 /* Prevent quitting in move_gap. */
370 tem = Vinhibit_quit;
371 Vinhibit_quit = Qt;
373 real_gap_loc = GPT;
374 real_gap_loc_byte = GPT_BYTE;
375 old_gap_size = GAP_SIZE;
377 /* Call the newly allocated space a gap at the end of the whole space. */
378 GPT = Z + GAP_SIZE;
379 GPT_BYTE = Z_BYTE + GAP_SIZE;
380 GAP_SIZE = nbytes_added;
382 /* Move the new gap down to be consecutive with the end of the old one. */
383 gap_left (real_gap_loc + old_gap_size, real_gap_loc_byte + old_gap_size, 1);
385 /* Now combine the two into one large gap. */
386 GAP_SIZE += old_gap_size;
387 GPT = real_gap_loc;
388 GPT_BYTE = real_gap_loc_byte;
390 /* Put an anchor. */
391 *(Z_ADDR) = 0;
393 Vinhibit_quit = tem;
396 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
398 /* Make the gap NBYTES_REMOVED bytes shorter. */
400 static void
401 make_gap_smaller (ptrdiff_t nbytes_removed)
403 Lisp_Object tem;
404 ptrdiff_t real_gap_loc;
405 ptrdiff_t real_gap_loc_byte;
406 ptrdiff_t real_Z;
407 ptrdiff_t real_Z_byte;
408 ptrdiff_t real_beg_unchanged;
409 ptrdiff_t new_gap_size;
411 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
412 if (GAP_SIZE - nbytes_removed < GAP_BYTES_MIN)
413 nbytes_removed = GAP_SIZE - GAP_BYTES_MIN;
415 /* Prevent quitting in move_gap. */
416 tem = Vinhibit_quit;
417 Vinhibit_quit = Qt;
419 real_gap_loc = GPT;
420 real_gap_loc_byte = GPT_BYTE;
421 new_gap_size = GAP_SIZE - nbytes_removed;
422 real_Z = Z;
423 real_Z_byte = Z_BYTE;
424 real_beg_unchanged = BEG_UNCHANGED;
426 /* Pretend that the last unwanted part of the gap is the entire gap,
427 and that the first desired part of the gap is part of the buffer
428 text. */
429 memset (GPT_ADDR, 0, new_gap_size);
430 GPT += new_gap_size;
431 GPT_BYTE += new_gap_size;
432 Z += new_gap_size;
433 Z_BYTE += new_gap_size;
434 GAP_SIZE = nbytes_removed;
436 /* Move the unwanted pretend gap to the end of the buffer. */
437 gap_right (Z, Z_BYTE);
439 enlarge_buffer_text (current_buffer, -nbytes_removed);
441 /* Now restore the desired gap. */
442 GAP_SIZE = new_gap_size;
443 GPT = real_gap_loc;
444 GPT_BYTE = real_gap_loc_byte;
445 Z = real_Z;
446 Z_BYTE = real_Z_byte;
447 BEG_UNCHANGED = real_beg_unchanged;
449 /* Put an anchor. */
450 *(Z_ADDR) = 0;
452 Vinhibit_quit = tem;
455 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
457 void
458 make_gap (ptrdiff_t nbytes_added)
460 if (nbytes_added >= 0)
461 make_gap_larger (nbytes_added);
462 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
463 else
464 make_gap_smaller (-nbytes_added);
465 #endif
468 /* Add NBYTES to B's gap. It's enough to temporarily
469 fake current_buffer and avoid real switch to B. */
471 void
472 make_gap_1 (struct buffer *b, ptrdiff_t nbytes)
474 struct buffer *oldb = current_buffer;
476 current_buffer = b;
477 make_gap (nbytes);
478 current_buffer = oldb;
481 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
482 FROM_MULTIBYTE says whether the incoming text is multibyte.
483 TO_MULTIBYTE says whether to store the text as multibyte.
484 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
486 Return the number of bytes stored at TO_ADDR. */
488 ptrdiff_t
489 copy_text (const unsigned char *from_addr, unsigned char *to_addr,
490 ptrdiff_t nbytes, bool from_multibyte, bool to_multibyte)
492 if (from_multibyte == to_multibyte)
494 memcpy (to_addr, from_addr, nbytes);
495 return nbytes;
497 else if (from_multibyte)
499 ptrdiff_t nchars = 0;
500 ptrdiff_t bytes_left = nbytes;
502 while (bytes_left > 0)
504 int thislen, c;
505 c = STRING_CHAR_AND_LENGTH (from_addr, thislen);
506 if (! ASCII_CHAR_P (c))
507 c &= 0xFF;
508 *to_addr++ = c;
509 from_addr += thislen;
510 bytes_left -= thislen;
511 nchars++;
513 return nchars;
515 else
517 unsigned char *initial_to_addr = to_addr;
519 /* Convert single-byte to multibyte. */
520 while (nbytes > 0)
522 int c = *from_addr++;
524 if (!ASCII_CHAR_P (c))
526 c = BYTE8_TO_CHAR (c);
527 to_addr += CHAR_STRING (c, to_addr);
528 nbytes--;
530 else
531 /* Special case for speed. */
532 *to_addr++ = c, nbytes--;
534 return to_addr - initial_to_addr;
538 /* Insert a string of specified length before point.
539 This function judges multibyteness based on
540 enable_multibyte_characters in the current buffer;
541 it never converts between single-byte and multibyte.
543 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
544 prepare_to_modify_buffer could relocate the text. */
546 void
547 insert (const char *string, ptrdiff_t nbytes)
549 if (nbytes > 0)
551 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
552 insert_1_both (string, len, nbytes, 0, 1, 0);
553 opoint = PT - len;
554 signal_after_change (opoint, 0, len);
555 update_compositions (opoint, PT, CHECK_BORDER);
559 /* Likewise, but inherit text properties from neighboring characters. */
561 void
562 insert_and_inherit (const char *string, ptrdiff_t nbytes)
564 if (nbytes > 0)
566 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
567 insert_1_both (string, len, nbytes, 1, 1, 0);
568 opoint = PT - len;
569 signal_after_change (opoint, 0, len);
570 update_compositions (opoint, PT, CHECK_BORDER);
574 /* Insert the character C before point. Do not inherit text properties. */
576 void
577 insert_char (int c)
579 unsigned char str[MAX_MULTIBYTE_LENGTH];
580 int len;
582 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
583 len = CHAR_STRING (c, str);
584 else
586 len = 1;
587 str[0] = c;
590 insert ((char *) str, len);
593 /* Insert the null-terminated string S before point. */
595 void
596 insert_string (const char *s)
598 insert (s, strlen (s));
601 /* Like `insert' except that all markers pointing at the place where
602 the insertion happens are adjusted to point after it.
603 Don't use this function to insert part of a Lisp string,
604 since gc could happen and relocate it. */
606 void
607 insert_before_markers (const char *string, ptrdiff_t nbytes)
609 if (nbytes > 0)
611 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
612 insert_1_both (string, len, nbytes, 0, 1, 1);
613 opoint = PT - len;
614 signal_after_change (opoint, 0, len);
615 update_compositions (opoint, PT, CHECK_BORDER);
619 /* Likewise, but inherit text properties from neighboring characters. */
621 void
622 insert_before_markers_and_inherit (const char *string,
623 ptrdiff_t nbytes)
625 if (nbytes > 0)
627 ptrdiff_t len = chars_in_text ((unsigned char *) string, nbytes), opoint;
628 insert_1_both (string, len, nbytes, 1, 1, 1);
629 opoint = PT - len;
630 signal_after_change (opoint, 0, len);
631 update_compositions (opoint, PT, CHECK_BORDER);
635 #ifdef BYTE_COMBINING_DEBUG
637 /* See if the bytes before POS/POS_BYTE combine with bytes
638 at the start of STRING to form a single character.
639 If so, return the number of bytes at the start of STRING
640 which combine in this way. Otherwise, return 0. */
643 count_combining_before (const unsigned char *string, ptrdiff_t length,
644 ptrdiff_t pos, ptrdiff_t pos_byte)
646 int len, combining_bytes;
647 const unsigned char *p;
649 if (NILP (current_buffer->enable_multibyte_characters))
650 return 0;
652 /* At first, we can exclude the following cases:
653 (1) STRING[0] can't be a following byte of multibyte sequence.
654 (2) POS is the start of the current buffer.
655 (3) A character before POS is not a multibyte character. */
656 if (length == 0 || CHAR_HEAD_P (*string)) /* case (1) */
657 return 0;
658 if (pos_byte == BEG_BYTE) /* case (2) */
659 return 0;
660 len = 1;
661 p = BYTE_POS_ADDR (pos_byte - 1);
662 while (! CHAR_HEAD_P (*p)) p--, len++;
663 if (! LEADING_CODE_P (*p)) /* case (3) */
664 return 0;
666 combining_bytes = BYTES_BY_CHAR_HEAD (*p) - len;
667 if (combining_bytes <= 0)
668 /* The character preceding POS is, complete and no room for
669 combining bytes (combining_bytes == 0), or an independent 8-bit
670 character (combining_bytes < 0). */
671 return 0;
673 /* We have a combination situation. Count the bytes at STRING that
674 may combine. */
675 p = string + 1;
676 while (!CHAR_HEAD_P (*p) && p < string + length)
677 p++;
679 return (combining_bytes < p - string ? combining_bytes : p - string);
682 /* See if the bytes after POS/POS_BYTE combine with bytes
683 at the end of STRING to form a single character.
684 If so, return the number of bytes after POS/POS_BYTE
685 which combine in this way. Otherwise, return 0. */
688 count_combining_after (const unsigned char *string,
689 ptrdiff_t length, ptrdiff_t pos, ptrdiff_t pos_byte)
691 ptrdiff_t opos_byte = pos_byte;
692 ptrdiff_t i;
693 ptrdiff_t bytes;
694 unsigned char *bufp;
696 if (NILP (current_buffer->enable_multibyte_characters))
697 return 0;
699 /* At first, we can exclude the following cases:
700 (1) The last byte of STRING is an ASCII.
701 (2) POS is the last of the current buffer.
702 (3) A character at POS can't be a following byte of multibyte
703 character. */
704 if (length > 0 && ASCII_CHAR_P (string[length - 1])) /* case (1) */
705 return 0;
706 if (pos_byte == Z_BYTE) /* case (2) */
707 return 0;
708 bufp = BYTE_POS_ADDR (pos_byte);
709 if (CHAR_HEAD_P (*bufp)) /* case (3) */
710 return 0;
712 i = length - 1;
713 while (i >= 0 && ! CHAR_HEAD_P (string[i]))
715 i--;
717 if (i < 0)
719 /* All characters in STRING are not character head. We must
720 check also preceding bytes at POS. We are sure that the gap
721 is at POS. */
722 unsigned char *p = BEG_ADDR;
723 i = pos_byte - 2;
724 while (i >= 0 && ! CHAR_HEAD_P (p[i]))
725 i--;
726 if (i < 0 || !LEADING_CODE_P (p[i]))
727 return 0;
729 bytes = BYTES_BY_CHAR_HEAD (p[i]);
730 return (bytes <= pos_byte - 1 - i + length
732 : bytes - (pos_byte - 1 - i + length));
734 if (!LEADING_CODE_P (string[i]))
735 return 0;
737 bytes = BYTES_BY_CHAR_HEAD (string[i]) - (length - i);
738 bufp++, pos_byte++;
739 while (!CHAR_HEAD_P (*bufp)) bufp++, pos_byte++;
741 return (bytes <= pos_byte - opos_byte ? bytes : pos_byte - opos_byte);
744 #endif
747 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
748 starting at STRING. INHERIT non-zero means inherit the text
749 properties from neighboring characters; zero means inserted text
750 will have no text properties. PREPARE non-zero means call
751 prepare_to_modify_buffer, which checks that the region is not
752 read-only, and calls before-change-function and any modification
753 properties the text may have. BEFORE_MARKERS non-zero means adjust
754 all markers that point at the insertion place to point after it. */
756 void
757 insert_1_both (const char *string,
758 ptrdiff_t nchars, ptrdiff_t nbytes,
759 bool inherit, bool prepare, bool before_markers)
761 if (nchars == 0)
762 return;
764 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
765 nchars = nbytes;
767 if (prepare)
768 /* Do this before moving and increasing the gap,
769 because the before-change hooks might move the gap
770 or make it smaller. */
771 prepare_to_modify_buffer (PT, PT, NULL);
773 if (PT != GPT)
774 move_gap_both (PT, PT_BYTE);
775 if (GAP_SIZE < nbytes)
776 make_gap (nbytes - GAP_SIZE);
778 #ifdef BYTE_COMBINING_DEBUG
779 if (count_combining_before (string, nbytes, PT, PT_BYTE)
780 || count_combining_after (string, nbytes, PT, PT_BYTE))
781 emacs_abort ();
782 #endif
784 /* Record deletion of the surrounding text that combines with
785 the insertion. This, together with recording the insertion,
786 will add up to the right stuff in the undo list. */
787 record_insert (PT, nchars);
788 MODIFF++;
789 CHARS_MODIFF = MODIFF;
791 memcpy (GPT_ADDR, string, nbytes);
793 GAP_SIZE -= nbytes;
794 GPT += nchars;
795 ZV += nchars;
796 Z += nchars;
797 GPT_BYTE += nbytes;
798 ZV_BYTE += nbytes;
799 Z_BYTE += nbytes;
800 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
802 eassert (GPT <= GPT_BYTE);
804 /* The insert may have been in the unchanged region, so check again. */
805 if (Z - GPT < END_UNCHANGED)
806 END_UNCHANGED = Z - GPT;
808 adjust_overlays_for_insert (PT, nchars);
809 adjust_markers_for_insert (PT, PT_BYTE,
810 PT + nchars, PT_BYTE + nbytes,
811 before_markers);
813 offset_intervals (current_buffer, PT, nchars);
815 if (!inherit && buffer_intervals (current_buffer))
816 set_text_properties (make_number (PT), make_number (PT + nchars),
817 Qnil, Qnil, Qnil);
819 adjust_point (nchars, nbytes);
821 check_markers ();
824 /* Insert the part of the text of STRING, a Lisp object assumed to be
825 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
826 starting at position POS / POS_BYTE. If the text of STRING has properties,
827 copy them into the buffer.
829 It does not work to use `insert' for this, because a GC could happen
830 before we copy the stuff into the buffer, and relocate the string
831 without insert noticing. */
833 void
834 insert_from_string (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
835 ptrdiff_t length, ptrdiff_t length_byte, bool inherit)
837 ptrdiff_t opoint = PT;
839 if (SCHARS (string) == 0)
840 return;
842 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
843 inherit, 0);
844 signal_after_change (opoint, 0, PT - opoint);
845 update_compositions (opoint, PT, CHECK_BORDER);
848 /* Like `insert_from_string' except that all markers pointing
849 at the place where the insertion happens are adjusted to point after it. */
851 void
852 insert_from_string_before_markers (Lisp_Object string,
853 ptrdiff_t pos, ptrdiff_t pos_byte,
854 ptrdiff_t length, ptrdiff_t length_byte,
855 bool inherit)
857 ptrdiff_t opoint = PT;
859 if (SCHARS (string) == 0)
860 return;
862 insert_from_string_1 (string, pos, pos_byte, length, length_byte,
863 inherit, 1);
864 signal_after_change (opoint, 0, PT - opoint);
865 update_compositions (opoint, PT, CHECK_BORDER);
868 /* Subroutine of the insertion functions above. */
870 static void
871 insert_from_string_1 (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
872 ptrdiff_t nchars, ptrdiff_t nbytes,
873 bool inherit, bool before_markers)
875 struct gcpro gcpro1;
876 ptrdiff_t outgoing_nbytes = nbytes;
877 INTERVAL intervals;
879 /* Make OUTGOING_NBYTES describe the text
880 as it will be inserted in this buffer. */
882 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
883 outgoing_nbytes = nchars;
884 else if (! STRING_MULTIBYTE (string))
885 outgoing_nbytes
886 = count_size_as_multibyte (SDATA (string) + pos_byte,
887 nbytes);
889 GCPRO1 (string);
890 /* Do this before moving and increasing the gap,
891 because the before-change hooks might move the gap
892 or make it smaller. */
893 prepare_to_modify_buffer (PT, PT, NULL);
895 if (PT != GPT)
896 move_gap_both (PT, PT_BYTE);
897 if (GAP_SIZE < outgoing_nbytes)
898 make_gap (outgoing_nbytes - GAP_SIZE);
899 UNGCPRO;
901 /* Copy the string text into the buffer, perhaps converting
902 between single-byte and multibyte. */
903 copy_text (SDATA (string) + pos_byte, GPT_ADDR, nbytes,
904 STRING_MULTIBYTE (string),
905 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
907 #ifdef BYTE_COMBINING_DEBUG
908 /* We have copied text into the gap, but we have not altered
909 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
910 to these functions and get the same results as we would
911 have got earlier on. Meanwhile, PT_ADDR does point to
912 the text that has been stored by copy_text. */
913 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
914 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
915 emacs_abort ();
916 #endif
918 record_insert (PT, nchars);
919 MODIFF++;
920 CHARS_MODIFF = MODIFF;
922 GAP_SIZE -= outgoing_nbytes;
923 GPT += nchars;
924 ZV += nchars;
925 Z += nchars;
926 GPT_BYTE += outgoing_nbytes;
927 ZV_BYTE += outgoing_nbytes;
928 Z_BYTE += outgoing_nbytes;
929 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
931 eassert (GPT <= GPT_BYTE);
933 /* The insert may have been in the unchanged region, so check again. */
934 if (Z - GPT < END_UNCHANGED)
935 END_UNCHANGED = Z - GPT;
937 adjust_overlays_for_insert (PT, nchars);
938 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
939 PT_BYTE + outgoing_nbytes,
940 before_markers);
942 offset_intervals (current_buffer, PT, nchars);
944 intervals = string_intervals (string);
945 /* Get the intervals for the part of the string we are inserting. */
946 if (nbytes < SBYTES (string))
947 intervals = copy_intervals (intervals, pos, nchars);
949 /* Insert those intervals. */
950 graft_intervals_into_buffer (intervals, PT, nchars,
951 current_buffer, inherit);
953 adjust_point (nchars, outgoing_nbytes);
955 check_markers ();
958 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
959 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
960 GPT_ADDR (if not text_at_gap_tail). */
962 void
963 insert_from_gap (ptrdiff_t nchars, ptrdiff_t nbytes, bool text_at_gap_tail)
965 ptrdiff_t ins_charpos = GPT, ins_bytepos = GPT_BYTE;
967 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
968 nchars = nbytes;
970 /* No need to call prepare_to_modify_buffer, since this is called
971 from places that replace some region with a different text, so
972 prepare_to_modify_buffer was already called by the deletion part
973 of this dance. */
974 invalidate_buffer_caches (current_buffer, GPT, GPT);
975 record_insert (GPT, nchars);
976 MODIFF++;
978 GAP_SIZE -= nbytes;
979 if (! text_at_gap_tail)
981 GPT += nchars;
982 GPT_BYTE += nbytes;
984 ZV += nchars;
985 Z += nchars;
986 ZV_BYTE += nbytes;
987 Z_BYTE += nbytes;
988 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
990 eassert (GPT <= GPT_BYTE);
992 adjust_overlays_for_insert (ins_charpos, nchars);
993 adjust_markers_for_insert (ins_charpos, ins_bytepos,
994 ins_charpos + nchars, ins_bytepos + nbytes, 0);
996 if (buffer_intervals (current_buffer))
998 offset_intervals (current_buffer, ins_charpos, nchars);
999 graft_intervals_into_buffer (NULL, ins_charpos, nchars,
1000 current_buffer, 0);
1003 if (ins_charpos < PT)
1004 adjust_point (nchars, nbytes);
1006 check_markers ();
1009 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1010 current buffer. If the text in BUF has properties, they are absorbed
1011 into the current buffer.
1013 It does not work to use `insert' for this, because a malloc could happen
1014 and relocate BUF's text before the copy happens. */
1016 void
1017 insert_from_buffer (struct buffer *buf,
1018 ptrdiff_t charpos, ptrdiff_t nchars, bool inherit)
1020 ptrdiff_t opoint = PT;
1022 insert_from_buffer_1 (buf, charpos, nchars, inherit);
1023 signal_after_change (opoint, 0, PT - opoint);
1024 update_compositions (opoint, PT, CHECK_BORDER);
1027 static void
1028 insert_from_buffer_1 (struct buffer *buf,
1029 ptrdiff_t from, ptrdiff_t nchars, bool inherit)
1031 ptrdiff_t chunk, chunk_expanded;
1032 ptrdiff_t from_byte = buf_charpos_to_bytepos (buf, from);
1033 ptrdiff_t to_byte = buf_charpos_to_bytepos (buf, from + nchars);
1034 ptrdiff_t incoming_nbytes = to_byte - from_byte;
1035 ptrdiff_t outgoing_nbytes = incoming_nbytes;
1036 INTERVAL intervals;
1038 if (nchars == 0)
1039 return;
1041 /* Make OUTGOING_NBYTES describe the text
1042 as it will be inserted in this buffer. */
1044 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1045 outgoing_nbytes = nchars;
1046 else if (NILP (BVAR (buf, enable_multibyte_characters)))
1048 ptrdiff_t outgoing_before_gap = 0;
1049 ptrdiff_t outgoing_after_gap = 0;
1051 if (from < BUF_GPT (buf))
1053 chunk = BUF_GPT_BYTE (buf) - from_byte;
1054 if (chunk > incoming_nbytes)
1055 chunk = incoming_nbytes;
1056 outgoing_before_gap
1057 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf, from_byte),
1058 chunk);
1060 else
1061 chunk = 0;
1063 if (chunk < incoming_nbytes)
1064 outgoing_after_gap
1065 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf,
1066 from_byte + chunk),
1067 incoming_nbytes - chunk);
1069 outgoing_nbytes = outgoing_before_gap + outgoing_after_gap;
1072 /* Do this before moving and increasing the gap,
1073 because the before-change hooks might move the gap
1074 or make it smaller. */
1075 prepare_to_modify_buffer (PT, PT, NULL);
1077 if (PT != GPT)
1078 move_gap_both (PT, PT_BYTE);
1079 if (GAP_SIZE < outgoing_nbytes)
1080 make_gap (outgoing_nbytes - GAP_SIZE);
1082 if (from < BUF_GPT (buf))
1084 chunk = BUF_GPT_BYTE (buf) - from_byte;
1085 if (chunk > incoming_nbytes)
1086 chunk = incoming_nbytes;
1087 /* Record number of output bytes, so we know where
1088 to put the output from the second copy_text. */
1089 chunk_expanded
1090 = copy_text (BUF_BYTE_ADDRESS (buf, from_byte),
1091 GPT_ADDR, chunk,
1092 ! NILP (BVAR (buf, enable_multibyte_characters)),
1093 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1095 else
1096 chunk_expanded = chunk = 0;
1098 if (chunk < incoming_nbytes)
1099 copy_text (BUF_BYTE_ADDRESS (buf, from_byte + chunk),
1100 GPT_ADDR + chunk_expanded, incoming_nbytes - chunk,
1101 ! NILP (BVAR (buf, enable_multibyte_characters)),
1102 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1104 #ifdef BYTE_COMBINING_DEBUG
1105 /* We have copied text into the gap, but we have not altered
1106 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1107 to these functions and get the same results as we would
1108 have got earlier on. Meanwhile, GPT_ADDR does point to
1109 the text that has been stored by copy_text. */
1110 if (count_combining_before (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE)
1111 || count_combining_after (GPT_ADDR, outgoing_nbytes, PT, PT_BYTE))
1112 emacs_abort ();
1113 #endif
1115 record_insert (PT, nchars);
1116 MODIFF++;
1117 CHARS_MODIFF = MODIFF;
1119 GAP_SIZE -= outgoing_nbytes;
1120 GPT += nchars;
1121 ZV += nchars;
1122 Z += nchars;
1123 GPT_BYTE += outgoing_nbytes;
1124 ZV_BYTE += outgoing_nbytes;
1125 Z_BYTE += outgoing_nbytes;
1126 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1128 eassert (GPT <= GPT_BYTE);
1130 /* The insert may have been in the unchanged region, so check again. */
1131 if (Z - GPT < END_UNCHANGED)
1132 END_UNCHANGED = Z - GPT;
1134 adjust_overlays_for_insert (PT, nchars);
1135 adjust_markers_for_insert (PT, PT_BYTE, PT + nchars,
1136 PT_BYTE + outgoing_nbytes,
1139 offset_intervals (current_buffer, PT, nchars);
1141 /* Get the intervals for the part of the string we are inserting. */
1142 intervals = buffer_intervals (buf);
1143 if (nchars < BUF_Z (buf) - BUF_BEG (buf))
1145 if (buf == current_buffer && PT <= from)
1146 from += nchars;
1147 intervals = copy_intervals (intervals, from, nchars);
1150 /* Insert those intervals. */
1151 graft_intervals_into_buffer (intervals, PT, nchars, current_buffer, inherit);
1153 adjust_point (nchars, outgoing_nbytes);
1156 /* Record undo information and adjust markers and position keepers for
1157 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1158 chars (LEN_BYTE bytes) which resides in the gap just after
1159 GPT_ADDR.
1161 PREV_TEXT nil means the new text was just inserted. */
1163 static void
1164 adjust_after_replace (ptrdiff_t from, ptrdiff_t from_byte,
1165 Lisp_Object prev_text, ptrdiff_t len, ptrdiff_t len_byte)
1167 ptrdiff_t nchars_del = 0, nbytes_del = 0;
1169 #ifdef BYTE_COMBINING_DEBUG
1170 if (count_combining_before (GPT_ADDR, len_byte, from, from_byte)
1171 || count_combining_after (GPT_ADDR, len_byte, from, from_byte))
1172 emacs_abort ();
1173 #endif
1175 if (STRINGP (prev_text))
1177 nchars_del = SCHARS (prev_text);
1178 nbytes_del = SBYTES (prev_text);
1181 /* Update various buffer positions for the new text. */
1182 GAP_SIZE -= len_byte;
1183 ZV += len; Z+= len;
1184 ZV_BYTE += len_byte; Z_BYTE += len_byte;
1185 GPT += len; GPT_BYTE += len_byte;
1186 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1188 if (nchars_del > 0)
1189 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1190 len, len_byte);
1191 else
1192 adjust_markers_for_insert (from, from_byte,
1193 from + len, from_byte + len_byte, 0);
1195 if (nchars_del > 0)
1196 record_delete (from, prev_text, false);
1197 record_insert (from, len);
1199 if (len > nchars_del)
1200 adjust_overlays_for_insert (from, len - nchars_del);
1201 else if (len < nchars_del)
1202 adjust_overlays_for_delete (from, nchars_del - len);
1204 offset_intervals (current_buffer, from, len - nchars_del);
1206 if (from < PT)
1207 adjust_point (len - nchars_del, len_byte - nbytes_del);
1209 /* As byte combining will decrease Z, we must check this again. */
1210 if (Z - GPT < END_UNCHANGED)
1211 END_UNCHANGED = Z - GPT;
1213 check_markers ();
1215 if (len == 0)
1216 evaporate_overlays (from);
1217 MODIFF++;
1218 CHARS_MODIFF = MODIFF;
1221 /* Record undo information, adjust markers and position keepers for an
1222 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1223 text already exists in the current buffer but character length (TO
1224 - FROM) may be incorrect, the correct length is NEWLEN. */
1226 void
1227 adjust_after_insert (ptrdiff_t from, ptrdiff_t from_byte,
1228 ptrdiff_t to, ptrdiff_t to_byte, ptrdiff_t newlen)
1230 ptrdiff_t len = to - from, len_byte = to_byte - from_byte;
1232 if (GPT != to)
1233 move_gap_both (to, to_byte);
1234 GAP_SIZE += len_byte;
1235 GPT -= len; GPT_BYTE -= len_byte;
1236 ZV -= len; ZV_BYTE -= len_byte;
1237 Z -= len; Z_BYTE -= len_byte;
1238 adjust_after_replace (from, from_byte, Qnil, newlen, len_byte);
1241 /* Replace the text from character positions FROM to TO with NEW,
1242 If PREPARE, call prepare_to_modify_buffer.
1243 If INHERIT, the newly inserted text should inherit text properties
1244 from the surrounding non-deleted text. */
1246 /* Note that this does not yet handle markers quite right.
1247 Also it needs to record a single undo-entry that does a replacement
1248 rather than a separate delete and insert.
1249 That way, undo will also handle markers properly.
1251 But if MARKERS is 0, don't relocate markers. */
1253 void
1254 replace_range (ptrdiff_t from, ptrdiff_t to, Lisp_Object new,
1255 bool prepare, bool inherit, bool markers)
1257 ptrdiff_t inschars = SCHARS (new);
1258 ptrdiff_t insbytes = SBYTES (new);
1259 ptrdiff_t from_byte, to_byte;
1260 ptrdiff_t nbytes_del, nchars_del;
1261 struct gcpro gcpro1;
1262 INTERVAL intervals;
1263 ptrdiff_t outgoing_insbytes = insbytes;
1264 Lisp_Object deletion;
1266 check_markers ();
1268 GCPRO1 (new);
1269 deletion = Qnil;
1271 if (prepare)
1273 ptrdiff_t range_length = to - from;
1274 prepare_to_modify_buffer (from, to, &from);
1275 to = from + range_length;
1278 UNGCPRO;
1280 /* Make args be valid. */
1281 if (from < BEGV)
1282 from = BEGV;
1283 if (to > ZV)
1284 to = ZV;
1286 from_byte = CHAR_TO_BYTE (from);
1287 to_byte = CHAR_TO_BYTE (to);
1289 nchars_del = to - from;
1290 nbytes_del = to_byte - from_byte;
1292 if (nbytes_del <= 0 && insbytes == 0)
1293 return;
1295 /* Make OUTGOING_INSBYTES describe the text
1296 as it will be inserted in this buffer. */
1298 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
1299 outgoing_insbytes = inschars;
1300 else if (! STRING_MULTIBYTE (new))
1301 outgoing_insbytes
1302 = count_size_as_multibyte (SDATA (new), insbytes);
1304 GCPRO1 (new);
1306 /* Make sure the gap is somewhere in or next to what we are deleting. */
1307 if (from > GPT)
1308 gap_right (from, from_byte);
1309 if (to < GPT)
1310 gap_left (to, to_byte, 0);
1312 /* Even if we don't record for undo, we must keep the original text
1313 because we may have to recover it because of inappropriate byte
1314 combining. */
1315 if (! EQ (BVAR (current_buffer, undo_list), Qt))
1316 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1318 GAP_SIZE += nbytes_del;
1319 ZV -= nchars_del;
1320 Z -= nchars_del;
1321 ZV_BYTE -= nbytes_del;
1322 Z_BYTE -= nbytes_del;
1323 GPT = from;
1324 GPT_BYTE = from_byte;
1325 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1327 eassert (GPT <= GPT_BYTE);
1329 if (GPT - BEG < BEG_UNCHANGED)
1330 BEG_UNCHANGED = GPT - BEG;
1331 if (Z - GPT < END_UNCHANGED)
1332 END_UNCHANGED = Z - GPT;
1334 if (GAP_SIZE < outgoing_insbytes)
1335 make_gap (outgoing_insbytes - GAP_SIZE);
1337 /* Copy the string text into the buffer, perhaps converting
1338 between single-byte and multibyte. */
1339 copy_text (SDATA (new), GPT_ADDR, insbytes,
1340 STRING_MULTIBYTE (new),
1341 ! NILP (BVAR (current_buffer, enable_multibyte_characters)));
1343 #ifdef BYTE_COMBINING_DEBUG
1344 /* We have copied text into the gap, but we have not marked
1345 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1346 here, for both the previous text and the following text.
1347 Meanwhile, GPT_ADDR does point to
1348 the text that has been stored by copy_text. */
1349 if (count_combining_before (GPT_ADDR, outgoing_insbytes, from, from_byte)
1350 || count_combining_after (GPT_ADDR, outgoing_insbytes, from, from_byte))
1351 emacs_abort ();
1352 #endif
1354 /* Record the insertion first, so that when we undo,
1355 the deletion will be undone first. Thus, undo
1356 will insert before deleting, and thus will keep
1357 the markers before and after this text separate. */
1358 if (!NILP (deletion))
1360 record_insert (from + SCHARS (deletion), inschars);
1361 record_delete (from, deletion, false);
1364 GAP_SIZE -= outgoing_insbytes;
1365 GPT += inschars;
1366 ZV += inschars;
1367 Z += inschars;
1368 GPT_BYTE += outgoing_insbytes;
1369 ZV_BYTE += outgoing_insbytes;
1370 Z_BYTE += outgoing_insbytes;
1371 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1373 eassert (GPT <= GPT_BYTE);
1375 /* Adjust markers for the deletion and the insertion. */
1376 if (markers)
1377 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1378 inschars, outgoing_insbytes);
1380 /* Adjust the overlay center as needed. This must be done after
1381 adjusting the markers that bound the overlays. */
1382 adjust_overlays_for_delete (from, nchars_del);
1383 adjust_overlays_for_insert (from, inschars);
1385 offset_intervals (current_buffer, from, inschars - nchars_del);
1387 /* Get the intervals for the part of the string we are inserting--
1388 not including the combined-before bytes. */
1389 intervals = string_intervals (new);
1390 /* Insert those intervals. */
1391 graft_intervals_into_buffer (intervals, from, inschars,
1392 current_buffer, inherit);
1394 /* Relocate point as if it were a marker. */
1395 if (from < PT)
1396 adjust_point ((from + inschars - (PT < to ? PT : to)),
1397 (from_byte + outgoing_insbytes
1398 - (PT_BYTE < to_byte ? PT_BYTE : to_byte)));
1400 if (outgoing_insbytes == 0)
1401 evaporate_overlays (from);
1403 check_markers ();
1405 MODIFF++;
1406 CHARS_MODIFF = MODIFF;
1407 UNGCPRO;
1409 signal_after_change (from, nchars_del, GPT - from);
1410 update_compositions (from, GPT, CHECK_BORDER);
1413 /* Replace the text from character positions FROM to TO with
1414 the text in INS of length INSCHARS.
1415 Keep the text properties that applied to the old characters
1416 (extending them to all the new chars if there are more new chars).
1418 Note that this does not yet handle markers quite right.
1420 If MARKERS, relocate markers.
1422 Unlike most functions at this level, never call
1423 prepare_to_modify_buffer and never call signal_after_change. */
1425 void
1426 replace_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1427 ptrdiff_t to, ptrdiff_t to_byte,
1428 const char *ins, ptrdiff_t inschars, ptrdiff_t insbytes,
1429 bool markers)
1431 ptrdiff_t nbytes_del, nchars_del;
1433 check_markers ();
1435 nchars_del = to - from;
1436 nbytes_del = to_byte - from_byte;
1438 if (nbytes_del <= 0 && insbytes == 0)
1439 return;
1441 /* Make sure the gap is somewhere in or next to what we are deleting. */
1442 if (from > GPT)
1443 gap_right (from, from_byte);
1444 if (to < GPT)
1445 gap_left (to, to_byte, 0);
1447 GAP_SIZE += nbytes_del;
1448 ZV -= nchars_del;
1449 Z -= nchars_del;
1450 ZV_BYTE -= nbytes_del;
1451 Z_BYTE -= nbytes_del;
1452 GPT = from;
1453 GPT_BYTE = from_byte;
1454 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1456 eassert (GPT <= GPT_BYTE);
1458 if (GPT - BEG < BEG_UNCHANGED)
1459 BEG_UNCHANGED = GPT - BEG;
1460 if (Z - GPT < END_UNCHANGED)
1461 END_UNCHANGED = Z - GPT;
1463 if (GAP_SIZE < insbytes)
1464 make_gap (insbytes - GAP_SIZE);
1466 /* Copy the replacement text into the buffer. */
1467 memcpy (GPT_ADDR, ins, insbytes);
1469 #ifdef BYTE_COMBINING_DEBUG
1470 /* We have copied text into the gap, but we have not marked
1471 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1472 here, for both the previous text and the following text.
1473 Meanwhile, GPT_ADDR does point to
1474 the text that has been stored by copy_text. */
1475 if (count_combining_before (GPT_ADDR, insbytes, from, from_byte)
1476 || count_combining_after (GPT_ADDR, insbytes, from, from_byte))
1477 emacs_abort ();
1478 #endif
1480 GAP_SIZE -= insbytes;
1481 GPT += inschars;
1482 ZV += inschars;
1483 Z += inschars;
1484 GPT_BYTE += insbytes;
1485 ZV_BYTE += insbytes;
1486 Z_BYTE += insbytes;
1487 if (GAP_SIZE > 0) *(GPT_ADDR) = 0; /* Put an anchor. */
1489 eassert (GPT <= GPT_BYTE);
1491 /* Adjust markers for the deletion and the insertion. */
1492 if (markers
1493 && ! (nchars_del == 1 && inschars == 1 && nbytes_del == insbytes))
1494 adjust_markers_for_replace (from, from_byte, nchars_del, nbytes_del,
1495 inschars, insbytes);
1497 /* Adjust the overlay center as needed. This must be done after
1498 adjusting the markers that bound the overlays. */
1499 if (nchars_del != inschars)
1501 adjust_overlays_for_insert (from, inschars);
1502 adjust_overlays_for_delete (from + inschars, nchars_del);
1505 offset_intervals (current_buffer, from, inschars - nchars_del);
1507 /* Relocate point as if it were a marker. */
1508 if (from < PT && (nchars_del != inschars || nbytes_del != insbytes))
1510 if (PT < to)
1511 /* PT was within the deleted text. Move it to FROM. */
1512 adjust_point (from - PT, from_byte - PT_BYTE);
1513 else
1514 adjust_point (inschars - nchars_del, insbytes - nbytes_del);
1517 if (insbytes == 0)
1518 evaporate_overlays (from);
1520 check_markers ();
1522 MODIFF++;
1523 CHARS_MODIFF = MODIFF;
1526 /* Delete characters in current buffer
1527 from FROM up to (but not including) TO.
1528 If TO comes before FROM, we delete nothing. */
1530 void
1531 del_range (ptrdiff_t from, ptrdiff_t to)
1533 del_range_1 (from, to, 1, 0);
1536 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1537 RET_STRING says to return the deleted text. */
1539 Lisp_Object
1540 del_range_1 (ptrdiff_t from, ptrdiff_t to, bool prepare, bool ret_string)
1542 ptrdiff_t from_byte, to_byte;
1543 Lisp_Object deletion;
1544 struct gcpro gcpro1;
1546 /* Make args be valid */
1547 if (from < BEGV)
1548 from = BEGV;
1549 if (to > ZV)
1550 to = ZV;
1552 if (to <= from)
1553 return Qnil;
1555 if (prepare)
1557 ptrdiff_t range_length = to - from;
1558 prepare_to_modify_buffer (from, to, &from);
1559 to = min (ZV, from + range_length);
1562 from_byte = CHAR_TO_BYTE (from);
1563 to_byte = CHAR_TO_BYTE (to);
1565 deletion = del_range_2 (from, from_byte, to, to_byte, ret_string);
1566 GCPRO1 (deletion);
1567 signal_after_change (from, to - from, 0);
1568 update_compositions (from, from, CHECK_HEAD);
1569 UNGCPRO;
1570 return deletion;
1573 /* Like del_range_1 but args are byte positions, not char positions. */
1575 void
1576 del_range_byte (ptrdiff_t from_byte, ptrdiff_t to_byte, bool prepare)
1578 ptrdiff_t from, to;
1580 /* Make args be valid */
1581 if (from_byte < BEGV_BYTE)
1582 from_byte = BEGV_BYTE;
1583 if (to_byte > ZV_BYTE)
1584 to_byte = ZV_BYTE;
1586 if (to_byte <= from_byte)
1587 return;
1589 from = BYTE_TO_CHAR (from_byte);
1590 to = BYTE_TO_CHAR (to_byte);
1592 if (prepare)
1594 ptrdiff_t old_from = from, old_to = Z - to;
1595 ptrdiff_t range_length = to - from;
1596 prepare_to_modify_buffer (from, to, &from);
1597 to = from + range_length;
1599 if (old_from != from)
1600 from_byte = CHAR_TO_BYTE (from);
1601 if (to > ZV)
1603 to = ZV;
1604 to_byte = ZV_BYTE;
1606 else if (old_to == Z - to)
1607 to_byte = CHAR_TO_BYTE (to);
1610 del_range_2 (from, from_byte, to, to_byte, 0);
1611 signal_after_change (from, to - from, 0);
1612 update_compositions (from, from, CHECK_HEAD);
1615 /* Like del_range_1, but positions are specified both as charpos
1616 and bytepos. */
1618 void
1619 del_range_both (ptrdiff_t from, ptrdiff_t from_byte,
1620 ptrdiff_t to, ptrdiff_t to_byte, bool prepare)
1622 /* Make args be valid */
1623 if (from_byte < BEGV_BYTE)
1624 from_byte = BEGV_BYTE;
1625 if (to_byte > ZV_BYTE)
1626 to_byte = ZV_BYTE;
1628 if (to_byte <= from_byte)
1629 return;
1631 if (from < BEGV)
1632 from = BEGV;
1633 if (to > ZV)
1634 to = ZV;
1636 if (prepare)
1638 ptrdiff_t old_from = from, old_to = Z - to;
1639 ptrdiff_t range_length = to - from;
1640 prepare_to_modify_buffer (from, to, &from);
1641 to = from + range_length;
1643 if (old_from != from)
1644 from_byte = CHAR_TO_BYTE (from);
1645 if (to > ZV)
1647 to = ZV;
1648 to_byte = ZV_BYTE;
1650 else if (old_to == Z - to)
1651 to_byte = CHAR_TO_BYTE (to);
1654 del_range_2 (from, from_byte, to, to_byte, 0);
1655 signal_after_change (from, to - from, 0);
1656 update_compositions (from, from, CHECK_HEAD);
1659 /* Delete a range of text, specified both as character positions
1660 and byte positions. FROM and TO are character positions,
1661 while FROM_BYTE and TO_BYTE are byte positions.
1662 If RET_STRING, the deleted area is returned as a string. */
1664 Lisp_Object
1665 del_range_2 (ptrdiff_t from, ptrdiff_t from_byte,
1666 ptrdiff_t to, ptrdiff_t to_byte, bool ret_string)
1668 ptrdiff_t nbytes_del, nchars_del;
1669 Lisp_Object deletion;
1671 check_markers ();
1673 nchars_del = to - from;
1674 nbytes_del = to_byte - from_byte;
1676 /* Make sure the gap is somewhere in or next to what we are deleting. */
1677 if (from > GPT)
1678 gap_right (from, from_byte);
1679 if (to < GPT)
1680 gap_left (to, to_byte, 0);
1682 #ifdef BYTE_COMBINING_DEBUG
1683 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer, to_byte),
1684 Z_BYTE - to_byte, from, from_byte))
1685 emacs_abort ();
1686 #endif
1688 if (ret_string || ! EQ (BVAR (current_buffer, undo_list), Qt))
1689 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
1690 else
1691 deletion = Qnil;
1693 /* Record marker adjustments, and text deletion into undo
1694 history. */
1695 record_delete (from, deletion, true);
1697 /* Relocate all markers pointing into the new, larger gap to point
1698 at the end of the text before the gap. */
1699 adjust_markers_for_delete (from, from_byte, to, to_byte);
1701 MODIFF++;
1702 CHARS_MODIFF = MODIFF;
1704 /* Relocate point as if it were a marker. */
1705 if (from < PT)
1706 adjust_point (from - (PT < to ? PT : to),
1707 from_byte - (PT_BYTE < to_byte ? PT_BYTE : to_byte));
1709 offset_intervals (current_buffer, from, - nchars_del);
1711 /* Adjust the overlay center as needed. This must be done after
1712 adjusting the markers that bound the overlays. */
1713 adjust_overlays_for_delete (from, nchars_del);
1715 GAP_SIZE += nbytes_del;
1716 ZV_BYTE -= nbytes_del;
1717 Z_BYTE -= nbytes_del;
1718 ZV -= nchars_del;
1719 Z -= nchars_del;
1720 GPT = from;
1721 GPT_BYTE = from_byte;
1722 if (GAP_SIZE > 0 && !current_buffer->text->inhibit_shrinking)
1723 /* Put an anchor, unless called from decode_coding_object which
1724 needs to access the previous gap contents. */
1725 *(GPT_ADDR) = 0;
1727 eassert (GPT <= GPT_BYTE);
1729 if (GPT - BEG < BEG_UNCHANGED)
1730 BEG_UNCHANGED = GPT - BEG;
1731 if (Z - GPT < END_UNCHANGED)
1732 END_UNCHANGED = Z - GPT;
1734 check_markers ();
1736 evaporate_overlays (from);
1738 return deletion;
1741 /* Call this if you're about to change the text of current buffer
1742 from character positions START to END. This checks the read-only
1743 properties of the region, calls the necessary modification hooks,
1744 and warns the next redisplay that it should pay attention to that
1745 area. */
1747 void
1748 modify_text (ptrdiff_t start, ptrdiff_t end)
1750 prepare_to_modify_buffer (start, end, NULL);
1752 BUF_COMPUTE_UNCHANGED (current_buffer, start - 1, end);
1753 if (MODIFF <= SAVE_MODIFF)
1754 record_first_change ();
1755 MODIFF++;
1756 CHARS_MODIFF = MODIFF;
1758 bset_point_before_scroll (current_buffer, Qnil);
1761 Lisp_Object Qregion_extract_function;
1763 /* Check that it is okay to modify the buffer between START and END,
1764 which are char positions.
1766 Run the before-change-function, if any. If intervals are in use,
1767 verify that the text to be modified is not read-only, and call
1768 any modification properties the text may have.
1770 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1771 by holding its value temporarily in a marker. */
1773 void
1774 prepare_to_modify_buffer_1 (ptrdiff_t start, ptrdiff_t end,
1775 ptrdiff_t *preserve_ptr)
1777 struct buffer *base_buffer;
1779 if (!NILP (BVAR (current_buffer, read_only)))
1780 Fbarf_if_buffer_read_only ();
1782 bset_redisplay (current_buffer);
1784 if (buffer_intervals (current_buffer))
1786 if (preserve_ptr)
1788 Lisp_Object preserve_marker;
1789 struct gcpro gcpro1;
1790 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil);
1791 GCPRO1 (preserve_marker);
1792 verify_interval_modification (current_buffer, start, end);
1793 *preserve_ptr = marker_position (preserve_marker);
1794 unchain_marker (XMARKER (preserve_marker));
1795 UNGCPRO;
1797 else
1798 verify_interval_modification (current_buffer, start, end);
1801 /* For indirect buffers, use the base buffer to check clashes. */
1802 if (current_buffer->base_buffer != 0)
1803 base_buffer = current_buffer->base_buffer;
1804 else
1805 base_buffer = current_buffer;
1807 if (inhibit_modification_hooks)
1808 return;
1810 if (!NILP (BVAR (base_buffer, file_truename))
1811 /* Make binding buffer-file-name to nil effective. */
1812 && !NILP (BVAR (base_buffer, filename))
1813 && SAVE_MODIFF >= MODIFF)
1814 lock_file (BVAR (base_buffer, file_truename));
1816 /* If `select-active-regions' is non-nil, save the region text. */
1817 /* FIXME: Move this to Elisp (via before-change-functions). */
1818 if (!NILP (BVAR (current_buffer, mark_active))
1819 && XMARKER (BVAR (current_buffer, mark))->buffer
1820 && NILP (Vsaved_region_selection)
1821 && (EQ (Vselect_active_regions, Qonly)
1822 ? EQ (CAR_SAFE (Vtransient_mark_mode), Qonly)
1823 : (!NILP (Vselect_active_regions)
1824 && !NILP (Vtransient_mark_mode))))
1825 Vsaved_region_selection
1826 = call1 (Fsymbol_value (Qregion_extract_function), Qnil);
1828 signal_before_change (start, end, preserve_ptr);
1829 Vdeactivate_mark = Qt;
1832 /* Like above, but called when we know that the buffer text
1833 will be modified and region caches should be invalidated. */
1835 void
1836 prepare_to_modify_buffer (ptrdiff_t start, ptrdiff_t end,
1837 ptrdiff_t *preserve_ptr)
1839 prepare_to_modify_buffer_1 (start, end, preserve_ptr);
1840 invalidate_buffer_caches (current_buffer, start, end);
1843 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1844 region between buffer positions START and END. */
1845 void
1846 invalidate_buffer_caches (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
1848 /* Indirect buffers usually have their caches set to NULL, but we
1849 need to consider the caches of their base buffer. */
1850 if (buf->base_buffer)
1851 buf = buf->base_buffer;
1852 /* The bidi_paragraph_cache must be invalidated first, because doing
1853 so might need to use the newline_cache (via find_newline_no_quit,
1854 see below). */
1855 if (buf->bidi_paragraph_cache)
1857 if (start != end
1858 && start > BUF_BEG (buf))
1860 /* If we are deleting or replacing characters, we could
1861 create a paragraph start, because all of the characters
1862 from START to the beginning of START's line are
1863 whitespace. Therefore, we must extend the region to be
1864 invalidated up to the newline before START. */
1865 ptrdiff_t line_beg = start;
1866 ptrdiff_t start_byte = buf_charpos_to_bytepos (buf, start);
1868 if (BUF_FETCH_BYTE (buf, start_byte - 1) != '\n')
1870 struct buffer *old = current_buffer;
1872 set_buffer_internal (buf);
1874 line_beg = find_newline_no_quit (start, start_byte, -1,
1875 &start_byte);
1876 set_buffer_internal (old);
1878 start = line_beg - (line_beg > BUF_BEG (buf));
1880 invalidate_region_cache (buf,
1881 buf->bidi_paragraph_cache,
1882 start - BUF_BEG (buf), BUF_Z (buf) - end);
1884 if (buf->newline_cache)
1885 invalidate_region_cache (buf,
1886 buf->newline_cache,
1887 start - BUF_BEG (buf), BUF_Z (buf) - end);
1888 if (buf->width_run_cache)
1889 invalidate_region_cache (buf,
1890 buf->width_run_cache,
1891 start - BUF_BEG (buf), BUF_Z (buf) - end);
1894 /* These macros work with an argument named `preserve_ptr'
1895 and a local variable named `preserve_marker'. */
1897 #define PRESERVE_VALUE \
1898 if (preserve_ptr && NILP (preserve_marker)) \
1899 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1901 #define RESTORE_VALUE \
1902 if (! NILP (preserve_marker)) \
1904 *preserve_ptr = marker_position (preserve_marker); \
1905 unchain_marker (XMARKER (preserve_marker)); \
1908 #define PRESERVE_START_END \
1909 if (NILP (start_marker)) \
1910 start_marker = Fcopy_marker (start, Qnil); \
1911 if (NILP (end_marker)) \
1912 end_marker = Fcopy_marker (end, Qnil);
1914 #define FETCH_START \
1915 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1917 #define FETCH_END \
1918 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1920 /* Set a variable to nil if an error occurred.
1921 Don't change the variable if there was no error.
1922 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1923 VARIABLE is the variable to maybe set to nil.
1924 NO-ERROR-FLAG is nil if there was an error,
1925 anything else meaning no error (so this function does nothing). */
1926 struct rvoe_arg
1928 Lisp_Object *location;
1929 bool errorp;
1932 static void
1933 reset_var_on_error (void *ptr)
1935 struct rvoe_arg *p = ptr;
1936 if (p->errorp)
1937 *p->location = Qnil;
1940 /* Signal a change to the buffer immediately before it happens.
1941 START_INT and END_INT are the bounds of the text to be changed.
1943 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1944 by holding its value temporarily in a marker. */
1946 static void
1947 signal_before_change (ptrdiff_t start_int, ptrdiff_t end_int,
1948 ptrdiff_t *preserve_ptr)
1950 Lisp_Object start, end;
1951 Lisp_Object start_marker, end_marker;
1952 Lisp_Object preserve_marker;
1953 struct gcpro gcpro1, gcpro2, gcpro3;
1954 ptrdiff_t count = SPECPDL_INDEX ();
1955 struct rvoe_arg rvoe_arg;
1957 start = make_number (start_int);
1958 end = make_number (end_int);
1959 preserve_marker = Qnil;
1960 start_marker = Qnil;
1961 end_marker = Qnil;
1962 GCPRO3 (preserve_marker, start_marker, end_marker);
1964 specbind (Qinhibit_modification_hooks, Qt);
1966 /* If buffer is unmodified, run a special hook for that case. The
1967 check for Vfirst_change_hook is just a minor optimization. */
1968 if (SAVE_MODIFF >= MODIFF
1969 && !NILP (Vfirst_change_hook))
1971 PRESERVE_VALUE;
1972 PRESERVE_START_END;
1973 Frun_hooks (1, &Qfirst_change_hook);
1976 /* Now run the before-change-functions if any. */
1977 if (!NILP (Vbefore_change_functions))
1979 Lisp_Object args[3];
1980 rvoe_arg.location = &Vbefore_change_functions;
1981 rvoe_arg.errorp = 1;
1983 PRESERVE_VALUE;
1984 PRESERVE_START_END;
1986 /* Mark before-change-functions to be reset to nil in case of error. */
1987 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
1989 /* Actually run the hook functions. */
1990 args[0] = Qbefore_change_functions;
1991 args[1] = FETCH_START;
1992 args[2] = FETCH_END;
1993 Frun_hook_with_args (3, args);
1995 /* There was no error: unarm the reset_on_error. */
1996 rvoe_arg.errorp = 0;
1999 if (buffer_has_overlays ())
2001 PRESERVE_VALUE;
2002 report_overlay_modification (FETCH_START, FETCH_END, 0,
2003 FETCH_START, FETCH_END, Qnil);
2006 if (! NILP (start_marker))
2007 free_marker (start_marker);
2008 if (! NILP (end_marker))
2009 free_marker (end_marker);
2010 RESTORE_VALUE;
2011 UNGCPRO;
2013 unbind_to (count, Qnil);
2016 /* Signal a change immediately after it happens.
2017 CHARPOS is the character position of the start of the changed text.
2018 LENDEL is the number of characters of the text before the change.
2019 (Not the whole buffer; just the part that was changed.)
2020 LENINS is the number of characters in that part of the text
2021 after the change. */
2023 void
2024 signal_after_change (ptrdiff_t charpos, ptrdiff_t lendel, ptrdiff_t lenins)
2026 ptrdiff_t count = SPECPDL_INDEX ();
2027 struct rvoe_arg rvoe_arg;
2029 if (inhibit_modification_hooks)
2030 return;
2032 /* If we are deferring calls to the after-change functions
2033 and there are no before-change functions,
2034 just record the args that we were going to use. */
2035 if (! NILP (Vcombine_after_change_calls)
2036 && NILP (Vbefore_change_functions)
2037 && !buffer_has_overlays ())
2039 Lisp_Object elt;
2041 if (!NILP (combine_after_change_list)
2042 && current_buffer != XBUFFER (combine_after_change_buffer))
2043 Fcombine_after_change_execute ();
2045 elt = list3i (charpos - BEG, Z - (charpos - lendel + lenins),
2046 lenins - lendel);
2047 combine_after_change_list
2048 = Fcons (elt, combine_after_change_list);
2049 combine_after_change_buffer = Fcurrent_buffer ();
2051 return;
2054 if (!NILP (combine_after_change_list))
2055 Fcombine_after_change_execute ();
2057 specbind (Qinhibit_modification_hooks, Qt);
2059 if (!NILP (Vafter_change_functions))
2061 Lisp_Object args[4];
2062 rvoe_arg.location = &Vafter_change_functions;
2063 rvoe_arg.errorp = 1;
2065 /* Mark after-change-functions to be reset to nil in case of error. */
2066 record_unwind_protect_ptr (reset_var_on_error, &rvoe_arg);
2068 /* Actually run the hook functions. */
2069 args[0] = Qafter_change_functions;
2070 XSETFASTINT (args[1], charpos);
2071 XSETFASTINT (args[2], charpos + lenins);
2072 XSETFASTINT (args[3], lendel);
2073 Frun_hook_with_args (4, args);
2075 /* There was no error: unarm the reset_on_error. */
2076 rvoe_arg.errorp = 0;
2079 if (buffer_has_overlays ())
2080 report_overlay_modification (make_number (charpos),
2081 make_number (charpos + lenins),
2083 make_number (charpos),
2084 make_number (charpos + lenins),
2085 make_number (lendel));
2087 /* After an insertion, call the text properties
2088 insert-behind-hooks or insert-in-front-hooks. */
2089 if (lendel == 0)
2090 report_interval_modification (make_number (charpos),
2091 make_number (charpos + lenins));
2093 unbind_to (count, Qnil);
2096 static void
2097 Fcombine_after_change_execute_1 (Lisp_Object val)
2099 Vcombine_after_change_calls = val;
2102 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute,
2103 Scombine_after_change_execute, 0, 0, 0,
2104 doc: /* This function is for use internally in the function `combine-after-change-calls'. */)
2105 (void)
2107 ptrdiff_t count = SPECPDL_INDEX ();
2108 ptrdiff_t beg, end, change;
2109 ptrdiff_t begpos, endpos;
2110 Lisp_Object tail;
2112 if (NILP (combine_after_change_list))
2113 return Qnil;
2115 /* It is rare for combine_after_change_buffer to be invalid, but
2116 possible. It can happen when combine-after-change-calls is
2117 non-nil, and insertion calls a file handler (e.g. through
2118 lock_file) which scribbles into a temp file -- cyd */
2119 if (!BUFFERP (combine_after_change_buffer)
2120 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer)))
2122 combine_after_change_list = Qnil;
2123 return Qnil;
2126 record_unwind_current_buffer ();
2128 Fset_buffer (combine_after_change_buffer);
2130 /* # chars unchanged at beginning of buffer. */
2131 beg = Z - BEG;
2132 /* # chars unchanged at end of buffer. */
2133 end = beg;
2134 /* Total amount of insertion (negative for deletion). */
2135 change = 0;
2137 /* Scan the various individual changes,
2138 accumulating the range info in BEG, END and CHANGE. */
2139 for (tail = combine_after_change_list; CONSP (tail);
2140 tail = XCDR (tail))
2142 Lisp_Object elt;
2143 ptrdiff_t thisbeg, thisend, thischange;
2145 /* Extract the info from the next element. */
2146 elt = XCAR (tail);
2147 if (! CONSP (elt))
2148 continue;
2149 thisbeg = XINT (XCAR (elt));
2151 elt = XCDR (elt);
2152 if (! CONSP (elt))
2153 continue;
2154 thisend = XINT (XCAR (elt));
2156 elt = XCDR (elt);
2157 if (! CONSP (elt))
2158 continue;
2159 thischange = XINT (XCAR (elt));
2161 /* Merge this range into the accumulated range. */
2162 change += thischange;
2163 if (thisbeg < beg)
2164 beg = thisbeg;
2165 if (thisend < end)
2166 end = thisend;
2169 /* Get the current start and end positions of the range
2170 that was changed. */
2171 begpos = BEG + beg;
2172 endpos = Z - end;
2174 /* We are about to handle these, so discard them. */
2175 combine_after_change_list = Qnil;
2177 /* Now run the after-change functions for real.
2178 Turn off the flag that defers them. */
2179 record_unwind_protect (Fcombine_after_change_execute_1,
2180 Vcombine_after_change_calls);
2181 signal_after_change (begpos, endpos - begpos - change, endpos - begpos);
2182 update_compositions (begpos, endpos, CHECK_ALL);
2184 return unbind_to (count, Qnil);
2187 void
2188 syms_of_insdel (void)
2190 staticpro (&combine_after_change_list);
2191 staticpro (&combine_after_change_buffer);
2192 combine_after_change_list = Qnil;
2193 combine_after_change_buffer = Qnil;
2195 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls,
2196 doc: /* Used internally by the function `combine-after-change-calls' macro. */);
2197 Vcombine_after_change_calls = Qnil;
2199 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks,
2200 doc: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2201 This affects `before-change-functions' and `after-change-functions',
2202 as well as hooks attached to text properties and overlays. */);
2203 inhibit_modification_hooks = 0;
2204 DEFSYM (Qinhibit_modification_hooks, "inhibit-modification-hooks");
2206 DEFSYM (Qregion_extract_function, "region-extract-function");
2208 defsubr (&Scombine_after_change_execute);