1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2017 Free Software
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/>. */
26 #include "composite.h"
27 #include "intervals.h"
28 #include "character.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.
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. */
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
)
70 if (tail
->charpos
> Z
)
72 if (tail
->bytepos
> Z_BYTE
)
74 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (tail
->bytepos
)))
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! */
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. */
105 gap_left (ptrdiff_t charpos
, ptrdiff_t bytepos
, bool newgap
)
107 unsigned char *to
, *from
;
112 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
119 /* Now copy the characters. To move the gap down,
120 copy characters up. */
124 /* I gets number of characters left to copy. */
125 i
= new_s1
- bytepos
;
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. */
136 charpos
= BYTE_TO_CHAR (bytepos
);
139 /* Move at most 32000 chars before checking again for a quit. */
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. */
152 eassert (charpos
<= bytepos
);
153 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
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. */
162 gap_right (ptrdiff_t charpos
, ptrdiff_t bytepos
)
164 register unsigned char *to
, *from
;
165 register ptrdiff_t i
;
168 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
175 /* Now copy the characters. To move the gap up,
176 copy characters down. */
180 /* I gets number of characters left to copy. */
181 i
= bytepos
- new_s1
;
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. */
192 charpos
= BYTE_TO_CHAR (bytepos
);
195 /* Move at most 32000 chars before checking again for a quit. */
199 memmove (to
, from
, i
);
205 eassert (charpos
<= bytepos
);
206 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
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. */
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. */
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
;
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. */
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
)
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. */
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
;
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
;
295 if (m
->insertion_type
)
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. */
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
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
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
)
360 m
->bytepos
= from_byte
;
367 /* Starting at POS (BYTEPOS), find the byte position corresponding to
368 ENDPOS, which could be either before or after POS. */
370 count_bytes (ptrdiff_t pos
, ptrdiff_t bytepos
, ptrdiff_t endpos
)
372 eassert (BEG_BYTE
<= bytepos
&& bytepos
<= Z_BYTE
373 && BEG
<= endpos
&& endpos
<= Z
);
376 for ( ; pos
< endpos
; pos
++)
379 for ( ; pos
> endpos
; pos
--)
385 /* Adjust byte positions of markers when their character positions
386 didn't change. This is used in several places that replace text,
387 but keep the character positions of the markers unchanged -- the
388 byte positions could still change due to different numbers of bytes
391 FROM (FROM_BYTE) and TO (TO_BYTE) specify the region of text where
392 changes have been done. TO_Z, if non-zero, means all the markers
393 whose positions are after TO should also be adjusted. */
395 adjust_markers_bytepos (ptrdiff_t from
, ptrdiff_t from_byte
,
396 ptrdiff_t to
, ptrdiff_t to_byte
, int to_z
)
398 register struct Lisp_Marker
*m
;
399 ptrdiff_t beg
= from
, begbyte
= from_byte
;
401 adjust_suspend_auto_hscroll (from
, to
);
403 if (Z
== Z_BYTE
|| (!to_z
&& to
== to_byte
))
405 /* Make sure each affected marker's bytepos is equal to
407 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
409 if (m
->bytepos
> from_byte
410 && (to_z
|| m
->bytepos
<= to_byte
))
411 m
->bytepos
= m
->charpos
;
416 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
418 /* Recompute each affected marker's bytepos. */
419 if (m
->bytepos
> from_byte
420 && (to_z
|| m
->bytepos
<= to_byte
))
423 && beg
- m
->charpos
> m
->charpos
- from
)
428 m
->bytepos
= count_bytes (beg
, begbyte
, m
->charpos
);
430 begbyte
= m
->bytepos
;
435 /* Make sure cached charpos/bytepos is invalid. */
436 clear_charpos_cache (current_buffer
);
441 buffer_overflow (void)
443 error ("Maximum buffer size exceeded");
446 /* Make the gap NBYTES_ADDED bytes longer. */
449 make_gap_larger (ptrdiff_t nbytes_added
)
452 ptrdiff_t real_gap_loc
;
453 ptrdiff_t real_gap_loc_byte
;
454 ptrdiff_t old_gap_size
;
455 ptrdiff_t current_size
= Z_BYTE
- BEG_BYTE
+ GAP_SIZE
;
457 if (BUF_BYTES_MAX
- current_size
< nbytes_added
)
460 /* If we have to get more space, get enough to last a while;
461 but do not exceed the maximum buffer size. */
462 nbytes_added
= min (nbytes_added
+ GAP_BYTES_DFL
,
463 BUF_BYTES_MAX
- current_size
);
465 enlarge_buffer_text (current_buffer
, nbytes_added
);
467 /* Prevent quitting in gap_left. We cannot allow a QUIT there,
468 because that would leave the buffer text in an inconsistent
469 state, with 2 gap holes instead of just one. */
474 real_gap_loc_byte
= GPT_BYTE
;
475 old_gap_size
= GAP_SIZE
;
477 /* Call the newly allocated space a gap at the end of the whole space. */
479 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
480 GAP_SIZE
= nbytes_added
;
482 /* Move the new gap down to be consecutive with the end of the old one. */
483 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
485 /* Now combine the two into one large gap. */
486 GAP_SIZE
+= old_gap_size
;
488 GPT_BYTE
= real_gap_loc_byte
;
496 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
498 /* Make the gap NBYTES_REMOVED bytes shorter. */
501 make_gap_smaller (ptrdiff_t nbytes_removed
)
504 ptrdiff_t real_gap_loc
;
505 ptrdiff_t real_gap_loc_byte
;
507 ptrdiff_t real_Z_byte
;
508 ptrdiff_t real_beg_unchanged
;
509 ptrdiff_t new_gap_size
;
511 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
512 if (GAP_SIZE
- nbytes_removed
< GAP_BYTES_MIN
)
513 nbytes_removed
= GAP_SIZE
- GAP_BYTES_MIN
;
515 /* Prevent quitting in gap_right. We cannot allow a QUIT there,
516 because that would leave the buffer text in an inconsistent
517 state, with 2 gap holes instead of just one. */
522 real_gap_loc_byte
= GPT_BYTE
;
523 new_gap_size
= GAP_SIZE
- nbytes_removed
;
525 real_Z_byte
= Z_BYTE
;
526 real_beg_unchanged
= BEG_UNCHANGED
;
528 /* Pretend that the last unwanted part of the gap is the entire gap,
529 and that the first desired part of the gap is part of the buffer
531 memset (GPT_ADDR
, 0, new_gap_size
);
533 GPT_BYTE
+= new_gap_size
;
535 Z_BYTE
+= new_gap_size
;
536 GAP_SIZE
= nbytes_removed
;
538 /* Move the unwanted pretend gap to the end of the buffer. */
539 gap_right (Z
, Z_BYTE
);
541 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
543 /* Now restore the desired gap. */
544 GAP_SIZE
= new_gap_size
;
546 GPT_BYTE
= real_gap_loc_byte
;
548 Z_BYTE
= real_Z_byte
;
549 BEG_UNCHANGED
= real_beg_unchanged
;
557 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
560 make_gap (ptrdiff_t nbytes_added
)
562 if (nbytes_added
>= 0)
563 make_gap_larger (nbytes_added
);
564 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
566 make_gap_smaller (-nbytes_added
);
570 /* Add NBYTES to B's gap. It's enough to temporarily
571 fake current_buffer and avoid real switch to B. */
574 make_gap_1 (struct buffer
*b
, ptrdiff_t nbytes
)
576 struct buffer
*oldb
= current_buffer
;
580 current_buffer
= oldb
;
583 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
584 FROM_MULTIBYTE says whether the incoming text is multibyte.
585 TO_MULTIBYTE says whether to store the text as multibyte.
586 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
588 Return the number of bytes stored at TO_ADDR. */
591 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
592 ptrdiff_t nbytes
, bool from_multibyte
, bool to_multibyte
)
594 if (from_multibyte
== to_multibyte
)
596 memcpy (to_addr
, from_addr
, nbytes
);
599 else if (from_multibyte
)
601 ptrdiff_t nchars
= 0;
602 ptrdiff_t bytes_left
= nbytes
;
604 while (bytes_left
> 0)
607 c
= STRING_CHAR_AND_LENGTH (from_addr
, thislen
);
608 if (! ASCII_CHAR_P (c
))
611 from_addr
+= thislen
;
612 bytes_left
-= thislen
;
619 unsigned char *initial_to_addr
= to_addr
;
621 /* Convert single-byte to multibyte. */
624 int c
= *from_addr
++;
626 if (!ASCII_CHAR_P (c
))
628 c
= BYTE8_TO_CHAR (c
);
629 to_addr
+= CHAR_STRING (c
, to_addr
);
633 /* Special case for speed. */
634 *to_addr
++ = c
, nbytes
--;
636 return to_addr
- initial_to_addr
;
640 /* Insert a string of specified length before point.
641 This function judges multibyteness based on
642 enable_multibyte_characters in the current buffer;
643 it never converts between single-byte and multibyte.
645 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
646 prepare_to_modify_buffer could relocate the text. */
649 insert (const char *string
, ptrdiff_t nbytes
)
653 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
654 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
656 signal_after_change (opoint
, 0, len
);
657 update_compositions (opoint
, PT
, CHECK_BORDER
);
661 /* Likewise, but inherit text properties from neighboring characters. */
664 insert_and_inherit (const char *string
, ptrdiff_t nbytes
)
668 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
669 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
671 signal_after_change (opoint
, 0, len
);
672 update_compositions (opoint
, PT
, CHECK_BORDER
);
676 /* Insert the character C before point. Do not inherit text properties. */
681 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
684 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
685 len
= CHAR_STRING (c
, str
);
692 insert ((char *) str
, len
);
695 /* Insert the null-terminated string S before point. */
698 insert_string (const char *s
)
700 insert (s
, strlen (s
));
703 /* Like `insert' except that all markers pointing at the place where
704 the insertion happens are adjusted to point after it.
705 Don't use this function to insert part of a Lisp string,
706 since gc could happen and relocate it. */
709 insert_before_markers (const char *string
, ptrdiff_t nbytes
)
713 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
714 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
716 signal_after_change (opoint
, 0, len
);
717 update_compositions (opoint
, PT
, CHECK_BORDER
);
721 /* Likewise, but inherit text properties from neighboring characters. */
724 insert_before_markers_and_inherit (const char *string
,
729 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
730 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
732 signal_after_change (opoint
, 0, len
);
733 update_compositions (opoint
, PT
, CHECK_BORDER
);
737 #ifdef BYTE_COMBINING_DEBUG
739 /* See if the bytes before POS/POS_BYTE combine with bytes
740 at the start of STRING to form a single character.
741 If so, return the number of bytes at the start of STRING
742 which combine in this way. Otherwise, return 0. */
745 count_combining_before (const unsigned char *string
, ptrdiff_t length
,
746 ptrdiff_t pos
, ptrdiff_t pos_byte
)
748 int len
, combining_bytes
;
749 const unsigned char *p
;
751 if (NILP (current_buffer
->enable_multibyte_characters
))
754 /* At first, we can exclude the following cases:
755 (1) STRING[0] can't be a following byte of multibyte sequence.
756 (2) POS is the start of the current buffer.
757 (3) A character before POS is not a multibyte character. */
758 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
760 if (pos_byte
== BEG_BYTE
) /* case (2) */
763 p
= BYTE_POS_ADDR (pos_byte
- 1);
764 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
765 if (! LEADING_CODE_P (*p
)) /* case (3) */
768 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
769 if (combining_bytes
<= 0)
770 /* The character preceding POS is, complete and no room for
771 combining bytes (combining_bytes == 0), or an independent 8-bit
772 character (combining_bytes < 0). */
775 /* We have a combination situation. Count the bytes at STRING that
778 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
781 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
784 /* See if the bytes after POS/POS_BYTE combine with bytes
785 at the end of STRING to form a single character.
786 If so, return the number of bytes after POS/POS_BYTE
787 which combine in this way. Otherwise, return 0. */
790 count_combining_after (const unsigned char *string
,
791 ptrdiff_t length
, ptrdiff_t pos
, ptrdiff_t pos_byte
)
793 ptrdiff_t opos_byte
= pos_byte
;
798 if (NILP (current_buffer
->enable_multibyte_characters
))
801 /* At first, we can exclude the following cases:
802 (1) The last byte of STRING is an ASCII.
803 (2) POS is the last of the current buffer.
804 (3) A character at POS can't be a following byte of multibyte
806 if (length
> 0 && ASCII_CHAR_P (string
[length
- 1])) /* case (1) */
808 if (pos_byte
== Z_BYTE
) /* case (2) */
810 bufp
= BYTE_POS_ADDR (pos_byte
);
811 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
815 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
821 /* All characters in STRING are not character head. We must
822 check also preceding bytes at POS. We are sure that the gap
824 unsigned char *p
= BEG_ADDR
;
826 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
828 if (i
< 0 || !LEADING_CODE_P (p
[i
]))
831 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
832 return (bytes
<= pos_byte
- 1 - i
+ length
834 : bytes
- (pos_byte
- 1 - i
+ length
));
836 if (!LEADING_CODE_P (string
[i
]))
839 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
841 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
843 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
849 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
850 starting at STRING. INHERIT non-zero means inherit the text
851 properties from neighboring characters; zero means inserted text
852 will have no text properties. PREPARE non-zero means call
853 prepare_to_modify_buffer, which checks that the region is not
854 read-only, and calls before-change-function and any modification
855 properties the text may have. BEFORE_MARKERS non-zero means adjust
856 all markers that point at the insertion place to point after it. */
859 insert_1_both (const char *string
,
860 ptrdiff_t nchars
, ptrdiff_t nbytes
,
861 bool inherit
, bool prepare
, bool before_markers
)
866 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
870 /* Do this before moving and increasing the gap,
871 because the before-change hooks might move the gap
872 or make it smaller. */
873 prepare_to_modify_buffer (PT
, PT
, NULL
);
876 move_gap_both (PT
, PT_BYTE
);
877 if (GAP_SIZE
< nbytes
)
878 make_gap (nbytes
- GAP_SIZE
);
880 #ifdef BYTE_COMBINING_DEBUG
881 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
882 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
886 /* Record deletion of the surrounding text that combines with
887 the insertion. This, together with recording the insertion,
888 will add up to the right stuff in the undo list. */
889 record_insert (PT
, nchars
);
891 CHARS_MODIFF
= MODIFF
;
893 memcpy (GPT_ADDR
, string
, nbytes
);
902 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
904 eassert (GPT
<= GPT_BYTE
);
906 /* The insert may have been in the unchanged region, so check again. */
907 if (Z
- GPT
< END_UNCHANGED
)
908 END_UNCHANGED
= Z
- GPT
;
910 adjust_overlays_for_insert (PT
, nchars
);
911 adjust_markers_for_insert (PT
, PT_BYTE
,
912 PT
+ nchars
, PT_BYTE
+ nbytes
,
915 offset_intervals (current_buffer
, PT
, nchars
);
917 if (!inherit
&& buffer_intervals (current_buffer
))
918 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
921 adjust_point (nchars
, nbytes
);
926 /* Insert the part of the text of STRING, a Lisp object assumed to be
927 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
928 starting at position POS / POS_BYTE. If the text of STRING has properties,
929 copy them into the buffer.
931 It does not work to use `insert' for this, because a GC could happen
932 before we copy the stuff into the buffer, and relocate the string
933 without insert noticing. */
936 insert_from_string (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
937 ptrdiff_t length
, ptrdiff_t length_byte
, bool inherit
)
939 ptrdiff_t opoint
= PT
;
941 if (SCHARS (string
) == 0)
944 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
946 signal_after_change (opoint
, 0, PT
- opoint
);
947 update_compositions (opoint
, PT
, CHECK_BORDER
);
950 /* Like `insert_from_string' except that all markers pointing
951 at the place where the insertion happens are adjusted to point after it. */
954 insert_from_string_before_markers (Lisp_Object string
,
955 ptrdiff_t pos
, ptrdiff_t pos_byte
,
956 ptrdiff_t length
, ptrdiff_t length_byte
,
959 ptrdiff_t opoint
= PT
;
961 if (SCHARS (string
) == 0)
964 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
966 signal_after_change (opoint
, 0, PT
- opoint
);
967 update_compositions (opoint
, PT
, CHECK_BORDER
);
970 /* Subroutine of the insertion functions above. */
973 insert_from_string_1 (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
974 ptrdiff_t nchars
, ptrdiff_t nbytes
,
975 bool inherit
, bool before_markers
)
977 ptrdiff_t outgoing_nbytes
= nbytes
;
980 /* Make OUTGOING_NBYTES describe the text
981 as it will be inserted in this buffer. */
983 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
984 outgoing_nbytes
= nchars
;
985 else if (! STRING_MULTIBYTE (string
))
987 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
990 /* Do this before moving and increasing the gap,
991 because the before-change hooks might move the gap
992 or make it smaller. */
993 prepare_to_modify_buffer (PT
, PT
, NULL
);
996 move_gap_both (PT
, PT_BYTE
);
997 if (GAP_SIZE
< outgoing_nbytes
)
998 make_gap (outgoing_nbytes
- GAP_SIZE
);
1000 /* Copy the string text into the buffer, perhaps converting
1001 between single-byte and multibyte. */
1002 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
1003 STRING_MULTIBYTE (string
),
1004 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1006 #ifdef BYTE_COMBINING_DEBUG
1007 /* We have copied text into the gap, but we have not altered
1008 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1009 to these functions and get the same results as we would
1010 have got earlier on. Meanwhile, PT_ADDR does point to
1011 the text that has been stored by copy_text. */
1012 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1013 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1017 record_insert (PT
, nchars
);
1019 CHARS_MODIFF
= MODIFF
;
1021 GAP_SIZE
-= outgoing_nbytes
;
1025 GPT_BYTE
+= outgoing_nbytes
;
1026 ZV_BYTE
+= outgoing_nbytes
;
1027 Z_BYTE
+= outgoing_nbytes
;
1028 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1030 eassert (GPT
<= GPT_BYTE
);
1032 /* The insert may have been in the unchanged region, so check again. */
1033 if (Z
- GPT
< END_UNCHANGED
)
1034 END_UNCHANGED
= Z
- GPT
;
1036 adjust_overlays_for_insert (PT
, nchars
);
1037 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1038 PT_BYTE
+ outgoing_nbytes
,
1041 offset_intervals (current_buffer
, PT
, nchars
);
1043 intervals
= string_intervals (string
);
1044 /* Get the intervals for the part of the string we are inserting. */
1045 if (nbytes
< SBYTES (string
))
1046 intervals
= copy_intervals (intervals
, pos
, nchars
);
1048 /* Insert those intervals. */
1049 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1050 current_buffer
, inherit
);
1052 adjust_point (nchars
, outgoing_nbytes
);
1057 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1058 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
1059 GPT_ADDR (if not text_at_gap_tail). */
1062 insert_from_gap (ptrdiff_t nchars
, ptrdiff_t nbytes
, bool text_at_gap_tail
)
1064 ptrdiff_t ins_charpos
= GPT
, ins_bytepos
= GPT_BYTE
;
1066 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1069 /* No need to call prepare_to_modify_buffer, since this is called
1070 from places that replace some region with a different text, so
1071 prepare_to_modify_buffer was already called by the deletion part
1073 invalidate_buffer_caches (current_buffer
, GPT
, GPT
);
1074 record_insert (GPT
, nchars
);
1078 if (! text_at_gap_tail
)
1087 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1089 eassert (GPT
<= GPT_BYTE
);
1091 adjust_overlays_for_insert (ins_charpos
, nchars
);
1092 adjust_markers_for_insert (ins_charpos
, ins_bytepos
,
1093 ins_charpos
+ nchars
, ins_bytepos
+ nbytes
, 0);
1095 if (buffer_intervals (current_buffer
))
1097 offset_intervals (current_buffer
, ins_charpos
, nchars
);
1098 graft_intervals_into_buffer (NULL
, ins_charpos
, nchars
,
1102 if (ins_charpos
< PT
)
1103 adjust_point (nchars
, nbytes
);
1108 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1109 current buffer. If the text in BUF has properties, they are absorbed
1110 into the current buffer.
1112 It does not work to use `insert' for this, because a malloc could happen
1113 and relocate BUF's text before the copy happens. */
1116 insert_from_buffer (struct buffer
*buf
,
1117 ptrdiff_t charpos
, ptrdiff_t nchars
, bool inherit
)
1119 ptrdiff_t opoint
= PT
;
1121 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1122 signal_after_change (opoint
, 0, PT
- opoint
);
1123 update_compositions (opoint
, PT
, CHECK_BORDER
);
1127 insert_from_buffer_1 (struct buffer
*buf
,
1128 ptrdiff_t from
, ptrdiff_t nchars
, bool inherit
)
1130 ptrdiff_t chunk
, chunk_expanded
;
1131 ptrdiff_t from_byte
= buf_charpos_to_bytepos (buf
, from
);
1132 ptrdiff_t to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1133 ptrdiff_t incoming_nbytes
= to_byte
- from_byte
;
1134 ptrdiff_t outgoing_nbytes
= incoming_nbytes
;
1140 /* Make OUTGOING_NBYTES describe the text
1141 as it will be inserted in this buffer. */
1143 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1144 outgoing_nbytes
= nchars
;
1145 else if (NILP (BVAR (buf
, enable_multibyte_characters
)))
1147 ptrdiff_t outgoing_before_gap
= 0;
1148 ptrdiff_t outgoing_after_gap
= 0;
1150 if (from
< BUF_GPT (buf
))
1152 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1153 if (chunk
> incoming_nbytes
)
1154 chunk
= incoming_nbytes
;
1156 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1162 if (chunk
< incoming_nbytes
)
1164 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1166 incoming_nbytes
- chunk
);
1168 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1171 /* Do this before moving and increasing the gap,
1172 because the before-change hooks might move the gap
1173 or make it smaller. */
1174 prepare_to_modify_buffer (PT
, PT
, NULL
);
1177 move_gap_both (PT
, PT_BYTE
);
1178 if (GAP_SIZE
< outgoing_nbytes
)
1179 make_gap (outgoing_nbytes
- GAP_SIZE
);
1181 if (from
< BUF_GPT (buf
))
1183 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1184 if (chunk
> incoming_nbytes
)
1185 chunk
= incoming_nbytes
;
1186 /* Record number of output bytes, so we know where
1187 to put the output from the second copy_text. */
1189 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1191 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1192 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1195 chunk_expanded
= chunk
= 0;
1197 if (chunk
< incoming_nbytes
)
1198 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1199 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1200 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1201 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1203 #ifdef BYTE_COMBINING_DEBUG
1204 /* We have copied text into the gap, but we have not altered
1205 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1206 to these functions and get the same results as we would
1207 have got earlier on. Meanwhile, GPT_ADDR does point to
1208 the text that has been stored by copy_text. */
1209 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1210 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1214 record_insert (PT
, nchars
);
1216 CHARS_MODIFF
= MODIFF
;
1218 GAP_SIZE
-= outgoing_nbytes
;
1222 GPT_BYTE
+= outgoing_nbytes
;
1223 ZV_BYTE
+= outgoing_nbytes
;
1224 Z_BYTE
+= outgoing_nbytes
;
1225 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1227 eassert (GPT
<= GPT_BYTE
);
1229 /* The insert may have been in the unchanged region, so check again. */
1230 if (Z
- GPT
< END_UNCHANGED
)
1231 END_UNCHANGED
= Z
- GPT
;
1233 adjust_overlays_for_insert (PT
, nchars
);
1234 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1235 PT_BYTE
+ outgoing_nbytes
,
1238 offset_intervals (current_buffer
, PT
, nchars
);
1240 /* Get the intervals for the part of the string we are inserting. */
1241 intervals
= buffer_intervals (buf
);
1242 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1244 if (buf
== current_buffer
&& PT
<= from
)
1246 intervals
= copy_intervals (intervals
, from
, nchars
);
1249 /* Insert those intervals. */
1250 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1252 adjust_point (nchars
, outgoing_nbytes
);
1255 /* Record undo information and adjust markers and position keepers for
1256 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1257 chars (LEN_BYTE bytes) which resides in the gap just after
1260 PREV_TEXT nil means the new text was just inserted. */
1263 adjust_after_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
1264 Lisp_Object prev_text
, ptrdiff_t len
, ptrdiff_t len_byte
)
1266 ptrdiff_t nchars_del
= 0, nbytes_del
= 0;
1268 #ifdef BYTE_COMBINING_DEBUG
1269 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1270 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1274 if (STRINGP (prev_text
))
1276 nchars_del
= SCHARS (prev_text
);
1277 nbytes_del
= SBYTES (prev_text
);
1280 /* Update various buffer positions for the new text. */
1281 GAP_SIZE
-= len_byte
;
1282 ZV
+= len
; Z
+= len
;
1283 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1284 GPT
+= len
; GPT_BYTE
+= len_byte
;
1285 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1288 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1291 adjust_markers_for_insert (from
, from_byte
,
1292 from
+ len
, from_byte
+ len_byte
, 0);
1295 record_delete (from
, prev_text
, false);
1296 record_insert (from
, len
);
1298 if (len
> nchars_del
)
1299 adjust_overlays_for_insert (from
, len
- nchars_del
);
1300 else if (len
< nchars_del
)
1301 adjust_overlays_for_delete (from
, nchars_del
- len
);
1303 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1306 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1308 /* As byte combining will decrease Z, we must check this again. */
1309 if (Z
- GPT
< END_UNCHANGED
)
1310 END_UNCHANGED
= Z
- GPT
;
1315 evaporate_overlays (from
);
1317 CHARS_MODIFF
= MODIFF
;
1320 /* Record undo information, adjust markers and position keepers for an
1321 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1322 text already exists in the current buffer but character length (TO
1323 - FROM) may be incorrect, the correct length is NEWLEN. */
1326 adjust_after_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
1327 ptrdiff_t to
, ptrdiff_t to_byte
, ptrdiff_t newlen
)
1329 ptrdiff_t len
= to
- from
, len_byte
= to_byte
- from_byte
;
1332 move_gap_both (to
, to_byte
);
1333 GAP_SIZE
+= len_byte
;
1334 GPT
-= len
; GPT_BYTE
-= len_byte
;
1335 ZV
-= len
; ZV_BYTE
-= len_byte
;
1336 Z
-= len
; Z_BYTE
-= len_byte
;
1337 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1340 /* Replace the text from character positions FROM to TO with NEW,
1341 If PREPARE, call prepare_to_modify_buffer.
1342 If INHERIT, the newly inserted text should inherit text properties
1343 from the surrounding non-deleted text.
1344 If ADJUST_MATCH_DATA, then adjust the match data before calling
1345 signal_after_change. */
1347 /* Note that this does not yet handle markers quite right.
1348 Also it needs to record a single undo-entry that does a replacement
1349 rather than a separate delete and insert.
1350 That way, undo will also handle markers properly.
1352 But if MARKERS is 0, don't relocate markers. */
1355 replace_range (ptrdiff_t from
, ptrdiff_t to
, Lisp_Object
new,
1356 bool prepare
, bool inherit
, bool markers
,
1357 bool adjust_match_data
)
1359 ptrdiff_t inschars
= SCHARS (new);
1360 ptrdiff_t insbytes
= SBYTES (new);
1361 ptrdiff_t from_byte
, to_byte
;
1362 ptrdiff_t nbytes_del
, nchars_del
;
1364 ptrdiff_t outgoing_insbytes
= insbytes
;
1365 Lisp_Object deletion
;
1373 ptrdiff_t range_length
= to
- from
;
1374 prepare_to_modify_buffer (from
, to
, &from
);
1375 to
= from
+ range_length
;
1378 /* Make args be valid. */
1384 from_byte
= CHAR_TO_BYTE (from
);
1385 to_byte
= CHAR_TO_BYTE (to
);
1387 nchars_del
= to
- from
;
1388 nbytes_del
= to_byte
- from_byte
;
1390 if (nbytes_del
<= 0 && insbytes
== 0)
1393 /* Make OUTGOING_INSBYTES describe the text
1394 as it will be inserted in this buffer. */
1396 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1397 outgoing_insbytes
= inschars
;
1398 else if (! STRING_MULTIBYTE (new))
1400 = count_size_as_multibyte (SDATA (new), insbytes
);
1402 /* Make sure the gap is somewhere in or next to what we are deleting. */
1404 gap_right (from
, from_byte
);
1406 gap_left (to
, to_byte
, 0);
1408 /* Even if we don't record for undo, we must keep the original text
1409 because we may have to recover it because of inappropriate byte
1411 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1412 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1414 GAP_SIZE
+= nbytes_del
;
1417 ZV_BYTE
-= nbytes_del
;
1418 Z_BYTE
-= nbytes_del
;
1420 GPT_BYTE
= from_byte
;
1421 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1423 eassert (GPT
<= GPT_BYTE
);
1425 if (GPT
- BEG
< BEG_UNCHANGED
)
1426 BEG_UNCHANGED
= GPT
- BEG
;
1427 if (Z
- GPT
< END_UNCHANGED
)
1428 END_UNCHANGED
= Z
- GPT
;
1430 if (GAP_SIZE
< outgoing_insbytes
)
1431 make_gap (outgoing_insbytes
- GAP_SIZE
);
1433 /* Copy the string text into the buffer, perhaps converting
1434 between single-byte and multibyte. */
1435 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1436 STRING_MULTIBYTE (new),
1437 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1439 #ifdef BYTE_COMBINING_DEBUG
1440 /* We have copied text into the gap, but we have not marked
1441 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1442 here, for both the previous text and the following text.
1443 Meanwhile, GPT_ADDR does point to
1444 the text that has been stored by copy_text. */
1445 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1446 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1450 /* Record the insertion first, so that when we undo,
1451 the deletion will be undone first. Thus, undo
1452 will insert before deleting, and thus will keep
1453 the markers before and after this text separate. */
1454 if (!NILP (deletion
))
1456 record_insert (from
+ SCHARS (deletion
), inschars
);
1457 record_delete (from
, deletion
, false);
1460 GAP_SIZE
-= outgoing_insbytes
;
1464 GPT_BYTE
+= outgoing_insbytes
;
1465 ZV_BYTE
+= outgoing_insbytes
;
1466 Z_BYTE
+= outgoing_insbytes
;
1467 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1469 eassert (GPT
<= GPT_BYTE
);
1471 /* Adjust markers for the deletion and the insertion. */
1473 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1474 inschars
, outgoing_insbytes
);
1477 /* The character positions of the markers remain intact, but we
1478 still need to update their byte positions, because the
1479 deleted and the inserted text might have multibyte sequences
1480 which make the original byte positions of the markers
1482 adjust_markers_bytepos (from
, from_byte
, from
+ inschars
,
1483 from_byte
+ outgoing_insbytes
, 1);
1486 /* Adjust the overlay center as needed. This must be done after
1487 adjusting the markers that bound the overlays. */
1488 adjust_overlays_for_delete (from
, nchars_del
);
1489 adjust_overlays_for_insert (from
, inschars
);
1491 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1493 /* Get the intervals for the part of the string we are inserting--
1494 not including the combined-before bytes. */
1495 intervals
= string_intervals (new);
1496 /* Insert those intervals. */
1497 graft_intervals_into_buffer (intervals
, from
, inschars
,
1498 current_buffer
, inherit
);
1500 /* Relocate point as if it were a marker. */
1502 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1503 (from_byte
+ outgoing_insbytes
1504 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1506 if (outgoing_insbytes
== 0)
1507 evaporate_overlays (from
);
1512 CHARS_MODIFF
= MODIFF
;
1514 if (adjust_match_data
)
1515 update_search_regs (from
, to
, from
+ SCHARS (new));
1517 signal_after_change (from
, nchars_del
, GPT
- from
);
1518 update_compositions (from
, GPT
, CHECK_BORDER
);
1521 /* Replace the text from character positions FROM to TO with
1522 the text in INS of length INSCHARS.
1523 Keep the text properties that applied to the old characters
1524 (extending them to all the new chars if there are more new chars).
1526 Note that this does not yet handle markers quite right.
1528 If MARKERS, relocate markers.
1530 Unlike most functions at this level, never call
1531 prepare_to_modify_buffer and never call signal_after_change. */
1534 replace_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1535 ptrdiff_t to
, ptrdiff_t to_byte
,
1536 const char *ins
, ptrdiff_t inschars
, ptrdiff_t insbytes
,
1539 ptrdiff_t nbytes_del
, nchars_del
;
1543 nchars_del
= to
- from
;
1544 nbytes_del
= to_byte
- from_byte
;
1546 if (nbytes_del
<= 0 && insbytes
== 0)
1549 /* Make sure the gap is somewhere in or next to what we are deleting. */
1551 gap_right (from
, from_byte
);
1553 gap_left (to
, to_byte
, 0);
1555 GAP_SIZE
+= nbytes_del
;
1558 ZV_BYTE
-= nbytes_del
;
1559 Z_BYTE
-= nbytes_del
;
1561 GPT_BYTE
= from_byte
;
1562 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1564 eassert (GPT
<= GPT_BYTE
);
1566 if (GPT
- BEG
< BEG_UNCHANGED
)
1567 BEG_UNCHANGED
= GPT
- BEG
;
1568 if (Z
- GPT
< END_UNCHANGED
)
1569 END_UNCHANGED
= Z
- GPT
;
1571 if (GAP_SIZE
< insbytes
)
1572 make_gap (insbytes
- GAP_SIZE
);
1574 /* Copy the replacement text into the buffer. */
1575 memcpy (GPT_ADDR
, ins
, insbytes
);
1577 #ifdef BYTE_COMBINING_DEBUG
1578 /* We have copied text into the gap, but we have not marked
1579 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1580 here, for both the previous text and the following text.
1581 Meanwhile, GPT_ADDR does point to
1582 the text that has been stored by copy_text. */
1583 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1584 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1588 GAP_SIZE
-= insbytes
;
1592 GPT_BYTE
+= insbytes
;
1593 ZV_BYTE
+= insbytes
;
1595 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1597 eassert (GPT
<= GPT_BYTE
);
1599 /* Adjust markers for the deletion and the insertion. */
1600 if (! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1603 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1604 inschars
, insbytes
);
1607 /* The character positions of the markers remain intact, but
1608 we still need to update their byte positions, because the
1609 deleted and the inserted text might have multibyte
1610 sequences which make the original byte positions of the
1612 adjust_markers_bytepos (from
, from_byte
, from
+ inschars
,
1613 from_byte
+ insbytes
, 1);
1617 /* Adjust the overlay center as needed. This must be done after
1618 adjusting the markers that bound the overlays. */
1619 if (nchars_del
!= inschars
)
1621 adjust_overlays_for_insert (from
, inschars
);
1622 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1625 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1627 /* Relocate point as if it were a marker. */
1628 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1631 /* PT was within the deleted text. Move it to FROM. */
1632 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1634 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1638 evaporate_overlays (from
);
1643 CHARS_MODIFF
= MODIFF
;
1646 /* Delete characters in current buffer
1647 from FROM up to (but not including) TO.
1648 If TO comes before FROM, we delete nothing. */
1651 del_range (ptrdiff_t from
, ptrdiff_t to
)
1653 del_range_1 (from
, to
, 1, 0);
1656 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1657 RET_STRING says to return the deleted text. */
1660 del_range_1 (ptrdiff_t from
, ptrdiff_t to
, bool prepare
, bool ret_string
)
1662 ptrdiff_t from_byte
, to_byte
;
1663 Lisp_Object deletion
;
1665 /* Make args be valid */
1676 ptrdiff_t range_length
= to
- from
;
1677 prepare_to_modify_buffer (from
, to
, &from
);
1678 to
= min (ZV
, from
+ range_length
);
1681 from_byte
= CHAR_TO_BYTE (from
);
1682 to_byte
= CHAR_TO_BYTE (to
);
1684 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1685 signal_after_change (from
, to
- from
, 0);
1686 update_compositions (from
, from
, CHECK_HEAD
);
1690 /* Like del_range_1 but args are byte positions, not char positions. */
1693 del_range_byte (ptrdiff_t from_byte
, ptrdiff_t to_byte
)
1697 /* Make args be valid. */
1698 if (from_byte
< BEGV_BYTE
)
1699 from_byte
= BEGV_BYTE
;
1700 if (to_byte
> ZV_BYTE
)
1703 if (to_byte
<= from_byte
)
1706 from
= BYTE_TO_CHAR (from_byte
);
1707 to
= BYTE_TO_CHAR (to_byte
);
1710 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1711 ptrdiff_t range_length
= to
- from
;
1712 prepare_to_modify_buffer (from
, to
, &from
);
1713 to
= from
+ range_length
;
1715 if (old_from
!= from
)
1716 from_byte
= CHAR_TO_BYTE (from
);
1722 else if (old_to
== Z
- to
)
1723 to_byte
= CHAR_TO_BYTE (to
);
1726 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1727 signal_after_change (from
, to
- from
, 0);
1728 update_compositions (from
, from
, CHECK_HEAD
);
1731 /* Like del_range_1, but positions are specified both as charpos
1735 del_range_both (ptrdiff_t from
, ptrdiff_t from_byte
,
1736 ptrdiff_t to
, ptrdiff_t to_byte
, bool prepare
)
1738 /* Make args be valid */
1739 if (from_byte
< BEGV_BYTE
)
1740 from_byte
= BEGV_BYTE
;
1741 if (to_byte
> ZV_BYTE
)
1744 if (to_byte
<= from_byte
)
1754 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1755 ptrdiff_t range_length
= to
- from
;
1756 prepare_to_modify_buffer (from
, to
, &from
);
1757 to
= from
+ range_length
;
1759 if (old_from
!= from
)
1760 from_byte
= CHAR_TO_BYTE (from
);
1766 else if (old_to
== Z
- to
)
1767 to_byte
= CHAR_TO_BYTE (to
);
1770 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1771 signal_after_change (from
, to
- from
, 0);
1772 update_compositions (from
, from
, CHECK_HEAD
);
1775 /* Delete a range of text, specified both as character positions
1776 and byte positions. FROM and TO are character positions,
1777 while FROM_BYTE and TO_BYTE are byte positions.
1778 If RET_STRING, the deleted area is returned as a string. */
1781 del_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1782 ptrdiff_t to
, ptrdiff_t to_byte
, bool ret_string
)
1784 ptrdiff_t nbytes_del
, nchars_del
;
1785 Lisp_Object deletion
;
1789 nchars_del
= to
- from
;
1790 nbytes_del
= to_byte
- from_byte
;
1792 /* Make sure the gap is somewhere in or next to what we are deleting. */
1794 gap_right (from
, from_byte
);
1796 gap_left (to
, to_byte
, 0);
1798 #ifdef BYTE_COMBINING_DEBUG
1799 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1800 Z_BYTE
- to_byte
, from
, from_byte
))
1804 if (ret_string
|| ! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1805 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1809 /* Record marker adjustments, and text deletion into undo
1811 record_delete (from
, deletion
, true);
1813 /* Relocate all markers pointing into the new, larger gap to point
1814 at the end of the text before the gap. */
1815 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1818 CHARS_MODIFF
= MODIFF
;
1820 /* Relocate point as if it were a marker. */
1822 adjust_point (from
- (PT
< to
? PT
: to
),
1823 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1825 offset_intervals (current_buffer
, from
, - nchars_del
);
1827 /* Adjust the overlay center as needed. This must be done after
1828 adjusting the markers that bound the overlays. */
1829 adjust_overlays_for_delete (from
, nchars_del
);
1831 GAP_SIZE
+= nbytes_del
;
1832 ZV_BYTE
-= nbytes_del
;
1833 Z_BYTE
-= nbytes_del
;
1837 GPT_BYTE
= from_byte
;
1838 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1839 /* Put an anchor, unless called from decode_coding_object which
1840 needs to access the previous gap contents. */
1843 eassert (GPT
<= GPT_BYTE
);
1845 if (GPT
- BEG
< BEG_UNCHANGED
)
1846 BEG_UNCHANGED
= GPT
- BEG
;
1847 if (Z
- GPT
< END_UNCHANGED
)
1848 END_UNCHANGED
= Z
- GPT
;
1852 evaporate_overlays (from
);
1857 /* Call this if you're about to change the text of current buffer
1858 from character positions START to END. This checks the read-only
1859 properties of the region, calls the necessary modification hooks,
1860 and warns the next redisplay that it should pay attention to that
1864 modify_text (ptrdiff_t start
, ptrdiff_t end
)
1866 prepare_to_modify_buffer (start
, end
, NULL
);
1868 BUF_COMPUTE_UNCHANGED (current_buffer
, start
- 1, end
);
1869 if (MODIFF
<= SAVE_MODIFF
)
1870 record_first_change ();
1872 CHARS_MODIFF
= MODIFF
;
1874 bset_point_before_scroll (current_buffer
, Qnil
);
1877 /* Signal that we are about to make a change that may result in new
1881 run_undoable_change (void)
1883 if (EQ (BVAR (current_buffer
, undo_list
), Qt
))
1886 call0 (Qundo_auto__undoable_change
);
1889 /* Check that it is okay to modify the buffer between START and END,
1890 which are char positions.
1892 Run the before-change-function, if any. If intervals are in use,
1893 verify that the text to be modified is not read-only, and call
1894 any modification properties the text may have.
1896 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1897 by holding its value temporarily in a marker.
1899 This function runs Lisp, which means it can GC, which means it can
1900 compact buffers, including the current buffer being worked on here.
1901 So don't you dare calling this function while manipulating the gap,
1902 or during some other similar "critical section". */
1905 prepare_to_modify_buffer_1 (ptrdiff_t start
, ptrdiff_t end
,
1906 ptrdiff_t *preserve_ptr
)
1908 struct buffer
*base_buffer
;
1911 XSETFASTINT (temp
, start
);
1912 if (!NILP (BVAR (current_buffer
, read_only
)))
1913 Fbarf_if_buffer_read_only (temp
);
1915 run_undoable_change();
1917 bset_redisplay (current_buffer
);
1919 if (buffer_intervals (current_buffer
))
1923 Lisp_Object preserve_marker
;
1924 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1925 verify_interval_modification (current_buffer
, start
, end
);
1926 *preserve_ptr
= marker_position (preserve_marker
);
1927 unchain_marker (XMARKER (preserve_marker
));
1930 verify_interval_modification (current_buffer
, start
, end
);
1933 /* For indirect buffers, use the base buffer to check clashes. */
1934 if (current_buffer
->base_buffer
!= 0)
1935 base_buffer
= current_buffer
->base_buffer
;
1937 base_buffer
= current_buffer
;
1939 if (inhibit_modification_hooks
)
1942 if (!NILP (BVAR (base_buffer
, file_truename
))
1943 /* Make binding buffer-file-name to nil effective. */
1944 && !NILP (BVAR (base_buffer
, filename
))
1945 && SAVE_MODIFF
>= MODIFF
)
1946 lock_file (BVAR (base_buffer
, file_truename
));
1948 /* If `select-active-regions' is non-nil, save the region text. */
1949 /* FIXME: Move this to Elisp (via before-change-functions). */
1950 if (!NILP (BVAR (current_buffer
, mark_active
))
1951 && XMARKER (BVAR (current_buffer
, mark
))->buffer
1952 && NILP (Vsaved_region_selection
)
1953 && (EQ (Vselect_active_regions
, Qonly
)
1954 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
1955 : (!NILP (Vselect_active_regions
)
1956 && !NILP (Vtransient_mark_mode
))))
1957 Vsaved_region_selection
1958 = call1 (Fsymbol_value (Qregion_extract_function
), Qnil
);
1960 signal_before_change (start
, end
, preserve_ptr
);
1961 Fset (Qdeactivate_mark
, Qt
);
1964 /* Like above, but called when we know that the buffer text
1965 will be modified and region caches should be invalidated. */
1968 prepare_to_modify_buffer (ptrdiff_t start
, ptrdiff_t end
,
1969 ptrdiff_t *preserve_ptr
)
1971 prepare_to_modify_buffer_1 (start
, end
, preserve_ptr
);
1972 invalidate_buffer_caches (current_buffer
, start
, end
);
1975 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1976 region between buffer positions START and END. */
1978 invalidate_buffer_caches (struct buffer
*buf
, ptrdiff_t start
, ptrdiff_t end
)
1980 /* Indirect buffers usually have their caches set to NULL, but we
1981 need to consider the caches of their base buffer. */
1982 if (buf
->base_buffer
)
1983 buf
= buf
->base_buffer
;
1984 /* The bidi_paragraph_cache must be invalidated first, because doing
1985 so might need to use the newline_cache (via find_newline_no_quit,
1987 if (buf
->bidi_paragraph_cache
)
1990 && start
> BUF_BEG (buf
))
1992 /* If we are deleting or replacing characters, we could
1993 create a paragraph start, because all of the characters
1994 from START to the beginning of START's line are
1995 whitespace. Therefore, we must extend the region to be
1996 invalidated up to the newline before START. */
1997 ptrdiff_t line_beg
= start
;
1998 ptrdiff_t start_byte
= buf_charpos_to_bytepos (buf
, start
);
2000 if (BUF_FETCH_BYTE (buf
, start_byte
- 1) != '\n')
2002 struct buffer
*old
= current_buffer
;
2004 set_buffer_internal (buf
);
2006 line_beg
= find_newline_no_quit (start
, start_byte
, -1,
2008 set_buffer_internal (old
);
2010 start
= line_beg
- (line_beg
> BUF_BEG (buf
));
2012 invalidate_region_cache (buf
,
2013 buf
->bidi_paragraph_cache
,
2014 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
2016 if (buf
->newline_cache
)
2017 invalidate_region_cache (buf
,
2019 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
2020 if (buf
->width_run_cache
)
2021 invalidate_region_cache (buf
,
2022 buf
->width_run_cache
,
2023 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
2026 /* These macros work with an argument named `preserve_ptr'
2027 and a local variable named `preserve_marker'. */
2029 #define PRESERVE_VALUE \
2030 if (preserve_ptr && NILP (preserve_marker)) \
2031 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2033 #define RESTORE_VALUE \
2034 if (! NILP (preserve_marker)) \
2036 *preserve_ptr = marker_position (preserve_marker); \
2037 unchain_marker (XMARKER (preserve_marker)); \
2040 #define PRESERVE_START_END \
2041 if (NILP (start_marker)) \
2042 start_marker = Fcopy_marker (start, Qnil); \
2043 if (NILP (end_marker)) \
2044 end_marker = Fcopy_marker (end, Qnil);
2046 #define FETCH_START \
2047 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2050 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2052 /* Set a variable to nil if an error occurred.
2053 Don't change the variable if there was no error.
2054 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2055 VARIABLE is the variable to maybe set to nil.
2056 NO-ERROR-FLAG is nil if there was an error,
2057 anything else meaning no error (so this function does nothing). */
2060 Lisp_Object
*location
;
2065 reset_var_on_error (void *ptr
)
2067 struct rvoe_arg
*p
= ptr
;
2069 *p
->location
= Qnil
;
2072 /* Signal a change to the buffer immediately before it happens.
2073 START_INT and END_INT are the bounds of the text to be changed.
2075 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2076 by holding its value temporarily in a marker. */
2079 signal_before_change (ptrdiff_t start_int
, ptrdiff_t end_int
,
2080 ptrdiff_t *preserve_ptr
)
2082 Lisp_Object start
, end
;
2083 Lisp_Object start_marker
, end_marker
;
2084 Lisp_Object preserve_marker
;
2085 ptrdiff_t count
= SPECPDL_INDEX ();
2086 struct rvoe_arg rvoe_arg
;
2088 start
= make_number (start_int
);
2089 end
= make_number (end_int
);
2090 preserve_marker
= Qnil
;
2091 start_marker
= Qnil
;
2094 specbind (Qinhibit_modification_hooks
, Qt
);
2096 /* If buffer is unmodified, run a special hook for that case. The
2097 check for Vfirst_change_hook is just a minor optimization. */
2098 if (SAVE_MODIFF
>= MODIFF
2099 && !NILP (Vfirst_change_hook
))
2103 run_hook (Qfirst_change_hook
);
2106 /* Now run the before-change-functions if any. */
2107 if (!NILP (Vbefore_change_functions
))
2109 rvoe_arg
.location
= &Vbefore_change_functions
;
2110 rvoe_arg
.errorp
= 1;
2115 /* Mark before-change-functions to be reset to nil in case of error. */
2116 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2118 /* Actually run the hook functions. */
2119 CALLN (Frun_hook_with_args
, Qbefore_change_functions
,
2120 FETCH_START
, FETCH_END
);
2122 /* There was no error: unarm the reset_on_error. */
2123 rvoe_arg
.errorp
= 0;
2126 if (buffer_has_overlays ())
2129 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2130 FETCH_START
, FETCH_END
, Qnil
);
2133 if (! NILP (start_marker
))
2134 free_marker (start_marker
);
2135 if (! NILP (end_marker
))
2136 free_marker (end_marker
);
2139 unbind_to (count
, Qnil
);
2142 /* Signal a change immediately after it happens.
2143 CHARPOS is the character position of the start of the changed text.
2144 LENDEL is the number of characters of the text before the change.
2145 (Not the whole buffer; just the part that was changed.)
2146 LENINS is the number of characters in that part of the text
2147 after the change. */
2150 signal_after_change (ptrdiff_t charpos
, ptrdiff_t lendel
, ptrdiff_t lenins
)
2152 ptrdiff_t count
= SPECPDL_INDEX ();
2153 struct rvoe_arg rvoe_arg
;
2155 if (inhibit_modification_hooks
)
2158 /* If we are deferring calls to the after-change functions
2159 and there are no before-change functions,
2160 just record the args that we were going to use. */
2161 if (! NILP (Vcombine_after_change_calls
)
2162 && NILP (Vbefore_change_functions
)
2163 && !buffer_has_overlays ())
2167 if (!NILP (combine_after_change_list
)
2168 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2169 Fcombine_after_change_execute ();
2171 elt
= list3i (charpos
- BEG
, Z
- (charpos
- lendel
+ lenins
),
2173 combine_after_change_list
2174 = Fcons (elt
, combine_after_change_list
);
2175 combine_after_change_buffer
= Fcurrent_buffer ();
2180 if (!NILP (combine_after_change_list
))
2181 Fcombine_after_change_execute ();
2183 specbind (Qinhibit_modification_hooks
, Qt
);
2185 if (!NILP (Vafter_change_functions
))
2187 rvoe_arg
.location
= &Vafter_change_functions
;
2188 rvoe_arg
.errorp
= 1;
2190 /* Mark after-change-functions to be reset to nil in case of error. */
2191 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2193 /* Actually run the hook functions. */
2194 CALLN (Frun_hook_with_args
, Qafter_change_functions
,
2195 make_number (charpos
), make_number (charpos
+ lenins
),
2196 make_number (lendel
));
2198 /* There was no error: unarm the reset_on_error. */
2199 rvoe_arg
.errorp
= 0;
2202 if (buffer_has_overlays ())
2203 report_overlay_modification (make_number (charpos
),
2204 make_number (charpos
+ lenins
),
2206 make_number (charpos
),
2207 make_number (charpos
+ lenins
),
2208 make_number (lendel
));
2210 /* After an insertion, call the text properties
2211 insert-behind-hooks or insert-in-front-hooks. */
2213 report_interval_modification (make_number (charpos
),
2214 make_number (charpos
+ lenins
));
2216 unbind_to (count
, Qnil
);
2220 Fcombine_after_change_execute_1 (Lisp_Object val
)
2222 Vcombine_after_change_calls
= val
;
2225 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2226 Scombine_after_change_execute
, 0, 0, 0,
2227 doc
: /* This function is for use internally in the function `combine-after-change-calls'. */)
2230 ptrdiff_t count
= SPECPDL_INDEX ();
2231 ptrdiff_t beg
, end
, change
;
2232 ptrdiff_t begpos
, endpos
;
2235 if (NILP (combine_after_change_list
))
2238 /* It is rare for combine_after_change_buffer to be invalid, but
2239 possible. It can happen when combine-after-change-calls is
2240 non-nil, and insertion calls a file handler (e.g. through
2241 lock_file) which scribbles into a temp file -- cyd */
2242 if (!BUFFERP (combine_after_change_buffer
)
2243 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer
)))
2245 combine_after_change_list
= Qnil
;
2249 record_unwind_current_buffer ();
2251 Fset_buffer (combine_after_change_buffer
);
2253 /* # chars unchanged at beginning of buffer. */
2255 /* # chars unchanged at end of buffer. */
2257 /* Total amount of insertion (negative for deletion). */
2260 /* Scan the various individual changes,
2261 accumulating the range info in BEG, END and CHANGE. */
2262 for (tail
= combine_after_change_list
; CONSP (tail
);
2266 ptrdiff_t thisbeg
, thisend
, thischange
;
2268 /* Extract the info from the next element. */
2272 thisbeg
= XINT (XCAR (elt
));
2277 thisend
= XINT (XCAR (elt
));
2282 thischange
= XINT (XCAR (elt
));
2284 /* Merge this range into the accumulated range. */
2285 change
+= thischange
;
2292 /* Get the current start and end positions of the range
2293 that was changed. */
2297 /* We are about to handle these, so discard them. */
2298 combine_after_change_list
= Qnil
;
2300 /* Now run the after-change functions for real.
2301 Turn off the flag that defers them. */
2302 record_unwind_protect (Fcombine_after_change_execute_1
,
2303 Vcombine_after_change_calls
);
2304 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2305 update_compositions (begpos
, endpos
, CHECK_ALL
);
2307 return unbind_to (count
, Qnil
);
2311 syms_of_insdel (void)
2313 staticpro (&combine_after_change_list
);
2314 staticpro (&combine_after_change_buffer
);
2315 combine_after_change_list
= Qnil
;
2316 combine_after_change_buffer
= Qnil
;
2318 DEFSYM (Qundo_auto__undoable_change
, "undo-auto--undoable-change");
2320 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls
,
2321 doc
: /* Used internally by the function `combine-after-change-calls' macro. */);
2322 Vcombine_after_change_calls
= Qnil
;
2324 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks
,
2325 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2326 This affects `before-change-functions' and `after-change-functions',
2327 as well as hooks attached to text properties and overlays. */);
2328 inhibit_modification_hooks
= 0;
2329 DEFSYM (Qinhibit_modification_hooks
, "inhibit-modification-hooks");
2331 DEFSYM (Qregion_extract_function
, "region-extract-function");
2333 defsubr (&Scombine_after_change_execute
);