1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include "intervals.h"
27 #include "character.h"
29 #include "blockinput.h"
30 #include "region-cache.h"
36 static void insert_from_string_1 (Lisp_Object string
,
37 EMACS_INT pos
, EMACS_INT pos_byte
,
38 EMACS_INT nchars
, EMACS_INT nbytes
,
39 int inherit
, int before_markers
);
40 static void insert_from_buffer_1 (struct buffer
*buf
,
41 EMACS_INT from
, EMACS_INT nchars
,
43 static void gap_left (EMACS_INT charpos
, EMACS_INT bytepos
, int newgap
);
44 static void gap_right (EMACS_INT charpos
, EMACS_INT bytepos
);
45 static void adjust_markers_gap_motion (EMACS_INT from
, EMACS_INT to
,
47 static void adjust_markers_for_insert (EMACS_INT from
, EMACS_INT from_byte
,
48 EMACS_INT to
, EMACS_INT to_byte
,
50 static void adjust_markers_for_replace (EMACS_INT
, EMACS_INT
, EMACS_INT
,
51 EMACS_INT
, EMACS_INT
, EMACS_INT
);
52 static void adjust_point (EMACS_INT nchars
, EMACS_INT nbytes
);
54 Lisp_Object
Fcombine_after_change_execute (void);
56 /* Non-nil means don't call the after-change-functions right away,
57 just record an element in combine_after_change_list. */
58 Lisp_Object Vcombine_after_change_calls
;
60 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
61 describing changes which happened while combine_after_change_calls
62 was nonzero. We use this to decide how to call them
63 once the deferral ends.
66 BEG-UNCHANGED is the number of chars before the changed range.
67 END-UNCHANGED is the number of chars after the changed range,
68 and CHANGE-AMOUNT is the number of characters inserted by the change
69 (negative for a deletion). */
70 Lisp_Object combine_after_change_list
;
72 /* Buffer which combine_after_change_list is about. */
73 Lisp_Object combine_after_change_buffer
;
75 Lisp_Object Qinhibit_modification_hooks
;
77 extern Lisp_Object Vselect_active_regions
, Vsaved_region_selection
, Qonly
;
80 /* Check all markers in the current buffer, looking for something invalid. */
82 static int check_markers_debug_flag
;
84 #define CHECK_MARKERS() \
85 if (check_markers_debug_flag) \
92 register struct Lisp_Marker
*tail
;
93 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
95 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
97 if (tail
->buffer
->text
!= current_buffer
->text
)
99 if (tail
->charpos
> Z
)
101 if (tail
->bytepos
> Z_BYTE
)
103 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (tail
->bytepos
)))
108 /* Move gap to position CHARPOS.
109 Note that this can quit! */
112 move_gap (EMACS_INT charpos
)
114 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
117 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
118 Note that this can quit! */
121 move_gap_both (EMACS_INT charpos
, EMACS_INT bytepos
)
123 if (bytepos
< GPT_BYTE
)
124 gap_left (charpos
, bytepos
, 0);
125 else if (bytepos
> GPT_BYTE
)
126 gap_right (charpos
, bytepos
);
129 /* Move the gap to a position less than the current GPT.
130 BYTEPOS describes the new position as a byte position,
131 and CHARPOS is the corresponding char position.
132 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
135 gap_left (EMACS_INT charpos
, EMACS_INT bytepos
, int newgap
)
137 register unsigned char *to
, *from
;
138 register EMACS_INT i
;
142 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
149 /* Now copy the characters. To move the gap down,
150 copy characters up. */
154 /* I gets number of characters left to copy. */
155 i
= new_s1
- bytepos
;
158 /* If a quit is requested, stop copying now.
159 Change BYTEPOS to be where we have actually moved the gap to. */
163 charpos
= BYTE_TO_CHAR (bytepos
);
166 /* Move at most 32000 chars before checking again for a quit. */
171 memmove (to
, from
, i
);
174 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
175 BYTEPOS is where the loop above stopped, which may be what was specified
176 or may be where a quit was detected. */
177 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
180 if (bytepos
< charpos
)
182 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
186 /* Move the gap to a position greater than the current GPT.
187 BYTEPOS describes the new position as a byte position,
188 and CHARPOS is the corresponding char position. */
191 gap_right (EMACS_INT charpos
, EMACS_INT bytepos
)
193 register unsigned char *to
, *from
;
194 register EMACS_INT i
;
197 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
204 /* Now copy the characters. To move the gap up,
205 copy characters down. */
209 /* I gets number of characters left to copy. */
210 i
= bytepos
- new_s1
;
213 /* If a quit is requested, stop copying now.
214 Change BYTEPOS to be where we have actually moved the gap to. */
218 charpos
= BYTE_TO_CHAR (bytepos
);
221 /* Move at most 32000 chars before checking again for a quit. */
225 memmove (to
, from
, i
);
229 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
233 if (bytepos
< charpos
)
235 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
239 /* Add AMOUNT to the byte position of every marker in the current buffer
240 whose current byte position is between FROM (exclusive) and TO (inclusive).
242 Also, any markers past the outside of that interval, in the direction
243 of adjustment, are first moved back to the near end of the interval
244 and then adjusted by AMOUNT.
246 When the latter adjustment is done, if AMOUNT is negative,
247 we record the adjustment for undo. (This case happens only for
250 The markers' character positions are not altered,
251 because gap motion does not affect character positions. */
253 int adjust_markers_test
;
256 adjust_markers_gap_motion (EMACS_INT from
, EMACS_INT to
, EMACS_INT amount
)
258 /* Now that a marker has a bytepos, not counting the gap,
259 nothing needs to be done here. */
262 register struct Lisp_Marker
*m
;
263 register EMACS_INT mpos
;
265 marker
= BUF_MARKERS (current_buffer
);
267 while (!NILP (marker
))
269 m
= XMARKER (marker
);
273 if (mpos
> to
&& mpos
< to
+ amount
)
275 if (adjust_markers_test
)
282 /* Here's the case where a marker is inside text being deleted.
283 AMOUNT can be negative for gap motion, too,
284 but then this range contains no markers. */
285 if (mpos
> from
+ amount
&& mpos
<= from
)
287 if (adjust_markers_test
)
289 mpos
= from
+ amount
;
292 if (mpos
> from
&& mpos
<= to
)
300 /* Adjust all markers for a deletion
301 whose range in bytes is FROM_BYTE to TO_BYTE.
302 The range in charpos is FROM to TO.
304 This function assumes that the gap is adjacent to
305 or inside of the range being deleted. */
308 adjust_markers_for_delete (EMACS_INT from
, EMACS_INT from_byte
,
309 EMACS_INT to
, EMACS_INT to_byte
)
312 register struct Lisp_Marker
*m
;
313 register EMACS_INT charpos
;
315 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
317 charpos
= m
->charpos
;
322 /* If the marker is after the deletion,
323 relocate by number of chars / bytes deleted. */
326 m
->charpos
-= to
- from
;
327 m
->bytepos
-= to_byte
- from_byte
;
329 /* Here's the case where a marker is inside text being deleted. */
330 else if (charpos
> from
)
332 if (! m
->insertion_type
)
333 { /* Normal markers will end up at the beginning of the
334 re-inserted text after undoing a deletion, and must be
335 adjusted to move them to the correct place. */
336 XSETMISC (marker
, m
);
337 record_marker_adjustment (marker
, from
- charpos
);
339 else if (charpos
< to
)
340 { /* Before-insertion markers will automatically move forward
341 upon re-inserting the deleted text, so we have to arrange
342 for them to move backward to the correct position. */
343 XSETMISC (marker
, m
);
344 record_marker_adjustment (marker
, to
- charpos
);
347 m
->bytepos
= from_byte
;
349 /* Here's the case where a before-insertion marker is immediately
350 before the deleted region. */
351 else if (charpos
== from
&& m
->insertion_type
)
353 /* Undoing the change uses normal insertion, which will
354 incorrectly make MARKER move forward, so we arrange for it
355 to then move backward to the correct place at the beginning
356 of the deleted region. */
357 XSETMISC (marker
, m
);
358 record_marker_adjustment (marker
, to
- from
);
364 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
365 to TO / TO_BYTE. We have to relocate the charpos of every marker
366 that points after the insertion (but not their bytepos).
368 When a marker points at the insertion point,
369 we advance it if either its insertion-type is t
370 or BEFORE_MARKERS is true. */
373 adjust_markers_for_insert (EMACS_INT from
, EMACS_INT from_byte
,
374 EMACS_INT to
, EMACS_INT to_byte
, int before_markers
)
376 struct Lisp_Marker
*m
;
378 EMACS_INT nchars
= to
- from
;
379 EMACS_INT nbytes
= to_byte
- from_byte
;
381 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
383 eassert (m
->bytepos
>= m
->charpos
384 && m
->bytepos
- m
->charpos
<= Z_BYTE
- Z
);
386 if (m
->bytepos
== from_byte
)
388 if (m
->insertion_type
|| before_markers
)
390 m
->bytepos
= to_byte
;
392 if (m
->insertion_type
)
396 else if (m
->bytepos
> from_byte
)
398 m
->bytepos
+= nbytes
;
399 m
->charpos
+= nchars
;
403 /* Adjusting only markers whose insertion-type is t may result in
404 - disordered start and end in overlays, and
405 - disordered overlays in the slot `overlays_before' of current_buffer. */
408 fix_start_end_in_overlays(from
, to
);
409 fix_overlays_before (current_buffer
, from
, to
);
413 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
415 This is used only when the value of point changes due to an insert
416 or delete; it does not represent a conceptual change in point as a
417 marker. In particular, point is not crossing any interval
418 boundaries, so there's no need to use the usual SET_PT macro. In
419 fact it would be incorrect to do so, because either the old or the
420 new value of point is out of sync with the current set of
424 adjust_point (EMACS_INT nchars
, EMACS_INT nbytes
)
426 BUF_PT (current_buffer
) += nchars
;
427 BUF_PT_BYTE (current_buffer
) += nbytes
;
429 /* In a single-byte buffer, the two positions must be equal. */
430 eassert (PT_BYTE
>= PT
&& PT_BYTE
- PT
<= ZV_BYTE
- ZV
);
433 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
434 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
435 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
439 adjust_markers_for_replace (EMACS_INT from
, EMACS_INT from_byte
,
440 EMACS_INT old_chars
, EMACS_INT old_bytes
,
441 EMACS_INT new_chars
, EMACS_INT new_bytes
)
443 register struct Lisp_Marker
*m
;
444 EMACS_INT prev_to_byte
= from_byte
+ old_bytes
;
445 EMACS_INT diff_chars
= new_chars
- old_chars
;
446 EMACS_INT diff_bytes
= new_bytes
- old_bytes
;
448 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
450 if (m
->bytepos
>= prev_to_byte
)
452 m
->charpos
+= diff_chars
;
453 m
->bytepos
+= diff_bytes
;
455 else if (m
->bytepos
> from_byte
)
458 m
->bytepos
= from_byte
;
466 /* Make the gap NBYTES_ADDED bytes longer. */
469 make_gap_larger (EMACS_INT nbytes_added
)
472 EMACS_INT real_gap_loc
;
473 EMACS_INT real_gap_loc_byte
;
474 EMACS_INT old_gap_size
;
476 /* If we have to get more space, get enough to last a while. */
477 nbytes_added
+= 2000;
479 { EMACS_INT total_size
= Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
;
481 /* Don't allow a buffer size that won't fit in a Lisp integer. */
482 || total_size
!= XINT (make_number (total_size
))
483 /* Don't allow a buffer size that won't fit in an int
484 even if it will fit in a Lisp integer.
485 That won't work because so many places still use `int'. */
486 || total_size
!= (EMACS_INT
) (int) total_size
)
487 error ("Buffer exceeds maximum size");
490 enlarge_buffer_text (current_buffer
, nbytes_added
);
492 /* Prevent quitting in move_gap. */
497 real_gap_loc_byte
= GPT_BYTE
;
498 old_gap_size
= GAP_SIZE
;
500 /* Call the newly allocated space a gap at the end of the whole space. */
502 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
503 GAP_SIZE
= nbytes_added
;
505 /* Move the new gap down to be consecutive with the end of the old one.
506 This adjusts the markers properly too. */
507 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
509 /* Now combine the two into one large gap. */
510 GAP_SIZE
+= old_gap_size
;
512 GPT_BYTE
= real_gap_loc_byte
;
521 /* Make the gap NBYTES_REMOVED bytes shorter. */
524 make_gap_smaller (EMACS_INT nbytes_removed
)
527 EMACS_INT real_gap_loc
;
528 EMACS_INT real_gap_loc_byte
;
530 EMACS_INT real_Z_byte
;
531 EMACS_INT real_beg_unchanged
;
532 EMACS_INT new_gap_size
;
534 /* Make sure the gap is at least 20 bytes. */
535 if (GAP_SIZE
- nbytes_removed
< 20)
536 nbytes_removed
= GAP_SIZE
- 20;
538 /* Prevent quitting in move_gap. */
543 real_gap_loc_byte
= GPT_BYTE
;
544 new_gap_size
= GAP_SIZE
- nbytes_removed
;
546 real_Z_byte
= Z_BYTE
;
547 real_beg_unchanged
= BEG_UNCHANGED
;
549 /* Pretend that the last unwanted part of the gap is the entire gap,
550 and that the first desired part of the gap is part of the buffer
552 memset (GPT_ADDR
, 0, new_gap_size
);
554 GPT_BYTE
+= new_gap_size
;
556 Z_BYTE
+= new_gap_size
;
557 GAP_SIZE
= nbytes_removed
;
559 /* Move the unwanted pretend gap to the end of the buffer. This
560 adjusts the markers properly too. */
561 gap_right (Z
, Z_BYTE
);
563 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
565 /* Now restore the desired gap. */
566 GAP_SIZE
= new_gap_size
;
568 GPT_BYTE
= real_gap_loc_byte
;
570 Z_BYTE
= real_Z_byte
;
571 BEG_UNCHANGED
= real_beg_unchanged
;
580 make_gap (EMACS_INT nbytes_added
)
582 if (nbytes_added
>= 0)
583 make_gap_larger (nbytes_added
);
584 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
586 make_gap_smaller (-nbytes_added
);
590 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
591 FROM_MULTIBYTE says whether the incoming text is multibyte.
592 TO_MULTIBYTE says whether to store the text as multibyte.
593 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
595 Return the number of bytes stored at TO_ADDR. */
598 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
599 EMACS_INT nbytes
, int from_multibyte
, int to_multibyte
)
601 if (from_multibyte
== to_multibyte
)
603 memcpy (to_addr
, from_addr
, nbytes
);
606 else if (from_multibyte
)
608 EMACS_INT nchars
= 0;
609 EMACS_INT bytes_left
= nbytes
;
610 Lisp_Object tbl
= Qnil
;
612 while (bytes_left
> 0)
615 c
= STRING_CHAR_AND_LENGTH (from_addr
, thislen
);
616 if (! ASCII_CHAR_P (c
))
619 from_addr
+= thislen
;
620 bytes_left
-= thislen
;
627 unsigned char *initial_to_addr
= to_addr
;
629 /* Convert single-byte to multibyte. */
632 int c
= *from_addr
++;
634 if (!ASCII_CHAR_P (c
))
636 c
= BYTE8_TO_CHAR (c
);
637 to_addr
+= CHAR_STRING (c
, to_addr
);
641 /* Special case for speed. */
642 *to_addr
++ = c
, nbytes
--;
644 return to_addr
- initial_to_addr
;
648 /* Return the number of bytes it would take
649 to convert some single-byte text to multibyte.
650 The single-byte text consists of NBYTES bytes at PTR. */
653 count_size_as_multibyte (const unsigned char *ptr
, EMACS_INT nbytes
)
656 EMACS_INT outgoing_nbytes
= 0;
658 for (i
= 0; i
< nbytes
; i
++)
660 unsigned int c
= *ptr
++;
662 if (ASCII_CHAR_P (c
))
666 c
= BYTE8_TO_CHAR (c
);
667 outgoing_nbytes
+= CHAR_BYTES (c
);
671 return outgoing_nbytes
;
674 /* Insert a string of specified length before point.
675 This function judges multibyteness based on
676 enable_multibyte_characters in the current buffer;
677 it never converts between single-byte and multibyte.
679 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
680 prepare_to_modify_buffer could relocate the text. */
683 insert (const unsigned char *string
, EMACS_INT nbytes
)
687 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
688 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
690 signal_after_change (opoint
, 0, len
);
691 update_compositions (opoint
, PT
, CHECK_BORDER
);
695 /* Likewise, but inherit text properties from neighboring characters. */
698 insert_and_inherit (const unsigned char *string
, EMACS_INT nbytes
)
702 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
703 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
705 signal_after_change (opoint
, 0, len
);
706 update_compositions (opoint
, PT
, CHECK_BORDER
);
710 /* Insert the character C before point. Do not inherit text properties. */
715 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
718 if (! NILP (current_buffer
->enable_multibyte_characters
))
719 len
= CHAR_STRING (c
, str
);
729 /* Insert the null-terminated string S before point. */
732 insert_string (const char *s
)
734 insert (s
, strlen (s
));
737 /* Like `insert' except that all markers pointing at the place where
738 the insertion happens are adjusted to point after it.
739 Don't use this function to insert part of a Lisp string,
740 since gc could happen and relocate it. */
743 insert_before_markers (const unsigned char *string
, EMACS_INT nbytes
)
747 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
748 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
750 signal_after_change (opoint
, 0, len
);
751 update_compositions (opoint
, PT
, CHECK_BORDER
);
755 /* Likewise, but inherit text properties from neighboring characters. */
758 insert_before_markers_and_inherit (const unsigned char *string
,
763 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
764 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
766 signal_after_change (opoint
, 0, len
);
767 update_compositions (opoint
, PT
, CHECK_BORDER
);
771 /* Subroutine used by the insert functions above. */
774 insert_1 (const unsigned char *string
, EMACS_INT nbytes
,
775 int inherit
, int prepare
, int before_markers
)
777 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
778 inherit
, prepare
, before_markers
);
782 #ifdef BYTE_COMBINING_DEBUG
784 /* See if the bytes before POS/POS_BYTE combine with bytes
785 at the start of STRING to form a single character.
786 If so, return the number of bytes at the start of STRING
787 which combine in this way. Otherwise, return 0. */
790 count_combining_before (const unsigned char *string
, EMACS_INT length
,
791 EMACS_INT pos
, EMACS_INT pos_byte
)
793 int len
, combining_bytes
;
794 const unsigned char *p
;
796 if (NILP (current_buffer
->enable_multibyte_characters
))
799 /* At first, we can exclude the following cases:
800 (1) STRING[0] can't be a following byte of multibyte sequence.
801 (2) POS is the start of the current buffer.
802 (3) A character before POS is not a multibyte character. */
803 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
805 if (pos_byte
== BEG_BYTE
) /* case (2) */
808 p
= BYTE_POS_ADDR (pos_byte
- 1);
809 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
810 if (! LEADING_CODE_P (*p
)) /* case (3) */
813 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
814 if (combining_bytes
<= 0)
815 /* The character preceding POS is, complete and no room for
816 combining bytes (combining_bytes == 0), or an independent 8-bit
817 character (combining_bytes < 0). */
820 /* We have a combination situation. Count the bytes at STRING that
823 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
826 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
829 /* See if the bytes after POS/POS_BYTE combine with bytes
830 at the end of STRING to form a single character.
831 If so, return the number of bytes after POS/POS_BYTE
832 which combine in this way. Otherwise, return 0. */
835 count_combining_after (const unsigned char *string
,
836 EMACS_INT length
, EMACS_INT pos
, EMACS_INT pos_byte
)
838 EMACS_INT opos_byte
= pos_byte
;
843 if (NILP (current_buffer
->enable_multibyte_characters
))
846 /* At first, we can exclude the following cases:
847 (1) The last byte of STRING is an ASCII.
848 (2) POS is the last of the current buffer.
849 (3) A character at POS can't be a following byte of multibyte
851 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1])) /* case (1) */
853 if (pos_byte
== Z_BYTE
) /* case (2) */
855 bufp
= BYTE_POS_ADDR (pos_byte
);
856 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
860 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
866 /* All characters in STRING are not character head. We must
867 check also preceding bytes at POS. We are sure that the gap
869 unsigned char *p
= BEG_ADDR
;
871 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
873 if (i
< 0 || !LEADING_CODE_P (p
[i
]))
876 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
877 return (bytes
<= pos_byte
- 1 - i
+ length
879 : bytes
- (pos_byte
- 1 - i
+ length
));
881 if (!LEADING_CODE_P (string
[i
]))
884 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
886 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
888 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
894 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
895 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
896 are the same as in insert_1. */
899 insert_1_both (const unsigned char *string
,
900 EMACS_INT nchars
, EMACS_INT nbytes
,
901 int inherit
, int prepare
, int before_markers
)
906 if (NILP (current_buffer
->enable_multibyte_characters
))
910 /* Do this before moving and increasing the gap,
911 because the before-change hooks might move the gap
912 or make it smaller. */
913 prepare_to_modify_buffer (PT
, PT
, NULL
);
916 move_gap_both (PT
, PT_BYTE
);
917 if (GAP_SIZE
< nbytes
)
918 make_gap (nbytes
- GAP_SIZE
);
920 #ifdef BYTE_COMBINING_DEBUG
921 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
922 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
926 /* Record deletion of the surrounding text that combines with
927 the insertion. This, together with recording the insertion,
928 will add up to the right stuff in the undo list. */
929 record_insert (PT
, nchars
);
931 CHARS_MODIFF
= MODIFF
;
933 memcpy (GPT_ADDR
, string
, nbytes
);
942 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
947 /* The insert may have been in the unchanged region, so check again. */
948 if (Z
- GPT
< END_UNCHANGED
)
949 END_UNCHANGED
= Z
- GPT
;
951 adjust_overlays_for_insert (PT
, nchars
);
952 adjust_markers_for_insert (PT
, PT_BYTE
,
953 PT
+ nchars
, PT_BYTE
+ nbytes
,
956 if (BUF_INTERVALS (current_buffer
) != 0)
957 offset_intervals (current_buffer
, PT
, nchars
);
959 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
960 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
963 adjust_point (nchars
, nbytes
);
968 /* Insert the part of the text of STRING, a Lisp object assumed to be
969 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
970 starting at position POS / POS_BYTE. If the text of STRING has properties,
971 copy them into the buffer.
973 It does not work to use `insert' for this, because a GC could happen
974 before we copy the stuff into the buffer, and relocate the string
975 without insert noticing. */
978 insert_from_string (Lisp_Object string
, EMACS_INT pos
, EMACS_INT pos_byte
,
979 EMACS_INT length
, EMACS_INT length_byte
, int inherit
)
981 EMACS_INT opoint
= PT
;
983 if (SCHARS (string
) == 0)
986 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
988 signal_after_change (opoint
, 0, PT
- opoint
);
989 update_compositions (opoint
, PT
, CHECK_BORDER
);
992 /* Like `insert_from_string' except that all markers pointing
993 at the place where the insertion happens are adjusted to point after it. */
996 insert_from_string_before_markers (Lisp_Object string
,
997 EMACS_INT pos
, EMACS_INT pos_byte
,
998 EMACS_INT length
, EMACS_INT length_byte
,
1001 EMACS_INT opoint
= PT
;
1003 if (SCHARS (string
) == 0)
1006 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1008 signal_after_change (opoint
, 0, PT
- opoint
);
1009 update_compositions (opoint
, PT
, CHECK_BORDER
);
1012 /* Subroutine of the insertion functions above. */
1015 insert_from_string_1 (Lisp_Object string
, EMACS_INT pos
, EMACS_INT pos_byte
,
1016 EMACS_INT nchars
, EMACS_INT nbytes
,
1017 int inherit
, int before_markers
)
1019 struct gcpro gcpro1
;
1020 EMACS_INT outgoing_nbytes
= nbytes
;
1023 /* Make OUTGOING_NBYTES describe the text
1024 as it will be inserted in this buffer. */
1026 if (NILP (current_buffer
->enable_multibyte_characters
))
1027 outgoing_nbytes
= nchars
;
1028 else if (! STRING_MULTIBYTE (string
))
1030 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
1034 /* Do this before moving and increasing the gap,
1035 because the before-change hooks might move the gap
1036 or make it smaller. */
1037 prepare_to_modify_buffer (PT
, PT
, NULL
);
1040 move_gap_both (PT
, PT_BYTE
);
1041 if (GAP_SIZE
< outgoing_nbytes
)
1042 make_gap (outgoing_nbytes
- GAP_SIZE
);
1045 /* Copy the string text into the buffer, perhaps converting
1046 between single-byte and multibyte. */
1047 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
1048 STRING_MULTIBYTE (string
),
1049 ! NILP (current_buffer
->enable_multibyte_characters
));
1051 #ifdef BYTE_COMBINING_DEBUG
1052 /* We have copied text into the gap, but we have not altered
1053 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1054 to these functions and get the same results as we would
1055 have got earlier on. Meanwhile, PT_ADDR does point to
1056 the text that has been stored by copy_text. */
1057 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1058 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1062 record_insert (PT
, nchars
);
1064 CHARS_MODIFF
= MODIFF
;
1066 GAP_SIZE
-= outgoing_nbytes
;
1070 GPT_BYTE
+= outgoing_nbytes
;
1071 ZV_BYTE
+= outgoing_nbytes
;
1072 Z_BYTE
+= outgoing_nbytes
;
1073 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1078 /* The insert may have been in the unchanged region, so check again. */
1079 if (Z
- GPT
< END_UNCHANGED
)
1080 END_UNCHANGED
= Z
- GPT
;
1082 adjust_overlays_for_insert (PT
, nchars
);
1083 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1084 PT_BYTE
+ outgoing_nbytes
,
1087 offset_intervals (current_buffer
, PT
, nchars
);
1089 intervals
= STRING_INTERVALS (string
);
1090 /* Get the intervals for the part of the string we are inserting. */
1091 if (nbytes
< SBYTES (string
))
1092 intervals
= copy_intervals (intervals
, pos
, nchars
);
1094 /* Insert those intervals. */
1095 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1096 current_buffer
, inherit
);
1098 adjust_point (nchars
, outgoing_nbytes
);
1103 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1104 starting at GPT_ADDR. */
1107 insert_from_gap (EMACS_INT nchars
, EMACS_INT nbytes
)
1109 if (NILP (current_buffer
->enable_multibyte_characters
))
1112 record_insert (GPT
, nchars
);
1122 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1127 adjust_overlays_for_insert (GPT
- nchars
, nchars
);
1128 adjust_markers_for_insert (GPT
- nchars
, GPT_BYTE
- nbytes
,
1131 if (BUF_INTERVALS (current_buffer
) != 0)
1133 offset_intervals (current_buffer
, GPT
- nchars
, nchars
);
1134 graft_intervals_into_buffer (NULL_INTERVAL
, GPT
- nchars
, nchars
,
1138 if (GPT
- nchars
< PT
)
1139 adjust_point (nchars
, nbytes
);
1144 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1145 current buffer. If the text in BUF has properties, they are absorbed
1146 into the current buffer.
1148 It does not work to use `insert' for this, because a malloc could happen
1149 and relocate BUF's text before the copy happens. */
1152 insert_from_buffer (struct buffer
*buf
,
1153 EMACS_INT charpos
, EMACS_INT nchars
, int inherit
)
1155 EMACS_INT opoint
= PT
;
1157 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1158 signal_after_change (opoint
, 0, PT
- opoint
);
1159 update_compositions (opoint
, PT
, CHECK_BORDER
);
1163 insert_from_buffer_1 (struct buffer
*buf
,
1164 EMACS_INT from
, EMACS_INT nchars
, int inherit
)
1166 register Lisp_Object temp
;
1167 EMACS_INT chunk
, chunk_expanded
;
1168 EMACS_INT from_byte
= buf_charpos_to_bytepos (buf
, from
);
1169 EMACS_INT to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1170 EMACS_INT incoming_nbytes
= to_byte
- from_byte
;
1171 EMACS_INT outgoing_nbytes
= incoming_nbytes
;
1174 /* Make OUTGOING_NBYTES describe the text
1175 as it will be inserted in this buffer. */
1177 if (NILP (current_buffer
->enable_multibyte_characters
))
1178 outgoing_nbytes
= nchars
;
1179 else if (NILP (buf
->enable_multibyte_characters
))
1181 EMACS_INT outgoing_before_gap
= 0;
1182 EMACS_INT outgoing_after_gap
= 0;
1184 if (from
< BUF_GPT (buf
))
1186 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1187 if (chunk
> incoming_nbytes
)
1188 chunk
= incoming_nbytes
;
1190 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1196 if (chunk
< incoming_nbytes
)
1198 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1200 incoming_nbytes
- chunk
);
1202 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1205 /* Make sure point-max won't overflow after this insertion. */
1206 XSETINT (temp
, outgoing_nbytes
+ Z
);
1207 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1208 error ("Maximum buffer size exceeded");
1210 /* Do this before moving and increasing the gap,
1211 because the before-change hooks might move the gap
1212 or make it smaller. */
1213 prepare_to_modify_buffer (PT
, PT
, NULL
);
1216 move_gap_both (PT
, PT_BYTE
);
1217 if (GAP_SIZE
< outgoing_nbytes
)
1218 make_gap (outgoing_nbytes
- GAP_SIZE
);
1220 if (from
< BUF_GPT (buf
))
1222 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1223 if (chunk
> incoming_nbytes
)
1224 chunk
= incoming_nbytes
;
1225 /* Record number of output bytes, so we know where
1226 to put the output from the second copy_text. */
1228 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1230 ! NILP (buf
->enable_multibyte_characters
),
1231 ! NILP (current_buffer
->enable_multibyte_characters
));
1234 chunk_expanded
= chunk
= 0;
1236 if (chunk
< incoming_nbytes
)
1237 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1238 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1239 ! NILP (buf
->enable_multibyte_characters
),
1240 ! NILP (current_buffer
->enable_multibyte_characters
));
1242 #ifdef BYTE_COMBINING_DEBUG
1243 /* We have copied text into the gap, but we have not altered
1244 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1245 to these functions and get the same results as we would
1246 have got earlier on. Meanwhile, GPT_ADDR does point to
1247 the text that has been stored by copy_text. */
1248 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1249 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1253 record_insert (PT
, nchars
);
1255 CHARS_MODIFF
= MODIFF
;
1257 GAP_SIZE
-= outgoing_nbytes
;
1261 GPT_BYTE
+= outgoing_nbytes
;
1262 ZV_BYTE
+= outgoing_nbytes
;
1263 Z_BYTE
+= outgoing_nbytes
;
1264 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1269 /* The insert may have been in the unchanged region, so check again. */
1270 if (Z
- GPT
< END_UNCHANGED
)
1271 END_UNCHANGED
= Z
- GPT
;
1273 adjust_overlays_for_insert (PT
, nchars
);
1274 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1275 PT_BYTE
+ outgoing_nbytes
,
1278 if (BUF_INTERVALS (current_buffer
) != 0)
1279 offset_intervals (current_buffer
, PT
, nchars
);
1281 /* Get the intervals for the part of the string we are inserting. */
1282 intervals
= BUF_INTERVALS (buf
);
1283 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1285 if (buf
== current_buffer
&& PT
<= from
)
1287 intervals
= copy_intervals (intervals
, from
, nchars
);
1290 /* Insert those intervals. */
1291 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1293 adjust_point (nchars
, outgoing_nbytes
);
1296 /* Record undo information and adjust markers and position keepers for
1297 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1298 chars (LEN_BYTE bytes) which resides in the gap just after
1301 PREV_TEXT nil means the new text was just inserted. */
1304 adjust_after_replace (EMACS_INT from
, EMACS_INT from_byte
,
1305 Lisp_Object prev_text
, EMACS_INT len
, EMACS_INT len_byte
)
1307 EMACS_INT nchars_del
= 0, nbytes_del
= 0;
1309 #ifdef BYTE_COMBINING_DEBUG
1310 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1311 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1315 if (STRINGP (prev_text
))
1317 nchars_del
= SCHARS (prev_text
);
1318 nbytes_del
= SBYTES (prev_text
);
1321 /* Update various buffer positions for the new text. */
1322 GAP_SIZE
-= len_byte
;
1324 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1325 GPT
+= len
; GPT_BYTE
+= len_byte
;
1326 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1329 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1332 adjust_markers_for_insert (from
, from_byte
,
1333 from
+ len
, from_byte
+ len_byte
, 0);
1335 if (! EQ (current_buffer
->undo_list
, Qt
))
1338 record_delete (from
, prev_text
);
1339 record_insert (from
, len
);
1342 if (len
> nchars_del
)
1343 adjust_overlays_for_insert (from
, len
- nchars_del
);
1344 else if (len
< nchars_del
)
1345 adjust_overlays_for_delete (from
, nchars_del
- len
);
1346 if (BUF_INTERVALS (current_buffer
) != 0)
1348 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1352 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1354 /* As byte combining will decrease Z, we must check this again. */
1355 if (Z
- GPT
< END_UNCHANGED
)
1356 END_UNCHANGED
= Z
- GPT
;
1361 evaporate_overlays (from
);
1363 CHARS_MODIFF
= MODIFF
;
1366 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1367 This is for use when undo is not enabled in the current buffer. */
1370 adjust_after_replace_noundo (EMACS_INT from
, EMACS_INT from_byte
,
1371 EMACS_INT nchars_del
, EMACS_INT nbytes_del
,
1372 EMACS_INT len
, EMACS_INT len_byte
)
1374 #ifdef BYTE_COMBINING_DEBUG
1375 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1376 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1380 /* Update various buffer positions for the new text. */
1381 GAP_SIZE
-= len_byte
;
1383 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1384 GPT
+= len
; GPT_BYTE
+= len_byte
;
1385 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1388 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1391 adjust_markers_for_insert (from
, from_byte
,
1392 from
+ len
, from_byte
+ len_byte
, 0);
1394 if (len
> nchars_del
)
1395 adjust_overlays_for_insert (from
, len
- nchars_del
);
1396 else if (len
< nchars_del
)
1397 adjust_overlays_for_delete (from
, nchars_del
- len
);
1398 if (BUF_INTERVALS (current_buffer
) != 0)
1400 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1404 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1406 /* As byte combining will decrease Z, we must check this again. */
1407 if (Z
- GPT
< END_UNCHANGED
)
1408 END_UNCHANGED
= Z
- GPT
;
1413 evaporate_overlays (from
);
1415 CHARS_MODIFF
= MODIFF
;
1418 /* Record undo information, adjust markers and position keepers for an
1419 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1420 text already exists in the current buffer but character length (TO
1421 - FROM) may be incorrect, the correct length is NEWLEN. */
1424 adjust_after_insert (EMACS_INT from
, EMACS_INT from_byte
,
1425 EMACS_INT to
, EMACS_INT to_byte
, EMACS_INT newlen
)
1427 EMACS_INT len
= to
- from
, len_byte
= to_byte
- from_byte
;
1430 move_gap_both (to
, to_byte
);
1431 GAP_SIZE
+= len_byte
;
1432 GPT
-= len
; GPT_BYTE
-= len_byte
;
1433 ZV
-= len
; ZV_BYTE
-= len_byte
;
1434 Z
-= len
; Z_BYTE
-= len_byte
;
1435 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1438 /* Replace the text from character positions FROM to TO with NEW,
1439 If PREPARE is nonzero, call prepare_to_modify_buffer.
1440 If INHERIT, the newly inserted text should inherit text properties
1441 from the surrounding non-deleted text. */
1443 /* Note that this does not yet handle markers quite right.
1444 Also it needs to record a single undo-entry that does a replacement
1445 rather than a separate delete and insert.
1446 That way, undo will also handle markers properly.
1448 But if MARKERS is 0, don't relocate markers. */
1451 replace_range (EMACS_INT from
, EMACS_INT to
, Lisp_Object
new,
1452 int prepare
, int inherit
, int markers
)
1454 EMACS_INT inschars
= SCHARS (new);
1455 EMACS_INT insbytes
= SBYTES (new);
1456 EMACS_INT from_byte
, to_byte
;
1457 EMACS_INT nbytes_del
, nchars_del
;
1458 register Lisp_Object temp
;
1459 struct gcpro gcpro1
;
1461 EMACS_INT outgoing_insbytes
= insbytes
;
1462 Lisp_Object deletion
;
1471 EMACS_INT range_length
= to
- from
;
1472 prepare_to_modify_buffer (from
, to
, &from
);
1473 to
= from
+ range_length
;
1478 /* Make args be valid */
1484 from_byte
= CHAR_TO_BYTE (from
);
1485 to_byte
= CHAR_TO_BYTE (to
);
1487 nchars_del
= to
- from
;
1488 nbytes_del
= to_byte
- from_byte
;
1490 if (nbytes_del
<= 0 && insbytes
== 0)
1493 /* Make OUTGOING_INSBYTES describe the text
1494 as it will be inserted in this buffer. */
1496 if (NILP (current_buffer
->enable_multibyte_characters
))
1497 outgoing_insbytes
= inschars
;
1498 else if (! STRING_MULTIBYTE (new))
1500 = count_size_as_multibyte (SDATA (new), insbytes
);
1502 /* Make sure point-max won't overflow after this insertion. */
1503 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1504 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1505 error ("Maximum buffer size exceeded");
1509 /* Make sure the gap is somewhere in or next to what we are deleting. */
1511 gap_right (from
, from_byte
);
1513 gap_left (to
, to_byte
, 0);
1515 /* Even if we don't record for undo, we must keep the original text
1516 because we may have to recover it because of inappropriate byte
1518 if (! EQ (current_buffer
->undo_list
, Qt
))
1519 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1521 GAP_SIZE
+= nbytes_del
;
1524 ZV_BYTE
-= nbytes_del
;
1525 Z_BYTE
-= nbytes_del
;
1527 GPT_BYTE
= from_byte
;
1528 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1533 if (GPT
- BEG
< BEG_UNCHANGED
)
1534 BEG_UNCHANGED
= GPT
- BEG
;
1535 if (Z
- GPT
< END_UNCHANGED
)
1536 END_UNCHANGED
= Z
- GPT
;
1538 if (GAP_SIZE
< insbytes
)
1539 make_gap (insbytes
- GAP_SIZE
);
1541 /* Copy the string text into the buffer, perhaps converting
1542 between single-byte and multibyte. */
1543 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1544 STRING_MULTIBYTE (new),
1545 ! NILP (current_buffer
->enable_multibyte_characters
));
1547 #ifdef BYTE_COMBINING_DEBUG
1548 /* We have copied text into the gap, but we have not marked
1549 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1550 here, for both the previous text and the following text.
1551 Meanwhile, GPT_ADDR does point to
1552 the text that has been stored by copy_text. */
1553 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1554 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1558 if (! EQ (current_buffer
->undo_list
, Qt
))
1560 /* Record the insertion first, so that when we undo,
1561 the deletion will be undone first. Thus, undo
1562 will insert before deleting, and thus will keep
1563 the markers before and after this text separate. */
1564 record_insert (from
+ SCHARS (deletion
), inschars
);
1565 record_delete (from
, deletion
);
1568 GAP_SIZE
-= outgoing_insbytes
;
1572 GPT_BYTE
+= outgoing_insbytes
;
1573 ZV_BYTE
+= outgoing_insbytes
;
1574 Z_BYTE
+= outgoing_insbytes
;
1575 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1580 /* Adjust the overlay center as needed. This must be done after
1581 adjusting the markers that bound the overlays. */
1582 adjust_overlays_for_delete (from
, nchars_del
);
1583 adjust_overlays_for_insert (from
, inschars
);
1585 /* Adjust markers for the deletion and the insertion. */
1587 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1588 inschars
, outgoing_insbytes
);
1590 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1592 /* Get the intervals for the part of the string we are inserting--
1593 not including the combined-before bytes. */
1594 intervals
= STRING_INTERVALS (new);
1595 /* Insert those intervals. */
1596 graft_intervals_into_buffer (intervals
, from
, inschars
,
1597 current_buffer
, inherit
);
1599 /* Relocate point as if it were a marker. */
1601 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1602 (from_byte
+ outgoing_insbytes
1603 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1605 if (outgoing_insbytes
== 0)
1606 evaporate_overlays (from
);
1611 CHARS_MODIFF
= MODIFF
;
1614 signal_after_change (from
, nchars_del
, GPT
- from
);
1615 update_compositions (from
, GPT
, CHECK_BORDER
);
1618 /* Replace the text from character positions FROM to TO with
1619 the text in INS of length INSCHARS.
1620 Keep the text properties that applied to the old characters
1621 (extending them to all the new chars if there are more new chars).
1623 Note that this does not yet handle markers quite right.
1625 If MARKERS is nonzero, relocate markers.
1627 Unlike most functions at this level, never call
1628 prepare_to_modify_buffer and never call signal_after_change. */
1631 replace_range_2 (EMACS_INT from
, EMACS_INT from_byte
,
1632 EMACS_INT to
, EMACS_INT to_byte
,
1633 const char *ins
, EMACS_INT inschars
, EMACS_INT insbytes
,
1636 EMACS_INT nbytes_del
, nchars_del
;
1641 nchars_del
= to
- from
;
1642 nbytes_del
= to_byte
- from_byte
;
1644 if (nbytes_del
<= 0 && insbytes
== 0)
1647 /* Make sure point-max won't overflow after this insertion. */
1648 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1649 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1650 error ("Maximum buffer size exceeded");
1652 /* Make sure the gap is somewhere in or next to what we are deleting. */
1654 gap_right (from
, from_byte
);
1656 gap_left (to
, to_byte
, 0);
1658 GAP_SIZE
+= nbytes_del
;
1661 ZV_BYTE
-= nbytes_del
;
1662 Z_BYTE
-= nbytes_del
;
1664 GPT_BYTE
= from_byte
;
1665 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1670 if (GPT
- BEG
< BEG_UNCHANGED
)
1671 BEG_UNCHANGED
= GPT
- BEG
;
1672 if (Z
- GPT
< END_UNCHANGED
)
1673 END_UNCHANGED
= Z
- GPT
;
1675 if (GAP_SIZE
< insbytes
)
1676 make_gap (insbytes
- GAP_SIZE
);
1678 /* Copy the replacement text into the buffer. */
1679 memcpy (GPT_ADDR
, ins
, insbytes
);
1681 #ifdef BYTE_COMBINING_DEBUG
1682 /* We have copied text into the gap, but we have not marked
1683 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1684 here, for both the previous text and the following text.
1685 Meanwhile, GPT_ADDR does point to
1686 the text that has been stored by copy_text. */
1687 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1688 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1692 GAP_SIZE
-= insbytes
;
1696 GPT_BYTE
+= insbytes
;
1697 ZV_BYTE
+= insbytes
;
1699 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1704 /* Adjust the overlay center as needed. This must be done after
1705 adjusting the markers that bound the overlays. */
1706 if (nchars_del
!= inschars
)
1708 adjust_overlays_for_insert (from
, inschars
);
1709 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1712 /* Adjust markers for the deletion and the insertion. */
1714 && ! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1715 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1716 inschars
, insbytes
);
1718 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1720 /* Relocate point as if it were a marker. */
1721 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1724 /* PT was within the deleted text. Move it to FROM. */
1725 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1727 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1731 evaporate_overlays (from
);
1736 CHARS_MODIFF
= MODIFF
;
1739 /* Delete characters in current buffer
1740 from FROM up to (but not including) TO.
1741 If TO comes before FROM, we delete nothing. */
1744 del_range (EMACS_INT from
, EMACS_INT to
)
1746 del_range_1 (from
, to
, 1, 0);
1749 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1750 RET_STRING says to return the deleted text. */
1753 del_range_1 (EMACS_INT from
, EMACS_INT to
, int prepare
, int ret_string
)
1755 EMACS_INT from_byte
, to_byte
;
1756 Lisp_Object deletion
;
1757 struct gcpro gcpro1
;
1759 /* Make args be valid */
1770 EMACS_INT range_length
= to
- from
;
1771 prepare_to_modify_buffer (from
, to
, &from
);
1772 to
= min (ZV
, from
+ range_length
);
1775 from_byte
= CHAR_TO_BYTE (from
);
1776 to_byte
= CHAR_TO_BYTE (to
);
1778 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1780 signal_after_change (from
, to
- from
, 0);
1781 update_compositions (from
, from
, CHECK_HEAD
);
1786 /* Like del_range_1 but args are byte positions, not char positions. */
1789 del_range_byte (EMACS_INT from_byte
, EMACS_INT to_byte
, int prepare
)
1793 /* Make args be valid */
1794 if (from_byte
< BEGV_BYTE
)
1795 from_byte
= BEGV_BYTE
;
1796 if (to_byte
> ZV_BYTE
)
1799 if (to_byte
<= from_byte
)
1802 from
= BYTE_TO_CHAR (from_byte
);
1803 to
= BYTE_TO_CHAR (to_byte
);
1807 EMACS_INT old_from
= from
, old_to
= Z
- to
;
1808 EMACS_INT range_length
= to
- from
;
1809 prepare_to_modify_buffer (from
, to
, &from
);
1810 to
= from
+ range_length
;
1812 if (old_from
!= from
)
1813 from_byte
= CHAR_TO_BYTE (from
);
1819 else if (old_to
== Z
- to
)
1820 to_byte
= CHAR_TO_BYTE (to
);
1823 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1824 signal_after_change (from
, to
- from
, 0);
1825 update_compositions (from
, from
, CHECK_HEAD
);
1828 /* Like del_range_1, but positions are specified both as charpos
1832 del_range_both (EMACS_INT from
, EMACS_INT from_byte
,
1833 EMACS_INT to
, EMACS_INT to_byte
, int prepare
)
1835 /* Make args be valid */
1836 if (from_byte
< BEGV_BYTE
)
1837 from_byte
= BEGV_BYTE
;
1838 if (to_byte
> ZV_BYTE
)
1841 if (to_byte
<= from_byte
)
1851 EMACS_INT old_from
= from
, old_to
= Z
- to
;
1852 EMACS_INT range_length
= to
- from
;
1853 prepare_to_modify_buffer (from
, to
, &from
);
1854 to
= from
+ range_length
;
1856 if (old_from
!= from
)
1857 from_byte
= CHAR_TO_BYTE (from
);
1863 else if (old_to
== Z
- to
)
1864 to_byte
= CHAR_TO_BYTE (to
);
1867 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1868 signal_after_change (from
, to
- from
, 0);
1869 update_compositions (from
, from
, CHECK_HEAD
);
1872 /* Delete a range of text, specified both as character positions
1873 and byte positions. FROM and TO are character positions,
1874 while FROM_BYTE and TO_BYTE are byte positions.
1875 If RET_STRING is true, the deleted area is returned as a string. */
1878 del_range_2 (EMACS_INT from
, EMACS_INT from_byte
,
1879 EMACS_INT to
, EMACS_INT to_byte
, int ret_string
)
1881 register EMACS_INT nbytes_del
, nchars_del
;
1882 Lisp_Object deletion
;
1886 nchars_del
= to
- from
;
1887 nbytes_del
= to_byte
- from_byte
;
1889 /* Make sure the gap is somewhere in or next to what we are deleting. */
1891 gap_right (from
, from_byte
);
1893 gap_left (to
, to_byte
, 0);
1895 #ifdef BYTE_COMBINING_DEBUG
1896 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1897 Z_BYTE
- to_byte
, from
, from_byte
))
1901 if (ret_string
|| ! EQ (current_buffer
->undo_list
, Qt
))
1902 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1906 /* Relocate all markers pointing into the new, larger gap
1907 to point at the end of the text before the gap.
1908 Do this before recording the deletion,
1909 so that undo handles this after reinserting the text. */
1910 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1912 if (! EQ (current_buffer
->undo_list
, Qt
))
1913 record_delete (from
, deletion
);
1915 CHARS_MODIFF
= MODIFF
;
1917 /* Relocate point as if it were a marker. */
1919 adjust_point (from
- (PT
< to
? PT
: to
),
1920 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1922 offset_intervals (current_buffer
, from
, - nchars_del
);
1924 /* Adjust the overlay center as needed. This must be done after
1925 adjusting the markers that bound the overlays. */
1926 adjust_overlays_for_delete (from
, nchars_del
);
1928 GAP_SIZE
+= nbytes_del
;
1929 ZV_BYTE
-= nbytes_del
;
1930 Z_BYTE
-= nbytes_del
;
1934 GPT_BYTE
= from_byte
;
1935 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1936 /* Put an anchor, unless called from decode_coding_object which
1937 needs to access the previous gap contents. */
1943 if (GPT
- BEG
< BEG_UNCHANGED
)
1944 BEG_UNCHANGED
= GPT
- BEG
;
1945 if (Z
- GPT
< END_UNCHANGED
)
1946 END_UNCHANGED
= Z
- GPT
;
1950 evaporate_overlays (from
);
1955 /* Call this if you're about to change the region of BUFFER from
1956 character positions START to END. This checks the read-only
1957 properties of the region, calls the necessary modification hooks,
1958 and warns the next redisplay that it should pay attention to that
1961 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
1962 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1965 modify_region (struct buffer
*buffer
, EMACS_INT start
, EMACS_INT end
,
1966 int preserve_chars_modiff
)
1968 struct buffer
*old_buffer
= current_buffer
;
1970 if (buffer
!= old_buffer
)
1971 set_buffer_internal (buffer
);
1973 prepare_to_modify_buffer (start
, end
, NULL
);
1975 BUF_COMPUTE_UNCHANGED (buffer
, start
- 1, end
);
1977 if (MODIFF
<= SAVE_MODIFF
)
1978 record_first_change ();
1980 if (! preserve_chars_modiff
)
1981 CHARS_MODIFF
= MODIFF
;
1983 buffer
->point_before_scroll
= Qnil
;
1985 if (buffer
!= old_buffer
)
1986 set_buffer_internal (old_buffer
);
1989 /* Check that it is okay to modify the buffer between START and END,
1990 which are char positions.
1992 Run the before-change-function, if any. If intervals are in use,
1993 verify that the text to be modified is not read-only, and call
1994 any modification properties the text may have.
1996 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1997 by holding its value temporarily in a marker. */
2000 prepare_to_modify_buffer (EMACS_INT start
, EMACS_INT end
,
2001 EMACS_INT
*preserve_ptr
)
2003 struct buffer
*base_buffer
;
2005 if (!NILP (current_buffer
->read_only
))
2006 Fbarf_if_buffer_read_only ();
2008 /* Let redisplay consider other windows than selected_window
2009 if modifying another buffer. */
2010 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
2011 ++windows_or_buffers_changed
;
2013 if (BUF_INTERVALS (current_buffer
) != 0)
2017 Lisp_Object preserve_marker
;
2018 struct gcpro gcpro1
;
2019 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2020 GCPRO1 (preserve_marker
);
2021 verify_interval_modification (current_buffer
, start
, end
);
2022 *preserve_ptr
= marker_position (preserve_marker
);
2023 unchain_marker (XMARKER (preserve_marker
));
2027 verify_interval_modification (current_buffer
, start
, end
);
2030 /* For indirect buffers, use the base buffer to check clashes. */
2031 if (current_buffer
->base_buffer
!= 0)
2032 base_buffer
= current_buffer
->base_buffer
;
2034 base_buffer
= current_buffer
;
2036 #ifdef CLASH_DETECTION
2037 if (!NILP (base_buffer
->file_truename
)
2038 /* Make binding buffer-file-name to nil effective. */
2039 && !NILP (base_buffer
->filename
)
2040 && SAVE_MODIFF
>= MODIFF
)
2041 lock_file (base_buffer
->file_truename
);
2043 /* At least warn if this file has changed on disk since it was visited. */
2044 if (!NILP (base_buffer
->filename
)
2045 && SAVE_MODIFF
>= MODIFF
2046 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2047 && !NILP (Ffile_exists_p (base_buffer
->filename
)))
2048 call1 (intern ("ask-user-about-supersession-threat"),
2049 base_buffer
->filename
);
2050 #endif /* not CLASH_DETECTION */
2052 /* If `select-active-regions' is non-nil, save the region text. */
2053 if (!NILP (current_buffer
->mark_active
)
2054 && !inhibit_modification_hooks
2055 && XMARKER (current_buffer
->mark
)->buffer
2056 && NILP (Vsaved_region_selection
)
2057 && (EQ (Vselect_active_regions
, Qonly
)
2058 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
2059 : (!NILP (Vselect_active_regions
)
2060 && !NILP (Vtransient_mark_mode
))))
2062 EMACS_INT b
= XMARKER (current_buffer
->mark
)->charpos
;
2065 Vsaved_region_selection
= make_buffer_string (b
, e
, 0);
2067 Vsaved_region_selection
= make_buffer_string (e
, b
, 0);
2070 signal_before_change (start
, end
, preserve_ptr
);
2072 if (current_buffer
->newline_cache
)
2073 invalidate_region_cache (current_buffer
,
2074 current_buffer
->newline_cache
,
2075 start
- BEG
, Z
- end
);
2076 if (current_buffer
->width_run_cache
)
2077 invalidate_region_cache (current_buffer
,
2078 current_buffer
->width_run_cache
,
2079 start
- BEG
, Z
- end
);
2081 Vdeactivate_mark
= Qt
;
2084 /* These macros work with an argument named `preserve_ptr'
2085 and a local variable named `preserve_marker'. */
2087 #define PRESERVE_VALUE \
2088 if (preserve_ptr && NILP (preserve_marker)) \
2089 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2091 #define RESTORE_VALUE \
2092 if (! NILP (preserve_marker)) \
2094 *preserve_ptr = marker_position (preserve_marker); \
2095 unchain_marker (XMARKER (preserve_marker)); \
2098 #define PRESERVE_START_END \
2099 if (NILP (start_marker)) \
2100 start_marker = Fcopy_marker (start, Qnil); \
2101 if (NILP (end_marker)) \
2102 end_marker = Fcopy_marker (end, Qnil);
2104 #define FETCH_START \
2105 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2108 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2110 /* Set a variable to nil if an error occurred.
2111 Don't change the variable if there was no error.
2112 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2113 VARIABLE is the variable to maybe set to nil.
2114 NO-ERROR-FLAG is nil if there was an error,
2115 anything else meaning no error (so this function does nothing). */
2117 reset_var_on_error (Lisp_Object val
)
2119 if (NILP (XCDR (val
)))
2120 Fset (XCAR (val
), Qnil
);
2124 /* Signal a change to the buffer immediately before it happens.
2125 START_INT and END_INT are the bounds of the text to be changed.
2127 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2128 by holding its value temporarily in a marker. */
2131 signal_before_change (EMACS_INT start_int
, EMACS_INT end_int
,
2132 EMACS_INT
*preserve_ptr
)
2134 Lisp_Object start
, end
;
2135 Lisp_Object start_marker
, end_marker
;
2136 Lisp_Object preserve_marker
;
2137 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2138 int count
= SPECPDL_INDEX ();
2140 if (inhibit_modification_hooks
)
2143 start
= make_number (start_int
);
2144 end
= make_number (end_int
);
2145 preserve_marker
= Qnil
;
2146 start_marker
= Qnil
;
2148 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2150 specbind (Qinhibit_modification_hooks
, Qt
);
2152 /* If buffer is unmodified, run a special hook for that case. */
2153 if (SAVE_MODIFF
>= MODIFF
2154 && !NILP (Vfirst_change_hook
)
2155 && !NILP (Vrun_hooks
))
2159 call1 (Vrun_hooks
, Qfirst_change_hook
);
2162 /* Now run the before-change-functions if any. */
2163 if (!NILP (Vbefore_change_functions
))
2165 Lisp_Object args
[3];
2166 Lisp_Object rvoe_arg
= Fcons (Qbefore_change_functions
, Qnil
);
2171 /* Mark before-change-functions to be reset to nil in case of error. */
2172 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2174 /* Actually run the hook functions. */
2175 args
[0] = Qbefore_change_functions
;
2176 args
[1] = FETCH_START
;
2177 args
[2] = FETCH_END
;
2178 Frun_hook_with_args (3, args
);
2180 /* There was no error: unarm the reset_on_error. */
2181 XSETCDR (rvoe_arg
, Qt
);
2184 if (current_buffer
->overlays_before
|| current_buffer
->overlays_after
)
2187 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2188 FETCH_START
, FETCH_END
, Qnil
);
2191 if (! NILP (start_marker
))
2192 free_marker (start_marker
);
2193 if (! NILP (end_marker
))
2194 free_marker (end_marker
);
2198 unbind_to (count
, Qnil
);
2201 /* Signal a change immediately after it happens.
2202 CHARPOS is the character position of the start of the changed text.
2203 LENDEL is the number of characters of the text before the change.
2204 (Not the whole buffer; just the part that was changed.)
2205 LENINS is the number of characters in that part of the text
2206 after the change. */
2209 signal_after_change (EMACS_INT charpos
, EMACS_INT lendel
, EMACS_INT lenins
)
2211 int count
= SPECPDL_INDEX ();
2212 if (inhibit_modification_hooks
)
2215 /* If we are deferring calls to the after-change functions
2216 and there are no before-change functions,
2217 just record the args that we were going to use. */
2218 if (! NILP (Vcombine_after_change_calls
)
2219 && NILP (Vbefore_change_functions
)
2220 && !current_buffer
->overlays_before
2221 && !current_buffer
->overlays_after
)
2225 if (!NILP (combine_after_change_list
)
2226 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2227 Fcombine_after_change_execute ();
2229 elt
= Fcons (make_number (charpos
- BEG
),
2230 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2231 Fcons (make_number (lenins
- lendel
), Qnil
)));
2232 combine_after_change_list
2233 = Fcons (elt
, combine_after_change_list
);
2234 combine_after_change_buffer
= Fcurrent_buffer ();
2239 if (!NILP (combine_after_change_list
))
2240 Fcombine_after_change_execute ();
2242 specbind (Qinhibit_modification_hooks
, Qt
);
2244 if (!NILP (Vafter_change_functions
))
2246 Lisp_Object args
[4];
2247 Lisp_Object rvoe_arg
= Fcons (Qafter_change_functions
, Qnil
);
2249 /* Mark after-change-functions to be reset to nil in case of error. */
2250 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2252 /* Actually run the hook functions. */
2253 args
[0] = Qafter_change_functions
;
2254 XSETFASTINT (args
[1], charpos
);
2255 XSETFASTINT (args
[2], charpos
+ lenins
);
2256 XSETFASTINT (args
[3], lendel
);
2257 Frun_hook_with_args (4, args
);
2259 /* There was no error: unarm the reset_on_error. */
2260 XSETCDR (rvoe_arg
, Qt
);
2263 if (current_buffer
->overlays_before
|| current_buffer
->overlays_after
)
2264 report_overlay_modification (make_number (charpos
),
2265 make_number (charpos
+ lenins
),
2267 make_number (charpos
),
2268 make_number (charpos
+ lenins
),
2269 make_number (lendel
));
2271 /* After an insertion, call the text properties
2272 insert-behind-hooks or insert-in-front-hooks. */
2274 report_interval_modification (make_number (charpos
),
2275 make_number (charpos
+ lenins
));
2277 unbind_to (count
, Qnil
);
2281 Fcombine_after_change_execute_1 (Lisp_Object val
)
2283 Vcombine_after_change_calls
= val
;
2287 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2288 Scombine_after_change_execute
, 0, 0, 0,
2289 doc
: /* This function is for use internally in `combine-after-change-calls'. */)
2292 int count
= SPECPDL_INDEX ();
2293 EMACS_INT beg
, end
, change
;
2294 EMACS_INT begpos
, endpos
;
2297 if (NILP (combine_after_change_list
))
2300 /* It is rare for combine_after_change_buffer to be invalid, but
2301 possible. It can happen when combine-after-change-calls is
2302 non-nil, and insertion calls a file handler (e.g. through
2303 lock_file) which scribbles into a temp file -- cyd */
2304 if (!BUFFERP (combine_after_change_buffer
)
2305 || NILP (XBUFFER (combine_after_change_buffer
)->name
))
2307 combine_after_change_list
= Qnil
;
2311 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2313 Fset_buffer (combine_after_change_buffer
);
2315 /* # chars unchanged at beginning of buffer. */
2317 /* # chars unchanged at end of buffer. */
2319 /* Total amount of insertion (negative for deletion). */
2322 /* Scan the various individual changes,
2323 accumulating the range info in BEG, END and CHANGE. */
2324 for (tail
= combine_after_change_list
; CONSP (tail
);
2328 EMACS_INT thisbeg
, thisend
, thischange
;
2330 /* Extract the info from the next element. */
2334 thisbeg
= XINT (XCAR (elt
));
2339 thisend
= XINT (XCAR (elt
));
2344 thischange
= XINT (XCAR (elt
));
2346 /* Merge this range into the accumulated range. */
2347 change
+= thischange
;
2354 /* Get the current start and end positions of the range
2355 that was changed. */
2359 /* We are about to handle these, so discard them. */
2360 combine_after_change_list
= Qnil
;
2362 /* Now run the after-change functions for real.
2363 Turn off the flag that defers them. */
2364 record_unwind_protect (Fcombine_after_change_execute_1
,
2365 Vcombine_after_change_calls
);
2366 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2367 update_compositions (begpos
, endpos
, CHECK_ALL
);
2369 return unbind_to (count
, Qnil
);
2373 syms_of_insdel (void)
2375 staticpro (&combine_after_change_list
);
2376 staticpro (&combine_after_change_buffer
);
2377 combine_after_change_list
= Qnil
;
2378 combine_after_change_buffer
= Qnil
;
2380 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2381 doc
: /* Non-nil means enable debugging checks for invalid marker positions. */);
2382 check_markers_debug_flag
= 0;
2383 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2384 doc
: /* Used internally by the `combine-after-change-calls' macro. */);
2385 Vcombine_after_change_calls
= Qnil
;
2387 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks
,
2388 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2389 This affects `before-change-functions' and `after-change-functions',
2390 as well as hooks attached to text properties and overlays. */);
2391 inhibit_modification_hooks
= 0;
2392 Qinhibit_modification_hooks
= intern_c_string ("inhibit-modification-hooks");
2393 staticpro (&Qinhibit_modification_hooks
);
2395 defsubr (&Scombine_after_change_execute
);
2398 /* arch-tag: 9b34b886-47d7-465e-a234-299af411b23d
2399 (do not change this comment) */