1 /* Buffer insertion/deletion and gap motion for GNU Emacs. -*- coding: utf-8 -*-
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 <https://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 set 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 set 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 /* With set-buffer-multibyte on a large buffer, we can end up growing the
564 * buffer *many* times. Avoid an O(N^2) behavior by increasing by an
565 * amount at least proportional to the size of the buffer.
566 * On my test (a 223.9MB zip file on a Thinkpad T61):
573 * With /∞ => gave up after 858s
574 * Of course, ideally we should never call set-buffer-multibyte on
575 * a non-empty buffer (e.g. use buffer-swap-text instead).
576 * We chose /64 because it already brings almost the best performance while
577 * limiting the potential wasted memory to 1.5%. */
578 make_gap_larger (max (nbytes_added
, (Z
- BEG
) / 64));
579 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
581 make_gap_smaller (-nbytes_added
);
585 /* Add NBYTES to B's gap. It's enough to temporarily
586 fake current_buffer and avoid real switch to B. */
589 make_gap_1 (struct buffer
*b
, ptrdiff_t nbytes
)
591 struct buffer
*oldb
= current_buffer
;
595 current_buffer
= oldb
;
598 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
599 FROM_MULTIBYTE says whether the incoming text is multibyte.
600 TO_MULTIBYTE says whether to store the text as multibyte.
601 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
603 Return the number of bytes stored at TO_ADDR. */
606 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
607 ptrdiff_t nbytes
, bool from_multibyte
, bool to_multibyte
)
609 if (from_multibyte
== to_multibyte
)
611 memcpy (to_addr
, from_addr
, nbytes
);
614 else if (from_multibyte
)
616 ptrdiff_t nchars
= 0;
617 ptrdiff_t bytes_left
= nbytes
;
619 while (bytes_left
> 0)
622 c
= STRING_CHAR_AND_LENGTH (from_addr
, thislen
);
623 if (! ASCII_CHAR_P (c
))
626 from_addr
+= thislen
;
627 bytes_left
-= thislen
;
634 unsigned char *initial_to_addr
= to_addr
;
636 /* Convert single-byte to multibyte. */
639 int c
= *from_addr
++;
641 if (!ASCII_CHAR_P (c
))
643 c
= BYTE8_TO_CHAR (c
);
644 to_addr
+= CHAR_STRING (c
, to_addr
);
648 /* Special case for speed. */
649 *to_addr
++ = c
, nbytes
--;
651 return to_addr
- initial_to_addr
;
655 /* Insert a string of specified length before point.
656 This function judges multibyteness based on
657 enable_multibyte_characters in the current buffer;
658 it never converts between single-byte and multibyte.
660 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
661 prepare_to_modify_buffer could relocate the text. */
664 insert (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
, 0, 1, 0);
671 signal_after_change (opoint
, 0, len
);
672 update_compositions (opoint
, PT
, CHECK_BORDER
);
676 /* Likewise, but inherit text properties from neighboring characters. */
679 insert_and_inherit (const char *string
, ptrdiff_t nbytes
)
683 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
684 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
686 signal_after_change (opoint
, 0, len
);
687 update_compositions (opoint
, PT
, CHECK_BORDER
);
691 /* Insert the character C before point. Do not inherit text properties. */
696 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
699 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
700 len
= CHAR_STRING (c
, str
);
707 insert ((char *) str
, len
);
710 /* Insert the null-terminated string S before point. */
713 insert_string (const char *s
)
715 insert (s
, strlen (s
));
718 /* Like `insert' except that all markers pointing at the place where
719 the insertion happens are adjusted to point after it.
720 Don't use this function to insert part of a Lisp string,
721 since gc could happen and relocate it. */
724 insert_before_markers (const char *string
, ptrdiff_t nbytes
)
728 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
729 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
731 signal_after_change (opoint
, 0, len
);
732 update_compositions (opoint
, PT
, CHECK_BORDER
);
736 /* Likewise, but inherit text properties from neighboring characters. */
739 insert_before_markers_and_inherit (const char *string
,
744 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
745 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
747 signal_after_change (opoint
, 0, len
);
748 update_compositions (opoint
, PT
, CHECK_BORDER
);
752 #ifdef BYTE_COMBINING_DEBUG
754 /* See if the bytes before POS/POS_BYTE combine with bytes
755 at the start of STRING to form a single character.
756 If so, return the number of bytes at the start of STRING
757 which combine in this way. Otherwise, return 0. */
760 count_combining_before (const unsigned char *string
, ptrdiff_t length
,
761 ptrdiff_t pos
, ptrdiff_t pos_byte
)
763 int len
, combining_bytes
;
764 const unsigned char *p
;
766 if (NILP (current_buffer
->enable_multibyte_characters
))
769 /* At first, we can exclude the following cases:
770 (1) STRING[0] can't be a following byte of multibyte sequence.
771 (2) POS is the start of the current buffer.
772 (3) A character before POS is not a multibyte character. */
773 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
775 if (pos_byte
== BEG_BYTE
) /* case (2) */
778 p
= BYTE_POS_ADDR (pos_byte
- 1);
779 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
780 if (! LEADING_CODE_P (*p
)) /* case (3) */
783 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
784 if (combining_bytes
<= 0)
785 /* The character preceding POS is, complete and no room for
786 combining bytes (combining_bytes == 0), or an independent 8-bit
787 character (combining_bytes < 0). */
790 /* We have a combination situation. Count the bytes at STRING that
793 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
796 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
799 /* See if the bytes after POS/POS_BYTE combine with bytes
800 at the end of STRING to form a single character.
801 If so, return the number of bytes after POS/POS_BYTE
802 which combine in this way. Otherwise, return 0. */
805 count_combining_after (const unsigned char *string
,
806 ptrdiff_t length
, ptrdiff_t pos
, ptrdiff_t pos_byte
)
808 ptrdiff_t opos_byte
= pos_byte
;
813 if (NILP (current_buffer
->enable_multibyte_characters
))
816 /* At first, we can exclude the following cases:
817 (1) The last byte of STRING is an ASCII.
818 (2) POS is the last of the current buffer.
819 (3) A character at POS can't be a following byte of multibyte
821 if (length
> 0 && ASCII_CHAR_P (string
[length
- 1])) /* case (1) */
823 if (pos_byte
== Z_BYTE
) /* case (2) */
825 bufp
= BYTE_POS_ADDR (pos_byte
);
826 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
830 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
836 /* All characters in STRING are not character head. We must
837 check also preceding bytes at POS. We are sure that the gap
839 unsigned char *p
= BEG_ADDR
;
841 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
843 if (i
< 0 || !LEADING_CODE_P (p
[i
]))
846 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
847 return (bytes
<= pos_byte
- 1 - i
+ length
849 : bytes
- (pos_byte
- 1 - i
+ length
));
851 if (!LEADING_CODE_P (string
[i
]))
854 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
856 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
858 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
864 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
865 starting at STRING. INHERIT non-zero means inherit the text
866 properties from neighboring characters; zero means inserted text
867 will have no text properties. PREPARE non-zero means call
868 prepare_to_modify_buffer, which checks that the region is not
869 read-only, and calls before-change-function and any modification
870 properties the text may have. BEFORE_MARKERS non-zero means adjust
871 all markers that point at the insertion place to point after it. */
874 insert_1_both (const char *string
,
875 ptrdiff_t nchars
, ptrdiff_t nbytes
,
876 bool inherit
, bool prepare
, bool before_markers
)
881 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
885 /* Do this before moving and increasing the gap,
886 because the before-change hooks might move the gap
887 or make it smaller. */
888 prepare_to_modify_buffer (PT
, PT
, NULL
);
891 move_gap_both (PT
, PT_BYTE
);
892 if (GAP_SIZE
< nbytes
)
893 make_gap (nbytes
- GAP_SIZE
);
895 #ifdef BYTE_COMBINING_DEBUG
896 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
897 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
901 /* Record deletion of the surrounding text that combines with
902 the insertion. This, together with recording the insertion,
903 will add up to the right stuff in the undo list. */
904 record_insert (PT
, nchars
);
906 CHARS_MODIFF
= MODIFF
;
908 memcpy (GPT_ADDR
, string
, nbytes
);
917 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
919 eassert (GPT
<= GPT_BYTE
);
921 /* The insert may have been in the unchanged region, so check again. */
922 if (Z
- GPT
< END_UNCHANGED
)
923 END_UNCHANGED
= Z
- GPT
;
925 adjust_overlays_for_insert (PT
, nchars
);
926 adjust_markers_for_insert (PT
, PT_BYTE
,
927 PT
+ nchars
, PT_BYTE
+ nbytes
,
930 offset_intervals (current_buffer
, PT
, nchars
);
932 if (!inherit
&& buffer_intervals (current_buffer
))
933 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
936 adjust_point (nchars
, nbytes
);
941 /* Insert the part of the text of STRING, a Lisp object assumed to be
942 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
943 starting at position POS / POS_BYTE. If the text of STRING has properties,
944 copy them into the buffer.
946 It does not work to use `insert' for this, because a GC could happen
947 before we copy the stuff into the buffer, and relocate the string
948 without insert noticing. */
951 insert_from_string (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
952 ptrdiff_t length
, ptrdiff_t length_byte
, bool inherit
)
954 ptrdiff_t opoint
= PT
;
956 if (SCHARS (string
) == 0)
959 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
961 signal_after_change (opoint
, 0, PT
- opoint
);
962 update_compositions (opoint
, PT
, CHECK_BORDER
);
965 /* Like `insert_from_string' except that all markers pointing
966 at the place where the insertion happens are adjusted to point after it. */
969 insert_from_string_before_markers (Lisp_Object string
,
970 ptrdiff_t pos
, ptrdiff_t pos_byte
,
971 ptrdiff_t length
, ptrdiff_t length_byte
,
974 ptrdiff_t opoint
= PT
;
976 if (SCHARS (string
) == 0)
979 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
981 signal_after_change (opoint
, 0, PT
- opoint
);
982 update_compositions (opoint
, PT
, CHECK_BORDER
);
985 /* Subroutine of the insertion functions above. */
988 insert_from_string_1 (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
989 ptrdiff_t nchars
, ptrdiff_t nbytes
,
990 bool inherit
, bool before_markers
)
992 ptrdiff_t outgoing_nbytes
= nbytes
;
995 /* Make OUTGOING_NBYTES describe the text
996 as it will be inserted in this buffer. */
998 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
999 outgoing_nbytes
= nchars
;
1000 else if (! STRING_MULTIBYTE (string
))
1002 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
1005 /* Do this before moving and increasing the gap,
1006 because the before-change hooks might move the gap
1007 or make it smaller. */
1008 prepare_to_modify_buffer (PT
, PT
, NULL
);
1011 move_gap_both (PT
, PT_BYTE
);
1012 if (GAP_SIZE
< outgoing_nbytes
)
1013 make_gap (outgoing_nbytes
- GAP_SIZE
);
1015 /* Copy the string text into the buffer, perhaps converting
1016 between single-byte and multibyte. */
1017 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
1018 STRING_MULTIBYTE (string
),
1019 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1021 #ifdef BYTE_COMBINING_DEBUG
1022 /* We have copied text into the gap, but we have not altered
1023 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1024 to these functions and get the same results as we would
1025 have got earlier on. Meanwhile, PT_ADDR does point to
1026 the text that has been stored by copy_text. */
1027 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1028 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1032 record_insert (PT
, nchars
);
1034 CHARS_MODIFF
= MODIFF
;
1036 GAP_SIZE
-= outgoing_nbytes
;
1040 GPT_BYTE
+= outgoing_nbytes
;
1041 ZV_BYTE
+= outgoing_nbytes
;
1042 Z_BYTE
+= outgoing_nbytes
;
1043 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1045 eassert (GPT
<= GPT_BYTE
);
1047 /* The insert may have been in the unchanged region, so check again. */
1048 if (Z
- GPT
< END_UNCHANGED
)
1049 END_UNCHANGED
= Z
- GPT
;
1051 adjust_overlays_for_insert (PT
, nchars
);
1052 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1053 PT_BYTE
+ outgoing_nbytes
,
1056 offset_intervals (current_buffer
, PT
, nchars
);
1058 intervals
= string_intervals (string
);
1059 /* Get the intervals for the part of the string we are inserting. */
1060 if (nbytes
< SBYTES (string
))
1061 intervals
= copy_intervals (intervals
, pos
, nchars
);
1063 /* Insert those intervals. */
1064 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1065 current_buffer
, inherit
);
1067 adjust_point (nchars
, outgoing_nbytes
);
1072 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1073 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
1074 GPT_ADDR (if not text_at_gap_tail). */
1077 insert_from_gap (ptrdiff_t nchars
, ptrdiff_t nbytes
, bool text_at_gap_tail
)
1079 ptrdiff_t ins_charpos
= GPT
, ins_bytepos
= GPT_BYTE
;
1081 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1084 /* No need to call prepare_to_modify_buffer, since this is called
1085 from places that replace some region with a different text, so
1086 prepare_to_modify_buffer was already called by the deletion part
1088 invalidate_buffer_caches (current_buffer
, GPT
, GPT
);
1089 record_insert (GPT
, nchars
);
1093 if (! text_at_gap_tail
)
1102 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1104 eassert (GPT
<= GPT_BYTE
);
1106 adjust_overlays_for_insert (ins_charpos
, nchars
);
1107 adjust_markers_for_insert (ins_charpos
, ins_bytepos
,
1108 ins_charpos
+ nchars
, ins_bytepos
+ nbytes
, 0);
1110 if (buffer_intervals (current_buffer
))
1112 offset_intervals (current_buffer
, ins_charpos
, nchars
);
1113 graft_intervals_into_buffer (NULL
, ins_charpos
, nchars
,
1117 if (ins_charpos
< PT
)
1118 adjust_point (nchars
, nbytes
);
1123 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1124 current buffer. If the text in BUF has properties, they are absorbed
1125 into the current buffer.
1127 It does not work to use `insert' for this, because a malloc could happen
1128 and relocate BUF's text before the copy happens. */
1131 insert_from_buffer (struct buffer
*buf
,
1132 ptrdiff_t charpos
, ptrdiff_t nchars
, bool inherit
)
1134 ptrdiff_t opoint
= PT
;
1136 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1137 signal_after_change (opoint
, 0, PT
- opoint
);
1138 update_compositions (opoint
, PT
, CHECK_BORDER
);
1142 insert_from_buffer_1 (struct buffer
*buf
,
1143 ptrdiff_t from
, ptrdiff_t nchars
, bool inherit
)
1145 ptrdiff_t chunk
, chunk_expanded
;
1146 ptrdiff_t from_byte
= buf_charpos_to_bytepos (buf
, from
);
1147 ptrdiff_t to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1148 ptrdiff_t incoming_nbytes
= to_byte
- from_byte
;
1149 ptrdiff_t outgoing_nbytes
= incoming_nbytes
;
1155 /* Make OUTGOING_NBYTES describe the text
1156 as it will be inserted in this buffer. */
1158 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1159 outgoing_nbytes
= nchars
;
1160 else if (NILP (BVAR (buf
, enable_multibyte_characters
)))
1162 ptrdiff_t outgoing_before_gap
= 0;
1163 ptrdiff_t outgoing_after_gap
= 0;
1165 if (from
< BUF_GPT (buf
))
1167 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1168 if (chunk
> incoming_nbytes
)
1169 chunk
= incoming_nbytes
;
1171 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1177 if (chunk
< incoming_nbytes
)
1179 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1181 incoming_nbytes
- chunk
);
1183 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1186 /* Do this before moving and increasing the gap,
1187 because the before-change hooks might move the gap
1188 or make it smaller. */
1189 prepare_to_modify_buffer (PT
, PT
, NULL
);
1192 move_gap_both (PT
, PT_BYTE
);
1193 if (GAP_SIZE
< outgoing_nbytes
)
1194 make_gap (outgoing_nbytes
- GAP_SIZE
);
1196 if (from
< BUF_GPT (buf
))
1198 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1199 if (chunk
> incoming_nbytes
)
1200 chunk
= incoming_nbytes
;
1201 /* Record number of output bytes, so we know where
1202 to put the output from the second copy_text. */
1204 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1206 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1207 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1210 chunk_expanded
= chunk
= 0;
1212 if (chunk
< incoming_nbytes
)
1213 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1214 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1215 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1216 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1218 #ifdef BYTE_COMBINING_DEBUG
1219 /* We have copied text into the gap, but we have not altered
1220 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1221 to these functions and get the same results as we would
1222 have got earlier on. Meanwhile, GPT_ADDR does point to
1223 the text that has been stored by copy_text. */
1224 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1225 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1229 record_insert (PT
, nchars
);
1231 CHARS_MODIFF
= MODIFF
;
1233 GAP_SIZE
-= outgoing_nbytes
;
1237 GPT_BYTE
+= outgoing_nbytes
;
1238 ZV_BYTE
+= outgoing_nbytes
;
1239 Z_BYTE
+= outgoing_nbytes
;
1240 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1242 eassert (GPT
<= GPT_BYTE
);
1244 /* The insert may have been in the unchanged region, so check again. */
1245 if (Z
- GPT
< END_UNCHANGED
)
1246 END_UNCHANGED
= Z
- GPT
;
1248 adjust_overlays_for_insert (PT
, nchars
);
1249 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1250 PT_BYTE
+ outgoing_nbytes
,
1253 offset_intervals (current_buffer
, PT
, nchars
);
1255 /* Get the intervals for the part of the string we are inserting. */
1256 intervals
= buffer_intervals (buf
);
1257 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1259 if (buf
== current_buffer
&& PT
<= from
)
1261 intervals
= copy_intervals (intervals
, from
, nchars
);
1264 /* Insert those intervals. */
1265 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1267 adjust_point (nchars
, outgoing_nbytes
);
1270 /* Record undo information and adjust markers and position keepers for
1271 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1272 chars (LEN_BYTE bytes) which resides in the gap just after
1275 PREV_TEXT nil means the new text was just inserted. */
1278 adjust_after_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
1279 Lisp_Object prev_text
, ptrdiff_t len
, ptrdiff_t len_byte
)
1281 ptrdiff_t nchars_del
= 0, nbytes_del
= 0;
1283 #ifdef BYTE_COMBINING_DEBUG
1284 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1285 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1289 if (STRINGP (prev_text
))
1291 nchars_del
= SCHARS (prev_text
);
1292 nbytes_del
= SBYTES (prev_text
);
1295 /* Update various buffer positions for the new text. */
1296 GAP_SIZE
-= len_byte
;
1297 ZV
+= len
; Z
+= len
;
1298 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1299 GPT
+= len
; GPT_BYTE
+= len_byte
;
1300 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1303 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1306 adjust_markers_for_insert (from
, from_byte
,
1307 from
+ len
, from_byte
+ len_byte
, 0);
1310 record_delete (from
, prev_text
, false);
1311 record_insert (from
, len
);
1313 if (len
> nchars_del
)
1314 adjust_overlays_for_insert (from
, len
- nchars_del
);
1315 else if (len
< nchars_del
)
1316 adjust_overlays_for_delete (from
, nchars_del
- len
);
1318 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1321 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1323 /* As byte combining will decrease Z, we must check this again. */
1324 if (Z
- GPT
< END_UNCHANGED
)
1325 END_UNCHANGED
= Z
- GPT
;
1330 evaporate_overlays (from
);
1332 CHARS_MODIFF
= MODIFF
;
1335 /* Record undo information, adjust markers and position keepers for an
1336 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1337 text already exists in the current buffer but character length (TO
1338 - FROM) may be incorrect, the correct length is NEWLEN. */
1341 adjust_after_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
1342 ptrdiff_t to
, ptrdiff_t to_byte
, ptrdiff_t newlen
)
1344 ptrdiff_t len
= to
- from
, len_byte
= to_byte
- from_byte
;
1347 move_gap_both (to
, to_byte
);
1348 GAP_SIZE
+= len_byte
;
1349 GPT
-= len
; GPT_BYTE
-= len_byte
;
1350 ZV
-= len
; ZV_BYTE
-= len_byte
;
1351 Z
-= len
; Z_BYTE
-= len_byte
;
1352 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1355 /* Replace the text from character positions FROM to TO with NEW,
1356 If PREPARE, call prepare_to_modify_buffer.
1357 If INHERIT, the newly inserted text should inherit text properties
1358 from the surrounding non-deleted text.
1359 If ADJUST_MATCH_DATA, then adjust the match data before calling
1360 signal_after_change. */
1362 /* Note that this does not yet handle markers quite right.
1363 Also it needs to record a single undo-entry that does a replacement
1364 rather than a separate delete and insert.
1365 That way, undo will also handle markers properly.
1367 But if MARKERS is 0, don't relocate markers. */
1370 replace_range (ptrdiff_t from
, ptrdiff_t to
, Lisp_Object
new,
1371 bool prepare
, bool inherit
, bool markers
,
1372 bool adjust_match_data
)
1374 ptrdiff_t inschars
= SCHARS (new);
1375 ptrdiff_t insbytes
= SBYTES (new);
1376 ptrdiff_t from_byte
, to_byte
;
1377 ptrdiff_t nbytes_del
, nchars_del
;
1379 ptrdiff_t outgoing_insbytes
= insbytes
;
1380 Lisp_Object deletion
;
1388 ptrdiff_t range_length
= to
- from
;
1389 prepare_to_modify_buffer (from
, to
, &from
);
1390 to
= from
+ range_length
;
1393 /* Make args be valid. */
1399 from_byte
= CHAR_TO_BYTE (from
);
1400 to_byte
= CHAR_TO_BYTE (to
);
1402 nchars_del
= to
- from
;
1403 nbytes_del
= to_byte
- from_byte
;
1405 if (nbytes_del
<= 0 && insbytes
== 0)
1408 /* Make OUTGOING_INSBYTES describe the text
1409 as it will be inserted in this buffer. */
1411 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1412 outgoing_insbytes
= inschars
;
1413 else if (! STRING_MULTIBYTE (new))
1415 = count_size_as_multibyte (SDATA (new), insbytes
);
1417 /* Make sure the gap is somewhere in or next to what we are deleting. */
1419 gap_right (from
, from_byte
);
1421 gap_left (to
, to_byte
, 0);
1423 /* Even if we don't record for undo, we must keep the original text
1424 because we may have to recover it because of inappropriate byte
1426 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1427 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1429 GAP_SIZE
+= nbytes_del
;
1432 ZV_BYTE
-= nbytes_del
;
1433 Z_BYTE
-= nbytes_del
;
1435 GPT_BYTE
= from_byte
;
1436 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1438 eassert (GPT
<= GPT_BYTE
);
1440 if (GPT
- BEG
< BEG_UNCHANGED
)
1441 BEG_UNCHANGED
= GPT
- BEG
;
1442 if (Z
- GPT
< END_UNCHANGED
)
1443 END_UNCHANGED
= Z
- GPT
;
1445 if (GAP_SIZE
< outgoing_insbytes
)
1446 make_gap (outgoing_insbytes
- GAP_SIZE
);
1448 /* Copy the string text into the buffer, perhaps converting
1449 between single-byte and multibyte. */
1450 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1451 STRING_MULTIBYTE (new),
1452 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1454 #ifdef BYTE_COMBINING_DEBUG
1455 /* We have copied text into the gap, but we have not marked
1456 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1457 here, for both the previous text and the following text.
1458 Meanwhile, GPT_ADDR does point to
1459 the text that has been stored by copy_text. */
1460 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1461 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1465 /* Record the insertion first, so that when we undo,
1466 the deletion will be undone first. Thus, undo
1467 will insert before deleting, and thus will keep
1468 the markers before and after this text separate. */
1469 if (!NILP (deletion
))
1471 record_insert (from
+ SCHARS (deletion
), inschars
);
1472 record_delete (from
, deletion
, false);
1475 GAP_SIZE
-= outgoing_insbytes
;
1479 GPT_BYTE
+= outgoing_insbytes
;
1480 ZV_BYTE
+= outgoing_insbytes
;
1481 Z_BYTE
+= outgoing_insbytes
;
1482 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1484 eassert (GPT
<= GPT_BYTE
);
1486 /* Adjust markers for the deletion and the insertion. */
1488 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1489 inschars
, outgoing_insbytes
);
1492 /* The character positions of the markers remain intact, but we
1493 still need to update their byte positions, because the
1494 deleted and the inserted text might have multibyte sequences
1495 which make the original byte positions of the markers
1497 adjust_markers_bytepos (from
, from_byte
, from
+ inschars
,
1498 from_byte
+ outgoing_insbytes
, 1);
1501 /* Adjust the overlay center as needed. This must be done after
1502 adjusting the markers that bound the overlays. */
1503 adjust_overlays_for_delete (from
, nchars_del
);
1504 adjust_overlays_for_insert (from
, inschars
);
1506 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1508 /* Get the intervals for the part of the string we are inserting--
1509 not including the combined-before bytes. */
1510 intervals
= string_intervals (new);
1511 /* Insert those intervals. */
1512 graft_intervals_into_buffer (intervals
, from
, inschars
,
1513 current_buffer
, inherit
);
1515 /* Relocate point as if it were a marker. */
1517 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1518 (from_byte
+ outgoing_insbytes
1519 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1521 if (outgoing_insbytes
== 0)
1522 evaporate_overlays (from
);
1527 CHARS_MODIFF
= MODIFF
;
1529 if (adjust_match_data
)
1530 update_search_regs (from
, to
, from
+ SCHARS (new));
1532 signal_after_change (from
, nchars_del
, GPT
- from
);
1533 update_compositions (from
, GPT
, CHECK_BORDER
);
1536 /* Replace the text from character positions FROM to TO with
1537 the text in INS of length INSCHARS.
1538 Keep the text properties that applied to the old characters
1539 (extending them to all the new chars if there are more new chars).
1541 Note that this does not yet handle markers quite right.
1543 If MARKERS, relocate markers.
1545 Unlike most functions at this level, never call
1546 prepare_to_modify_buffer and never call signal_after_change. */
1549 replace_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1550 ptrdiff_t to
, ptrdiff_t to_byte
,
1551 const char *ins
, ptrdiff_t inschars
, ptrdiff_t insbytes
,
1554 ptrdiff_t nbytes_del
, nchars_del
;
1558 nchars_del
= to
- from
;
1559 nbytes_del
= to_byte
- from_byte
;
1561 if (nbytes_del
<= 0 && insbytes
== 0)
1564 /* Make sure the gap is somewhere in or next to what we are deleting. */
1566 gap_right (from
, from_byte
);
1568 gap_left (to
, to_byte
, 0);
1570 GAP_SIZE
+= nbytes_del
;
1573 ZV_BYTE
-= nbytes_del
;
1574 Z_BYTE
-= nbytes_del
;
1576 GPT_BYTE
= from_byte
;
1577 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1579 eassert (GPT
<= GPT_BYTE
);
1581 if (GPT
- BEG
< BEG_UNCHANGED
)
1582 BEG_UNCHANGED
= GPT
- BEG
;
1583 if (Z
- GPT
< END_UNCHANGED
)
1584 END_UNCHANGED
= Z
- GPT
;
1586 if (GAP_SIZE
< insbytes
)
1587 make_gap (insbytes
- GAP_SIZE
);
1589 /* Copy the replacement text into the buffer. */
1590 memcpy (GPT_ADDR
, ins
, insbytes
);
1592 #ifdef BYTE_COMBINING_DEBUG
1593 /* We have copied text into the gap, but we have not marked
1594 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1595 here, for both the previous text and the following text.
1596 Meanwhile, GPT_ADDR does point to
1597 the text that has been stored by copy_text. */
1598 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1599 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1603 GAP_SIZE
-= insbytes
;
1607 GPT_BYTE
+= insbytes
;
1608 ZV_BYTE
+= insbytes
;
1610 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1612 eassert (GPT
<= GPT_BYTE
);
1614 /* Adjust markers for the deletion and the insertion. */
1615 if (! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1618 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1619 inschars
, insbytes
);
1622 /* The character positions of the markers remain intact, but
1623 we still need to update their byte positions, because the
1624 deleted and the inserted text might have multibyte
1625 sequences which make the original byte positions of the
1627 adjust_markers_bytepos (from
, from_byte
, from
+ inschars
,
1628 from_byte
+ insbytes
, 1);
1632 /* Adjust the overlay center as needed. This must be done after
1633 adjusting the markers that bound the overlays. */
1634 if (nchars_del
!= inschars
)
1636 adjust_overlays_for_insert (from
, inschars
);
1637 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1640 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1642 /* Relocate point as if it were a marker. */
1643 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1646 /* PT was within the deleted text. Move it to FROM. */
1647 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1649 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1653 evaporate_overlays (from
);
1658 CHARS_MODIFF
= MODIFF
;
1661 /* Delete characters in current buffer
1662 from FROM up to (but not including) TO.
1663 If TO comes before FROM, we delete nothing. */
1666 del_range (ptrdiff_t from
, ptrdiff_t to
)
1668 del_range_1 (from
, to
, 1, 0);
1671 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1672 RET_STRING says to return the deleted text. */
1675 del_range_1 (ptrdiff_t from
, ptrdiff_t to
, bool prepare
, bool ret_string
)
1677 ptrdiff_t from_byte
, to_byte
;
1678 Lisp_Object deletion
;
1680 /* Make args be valid */
1691 ptrdiff_t range_length
= to
- from
;
1692 prepare_to_modify_buffer (from
, to
, &from
);
1693 to
= min (ZV
, from
+ range_length
);
1696 from_byte
= CHAR_TO_BYTE (from
);
1697 to_byte
= CHAR_TO_BYTE (to
);
1699 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1700 signal_after_change (from
, to
- from
, 0);
1701 update_compositions (from
, from
, CHECK_HEAD
);
1705 /* Like del_range_1 but args are byte positions, not char positions. */
1708 del_range_byte (ptrdiff_t from_byte
, ptrdiff_t to_byte
)
1712 /* Make args be valid. */
1713 if (from_byte
< BEGV_BYTE
)
1714 from_byte
= BEGV_BYTE
;
1715 if (to_byte
> ZV_BYTE
)
1718 if (to_byte
<= from_byte
)
1721 from
= BYTE_TO_CHAR (from_byte
);
1722 to
= BYTE_TO_CHAR (to_byte
);
1725 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1726 ptrdiff_t range_length
= to
- from
;
1727 prepare_to_modify_buffer (from
, to
, &from
);
1728 to
= from
+ range_length
;
1730 if (old_from
!= from
)
1731 from_byte
= CHAR_TO_BYTE (from
);
1737 else if (old_to
== Z
- to
)
1738 to_byte
= CHAR_TO_BYTE (to
);
1741 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1742 signal_after_change (from
, to
- from
, 0);
1743 update_compositions (from
, from
, CHECK_HEAD
);
1746 /* Like del_range_1, but positions are specified both as charpos
1750 del_range_both (ptrdiff_t from
, ptrdiff_t from_byte
,
1751 ptrdiff_t to
, ptrdiff_t to_byte
, bool prepare
)
1753 /* Make args be valid */
1754 if (from_byte
< BEGV_BYTE
)
1755 from_byte
= BEGV_BYTE
;
1756 if (to_byte
> ZV_BYTE
)
1759 if (to_byte
<= from_byte
)
1769 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1770 ptrdiff_t range_length
= to
- from
;
1771 prepare_to_modify_buffer (from
, to
, &from
);
1772 to
= from
+ range_length
;
1774 if (old_from
!= from
)
1775 from_byte
= CHAR_TO_BYTE (from
);
1781 else if (old_to
== Z
- to
)
1782 to_byte
= CHAR_TO_BYTE (to
);
1785 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1786 signal_after_change (from
, to
- from
, 0);
1787 update_compositions (from
, from
, CHECK_HEAD
);
1790 /* Delete a range of text, specified both as character positions
1791 and byte positions. FROM and TO are character positions,
1792 while FROM_BYTE and TO_BYTE are byte positions.
1793 If RET_STRING, the deleted area is returned as a string. */
1796 del_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1797 ptrdiff_t to
, ptrdiff_t to_byte
, bool ret_string
)
1799 ptrdiff_t nbytes_del
, nchars_del
;
1800 Lisp_Object deletion
;
1804 nchars_del
= to
- from
;
1805 nbytes_del
= to_byte
- from_byte
;
1807 /* Make sure the gap is somewhere in or next to what we are deleting. */
1809 gap_right (from
, from_byte
);
1811 gap_left (to
, to_byte
, 0);
1813 #ifdef BYTE_COMBINING_DEBUG
1814 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1815 Z_BYTE
- to_byte
, from
, from_byte
))
1819 if (ret_string
|| ! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1820 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1824 /* Record marker adjustments, and text deletion into undo
1826 record_delete (from
, deletion
, true);
1828 /* Relocate all markers pointing into the new, larger gap to point
1829 at the end of the text before the gap. */
1830 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1833 CHARS_MODIFF
= MODIFF
;
1835 /* Relocate point as if it were a marker. */
1837 adjust_point (from
- (PT
< to
? PT
: to
),
1838 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1840 offset_intervals (current_buffer
, from
, - nchars_del
);
1842 /* Adjust the overlay center as needed. This must be done after
1843 adjusting the markers that bound the overlays. */
1844 adjust_overlays_for_delete (from
, nchars_del
);
1846 GAP_SIZE
+= nbytes_del
;
1847 ZV_BYTE
-= nbytes_del
;
1848 Z_BYTE
-= nbytes_del
;
1852 GPT_BYTE
= from_byte
;
1853 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1854 /* Put an anchor, unless called from decode_coding_object which
1855 needs to access the previous gap contents. */
1858 eassert (GPT
<= GPT_BYTE
);
1860 if (GPT
- BEG
< BEG_UNCHANGED
)
1861 BEG_UNCHANGED
= GPT
- BEG
;
1862 if (Z
- GPT
< END_UNCHANGED
)
1863 END_UNCHANGED
= Z
- GPT
;
1867 evaporate_overlays (from
);
1872 /* Call this if you're about to change the text of current buffer
1873 from character positions START to END. This checks the read-only
1874 properties of the region, calls the necessary modification hooks,
1875 and warns the next redisplay that it should pay attention to that
1879 modify_text (ptrdiff_t start
, ptrdiff_t end
)
1881 prepare_to_modify_buffer (start
, end
, NULL
);
1883 BUF_COMPUTE_UNCHANGED (current_buffer
, start
- 1, end
);
1884 if (MODIFF
<= SAVE_MODIFF
)
1885 record_first_change ();
1887 CHARS_MODIFF
= MODIFF
;
1889 bset_point_before_scroll (current_buffer
, Qnil
);
1892 /* Signal that we are about to make a change that may result in new
1896 run_undoable_change (void)
1898 if (EQ (BVAR (current_buffer
, undo_list
), Qt
))
1901 call0 (Qundo_auto__undoable_change
);
1904 /* Check that it is okay to modify the buffer between START and END,
1905 which are char positions.
1907 Run the before-change-function, if any. If intervals are in use,
1908 verify that the text to be modified is not read-only, and call
1909 any modification properties the text may have.
1911 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1912 by holding its value temporarily in a marker.
1914 This function runs Lisp, which means it can GC, which means it can
1915 compact buffers, including the current buffer being worked on here.
1916 So don't you dare calling this function while manipulating the gap,
1917 or during some other similar "critical section". */
1920 prepare_to_modify_buffer_1 (ptrdiff_t start
, ptrdiff_t end
,
1921 ptrdiff_t *preserve_ptr
)
1923 struct buffer
*base_buffer
;
1926 XSETFASTINT (temp
, start
);
1927 if (!NILP (BVAR (current_buffer
, read_only
)))
1928 Fbarf_if_buffer_read_only (temp
);
1930 run_undoable_change();
1932 bset_redisplay (current_buffer
);
1934 if (buffer_intervals (current_buffer
))
1938 Lisp_Object preserve_marker
;
1939 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1940 verify_interval_modification (current_buffer
, start
, end
);
1941 *preserve_ptr
= marker_position (preserve_marker
);
1942 unchain_marker (XMARKER (preserve_marker
));
1945 verify_interval_modification (current_buffer
, start
, end
);
1948 /* For indirect buffers, use the base buffer to check clashes. */
1949 if (current_buffer
->base_buffer
!= 0)
1950 base_buffer
= current_buffer
->base_buffer
;
1952 base_buffer
= current_buffer
;
1954 if (inhibit_modification_hooks
)
1957 if (!NILP (BVAR (base_buffer
, file_truename
))
1958 /* Make binding buffer-file-name to nil effective. */
1959 && !NILP (BVAR (base_buffer
, filename
))
1960 && SAVE_MODIFF
>= MODIFF
)
1961 lock_file (BVAR (base_buffer
, file_truename
));
1963 /* If `select-active-regions' is non-nil, save the region text. */
1964 /* FIXME: Move this to Elisp (via before-change-functions). */
1965 if (!NILP (BVAR (current_buffer
, mark_active
))
1966 && XMARKER (BVAR (current_buffer
, mark
))->buffer
1967 && NILP (Vsaved_region_selection
)
1968 && (EQ (Vselect_active_regions
, Qonly
)
1969 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
1970 : (!NILP (Vselect_active_regions
)
1971 && !NILP (Vtransient_mark_mode
))))
1972 Vsaved_region_selection
1973 = call1 (Fsymbol_value (Qregion_extract_function
), Qnil
);
1975 signal_before_change (start
, end
, preserve_ptr
);
1976 Fset (Qdeactivate_mark
, Qt
);
1979 /* Like above, but called when we know that the buffer text
1980 will be modified and region caches should be invalidated. */
1983 prepare_to_modify_buffer (ptrdiff_t start
, ptrdiff_t end
,
1984 ptrdiff_t *preserve_ptr
)
1986 prepare_to_modify_buffer_1 (start
, end
, preserve_ptr
);
1987 invalidate_buffer_caches (current_buffer
, start
, end
);
1990 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1991 region between buffer positions START and END. */
1993 invalidate_buffer_caches (struct buffer
*buf
, ptrdiff_t start
, ptrdiff_t end
)
1995 /* Indirect buffers usually have their caches set to NULL, but we
1996 need to consider the caches of their base buffer. */
1997 if (buf
->base_buffer
)
1998 buf
= buf
->base_buffer
;
1999 /* The bidi_paragraph_cache must be invalidated first, because doing
2000 so might need to use the newline_cache (via find_newline_no_quit,
2002 if (buf
->bidi_paragraph_cache
)
2004 if (start
> BUF_BEG (buf
))
2006 /* If we are deleting or replacing characters, we could
2007 create a paragraph start, because all of the characters
2008 from START to the beginning of START's line are
2009 whitespace. Therefore, we must extend the region to be
2010 invalidated up to the newline before START. Similarly,
2011 if we are inserting characters immediately after a
2012 newline, we could create a paragraph start if the
2013 inserted characters start with a newline. */
2014 ptrdiff_t line_beg
= start
;
2015 ptrdiff_t start_byte
= buf_charpos_to_bytepos (buf
, start
);
2016 int prev_char
= BUF_FETCH_BYTE (buf
, start_byte
- 1);
2018 if ((start
== end
) == (prev_char
== '\n'))
2020 struct buffer
*old
= current_buffer
;
2022 set_buffer_internal (buf
);
2024 line_beg
= find_newline_no_quit (start
, start_byte
, -1,
2026 set_buffer_internal (old
);
2028 start
= line_beg
- (line_beg
> BUF_BEG (buf
));
2030 invalidate_region_cache (buf
,
2031 buf
->bidi_paragraph_cache
,
2032 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
2034 if (buf
->newline_cache
)
2035 invalidate_region_cache (buf
,
2037 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
2038 if (buf
->width_run_cache
)
2039 invalidate_region_cache (buf
,
2040 buf
->width_run_cache
,
2041 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
2044 /* These macros work with an argument named `preserve_ptr'
2045 and a local variable named `preserve_marker'. */
2047 #define PRESERVE_VALUE \
2048 if (preserve_ptr && NILP (preserve_marker)) \
2049 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2051 #define RESTORE_VALUE \
2052 if (! NILP (preserve_marker)) \
2054 *preserve_ptr = marker_position (preserve_marker); \
2055 unchain_marker (XMARKER (preserve_marker)); \
2058 #define PRESERVE_START_END \
2059 if (NILP (start_marker)) \
2060 start_marker = Fcopy_marker (start, Qnil); \
2061 if (NILP (end_marker)) \
2062 end_marker = Fcopy_marker (end, Qnil);
2064 #define FETCH_START \
2065 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2068 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2070 /* Set a variable to nil if an error occurred.
2071 Don't change the variable if there was no error.
2072 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2073 VARIABLE is the variable to maybe set to nil.
2074 NO-ERROR-FLAG is nil if there was an error,
2075 anything else meaning no error (so this function does nothing). */
2078 Lisp_Object
*location
;
2083 reset_var_on_error (void *ptr
)
2085 struct rvoe_arg
*p
= ptr
;
2087 *p
->location
= Qnil
;
2090 /* Signal a change to the buffer immediately before it happens.
2091 START_INT and END_INT are the bounds of the text to be changed.
2093 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2094 by holding its value temporarily in a marker. */
2097 signal_before_change (ptrdiff_t start_int
, ptrdiff_t end_int
,
2098 ptrdiff_t *preserve_ptr
)
2100 Lisp_Object start
, end
;
2101 Lisp_Object start_marker
, end_marker
;
2102 Lisp_Object preserve_marker
;
2103 ptrdiff_t count
= SPECPDL_INDEX ();
2104 struct rvoe_arg rvoe_arg
;
2106 start
= make_number (start_int
);
2107 end
= make_number (end_int
);
2108 preserve_marker
= Qnil
;
2109 start_marker
= Qnil
;
2112 specbind (Qinhibit_modification_hooks
, Qt
);
2114 /* If buffer is unmodified, run a special hook for that case. The
2115 check for Vfirst_change_hook is just a minor optimization. */
2116 if (SAVE_MODIFF
>= MODIFF
2117 && !NILP (Vfirst_change_hook
))
2121 run_hook (Qfirst_change_hook
);
2124 /* Now run the before-change-functions if any. */
2125 if (!NILP (Vbefore_change_functions
))
2127 rvoe_arg
.location
= &Vbefore_change_functions
;
2128 rvoe_arg
.errorp
= 1;
2133 /* Mark before-change-functions to be reset to nil in case of error. */
2134 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2136 /* Actually run the hook functions. */
2137 CALLN (Frun_hook_with_args
, Qbefore_change_functions
,
2138 FETCH_START
, FETCH_END
);
2140 /* There was no error: unarm the reset_on_error. */
2141 rvoe_arg
.errorp
= 0;
2144 if (buffer_has_overlays ())
2147 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2148 FETCH_START
, FETCH_END
, Qnil
);
2151 if (! NILP (start_marker
))
2152 free_marker (start_marker
);
2153 if (! NILP (end_marker
))
2154 free_marker (end_marker
);
2157 unbind_to (count
, Qnil
);
2160 /* Signal a change immediately after it happens.
2161 CHARPOS is the character position of the start of the changed text.
2162 LENDEL is the number of characters of the text before the change.
2163 (Not the whole buffer; just the part that was changed.)
2164 LENINS is the number of characters in that part of the text
2165 after the change. */
2168 signal_after_change (ptrdiff_t charpos
, ptrdiff_t lendel
, ptrdiff_t lenins
)
2170 ptrdiff_t count
= SPECPDL_INDEX ();
2171 struct rvoe_arg rvoe_arg
;
2173 if (inhibit_modification_hooks
)
2176 /* If we are deferring calls to the after-change functions
2177 and there are no before-change functions,
2178 just record the args that we were going to use. */
2179 if (! NILP (Vcombine_after_change_calls
)
2180 && NILP (Vbefore_change_functions
)
2181 && !buffer_has_overlays ())
2185 if (!NILP (combine_after_change_list
)
2186 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2187 Fcombine_after_change_execute ();
2189 elt
= list3i (charpos
- BEG
, Z
- (charpos
- lendel
+ lenins
),
2191 combine_after_change_list
2192 = Fcons (elt
, combine_after_change_list
);
2193 combine_after_change_buffer
= Fcurrent_buffer ();
2198 if (!NILP (combine_after_change_list
))
2199 Fcombine_after_change_execute ();
2201 specbind (Qinhibit_modification_hooks
, Qt
);
2203 if (!NILP (Vafter_change_functions
))
2205 rvoe_arg
.location
= &Vafter_change_functions
;
2206 rvoe_arg
.errorp
= 1;
2208 /* Mark after-change-functions to be reset to nil in case of error. */
2209 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2211 /* Actually run the hook functions. */
2212 CALLN (Frun_hook_with_args
, Qafter_change_functions
,
2213 make_number (charpos
), make_number (charpos
+ lenins
),
2214 make_number (lendel
));
2216 /* There was no error: unarm the reset_on_error. */
2217 rvoe_arg
.errorp
= 0;
2220 if (buffer_has_overlays ())
2221 report_overlay_modification (make_number (charpos
),
2222 make_number (charpos
+ lenins
),
2224 make_number (charpos
),
2225 make_number (charpos
+ lenins
),
2226 make_number (lendel
));
2228 /* After an insertion, call the text properties
2229 insert-behind-hooks or insert-in-front-hooks. */
2231 report_interval_modification (make_number (charpos
),
2232 make_number (charpos
+ lenins
));
2234 unbind_to (count
, Qnil
);
2238 Fcombine_after_change_execute_1 (Lisp_Object val
)
2240 Vcombine_after_change_calls
= val
;
2243 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2244 Scombine_after_change_execute
, 0, 0, 0,
2245 doc
: /* This function is for use internally in the function `combine-after-change-calls'. */)
2248 ptrdiff_t count
= SPECPDL_INDEX ();
2249 ptrdiff_t beg
, end
, change
;
2250 ptrdiff_t begpos
, endpos
;
2253 if (NILP (combine_after_change_list
))
2256 /* It is rare for combine_after_change_buffer to be invalid, but
2257 possible. It can happen when combine-after-change-calls is
2258 non-nil, and insertion calls a file handler (e.g. through
2259 lock_file) which scribbles into a temp file -- cyd */
2260 if (!BUFFERP (combine_after_change_buffer
)
2261 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer
)))
2263 combine_after_change_list
= Qnil
;
2267 record_unwind_current_buffer ();
2269 Fset_buffer (combine_after_change_buffer
);
2271 /* # chars unchanged at beginning of buffer. */
2273 /* # chars unchanged at end of buffer. */
2275 /* Total amount of insertion (negative for deletion). */
2278 /* Scan the various individual changes,
2279 accumulating the range info in BEG, END and CHANGE. */
2280 for (tail
= combine_after_change_list
; CONSP (tail
);
2284 ptrdiff_t thisbeg
, thisend
, thischange
;
2286 /* Extract the info from the next element. */
2290 thisbeg
= XINT (XCAR (elt
));
2295 thisend
= XINT (XCAR (elt
));
2300 thischange
= XINT (XCAR (elt
));
2302 /* Merge this range into the accumulated range. */
2303 change
+= thischange
;
2310 /* Get the current start and end positions of the range
2311 that was changed. */
2315 /* We are about to handle these, so discard them. */
2316 combine_after_change_list
= Qnil
;
2318 /* Now run the after-change functions for real.
2319 Turn off the flag that defers them. */
2320 record_unwind_protect (Fcombine_after_change_execute_1
,
2321 Vcombine_after_change_calls
);
2322 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2323 update_compositions (begpos
, endpos
, CHECK_ALL
);
2325 return unbind_to (count
, Qnil
);
2329 syms_of_insdel (void)
2331 staticpro (&combine_after_change_list
);
2332 staticpro (&combine_after_change_buffer
);
2333 combine_after_change_list
= Qnil
;
2334 combine_after_change_buffer
= Qnil
;
2336 DEFSYM (Qundo_auto__undoable_change
, "undo-auto--undoable-change");
2338 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls
,
2339 doc
: /* Used internally by the function `combine-after-change-calls' macro. */);
2340 Vcombine_after_change_calls
= Qnil
;
2342 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks
,
2343 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2344 This affects `before-change-functions' and `after-change-functions',
2345 as well as hooks attached to text properties and overlays. */);
2346 inhibit_modification_hooks
= 0;
2347 DEFSYM (Qinhibit_modification_hooks
, "inhibit-modification-hooks");
2349 DEFSYM (Qregion_extract_function
, "region-extract-function");
2351 defsubr (&Scombine_after_change_execute
);