1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2016 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. */
1345 /* Note that this does not yet handle markers quite right.
1346 Also it needs to record a single undo-entry that does a replacement
1347 rather than a separate delete and insert.
1348 That way, undo will also handle markers properly.
1350 But if MARKERS is 0, don't relocate markers. */
1353 replace_range (ptrdiff_t from
, ptrdiff_t to
, Lisp_Object
new,
1354 bool prepare
, bool inherit
, bool markers
)
1356 ptrdiff_t inschars
= SCHARS (new);
1357 ptrdiff_t insbytes
= SBYTES (new);
1358 ptrdiff_t from_byte
, to_byte
;
1359 ptrdiff_t nbytes_del
, nchars_del
;
1361 ptrdiff_t outgoing_insbytes
= insbytes
;
1362 Lisp_Object deletion
;
1370 ptrdiff_t range_length
= to
- from
;
1371 prepare_to_modify_buffer (from
, to
, &from
);
1372 to
= from
+ range_length
;
1375 /* Make args be valid. */
1381 from_byte
= CHAR_TO_BYTE (from
);
1382 to_byte
= CHAR_TO_BYTE (to
);
1384 nchars_del
= to
- from
;
1385 nbytes_del
= to_byte
- from_byte
;
1387 if (nbytes_del
<= 0 && insbytes
== 0)
1390 /* Make OUTGOING_INSBYTES describe the text
1391 as it will be inserted in this buffer. */
1393 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1394 outgoing_insbytes
= inschars
;
1395 else if (! STRING_MULTIBYTE (new))
1397 = count_size_as_multibyte (SDATA (new), insbytes
);
1399 /* Make sure the gap is somewhere in or next to what we are deleting. */
1401 gap_right (from
, from_byte
);
1403 gap_left (to
, to_byte
, 0);
1405 /* Even if we don't record for undo, we must keep the original text
1406 because we may have to recover it because of inappropriate byte
1408 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1409 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1411 GAP_SIZE
+= nbytes_del
;
1414 ZV_BYTE
-= nbytes_del
;
1415 Z_BYTE
-= nbytes_del
;
1417 GPT_BYTE
= from_byte
;
1418 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1420 eassert (GPT
<= GPT_BYTE
);
1422 if (GPT
- BEG
< BEG_UNCHANGED
)
1423 BEG_UNCHANGED
= GPT
- BEG
;
1424 if (Z
- GPT
< END_UNCHANGED
)
1425 END_UNCHANGED
= Z
- GPT
;
1427 if (GAP_SIZE
< outgoing_insbytes
)
1428 make_gap (outgoing_insbytes
- GAP_SIZE
);
1430 /* Copy the string text into the buffer, perhaps converting
1431 between single-byte and multibyte. */
1432 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1433 STRING_MULTIBYTE (new),
1434 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1436 #ifdef BYTE_COMBINING_DEBUG
1437 /* We have copied text into the gap, but we have not marked
1438 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1439 here, for both the previous text and the following text.
1440 Meanwhile, GPT_ADDR does point to
1441 the text that has been stored by copy_text. */
1442 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1443 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1447 /* Record the insertion first, so that when we undo,
1448 the deletion will be undone first. Thus, undo
1449 will insert before deleting, and thus will keep
1450 the markers before and after this text separate. */
1451 if (!NILP (deletion
))
1453 record_insert (from
+ SCHARS (deletion
), inschars
);
1454 record_delete (from
, deletion
, false);
1457 GAP_SIZE
-= outgoing_insbytes
;
1461 GPT_BYTE
+= outgoing_insbytes
;
1462 ZV_BYTE
+= outgoing_insbytes
;
1463 Z_BYTE
+= outgoing_insbytes
;
1464 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1466 eassert (GPT
<= GPT_BYTE
);
1468 /* Adjust markers for the deletion and the insertion. */
1470 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1471 inschars
, outgoing_insbytes
);
1474 /* The character positions of the markers remain intact, but we
1475 still need to update their byte positions, because the
1476 deleted and the inserted text might have multibyte sequences
1477 which make the original byte positions of the markers
1479 adjust_markers_bytepos (from
, from_byte
, from
+ inschars
,
1480 from_byte
+ outgoing_insbytes
, 1);
1483 /* Adjust the overlay center as needed. This must be done after
1484 adjusting the markers that bound the overlays. */
1485 adjust_overlays_for_delete (from
, nchars_del
);
1486 adjust_overlays_for_insert (from
, inschars
);
1488 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1490 /* Get the intervals for the part of the string we are inserting--
1491 not including the combined-before bytes. */
1492 intervals
= string_intervals (new);
1493 /* Insert those intervals. */
1494 graft_intervals_into_buffer (intervals
, from
, inschars
,
1495 current_buffer
, inherit
);
1497 /* Relocate point as if it were a marker. */
1499 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1500 (from_byte
+ outgoing_insbytes
1501 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1503 if (outgoing_insbytes
== 0)
1504 evaporate_overlays (from
);
1509 CHARS_MODIFF
= MODIFF
;
1511 signal_after_change (from
, nchars_del
, GPT
- from
);
1512 update_compositions (from
, GPT
, CHECK_BORDER
);
1515 /* Replace the text from character positions FROM to TO with
1516 the text in INS of length INSCHARS.
1517 Keep the text properties that applied to the old characters
1518 (extending them to all the new chars if there are more new chars).
1520 Note that this does not yet handle markers quite right.
1522 If MARKERS, relocate markers.
1524 Unlike most functions at this level, never call
1525 prepare_to_modify_buffer and never call signal_after_change. */
1528 replace_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1529 ptrdiff_t to
, ptrdiff_t to_byte
,
1530 const char *ins
, ptrdiff_t inschars
, ptrdiff_t insbytes
,
1533 ptrdiff_t nbytes_del
, nchars_del
;
1537 nchars_del
= to
- from
;
1538 nbytes_del
= to_byte
- from_byte
;
1540 if (nbytes_del
<= 0 && insbytes
== 0)
1543 /* Make sure the gap is somewhere in or next to what we are deleting. */
1545 gap_right (from
, from_byte
);
1547 gap_left (to
, to_byte
, 0);
1549 GAP_SIZE
+= nbytes_del
;
1552 ZV_BYTE
-= nbytes_del
;
1553 Z_BYTE
-= nbytes_del
;
1555 GPT_BYTE
= from_byte
;
1556 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1558 eassert (GPT
<= GPT_BYTE
);
1560 if (GPT
- BEG
< BEG_UNCHANGED
)
1561 BEG_UNCHANGED
= GPT
- BEG
;
1562 if (Z
- GPT
< END_UNCHANGED
)
1563 END_UNCHANGED
= Z
- GPT
;
1565 if (GAP_SIZE
< insbytes
)
1566 make_gap (insbytes
- GAP_SIZE
);
1568 /* Copy the replacement text into the buffer. */
1569 memcpy (GPT_ADDR
, ins
, insbytes
);
1571 #ifdef BYTE_COMBINING_DEBUG
1572 /* We have copied text into the gap, but we have not marked
1573 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1574 here, for both the previous text and the following text.
1575 Meanwhile, GPT_ADDR does point to
1576 the text that has been stored by copy_text. */
1577 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1578 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1582 GAP_SIZE
-= insbytes
;
1586 GPT_BYTE
+= insbytes
;
1587 ZV_BYTE
+= insbytes
;
1589 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1591 eassert (GPT
<= GPT_BYTE
);
1593 /* Adjust markers for the deletion and the insertion. */
1594 if (! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1597 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1598 inschars
, insbytes
);
1601 /* The character positions of the markers remain intact, but
1602 we still need to update their byte positions, because the
1603 deleted and the inserted text might have multibyte
1604 sequences which make the original byte positions of the
1606 adjust_markers_bytepos (from
, from_byte
, from
+ inschars
,
1607 from_byte
+ insbytes
, 1);
1611 /* Adjust the overlay center as needed. This must be done after
1612 adjusting the markers that bound the overlays. */
1613 if (nchars_del
!= inschars
)
1615 adjust_overlays_for_insert (from
, inschars
);
1616 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1619 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1621 /* Relocate point as if it were a marker. */
1622 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1625 /* PT was within the deleted text. Move it to FROM. */
1626 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1628 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1632 evaporate_overlays (from
);
1637 CHARS_MODIFF
= MODIFF
;
1640 /* Delete characters in current buffer
1641 from FROM up to (but not including) TO.
1642 If TO comes before FROM, we delete nothing. */
1645 del_range (ptrdiff_t from
, ptrdiff_t to
)
1647 del_range_1 (from
, to
, 1, 0);
1650 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1651 RET_STRING says to return the deleted text. */
1654 del_range_1 (ptrdiff_t from
, ptrdiff_t to
, bool prepare
, bool ret_string
)
1656 ptrdiff_t from_byte
, to_byte
;
1657 Lisp_Object deletion
;
1659 /* Make args be valid */
1670 ptrdiff_t range_length
= to
- from
;
1671 prepare_to_modify_buffer (from
, to
, &from
);
1672 to
= min (ZV
, from
+ range_length
);
1675 from_byte
= CHAR_TO_BYTE (from
);
1676 to_byte
= CHAR_TO_BYTE (to
);
1678 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1679 signal_after_change (from
, to
- from
, 0);
1680 update_compositions (from
, from
, CHECK_HEAD
);
1684 /* Like del_range_1 but args are byte positions, not char positions. */
1687 del_range_byte (ptrdiff_t from_byte
, ptrdiff_t to_byte
, bool prepare
)
1691 /* Make args be valid. */
1692 if (from_byte
< BEGV_BYTE
)
1693 from_byte
= BEGV_BYTE
;
1694 if (to_byte
> ZV_BYTE
)
1697 if (to_byte
<= from_byte
)
1700 from
= BYTE_TO_CHAR (from_byte
);
1701 to
= BYTE_TO_CHAR (to_byte
);
1705 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1706 ptrdiff_t range_length
= to
- from
;
1707 prepare_to_modify_buffer (from
, to
, &from
);
1708 to
= from
+ range_length
;
1710 if (old_from
!= from
)
1711 from_byte
= CHAR_TO_BYTE (from
);
1717 else if (old_to
== Z
- to
)
1718 to_byte
= CHAR_TO_BYTE (to
);
1721 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1722 signal_after_change (from
, to
- from
, 0);
1723 update_compositions (from
, from
, CHECK_HEAD
);
1726 /* Like del_range_1, but positions are specified both as charpos
1730 del_range_both (ptrdiff_t from
, ptrdiff_t from_byte
,
1731 ptrdiff_t to
, ptrdiff_t to_byte
, bool prepare
)
1733 /* Make args be valid */
1734 if (from_byte
< BEGV_BYTE
)
1735 from_byte
= BEGV_BYTE
;
1736 if (to_byte
> ZV_BYTE
)
1739 if (to_byte
<= from_byte
)
1749 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1750 ptrdiff_t range_length
= to
- from
;
1751 prepare_to_modify_buffer (from
, to
, &from
);
1752 to
= from
+ range_length
;
1754 if (old_from
!= from
)
1755 from_byte
= CHAR_TO_BYTE (from
);
1761 else if (old_to
== Z
- to
)
1762 to_byte
= CHAR_TO_BYTE (to
);
1765 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1766 signal_after_change (from
, to
- from
, 0);
1767 update_compositions (from
, from
, CHECK_HEAD
);
1770 /* Delete a range of text, specified both as character positions
1771 and byte positions. FROM and TO are character positions,
1772 while FROM_BYTE and TO_BYTE are byte positions.
1773 If RET_STRING, the deleted area is returned as a string. */
1776 del_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1777 ptrdiff_t to
, ptrdiff_t to_byte
, bool ret_string
)
1779 ptrdiff_t nbytes_del
, nchars_del
;
1780 Lisp_Object deletion
;
1784 nchars_del
= to
- from
;
1785 nbytes_del
= to_byte
- from_byte
;
1787 /* Make sure the gap is somewhere in or next to what we are deleting. */
1789 gap_right (from
, from_byte
);
1791 gap_left (to
, to_byte
, 0);
1793 #ifdef BYTE_COMBINING_DEBUG
1794 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1795 Z_BYTE
- to_byte
, from
, from_byte
))
1799 if (ret_string
|| ! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1800 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1804 /* Record marker adjustments, and text deletion into undo
1806 record_delete (from
, deletion
, true);
1808 /* Relocate all markers pointing into the new, larger gap to point
1809 at the end of the text before the gap. */
1810 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1813 CHARS_MODIFF
= MODIFF
;
1815 /* Relocate point as if it were a marker. */
1817 adjust_point (from
- (PT
< to
? PT
: to
),
1818 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1820 offset_intervals (current_buffer
, from
, - nchars_del
);
1822 /* Adjust the overlay center as needed. This must be done after
1823 adjusting the markers that bound the overlays. */
1824 adjust_overlays_for_delete (from
, nchars_del
);
1826 GAP_SIZE
+= nbytes_del
;
1827 ZV_BYTE
-= nbytes_del
;
1828 Z_BYTE
-= nbytes_del
;
1832 GPT_BYTE
= from_byte
;
1833 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1834 /* Put an anchor, unless called from decode_coding_object which
1835 needs to access the previous gap contents. */
1838 eassert (GPT
<= GPT_BYTE
);
1840 if (GPT
- BEG
< BEG_UNCHANGED
)
1841 BEG_UNCHANGED
= GPT
- BEG
;
1842 if (Z
- GPT
< END_UNCHANGED
)
1843 END_UNCHANGED
= Z
- GPT
;
1847 evaporate_overlays (from
);
1852 /* Call this if you're about to change the text of current buffer
1853 from character positions START to END. This checks the read-only
1854 properties of the region, calls the necessary modification hooks,
1855 and warns the next redisplay that it should pay attention to that
1859 modify_text (ptrdiff_t start
, ptrdiff_t end
)
1861 prepare_to_modify_buffer (start
, end
, NULL
);
1863 BUF_COMPUTE_UNCHANGED (current_buffer
, start
- 1, end
);
1864 if (MODIFF
<= SAVE_MODIFF
)
1865 record_first_change ();
1867 CHARS_MODIFF
= MODIFF
;
1869 bset_point_before_scroll (current_buffer
, Qnil
);
1872 /* Signal that we are about to make a change that may result in new
1876 run_undoable_change (void)
1878 if (EQ (BVAR (current_buffer
, undo_list
), Qt
))
1881 call0 (Qundo_auto__undoable_change
);
1884 /* Check that it is okay to modify the buffer between START and END,
1885 which are char positions.
1887 Run the before-change-function, if any. If intervals are in use,
1888 verify that the text to be modified is not read-only, and call
1889 any modification properties the text may have.
1891 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1892 by holding its value temporarily in a marker.
1894 This function runs Lisp, which means it can GC, which means it can
1895 compact buffers, including the current buffer being worked on here.
1896 So don't you dare calling this function while manipulating the gap,
1897 or during some other similar "critical section". */
1900 prepare_to_modify_buffer_1 (ptrdiff_t start
, ptrdiff_t end
,
1901 ptrdiff_t *preserve_ptr
)
1903 struct buffer
*base_buffer
;
1906 XSETFASTINT (temp
, start
);
1907 if (!NILP (BVAR (current_buffer
, read_only
)))
1908 Fbarf_if_buffer_read_only (temp
);
1910 run_undoable_change();
1912 bset_redisplay (current_buffer
);
1914 if (buffer_intervals (current_buffer
))
1918 Lisp_Object preserve_marker
;
1919 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1920 verify_interval_modification (current_buffer
, start
, end
);
1921 *preserve_ptr
= marker_position (preserve_marker
);
1922 unchain_marker (XMARKER (preserve_marker
));
1925 verify_interval_modification (current_buffer
, start
, end
);
1928 /* For indirect buffers, use the base buffer to check clashes. */
1929 if (current_buffer
->base_buffer
!= 0)
1930 base_buffer
= current_buffer
->base_buffer
;
1932 base_buffer
= current_buffer
;
1934 if (inhibit_modification_hooks
)
1937 if (!NILP (BVAR (base_buffer
, file_truename
))
1938 /* Make binding buffer-file-name to nil effective. */
1939 && !NILP (BVAR (base_buffer
, filename
))
1940 && SAVE_MODIFF
>= MODIFF
)
1941 lock_file (BVAR (base_buffer
, file_truename
));
1943 /* If `select-active-regions' is non-nil, save the region text. */
1944 /* FIXME: Move this to Elisp (via before-change-functions). */
1945 if (!NILP (BVAR (current_buffer
, mark_active
))
1946 && XMARKER (BVAR (current_buffer
, mark
))->buffer
1947 && NILP (Vsaved_region_selection
)
1948 && (EQ (Vselect_active_regions
, Qonly
)
1949 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
1950 : (!NILP (Vselect_active_regions
)
1951 && !NILP (Vtransient_mark_mode
))))
1952 Vsaved_region_selection
1953 = call1 (Fsymbol_value (Qregion_extract_function
), Qnil
);
1955 signal_before_change (start
, end
, preserve_ptr
);
1956 Fset (Qdeactivate_mark
, Qt
);
1959 /* Like above, but called when we know that the buffer text
1960 will be modified and region caches should be invalidated. */
1963 prepare_to_modify_buffer (ptrdiff_t start
, ptrdiff_t end
,
1964 ptrdiff_t *preserve_ptr
)
1966 prepare_to_modify_buffer_1 (start
, end
, preserve_ptr
);
1967 invalidate_buffer_caches (current_buffer
, start
, end
);
1970 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1971 region between buffer positions START and END. */
1973 invalidate_buffer_caches (struct buffer
*buf
, ptrdiff_t start
, ptrdiff_t end
)
1975 /* Indirect buffers usually have their caches set to NULL, but we
1976 need to consider the caches of their base buffer. */
1977 if (buf
->base_buffer
)
1978 buf
= buf
->base_buffer
;
1979 /* The bidi_paragraph_cache must be invalidated first, because doing
1980 so might need to use the newline_cache (via find_newline_no_quit,
1982 if (buf
->bidi_paragraph_cache
)
1985 && start
> BUF_BEG (buf
))
1987 /* If we are deleting or replacing characters, we could
1988 create a paragraph start, because all of the characters
1989 from START to the beginning of START's line are
1990 whitespace. Therefore, we must extend the region to be
1991 invalidated up to the newline before START. */
1992 ptrdiff_t line_beg
= start
;
1993 ptrdiff_t start_byte
= buf_charpos_to_bytepos (buf
, start
);
1995 if (BUF_FETCH_BYTE (buf
, start_byte
- 1) != '\n')
1997 struct buffer
*old
= current_buffer
;
1999 set_buffer_internal (buf
);
2001 line_beg
= find_newline_no_quit (start
, start_byte
, -1,
2003 set_buffer_internal (old
);
2005 start
= line_beg
- (line_beg
> BUF_BEG (buf
));
2007 invalidate_region_cache (buf
,
2008 buf
->bidi_paragraph_cache
,
2009 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
2011 if (buf
->newline_cache
)
2012 invalidate_region_cache (buf
,
2014 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
2015 if (buf
->width_run_cache
)
2016 invalidate_region_cache (buf
,
2017 buf
->width_run_cache
,
2018 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
2021 /* These macros work with an argument named `preserve_ptr'
2022 and a local variable named `preserve_marker'. */
2024 #define PRESERVE_VALUE \
2025 if (preserve_ptr && NILP (preserve_marker)) \
2026 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2028 #define RESTORE_VALUE \
2029 if (! NILP (preserve_marker)) \
2031 *preserve_ptr = marker_position (preserve_marker); \
2032 unchain_marker (XMARKER (preserve_marker)); \
2035 #define PRESERVE_START_END \
2036 if (NILP (start_marker)) \
2037 start_marker = Fcopy_marker (start, Qnil); \
2038 if (NILP (end_marker)) \
2039 end_marker = Fcopy_marker (end, Qnil);
2041 #define FETCH_START \
2042 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2045 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2047 /* Set a variable to nil if an error occurred.
2048 Don't change the variable if there was no error.
2049 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2050 VARIABLE is the variable to maybe set to nil.
2051 NO-ERROR-FLAG is nil if there was an error,
2052 anything else meaning no error (so this function does nothing). */
2055 Lisp_Object
*location
;
2060 reset_var_on_error (void *ptr
)
2062 struct rvoe_arg
*p
= ptr
;
2064 *p
->location
= Qnil
;
2067 /* Signal a change to the buffer immediately before it happens.
2068 START_INT and END_INT are the bounds of the text to be changed.
2070 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2071 by holding its value temporarily in a marker. */
2074 signal_before_change (ptrdiff_t start_int
, ptrdiff_t end_int
,
2075 ptrdiff_t *preserve_ptr
)
2077 Lisp_Object start
, end
;
2078 Lisp_Object start_marker
, end_marker
;
2079 Lisp_Object preserve_marker
;
2080 ptrdiff_t count
= SPECPDL_INDEX ();
2081 struct rvoe_arg rvoe_arg
;
2083 start
= make_number (start_int
);
2084 end
= make_number (end_int
);
2085 preserve_marker
= Qnil
;
2086 start_marker
= Qnil
;
2089 specbind (Qinhibit_modification_hooks
, Qt
);
2091 /* If buffer is unmodified, run a special hook for that case. The
2092 check for Vfirst_change_hook is just a minor optimization. */
2093 if (SAVE_MODIFF
>= MODIFF
2094 && !NILP (Vfirst_change_hook
))
2098 run_hook (Qfirst_change_hook
);
2101 /* Now run the before-change-functions if any. */
2102 if (!NILP (Vbefore_change_functions
))
2104 rvoe_arg
.location
= &Vbefore_change_functions
;
2105 rvoe_arg
.errorp
= 1;
2110 /* Mark before-change-functions to be reset to nil in case of error. */
2111 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2113 /* Actually run the hook functions. */
2114 CALLN (Frun_hook_with_args
, Qbefore_change_functions
,
2115 FETCH_START
, FETCH_END
);
2117 /* There was no error: unarm the reset_on_error. */
2118 rvoe_arg
.errorp
= 0;
2121 if (buffer_has_overlays ())
2124 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2125 FETCH_START
, FETCH_END
, Qnil
);
2128 if (! NILP (start_marker
))
2129 free_marker (start_marker
);
2130 if (! NILP (end_marker
))
2131 free_marker (end_marker
);
2134 unbind_to (count
, Qnil
);
2137 /* Signal a change immediately after it happens.
2138 CHARPOS is the character position of the start of the changed text.
2139 LENDEL is the number of characters of the text before the change.
2140 (Not the whole buffer; just the part that was changed.)
2141 LENINS is the number of characters in that part of the text
2142 after the change. */
2145 signal_after_change (ptrdiff_t charpos
, ptrdiff_t lendel
, ptrdiff_t lenins
)
2147 ptrdiff_t count
= SPECPDL_INDEX ();
2148 struct rvoe_arg rvoe_arg
;
2150 if (inhibit_modification_hooks
)
2153 /* If we are deferring calls to the after-change functions
2154 and there are no before-change functions,
2155 just record the args that we were going to use. */
2156 if (! NILP (Vcombine_after_change_calls
)
2157 && NILP (Vbefore_change_functions
)
2158 && !buffer_has_overlays ())
2162 if (!NILP (combine_after_change_list
)
2163 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2164 Fcombine_after_change_execute ();
2166 elt
= list3i (charpos
- BEG
, Z
- (charpos
- lendel
+ lenins
),
2168 combine_after_change_list
2169 = Fcons (elt
, combine_after_change_list
);
2170 combine_after_change_buffer
= Fcurrent_buffer ();
2175 if (!NILP (combine_after_change_list
))
2176 Fcombine_after_change_execute ();
2178 specbind (Qinhibit_modification_hooks
, Qt
);
2180 if (!NILP (Vafter_change_functions
))
2182 rvoe_arg
.location
= &Vafter_change_functions
;
2183 rvoe_arg
.errorp
= 1;
2185 /* Mark after-change-functions to be reset to nil in case of error. */
2186 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2188 /* Actually run the hook functions. */
2189 CALLN (Frun_hook_with_args
, Qafter_change_functions
,
2190 make_number (charpos
), make_number (charpos
+ lenins
),
2191 make_number (lendel
));
2193 /* There was no error: unarm the reset_on_error. */
2194 rvoe_arg
.errorp
= 0;
2197 if (buffer_has_overlays ())
2198 report_overlay_modification (make_number (charpos
),
2199 make_number (charpos
+ lenins
),
2201 make_number (charpos
),
2202 make_number (charpos
+ lenins
),
2203 make_number (lendel
));
2205 /* After an insertion, call the text properties
2206 insert-behind-hooks or insert-in-front-hooks. */
2208 report_interval_modification (make_number (charpos
),
2209 make_number (charpos
+ lenins
));
2211 unbind_to (count
, Qnil
);
2215 Fcombine_after_change_execute_1 (Lisp_Object val
)
2217 Vcombine_after_change_calls
= val
;
2220 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2221 Scombine_after_change_execute
, 0, 0, 0,
2222 doc
: /* This function is for use internally in the function `combine-after-change-calls'. */)
2225 ptrdiff_t count
= SPECPDL_INDEX ();
2226 ptrdiff_t beg
, end
, change
;
2227 ptrdiff_t begpos
, endpos
;
2230 if (NILP (combine_after_change_list
))
2233 /* It is rare for combine_after_change_buffer to be invalid, but
2234 possible. It can happen when combine-after-change-calls is
2235 non-nil, and insertion calls a file handler (e.g. through
2236 lock_file) which scribbles into a temp file -- cyd */
2237 if (!BUFFERP (combine_after_change_buffer
)
2238 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer
)))
2240 combine_after_change_list
= Qnil
;
2244 record_unwind_current_buffer ();
2246 Fset_buffer (combine_after_change_buffer
);
2248 /* # chars unchanged at beginning of buffer. */
2250 /* # chars unchanged at end of buffer. */
2252 /* Total amount of insertion (negative for deletion). */
2255 /* Scan the various individual changes,
2256 accumulating the range info in BEG, END and CHANGE. */
2257 for (tail
= combine_after_change_list
; CONSP (tail
);
2261 ptrdiff_t thisbeg
, thisend
, thischange
;
2263 /* Extract the info from the next element. */
2267 thisbeg
= XINT (XCAR (elt
));
2272 thisend
= XINT (XCAR (elt
));
2277 thischange
= XINT (XCAR (elt
));
2279 /* Merge this range into the accumulated range. */
2280 change
+= thischange
;
2287 /* Get the current start and end positions of the range
2288 that was changed. */
2292 /* We are about to handle these, so discard them. */
2293 combine_after_change_list
= Qnil
;
2295 /* Now run the after-change functions for real.
2296 Turn off the flag that defers them. */
2297 record_unwind_protect (Fcombine_after_change_execute_1
,
2298 Vcombine_after_change_calls
);
2299 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2300 update_compositions (begpos
, endpos
, CHECK_ALL
);
2302 return unbind_to (count
, Qnil
);
2306 syms_of_insdel (void)
2308 staticpro (&combine_after_change_list
);
2309 staticpro (&combine_after_change_buffer
);
2310 combine_after_change_list
= Qnil
;
2311 combine_after_change_buffer
= Qnil
;
2313 DEFSYM (Qundo_auto__undoable_change
, "undo-auto--undoable-change");
2315 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls
,
2316 doc
: /* Used internally by the function `combine-after-change-calls' macro. */);
2317 Vcombine_after_change_calls
= Qnil
;
2319 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks
,
2320 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2321 This affects `before-change-functions' and `after-change-functions',
2322 as well as hooks attached to text properties and overlays. */);
2323 inhibit_modification_hooks
= 0;
2324 DEFSYM (Qinhibit_modification_hooks
, "inhibit-modification-hooks");
2326 DEFSYM (Qregion_extract_function
, "region-extract-function");
2328 defsubr (&Scombine_after_change_execute
);