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
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/>. */
24 #include "intervals.h"
26 #include "character.h"
28 #include "blockinput.h"
29 #include "region-cache.h"
35 static void insert_from_string_1 (Lisp_Object string
,
36 EMACS_INT pos
, EMACS_INT pos_byte
,
37 EMACS_INT nchars
, EMACS_INT nbytes
,
38 int inherit
, int before_markers
);
39 static void insert_from_buffer_1 (struct buffer
*buf
,
40 EMACS_INT from
, EMACS_INT nchars
,
42 static void gap_left (EMACS_INT charpos
, EMACS_INT bytepos
, int newgap
);
43 static void gap_right (EMACS_INT charpos
, EMACS_INT bytepos
);
44 static void adjust_markers_gap_motion (EMACS_INT from
, EMACS_INT to
,
46 static void adjust_markers_for_insert (EMACS_INT from
, EMACS_INT from_byte
,
47 EMACS_INT to
, EMACS_INT to_byte
,
49 static void adjust_markers_for_replace (EMACS_INT
, EMACS_INT
, EMACS_INT
,
50 EMACS_INT
, EMACS_INT
, EMACS_INT
);
51 static void adjust_point (EMACS_INT nchars
, EMACS_INT nbytes
);
53 Lisp_Object
Fcombine_after_change_execute ();
55 /* Non-nil means don't call the after-change-functions right away,
56 just record an element in Vcombine_after_change_calls_list. */
57 Lisp_Object Vcombine_after_change_calls
;
59 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
60 describing changes which happened while combine_after_change_calls
61 was nonzero. We use this to decide how to call them
62 once the deferral ends.
65 BEG-UNCHANGED is the number of chars before the changed range.
66 END-UNCHANGED is the number of chars after the changed range,
67 and CHANGE-AMOUNT is the number of characters inserted by the change
68 (negative for a deletion). */
69 Lisp_Object combine_after_change_list
;
71 /* Buffer which combine_after_change_list is about. */
72 Lisp_Object combine_after_change_buffer
;
74 Lisp_Object Qinhibit_modification_hooks
;
77 /* Check all markers in the current buffer, looking for something invalid. */
79 static int check_markers_debug_flag
;
81 #define CHECK_MARKERS() \
82 if (check_markers_debug_flag) \
89 register struct Lisp_Marker
*tail
;
90 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
92 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
94 if (tail
->buffer
->text
!= current_buffer
->text
)
96 if (tail
->charpos
> Z
)
98 if (tail
->bytepos
> Z_BYTE
)
100 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (tail
->bytepos
)))
105 /* Move gap to position CHARPOS.
106 Note that this can quit! */
109 move_gap (EMACS_INT charpos
)
111 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
114 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
115 Note that this can quit! */
118 move_gap_both (EMACS_INT charpos
, EMACS_INT bytepos
)
120 if (bytepos
< GPT_BYTE
)
121 gap_left (charpos
, bytepos
, 0);
122 else if (bytepos
> GPT_BYTE
)
123 gap_right (charpos
, bytepos
);
126 /* Move the gap to a position less than the current GPT.
127 BYTEPOS describes the new position as a byte position,
128 and CHARPOS is the corresponding char position.
129 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
132 gap_left (EMACS_INT charpos
, EMACS_INT bytepos
, int newgap
)
134 register unsigned char *to
, *from
;
135 register EMACS_INT i
;
139 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
146 /* Now copy the characters. To move the gap down,
147 copy characters up. */
151 /* I gets number of characters left to copy. */
152 i
= new_s1
- bytepos
;
155 /* If a quit is requested, stop copying now.
156 Change BYTEPOS to be where we have actually moved the gap to. */
160 charpos
= BYTE_TO_CHAR (bytepos
);
163 /* Move at most 32000 chars before checking again for a quit. */
168 /* bcopy is safe if the two areas of memory do not overlap
169 or on systems where bcopy is always safe for moving upward. */
170 && (BCOPY_UPWARD_SAFE
171 || to
- from
>= 128))
173 /* If overlap is not safe, avoid it by not moving too many
174 characters at once. */
175 if (!BCOPY_UPWARD_SAFE
&& i
> to
- from
)
190 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
191 BYTEPOS is where the loop above stopped, which may be what was specified
192 or may be where a quit was detected. */
193 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
196 if (bytepos
< charpos
)
198 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
202 /* Move the gap to a position greater than than the current GPT.
203 BYTEPOS describes the new position as a byte position,
204 and CHARPOS is the corresponding char position. */
207 gap_right (EMACS_INT charpos
, EMACS_INT bytepos
)
209 register unsigned char *to
, *from
;
210 register EMACS_INT i
;
213 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
220 /* Now copy the characters. To move the gap up,
221 copy characters down. */
225 /* I gets number of characters left to copy. */
226 i
= bytepos
- new_s1
;
229 /* If a quit is requested, stop copying now.
230 Change BYTEPOS to be where we have actually moved the gap to. */
234 charpos
= BYTE_TO_CHAR (bytepos
);
237 /* Move at most 32000 chars before checking again for a quit. */
242 /* bcopy is safe if the two areas of memory do not overlap
243 or on systems where bcopy is always safe for moving downward. */
244 && (BCOPY_DOWNWARD_SAFE
245 || from
- to
>= 128))
247 /* If overlap is not safe, avoid it by not moving too many
248 characters at once. */
249 if (!BCOPY_DOWNWARD_SAFE
&& i
> from
- to
)
264 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
268 if (bytepos
< charpos
)
270 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
274 /* Add AMOUNT to the byte position of every marker in the current buffer
275 whose current byte position is between FROM (exclusive) and TO (inclusive).
277 Also, any markers past the outside of that interval, in the direction
278 of adjustment, are first moved back to the near end of the interval
279 and then adjusted by AMOUNT.
281 When the latter adjustment is done, if AMOUNT is negative,
282 we record the adjustment for undo. (This case happens only for
285 The markers' character positions are not altered,
286 because gap motion does not affect character positions. */
288 int adjust_markers_test
;
291 adjust_markers_gap_motion (EMACS_INT from
, EMACS_INT to
, EMACS_INT amount
)
293 /* Now that a marker has a bytepos, not counting the gap,
294 nothing needs to be done here. */
297 register struct Lisp_Marker
*m
;
298 register EMACS_INT mpos
;
300 marker
= BUF_MARKERS (current_buffer
);
302 while (!NILP (marker
))
304 m
= XMARKER (marker
);
308 if (mpos
> to
&& mpos
< to
+ amount
)
310 if (adjust_markers_test
)
317 /* Here's the case where a marker is inside text being deleted.
318 AMOUNT can be negative for gap motion, too,
319 but then this range contains no markers. */
320 if (mpos
> from
+ amount
&& mpos
<= from
)
322 if (adjust_markers_test
)
324 mpos
= from
+ amount
;
327 if (mpos
> from
&& mpos
<= to
)
335 /* Adjust all markers for a deletion
336 whose range in bytes is FROM_BYTE to TO_BYTE.
337 The range in charpos is FROM to TO.
339 This function assumes that the gap is adjacent to
340 or inside of the range being deleted. */
343 adjust_markers_for_delete (EMACS_INT from
, EMACS_INT from_byte
,
344 EMACS_INT to
, EMACS_INT to_byte
)
347 register struct Lisp_Marker
*m
;
348 register EMACS_INT charpos
;
350 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
352 charpos
= m
->charpos
;
357 /* If the marker is after the deletion,
358 relocate by number of chars / bytes deleted. */
361 m
->charpos
-= to
- from
;
362 m
->bytepos
-= to_byte
- from_byte
;
364 /* Here's the case where a marker is inside text being deleted. */
365 else if (charpos
> from
)
367 if (! m
->insertion_type
)
368 { /* Normal markers will end up at the beginning of the
369 re-inserted text after undoing a deletion, and must be
370 adjusted to move them to the correct place. */
371 XSETMISC (marker
, m
);
372 record_marker_adjustment (marker
, from
- charpos
);
374 else if (charpos
< to
)
375 { /* Before-insertion markers will automatically move forward
376 upon re-inserting the deleted text, so we have to arrange
377 for them to move backward to the correct position. */
378 XSETMISC (marker
, m
);
379 record_marker_adjustment (marker
, charpos
- to
);
382 m
->bytepos
= from_byte
;
384 /* Here's the case where a before-insertion marker is immediately
385 before the deleted region. */
386 else if (charpos
== from
&& m
->insertion_type
)
388 /* Undoing the change uses normal insertion, which will
389 incorrectly make MARKER move forward, so we arrange for it
390 to then move backward to the correct place at the beginning
391 of the deleted region. */
392 XSETMISC (marker
, m
);
393 record_marker_adjustment (marker
, to
- from
);
399 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
400 to TO / TO_BYTE. We have to relocate the charpos of every marker
401 that points after the insertion (but not their bytepos).
403 When a marker points at the insertion point,
404 we advance it if either its insertion-type is t
405 or BEFORE_MARKERS is true. */
408 adjust_markers_for_insert (EMACS_INT from
, EMACS_INT from_byte
,
409 EMACS_INT to
, EMACS_INT to_byte
, int before_markers
)
411 struct Lisp_Marker
*m
;
413 EMACS_INT nchars
= to
- from
;
414 EMACS_INT nbytes
= to_byte
- from_byte
;
416 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
418 eassert (m
->bytepos
>= m
->charpos
419 && m
->bytepos
- m
->charpos
<= Z_BYTE
- Z
);
421 if (m
->bytepos
== from_byte
)
423 if (m
->insertion_type
|| before_markers
)
425 m
->bytepos
= to_byte
;
427 if (m
->insertion_type
)
431 else if (m
->bytepos
> from_byte
)
433 m
->bytepos
+= nbytes
;
434 m
->charpos
+= nchars
;
438 /* Adjusting only markers whose insertion-type is t may result in
439 - disordered start and end in overlays, and
440 - disordered overlays in the slot `overlays_before' of current_buffer. */
443 fix_start_end_in_overlays(from
, to
);
444 fix_overlays_before (current_buffer
, from
, to
);
448 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
450 This is used only when the value of point changes due to an insert
451 or delete; it does not represent a conceptual change in point as a
452 marker. In particular, point is not crossing any interval
453 boundaries, so there's no need to use the usual SET_PT macro. In
454 fact it would be incorrect to do so, because either the old or the
455 new value of point is out of sync with the current set of
459 adjust_point (EMACS_INT nchars
, EMACS_INT nbytes
)
461 BUF_PT (current_buffer
) += nchars
;
462 BUF_PT_BYTE (current_buffer
) += nbytes
;
464 /* In a single-byte buffer, the two positions must be equal. */
465 eassert (PT_BYTE
>= PT
&& PT_BYTE
- PT
<= ZV_BYTE
- ZV
);
468 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
469 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
470 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
474 adjust_markers_for_replace (EMACS_INT from
, EMACS_INT from_byte
,
475 EMACS_INT old_chars
, EMACS_INT old_bytes
,
476 EMACS_INT new_chars
, EMACS_INT new_bytes
)
478 register struct Lisp_Marker
*m
;
479 EMACS_INT prev_to_byte
= from_byte
+ old_bytes
;
480 EMACS_INT diff_chars
= new_chars
- old_chars
;
481 EMACS_INT diff_bytes
= new_bytes
- old_bytes
;
483 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
485 if (m
->bytepos
>= prev_to_byte
)
487 m
->charpos
+= diff_chars
;
488 m
->bytepos
+= diff_bytes
;
490 else if (m
->bytepos
> from_byte
)
493 m
->bytepos
= from_byte
;
501 /* Make the gap NBYTES_ADDED bytes longer. */
504 make_gap_larger (EMACS_INT nbytes_added
)
507 EMACS_INT real_gap_loc
;
508 EMACS_INT real_gap_loc_byte
;
509 EMACS_INT old_gap_size
;
511 /* If we have to get more space, get enough to last a while. */
512 nbytes_added
+= 2000;
514 /* Don't allow a buffer size that won't fit in an int
515 even if it will fit in a Lisp integer.
516 That won't work because so many places use `int'.
518 Make sure we don't introduce overflows in the calculation. */
520 if (Z_BYTE
- BEG_BYTE
+ GAP_SIZE
521 >= (((EMACS_INT
) 1 << (min (VALBITS
, BITS_PER_INT
) - 1)) - 1
523 error ("Buffer exceeds maximum size");
525 enlarge_buffer_text (current_buffer
, nbytes_added
);
527 /* Prevent quitting in move_gap. */
532 real_gap_loc_byte
= GPT_BYTE
;
533 old_gap_size
= GAP_SIZE
;
535 /* Call the newly allocated space a gap at the end of the whole space. */
537 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
538 GAP_SIZE
= nbytes_added
;
540 /* Move the new gap down to be consecutive with the end of the old one.
541 This adjusts the markers properly too. */
542 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
544 /* Now combine the two into one large gap. */
545 GAP_SIZE
+= old_gap_size
;
547 GPT_BYTE
= real_gap_loc_byte
;
556 /* Make the gap NBYTES_REMOVED bytes shorter. */
559 make_gap_smaller (EMACS_INT nbytes_removed
)
562 EMACS_INT real_gap_loc
;
563 EMACS_INT real_gap_loc_byte
;
565 EMACS_INT real_Z_byte
;
566 EMACS_INT real_beg_unchanged
;
567 EMACS_INT new_gap_size
;
569 /* Make sure the gap is at least 20 bytes. */
570 if (GAP_SIZE
- nbytes_removed
< 20)
571 nbytes_removed
= GAP_SIZE
- 20;
573 /* Prevent quitting in move_gap. */
578 real_gap_loc_byte
= GPT_BYTE
;
579 new_gap_size
= GAP_SIZE
- nbytes_removed
;
581 real_Z_byte
= Z_BYTE
;
582 real_beg_unchanged
= BEG_UNCHANGED
;
584 /* Pretend that the last unwanted part of the gap is the entire gap,
585 and that the first desired part of the gap is part of the buffer
587 bzero (GPT_ADDR
, new_gap_size
);
589 GPT_BYTE
+= new_gap_size
;
591 Z_BYTE
+= new_gap_size
;
592 GAP_SIZE
= nbytes_removed
;
594 /* Move the unwanted pretend gap to the end of the buffer. This
595 adjusts the markers properly too. */
596 gap_right (Z
, Z_BYTE
);
598 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
600 /* Now restore the desired gap. */
601 GAP_SIZE
= new_gap_size
;
603 GPT_BYTE
= real_gap_loc_byte
;
605 Z_BYTE
= real_Z_byte
;
606 BEG_UNCHANGED
= real_beg_unchanged
;
615 make_gap (EMACS_INT nbytes_added
)
617 if (nbytes_added
>= 0)
618 make_gap_larger (nbytes_added
);
619 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
621 make_gap_smaller (-nbytes_added
);
625 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
626 FROM_MULTIBYTE says whether the incoming text is multibyte.
627 TO_MULTIBYTE says whether to store the text as multibyte.
628 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
630 Return the number of bytes stored at TO_ADDR. */
633 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
634 EMACS_INT nbytes
, int from_multibyte
, int to_multibyte
)
636 if (from_multibyte
== to_multibyte
)
638 bcopy (from_addr
, to_addr
, nbytes
);
641 else if (from_multibyte
)
643 EMACS_INT nchars
= 0;
644 EMACS_INT bytes_left
= nbytes
;
645 Lisp_Object tbl
= Qnil
;
647 while (bytes_left
> 0)
650 c
= STRING_CHAR_AND_LENGTH (from_addr
, bytes_left
, thislen
);
651 if (! ASCII_CHAR_P (c
))
654 from_addr
+= thislen
;
655 bytes_left
-= thislen
;
662 unsigned char *initial_to_addr
= to_addr
;
664 /* Convert single-byte to multibyte. */
667 int c
= *from_addr
++;
671 c
= unibyte_char_to_multibyte (c
);
672 to_addr
+= CHAR_STRING (c
, to_addr
);
676 /* Special case for speed. */
677 *to_addr
++ = c
, nbytes
--;
679 return to_addr
- initial_to_addr
;
683 /* Return the number of bytes it would take
684 to convert some single-byte text to multibyte.
685 The single-byte text consists of NBYTES bytes at PTR. */
688 count_size_as_multibyte (const unsigned char *ptr
, EMACS_INT nbytes
)
691 EMACS_INT outgoing_nbytes
= 0;
693 for (i
= 0; i
< nbytes
; i
++)
695 unsigned int c
= *ptr
++;
701 c
= unibyte_char_to_multibyte (c
);
702 outgoing_nbytes
+= CHAR_BYTES (c
);
706 return outgoing_nbytes
;
709 /* Insert a string of specified length before point.
710 This function judges multibyteness based on
711 enable_multibyte_characters in the current buffer;
712 it never converts between single-byte and multibyte.
714 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
715 prepare_to_modify_buffer could relocate the text. */
718 insert (const unsigned char *string
, EMACS_INT nbytes
)
722 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
723 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
725 signal_after_change (opoint
, 0, len
);
726 update_compositions (opoint
, PT
, CHECK_BORDER
);
730 /* Likewise, but inherit text properties from neighboring characters. */
733 insert_and_inherit (const unsigned char *string
, EMACS_INT nbytes
)
737 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
738 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
740 signal_after_change (opoint
, 0, len
);
741 update_compositions (opoint
, PT
, CHECK_BORDER
);
745 /* Insert the character C before point. Do not inherit text properties. */
750 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
753 if (! NILP (current_buffer
->enable_multibyte_characters
))
754 len
= CHAR_STRING (c
, str
);
764 /* Insert the null-terminated string S before point. */
767 insert_string (const char *s
)
769 insert (s
, strlen (s
));
772 /* Like `insert' except that all markers pointing at the place where
773 the insertion happens are adjusted to point after it.
774 Don't use this function to insert part of a Lisp string,
775 since gc could happen and relocate it. */
778 insert_before_markers (const unsigned char *string
, EMACS_INT nbytes
)
782 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
783 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
785 signal_after_change (opoint
, 0, len
);
786 update_compositions (opoint
, PT
, CHECK_BORDER
);
790 /* Likewise, but inherit text properties from neighboring characters. */
793 insert_before_markers_and_inherit (const unsigned char *string
,
798 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
799 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
801 signal_after_change (opoint
, 0, len
);
802 update_compositions (opoint
, PT
, CHECK_BORDER
);
806 /* Subroutine used by the insert functions above. */
809 insert_1 (const unsigned char *string
, EMACS_INT nbytes
,
810 int inherit
, int prepare
, int before_markers
)
812 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
813 inherit
, prepare
, before_markers
);
817 #ifdef BYTE_COMBINING_DEBUG
819 /* See if the bytes before POS/POS_BYTE combine with bytes
820 at the start of STRING to form a single character.
821 If so, return the number of bytes at the start of STRING
822 which combine in this way. Otherwise, return 0. */
825 count_combining_before (const unsigned char *string
, EMACS_INT length
,
826 EMACS_INT pos
, EMACS_INT pos_byte
)
828 int len
, combining_bytes
;
829 const unsigned char *p
;
831 if (NILP (current_buffer
->enable_multibyte_characters
))
834 /* At first, we can exclude the following cases:
835 (1) STRING[0] can't be a following byte of multibyte sequence.
836 (2) POS is the start of the current buffer.
837 (3) A character before POS is not a multibyte character. */
838 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
840 if (pos_byte
== BEG_BYTE
) /* case (2) */
843 p
= BYTE_POS_ADDR (pos_byte
- 1);
844 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
845 if (! BASE_LEADING_CODE_P (*p
)) /* case (3) */
848 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
849 if (combining_bytes
<= 0)
850 /* The character preceding POS is, complete and no room for
851 combining bytes (combining_bytes == 0), or an independent 8-bit
852 character (combining_bytes < 0). */
855 /* We have a combination situation. Count the bytes at STRING that
858 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
861 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
864 /* See if the bytes after POS/POS_BYTE combine with bytes
865 at the end of STRING to form a single character.
866 If so, return the number of bytes after POS/POS_BYTE
867 which combine in this way. Otherwise, return 0. */
870 count_combining_after (const unsigned char *string
,
871 EMACS_INT length
, EMACS_INT pos
, EMACS_INT pos_byte
)
873 EMACS_INT opos_byte
= pos_byte
;
878 if (NILP (current_buffer
->enable_multibyte_characters
))
881 /* At first, we can exclude the following cases:
882 (1) The last byte of STRING is an ASCII.
883 (2) POS is the last of the current buffer.
884 (3) A character at POS can't be a following byte of multibyte
886 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1])) /* case (1) */
888 if (pos_byte
== Z_BYTE
) /* case (2) */
890 bufp
= BYTE_POS_ADDR (pos_byte
);
891 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
895 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
901 /* All characters in STRING are not character head. We must
902 check also preceding bytes at POS. We are sure that the gap
904 unsigned char *p
= BEG_ADDR
;
906 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
908 if (i
< 0 || !BASE_LEADING_CODE_P (p
[i
]))
911 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
912 return (bytes
<= pos_byte
- 1 - i
+ length
914 : bytes
- (pos_byte
- 1 - i
+ length
));
916 if (!BASE_LEADING_CODE_P (string
[i
]))
919 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
921 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
923 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
929 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
930 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
931 are the same as in insert_1. */
934 insert_1_both (const unsigned char *string
,
935 EMACS_INT nchars
, EMACS_INT nbytes
,
936 int inherit
, int prepare
, int before_markers
)
941 if (NILP (current_buffer
->enable_multibyte_characters
))
945 /* Do this before moving and increasing the gap,
946 because the before-change hooks might move the gap
947 or make it smaller. */
948 prepare_to_modify_buffer (PT
, PT
, NULL
);
951 move_gap_both (PT
, PT_BYTE
);
952 if (GAP_SIZE
< nbytes
)
953 make_gap (nbytes
- GAP_SIZE
);
955 #ifdef BYTE_COMBINING_DEBUG
956 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
957 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
961 /* Record deletion of the surrounding text that combines with
962 the insertion. This, together with recording the insertion,
963 will add up to the right stuff in the undo list. */
964 record_insert (PT
, nchars
);
966 CHARS_MODIFF
= MODIFF
;
968 bcopy (string
, GPT_ADDR
, nbytes
);
977 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
982 /* The insert may have been in the unchanged region, so check again. */
983 if (Z
- GPT
< END_UNCHANGED
)
984 END_UNCHANGED
= Z
- GPT
;
986 adjust_overlays_for_insert (PT
, nchars
);
987 adjust_markers_for_insert (PT
, PT_BYTE
,
988 PT
+ nchars
, PT_BYTE
+ nbytes
,
991 if (BUF_INTERVALS (current_buffer
) != 0)
992 offset_intervals (current_buffer
, PT
, nchars
);
994 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
995 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
998 adjust_point (nchars
, nbytes
);
1003 /* Insert the part of the text of STRING, a Lisp object assumed to be
1004 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
1005 starting at position POS / POS_BYTE. If the text of STRING has properties,
1006 copy them into the buffer.
1008 It does not work to use `insert' for this, because a GC could happen
1009 before we bcopy the stuff into the buffer, and relocate the string
1010 without insert noticing. */
1013 insert_from_string (Lisp_Object string
, EMACS_INT pos
, EMACS_INT pos_byte
,
1014 EMACS_INT length
, EMACS_INT length_byte
, int inherit
)
1016 EMACS_INT opoint
= PT
;
1018 if (SCHARS (string
) == 0)
1021 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1023 signal_after_change (opoint
, 0, PT
- opoint
);
1024 update_compositions (opoint
, PT
, CHECK_BORDER
);
1027 /* Like `insert_from_string' except that all markers pointing
1028 at the place where the insertion happens are adjusted to point after it. */
1031 insert_from_string_before_markers (Lisp_Object string
,
1032 EMACS_INT pos
, EMACS_INT pos_byte
,
1033 EMACS_INT length
, EMACS_INT length_byte
,
1036 EMACS_INT opoint
= PT
;
1038 if (SCHARS (string
) == 0)
1041 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
1043 signal_after_change (opoint
, 0, PT
- opoint
);
1044 update_compositions (opoint
, PT
, CHECK_BORDER
);
1047 /* Subroutine of the insertion functions above. */
1050 insert_from_string_1 (Lisp_Object string
, EMACS_INT pos
, EMACS_INT pos_byte
,
1051 EMACS_INT nchars
, EMACS_INT nbytes
,
1052 int inherit
, int before_markers
)
1054 struct gcpro gcpro1
;
1055 EMACS_INT outgoing_nbytes
= nbytes
;
1058 /* Make OUTGOING_NBYTES describe the text
1059 as it will be inserted in this buffer. */
1061 if (NILP (current_buffer
->enable_multibyte_characters
))
1062 outgoing_nbytes
= nchars
;
1063 else if (! STRING_MULTIBYTE (string
))
1065 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
1069 /* Do this before moving and increasing the gap,
1070 because the before-change hooks might move the gap
1071 or make it smaller. */
1072 prepare_to_modify_buffer (PT
, PT
, NULL
);
1075 move_gap_both (PT
, PT_BYTE
);
1076 if (GAP_SIZE
< outgoing_nbytes
)
1077 make_gap (outgoing_nbytes
- GAP_SIZE
);
1080 /* Copy the string text into the buffer, perhaps converting
1081 between single-byte and multibyte. */
1082 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
1083 STRING_MULTIBYTE (string
),
1084 ! NILP (current_buffer
->enable_multibyte_characters
));
1086 #ifdef BYTE_COMBINING_DEBUG
1087 /* We have copied text into the gap, but we have not altered
1088 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1089 to these functions and get the same results as we would
1090 have got earlier on. Meanwhile, PT_ADDR does point to
1091 the text that has been stored by copy_text. */
1092 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1093 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1097 record_insert (PT
, nchars
);
1099 CHARS_MODIFF
= MODIFF
;
1101 GAP_SIZE
-= outgoing_nbytes
;
1105 GPT_BYTE
+= outgoing_nbytes
;
1106 ZV_BYTE
+= outgoing_nbytes
;
1107 Z_BYTE
+= outgoing_nbytes
;
1108 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1113 /* The insert may have been in the unchanged region, so check again. */
1114 if (Z
- GPT
< END_UNCHANGED
)
1115 END_UNCHANGED
= Z
- GPT
;
1117 adjust_overlays_for_insert (PT
, nchars
);
1118 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1119 PT_BYTE
+ outgoing_nbytes
,
1122 offset_intervals (current_buffer
, PT
, nchars
);
1124 intervals
= STRING_INTERVALS (string
);
1125 /* Get the intervals for the part of the string we are inserting. */
1126 if (nbytes
< SBYTES (string
))
1127 intervals
= copy_intervals (intervals
, pos
, nchars
);
1129 /* Insert those intervals. */
1130 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1131 current_buffer
, inherit
);
1133 adjust_point (nchars
, outgoing_nbytes
);
1138 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1139 starting at GPT_ADDR. */
1142 insert_from_gap (EMACS_INT nchars
, EMACS_INT nbytes
)
1144 if (NILP (current_buffer
->enable_multibyte_characters
))
1147 record_insert (GPT
, nchars
);
1157 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1162 adjust_overlays_for_insert (GPT
- nchars
, nchars
);
1163 adjust_markers_for_insert (GPT
- nchars
, GPT_BYTE
- nbytes
,
1166 if (BUF_INTERVALS (current_buffer
) != 0)
1168 offset_intervals (current_buffer
, GPT
- nchars
, nchars
);
1169 graft_intervals_into_buffer (NULL_INTERVAL
, GPT
- nchars
, nchars
,
1173 if (GPT
- nchars
< PT
)
1174 adjust_point (nchars
, nbytes
);
1179 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1180 current buffer. If the text in BUF has properties, they are absorbed
1181 into the current buffer.
1183 It does not work to use `insert' for this, because a malloc could happen
1184 and relocate BUF's text before the bcopy happens. */
1187 insert_from_buffer (struct buffer
*buf
,
1188 EMACS_INT charpos
, EMACS_INT nchars
, int inherit
)
1190 EMACS_INT opoint
= PT
;
1192 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1193 signal_after_change (opoint
, 0, PT
- opoint
);
1194 update_compositions (opoint
, PT
, CHECK_BORDER
);
1198 insert_from_buffer_1 (struct buffer
*buf
,
1199 EMACS_INT from
, EMACS_INT nchars
, int inherit
)
1201 register Lisp_Object temp
;
1202 EMACS_INT chunk
, chunk_expanded
;
1203 EMACS_INT from_byte
= buf_charpos_to_bytepos (buf
, from
);
1204 EMACS_INT to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1205 EMACS_INT incoming_nbytes
= to_byte
- from_byte
;
1206 EMACS_INT outgoing_nbytes
= incoming_nbytes
;
1209 /* Make OUTGOING_NBYTES describe the text
1210 as it will be inserted in this buffer. */
1212 if (NILP (current_buffer
->enable_multibyte_characters
))
1213 outgoing_nbytes
= nchars
;
1214 else if (NILP (buf
->enable_multibyte_characters
))
1216 EMACS_INT outgoing_before_gap
= 0;
1217 EMACS_INT outgoing_after_gap
= 0;
1219 if (from
< BUF_GPT (buf
))
1221 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1222 if (chunk
> incoming_nbytes
)
1223 chunk
= incoming_nbytes
;
1225 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1231 if (chunk
< incoming_nbytes
)
1233 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1235 incoming_nbytes
- chunk
);
1237 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1240 /* Make sure point-max won't overflow after this insertion. */
1241 XSETINT (temp
, outgoing_nbytes
+ Z
);
1242 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1243 error ("Maximum buffer size exceeded");
1245 /* Do this before moving and increasing the gap,
1246 because the before-change hooks might move the gap
1247 or make it smaller. */
1248 prepare_to_modify_buffer (PT
, PT
, NULL
);
1251 move_gap_both (PT
, PT_BYTE
);
1252 if (GAP_SIZE
< outgoing_nbytes
)
1253 make_gap (outgoing_nbytes
- GAP_SIZE
);
1255 if (from
< BUF_GPT (buf
))
1257 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1258 if (chunk
> incoming_nbytes
)
1259 chunk
= incoming_nbytes
;
1260 /* Record number of output bytes, so we know where
1261 to put the output from the second copy_text. */
1263 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1265 ! NILP (buf
->enable_multibyte_characters
),
1266 ! NILP (current_buffer
->enable_multibyte_characters
));
1269 chunk_expanded
= chunk
= 0;
1271 if (chunk
< incoming_nbytes
)
1272 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1273 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1274 ! NILP (buf
->enable_multibyte_characters
),
1275 ! NILP (current_buffer
->enable_multibyte_characters
));
1277 #ifdef BYTE_COMBINING_DEBUG
1278 /* We have copied text into the gap, but we have not altered
1279 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1280 to these functions and get the same results as we would
1281 have got earlier on. Meanwhile, GPT_ADDR does point to
1282 the text that has been stored by copy_text. */
1283 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1284 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1288 record_insert (PT
, nchars
);
1290 CHARS_MODIFF
= MODIFF
;
1292 GAP_SIZE
-= outgoing_nbytes
;
1296 GPT_BYTE
+= outgoing_nbytes
;
1297 ZV_BYTE
+= outgoing_nbytes
;
1298 Z_BYTE
+= outgoing_nbytes
;
1299 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1304 /* The insert may have been in the unchanged region, so check again. */
1305 if (Z
- GPT
< END_UNCHANGED
)
1306 END_UNCHANGED
= Z
- GPT
;
1308 adjust_overlays_for_insert (PT
, nchars
);
1309 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1310 PT_BYTE
+ outgoing_nbytes
,
1313 if (BUF_INTERVALS (current_buffer
) != 0)
1314 offset_intervals (current_buffer
, PT
, nchars
);
1316 /* Get the intervals for the part of the string we are inserting. */
1317 intervals
= BUF_INTERVALS (buf
);
1318 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1320 if (buf
== current_buffer
&& PT
<= from
)
1322 intervals
= copy_intervals (intervals
, from
, nchars
);
1325 /* Insert those intervals. */
1326 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1328 adjust_point (nchars
, outgoing_nbytes
);
1331 /* Record undo information and adjust markers and position keepers for
1332 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1333 chars (LEN_BYTE bytes) which resides in the gap just after
1336 PREV_TEXT nil means the new text was just inserted. */
1339 adjust_after_replace (EMACS_INT from
, EMACS_INT from_byte
,
1340 Lisp_Object prev_text
, EMACS_INT len
, EMACS_INT len_byte
)
1342 EMACS_INT nchars_del
= 0, nbytes_del
= 0;
1344 #ifdef BYTE_COMBINING_DEBUG
1345 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1346 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1350 if (STRINGP (prev_text
))
1352 nchars_del
= SCHARS (prev_text
);
1353 nbytes_del
= SBYTES (prev_text
);
1356 /* Update various buffer positions for the new text. */
1357 GAP_SIZE
-= len_byte
;
1359 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1360 GPT
+= len
; GPT_BYTE
+= len_byte
;
1361 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1364 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1367 adjust_markers_for_insert (from
, from_byte
,
1368 from
+ len
, from_byte
+ len_byte
, 0);
1370 if (! EQ (current_buffer
->undo_list
, Qt
))
1373 record_delete (from
, prev_text
);
1374 record_insert (from
, len
);
1377 if (len
> nchars_del
)
1378 adjust_overlays_for_insert (from
, len
- nchars_del
);
1379 else if (len
< nchars_del
)
1380 adjust_overlays_for_delete (from
, nchars_del
- len
);
1381 if (BUF_INTERVALS (current_buffer
) != 0)
1383 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1387 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1389 /* As byte combining will decrease Z, we must check this again. */
1390 if (Z
- GPT
< END_UNCHANGED
)
1391 END_UNCHANGED
= Z
- GPT
;
1396 evaporate_overlays (from
);
1398 CHARS_MODIFF
= MODIFF
;
1401 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1402 This is for use when undo is not enabled in the current buffer. */
1405 adjust_after_replace_noundo (EMACS_INT from
, EMACS_INT from_byte
,
1406 EMACS_INT nchars_del
, EMACS_INT nbytes_del
,
1407 EMACS_INT len
, EMACS_INT len_byte
)
1409 #ifdef BYTE_COMBINING_DEBUG
1410 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1411 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1415 /* Update various buffer positions for the new text. */
1416 GAP_SIZE
-= len_byte
;
1418 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1419 GPT
+= len
; GPT_BYTE
+= len_byte
;
1420 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1423 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1426 adjust_markers_for_insert (from
, from_byte
,
1427 from
+ len
, from_byte
+ len_byte
, 0);
1429 if (len
> nchars_del
)
1430 adjust_overlays_for_insert (from
, len
- nchars_del
);
1431 else if (len
< nchars_del
)
1432 adjust_overlays_for_delete (from
, nchars_del
- len
);
1433 if (BUF_INTERVALS (current_buffer
) != 0)
1435 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1439 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1441 /* As byte combining will decrease Z, we must check this again. */
1442 if (Z
- GPT
< END_UNCHANGED
)
1443 END_UNCHANGED
= Z
- GPT
;
1448 evaporate_overlays (from
);
1450 CHARS_MODIFF
= MODIFF
;
1453 /* Record undo information, adjust markers and position keepers for an
1454 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1455 text already exists in the current buffer but character length (TO
1456 - FROM) may be incorrect, the correct length is NEWLEN. */
1459 adjust_after_insert (EMACS_INT from
, EMACS_INT from_byte
,
1460 EMACS_INT to
, EMACS_INT to_byte
, EMACS_INT newlen
)
1462 EMACS_INT len
= to
- from
, len_byte
= to_byte
- from_byte
;
1465 move_gap_both (to
, to_byte
);
1466 GAP_SIZE
+= len_byte
;
1467 GPT
-= len
; GPT_BYTE
-= len_byte
;
1468 ZV
-= len
; ZV_BYTE
-= len_byte
;
1469 Z
-= len
; Z_BYTE
-= len_byte
;
1470 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1473 /* Replace the text from character positions FROM to TO with NEW,
1474 If PREPARE is nonzero, call prepare_to_modify_buffer.
1475 If INHERIT, the newly inserted text should inherit text properties
1476 from the surrounding non-deleted text. */
1478 /* Note that this does not yet handle markers quite right.
1479 Also it needs to record a single undo-entry that does a replacement
1480 rather than a separate delete and insert.
1481 That way, undo will also handle markers properly.
1483 But if MARKERS is 0, don't relocate markers. */
1486 replace_range (EMACS_INT from
, EMACS_INT to
, Lisp_Object
new,
1487 int prepare
, int inherit
, int markers
)
1489 EMACS_INT inschars
= SCHARS (new);
1490 EMACS_INT insbytes
= SBYTES (new);
1491 EMACS_INT from_byte
, to_byte
;
1492 EMACS_INT nbytes_del
, nchars_del
;
1493 register Lisp_Object temp
;
1494 struct gcpro gcpro1
;
1496 EMACS_INT outgoing_insbytes
= insbytes
;
1497 Lisp_Object deletion
;
1506 EMACS_INT range_length
= to
- from
;
1507 prepare_to_modify_buffer (from
, to
, &from
);
1508 to
= from
+ range_length
;
1513 /* Make args be valid */
1519 from_byte
= CHAR_TO_BYTE (from
);
1520 to_byte
= CHAR_TO_BYTE (to
);
1522 nchars_del
= to
- from
;
1523 nbytes_del
= to_byte
- from_byte
;
1525 if (nbytes_del
<= 0 && insbytes
== 0)
1528 /* Make OUTGOING_INSBYTES describe the text
1529 as it will be inserted in this buffer. */
1531 if (NILP (current_buffer
->enable_multibyte_characters
))
1532 outgoing_insbytes
= inschars
;
1533 else if (! STRING_MULTIBYTE (new))
1535 = count_size_as_multibyte (SDATA (new), insbytes
);
1537 /* Make sure point-max won't overflow after this insertion. */
1538 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1539 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1540 error ("Maximum buffer size exceeded");
1544 /* Make sure the gap is somewhere in or next to what we are deleting. */
1546 gap_right (from
, from_byte
);
1548 gap_left (to
, to_byte
, 0);
1550 /* Even if we don't record for undo, we must keep the original text
1551 because we may have to recover it because of inappropriate byte
1553 if (! EQ (current_buffer
->undo_list
, Qt
))
1554 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1556 GAP_SIZE
+= nbytes_del
;
1559 ZV_BYTE
-= nbytes_del
;
1560 Z_BYTE
-= nbytes_del
;
1562 GPT_BYTE
= from_byte
;
1563 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1568 if (GPT
- BEG
< BEG_UNCHANGED
)
1569 BEG_UNCHANGED
= GPT
- BEG
;
1570 if (Z
- GPT
< END_UNCHANGED
)
1571 END_UNCHANGED
= Z
- GPT
;
1573 if (GAP_SIZE
< insbytes
)
1574 make_gap (insbytes
- GAP_SIZE
);
1576 /* Copy the string text into the buffer, perhaps converting
1577 between single-byte and multibyte. */
1578 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1579 STRING_MULTIBYTE (new),
1580 ! NILP (current_buffer
->enable_multibyte_characters
));
1582 #ifdef BYTE_COMBINING_DEBUG
1583 /* We have copied text into the gap, but we have not marked
1584 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1585 here, for both the previous text and the following text.
1586 Meanwhile, GPT_ADDR does point to
1587 the text that has been stored by copy_text. */
1588 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1589 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1593 if (! EQ (current_buffer
->undo_list
, Qt
))
1595 /* Record the insertion first, so that when we undo,
1596 the deletion will be undone first. Thus, undo
1597 will insert before deleting, and thus will keep
1598 the markers before and after this text separate. */
1599 record_insert (from
+ SCHARS (deletion
), inschars
);
1600 record_delete (from
, deletion
);
1603 GAP_SIZE
-= outgoing_insbytes
;
1607 GPT_BYTE
+= outgoing_insbytes
;
1608 ZV_BYTE
+= outgoing_insbytes
;
1609 Z_BYTE
+= outgoing_insbytes
;
1610 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1615 /* Adjust the overlay center as needed. This must be done after
1616 adjusting the markers that bound the overlays. */
1617 adjust_overlays_for_delete (from
, nchars_del
);
1618 adjust_overlays_for_insert (from
, inschars
);
1620 /* Adjust markers for the deletion and the insertion. */
1622 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1623 inschars
, outgoing_insbytes
);
1625 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1627 /* Get the intervals for the part of the string we are inserting--
1628 not including the combined-before bytes. */
1629 intervals
= STRING_INTERVALS (new);
1630 /* Insert those intervals. */
1631 graft_intervals_into_buffer (intervals
, from
, inschars
,
1632 current_buffer
, inherit
);
1634 /* Relocate point as if it were a marker. */
1636 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1637 (from_byte
+ outgoing_insbytes
1638 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1640 if (outgoing_insbytes
== 0)
1641 evaporate_overlays (from
);
1646 CHARS_MODIFF
= MODIFF
;
1649 signal_after_change (from
, nchars_del
, GPT
- from
);
1650 update_compositions (from
, GPT
, CHECK_BORDER
);
1653 /* Replace the text from character positions FROM to TO with
1654 the text in INS of length INSCHARS.
1655 Keep the text properties that applied to the old characters
1656 (extending them to all the new chars if there are more new chars).
1658 Note that this does not yet handle markers quite right.
1660 If MARKERS is nonzero, relocate markers.
1662 Unlike most functions at this level, never call
1663 prepare_to_modify_buffer and never call signal_after_change. */
1666 replace_range_2 (EMACS_INT from
, EMACS_INT from_byte
,
1667 EMACS_INT to
, EMACS_INT to_byte
,
1668 char *ins
, EMACS_INT inschars
, EMACS_INT insbytes
,
1671 EMACS_INT nbytes_del
, nchars_del
;
1676 nchars_del
= to
- from
;
1677 nbytes_del
= to_byte
- from_byte
;
1679 if (nbytes_del
<= 0 && insbytes
== 0)
1682 /* Make sure point-max won't overflow after this insertion. */
1683 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1684 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1685 error ("Maximum buffer size exceeded");
1687 /* Make sure the gap is somewhere in or next to what we are deleting. */
1689 gap_right (from
, from_byte
);
1691 gap_left (to
, to_byte
, 0);
1693 GAP_SIZE
+= nbytes_del
;
1696 ZV_BYTE
-= nbytes_del
;
1697 Z_BYTE
-= nbytes_del
;
1699 GPT_BYTE
= from_byte
;
1700 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1705 if (GPT
- BEG
< BEG_UNCHANGED
)
1706 BEG_UNCHANGED
= GPT
- BEG
;
1707 if (Z
- GPT
< END_UNCHANGED
)
1708 END_UNCHANGED
= Z
- GPT
;
1710 if (GAP_SIZE
< insbytes
)
1711 make_gap (insbytes
- GAP_SIZE
);
1713 /* Copy the replacement text into the buffer. */
1714 bcopy (ins
, GPT_ADDR
, insbytes
);
1716 #ifdef BYTE_COMBINING_DEBUG
1717 /* We have copied text into the gap, but we have not marked
1718 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1719 here, for both the previous text and the following text.
1720 Meanwhile, GPT_ADDR does point to
1721 the text that has been stored by copy_text. */
1722 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1723 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1727 GAP_SIZE
-= insbytes
;
1731 GPT_BYTE
+= insbytes
;
1732 ZV_BYTE
+= insbytes
;
1734 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1739 /* Adjust the overlay center as needed. This must be done after
1740 adjusting the markers that bound the overlays. */
1741 if (nchars_del
!= inschars
)
1743 adjust_overlays_for_insert (from
, inschars
);
1744 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1747 /* Adjust markers for the deletion and the insertion. */
1749 && ! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1750 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1751 inschars
, insbytes
);
1753 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1755 /* Relocate point as if it were a marker. */
1756 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1759 /* PT was within the deleted text. Move it to FROM. */
1760 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1762 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1766 evaporate_overlays (from
);
1771 CHARS_MODIFF
= MODIFF
;
1774 /* Delete characters in current buffer
1775 from FROM up to (but not including) TO.
1776 If TO comes before FROM, we delete nothing. */
1779 del_range (EMACS_INT from
, EMACS_INT to
)
1781 del_range_1 (from
, to
, 1, 0);
1784 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1785 RET_STRING says to return the deleted text. */
1788 del_range_1 (EMACS_INT from
, EMACS_INT to
, int prepare
, int ret_string
)
1790 EMACS_INT from_byte
, to_byte
;
1791 Lisp_Object deletion
;
1792 struct gcpro gcpro1
;
1794 /* Make args be valid */
1805 EMACS_INT range_length
= to
- from
;
1806 prepare_to_modify_buffer (from
, to
, &from
);
1807 to
= min (ZV
, from
+ range_length
);
1810 from_byte
= CHAR_TO_BYTE (from
);
1811 to_byte
= CHAR_TO_BYTE (to
);
1813 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1815 signal_after_change (from
, to
- from
, 0);
1816 update_compositions (from
, from
, CHECK_HEAD
);
1821 /* Like del_range_1 but args are byte positions, not char positions. */
1824 del_range_byte (EMACS_INT from_byte
, EMACS_INT to_byte
, int prepare
)
1828 /* Make args be valid */
1829 if (from_byte
< BEGV_BYTE
)
1830 from_byte
= BEGV_BYTE
;
1831 if (to_byte
> ZV_BYTE
)
1834 if (to_byte
<= from_byte
)
1837 from
= BYTE_TO_CHAR (from_byte
);
1838 to
= BYTE_TO_CHAR (to_byte
);
1842 EMACS_INT old_from
= from
, old_to
= Z
- to
;
1843 EMACS_INT range_length
= to
- from
;
1844 prepare_to_modify_buffer (from
, to
, &from
);
1845 to
= from
+ range_length
;
1847 if (old_from
!= from
)
1848 from_byte
= CHAR_TO_BYTE (from
);
1854 else if (old_to
== Z
- to
)
1855 to_byte
= CHAR_TO_BYTE (to
);
1858 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1859 signal_after_change (from
, to
- from
, 0);
1860 update_compositions (from
, from
, CHECK_HEAD
);
1863 /* Like del_range_1, but positions are specified both as charpos
1867 del_range_both (EMACS_INT from
, EMACS_INT from_byte
,
1868 EMACS_INT to
, EMACS_INT to_byte
, int prepare
)
1870 /* Make args be valid */
1871 if (from_byte
< BEGV_BYTE
)
1872 from_byte
= BEGV_BYTE
;
1873 if (to_byte
> ZV_BYTE
)
1876 if (to_byte
<= from_byte
)
1886 EMACS_INT old_from
= from
, old_to
= Z
- to
;
1887 EMACS_INT range_length
= to
- from
;
1888 prepare_to_modify_buffer (from
, to
, &from
);
1889 to
= from
+ range_length
;
1891 if (old_from
!= from
)
1892 from_byte
= CHAR_TO_BYTE (from
);
1898 else if (old_to
== Z
- to
)
1899 to_byte
= CHAR_TO_BYTE (to
);
1902 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1903 signal_after_change (from
, to
- from
, 0);
1904 update_compositions (from
, from
, CHECK_HEAD
);
1907 /* Delete a range of text, specified both as character positions
1908 and byte positions. FROM and TO are character positions,
1909 while FROM_BYTE and TO_BYTE are byte positions.
1910 If RET_STRING is true, the deleted area is returned as a string. */
1913 del_range_2 (EMACS_INT from
, EMACS_INT from_byte
,
1914 EMACS_INT to
, EMACS_INT to_byte
, int ret_string
)
1916 register EMACS_INT nbytes_del
, nchars_del
;
1917 Lisp_Object deletion
;
1921 nchars_del
= to
- from
;
1922 nbytes_del
= to_byte
- from_byte
;
1924 /* Make sure the gap is somewhere in or next to what we are deleting. */
1926 gap_right (from
, from_byte
);
1928 gap_left (to
, to_byte
, 0);
1930 #ifdef BYTE_COMBINING_DEBUG
1931 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1932 Z_BYTE
- to_byte
, from
, from_byte
))
1936 if (ret_string
|| ! EQ (current_buffer
->undo_list
, Qt
))
1937 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1941 /* Relocate all markers pointing into the new, larger gap
1942 to point at the end of the text before the gap.
1943 Do this before recording the deletion,
1944 so that undo handles this after reinserting the text. */
1945 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1947 if (! EQ (current_buffer
->undo_list
, Qt
))
1948 record_delete (from
, deletion
);
1950 CHARS_MODIFF
= MODIFF
;
1952 /* Relocate point as if it were a marker. */
1954 adjust_point (from
- (PT
< to
? PT
: to
),
1955 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1957 offset_intervals (current_buffer
, from
, - nchars_del
);
1959 /* Adjust the overlay center as needed. This must be done after
1960 adjusting the markers that bound the overlays. */
1961 adjust_overlays_for_delete (from
, nchars_del
);
1963 GAP_SIZE
+= nbytes_del
;
1964 ZV_BYTE
-= nbytes_del
;
1965 Z_BYTE
-= nbytes_del
;
1969 GPT_BYTE
= from_byte
;
1970 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1971 /* Put an anchor, unless called from decode_coding_object which
1972 needs to access the previous gap contents. */
1978 if (GPT
- BEG
< BEG_UNCHANGED
)
1979 BEG_UNCHANGED
= GPT
- BEG
;
1980 if (Z
- GPT
< END_UNCHANGED
)
1981 END_UNCHANGED
= Z
- GPT
;
1985 evaporate_overlays (from
);
1990 /* Call this if you're about to change the region of BUFFER from
1991 character positions START to END. This checks the read-only
1992 properties of the region, calls the necessary modification hooks,
1993 and warns the next redisplay that it should pay attention to that
1996 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
1997 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
2000 modify_region (struct buffer
*buffer
, EMACS_INT start
, EMACS_INT end
,
2001 int preserve_chars_modiff
)
2003 struct buffer
*old_buffer
= current_buffer
;
2005 if (buffer
!= old_buffer
)
2006 set_buffer_internal (buffer
);
2008 prepare_to_modify_buffer (start
, end
, NULL
);
2010 BUF_COMPUTE_UNCHANGED (buffer
, start
- 1, end
);
2012 if (MODIFF
<= SAVE_MODIFF
)
2013 record_first_change ();
2015 if (! preserve_chars_modiff
)
2016 CHARS_MODIFF
= MODIFF
;
2018 buffer
->point_before_scroll
= Qnil
;
2020 if (buffer
!= old_buffer
)
2021 set_buffer_internal (old_buffer
);
2024 /* Check that it is okay to modify the buffer between START and END,
2025 which are char positions.
2027 Run the before-change-function, if any. If intervals are in use,
2028 verify that the text to be modified is not read-only, and call
2029 any modification properties the text may have.
2031 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2032 by holding its value temporarily in a marker. */
2035 prepare_to_modify_buffer (EMACS_INT start
, EMACS_INT end
,
2036 EMACS_INT
*preserve_ptr
)
2038 struct buffer
*base_buffer
;
2040 if (!NILP (current_buffer
->read_only
))
2041 Fbarf_if_buffer_read_only ();
2043 /* Let redisplay consider other windows than selected_window
2044 if modifying another buffer. */
2045 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
2046 ++windows_or_buffers_changed
;
2048 if (BUF_INTERVALS (current_buffer
) != 0)
2052 Lisp_Object preserve_marker
;
2053 struct gcpro gcpro1
;
2054 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2055 GCPRO1 (preserve_marker
);
2056 verify_interval_modification (current_buffer
, start
, end
);
2057 *preserve_ptr
= marker_position (preserve_marker
);
2058 unchain_marker (XMARKER (preserve_marker
));
2062 verify_interval_modification (current_buffer
, start
, end
);
2065 /* For indirect buffers, use the base buffer to check clashes. */
2066 if (current_buffer
->base_buffer
!= 0)
2067 base_buffer
= current_buffer
->base_buffer
;
2069 base_buffer
= current_buffer
;
2071 #ifdef CLASH_DETECTION
2072 if (!NILP (base_buffer
->file_truename
)
2073 /* Make binding buffer-file-name to nil effective. */
2074 && !NILP (base_buffer
->filename
)
2075 && SAVE_MODIFF
>= MODIFF
)
2076 lock_file (base_buffer
->file_truename
);
2078 /* At least warn if this file has changed on disk since it was visited. */
2079 if (!NILP (base_buffer
->filename
)
2080 && SAVE_MODIFF
>= MODIFF
2081 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2082 && !NILP (Ffile_exists_p (base_buffer
->filename
)))
2083 call1 (intern ("ask-user-about-supersession-threat"),
2084 base_buffer
->filename
);
2085 #endif /* not CLASH_DETECTION */
2087 signal_before_change (start
, end
, preserve_ptr
);
2089 if (current_buffer
->newline_cache
)
2090 invalidate_region_cache (current_buffer
,
2091 current_buffer
->newline_cache
,
2092 start
- BEG
, Z
- end
);
2093 if (current_buffer
->width_run_cache
)
2094 invalidate_region_cache (current_buffer
,
2095 current_buffer
->width_run_cache
,
2096 start
- BEG
, Z
- end
);
2098 Vdeactivate_mark
= Qt
;
2101 /* These macros work with an argument named `preserve_ptr'
2102 and a local variable named `preserve_marker'. */
2104 #define PRESERVE_VALUE \
2105 if (preserve_ptr && NILP (preserve_marker)) \
2106 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2108 #define RESTORE_VALUE \
2109 if (! NILP (preserve_marker)) \
2111 *preserve_ptr = marker_position (preserve_marker); \
2112 unchain_marker (XMARKER (preserve_marker)); \
2115 #define PRESERVE_START_END \
2116 if (NILP (start_marker)) \
2117 start_marker = Fcopy_marker (start, Qnil); \
2118 if (NILP (end_marker)) \
2119 end_marker = Fcopy_marker (end, Qnil);
2121 #define FETCH_START \
2122 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2125 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2127 /* Set a variable to nil if an error occurred.
2128 Don't change the variable if there was no error.
2129 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2130 VARIABLE is the variable to maybe set to nil.
2131 NO-ERROR-FLAG is nil if there was an error,
2132 anything else meaning no error (so this function does nothing). */
2134 reset_var_on_error (val
)
2137 if (NILP (XCDR (val
)))
2138 Fset (XCAR (val
), Qnil
);
2142 /* Signal a change to the buffer immediately before it happens.
2143 START_INT and END_INT are the bounds of the text to be changed.
2145 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2146 by holding its value temporarily in a marker. */
2149 signal_before_change (EMACS_INT start_int
, EMACS_INT end_int
,
2150 EMACS_INT
*preserve_ptr
)
2152 Lisp_Object start
, end
;
2153 Lisp_Object start_marker
, end_marker
;
2154 Lisp_Object preserve_marker
;
2155 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2156 int count
= SPECPDL_INDEX ();
2158 if (inhibit_modification_hooks
)
2161 start
= make_number (start_int
);
2162 end
= make_number (end_int
);
2163 preserve_marker
= Qnil
;
2164 start_marker
= Qnil
;
2166 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2168 specbind (Qinhibit_modification_hooks
, Qt
);
2170 /* If buffer is unmodified, run a special hook for that case. */
2171 if (SAVE_MODIFF
>= MODIFF
2172 && !NILP (Vfirst_change_hook
)
2173 && !NILP (Vrun_hooks
))
2177 call1 (Vrun_hooks
, Qfirst_change_hook
);
2180 /* Now run the before-change-functions if any. */
2181 if (!NILP (Vbefore_change_functions
))
2183 Lisp_Object args
[3];
2184 Lisp_Object rvoe_arg
= Fcons (Qbefore_change_functions
, Qnil
);
2189 /* Mark before-change-functions to be reset to nil in case of error. */
2190 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2192 /* Actually run the hook functions. */
2193 args
[0] = Qbefore_change_functions
;
2194 args
[1] = FETCH_START
;
2195 args
[2] = FETCH_END
;
2196 Frun_hook_with_args (3, args
);
2198 /* There was no error: unarm the reset_on_error. */
2199 XSETCDR (rvoe_arg
, Qt
);
2202 if (current_buffer
->overlays_before
|| current_buffer
->overlays_after
)
2205 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2206 FETCH_START
, FETCH_END
, Qnil
);
2209 if (! NILP (start_marker
))
2210 free_marker (start_marker
);
2211 if (! NILP (end_marker
))
2212 free_marker (end_marker
);
2216 unbind_to (count
, Qnil
);
2219 /* Signal a change immediately after it happens.
2220 CHARPOS is the character position of the start of the changed text.
2221 LENDEL is the number of characters of the text before the change.
2222 (Not the whole buffer; just the part that was changed.)
2223 LENINS is the number of characters in that part of the text
2224 after the change. */
2227 signal_after_change (EMACS_INT charpos
, EMACS_INT lendel
, EMACS_INT lenins
)
2229 int count
= SPECPDL_INDEX ();
2230 if (inhibit_modification_hooks
)
2233 /* If we are deferring calls to the after-change functions
2234 and there are no before-change functions,
2235 just record the args that we were going to use. */
2236 if (! NILP (Vcombine_after_change_calls
)
2237 && NILP (Vbefore_change_functions
)
2238 && !current_buffer
->overlays_before
2239 && !current_buffer
->overlays_after
)
2243 if (!NILP (combine_after_change_list
)
2244 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2245 Fcombine_after_change_execute ();
2247 elt
= Fcons (make_number (charpos
- BEG
),
2248 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2249 Fcons (make_number (lenins
- lendel
), Qnil
)));
2250 combine_after_change_list
2251 = Fcons (elt
, combine_after_change_list
);
2252 combine_after_change_buffer
= Fcurrent_buffer ();
2257 if (!NILP (combine_after_change_list
))
2258 Fcombine_after_change_execute ();
2260 specbind (Qinhibit_modification_hooks
, Qt
);
2262 if (!NILP (Vafter_change_functions
))
2264 Lisp_Object args
[4];
2265 Lisp_Object rvoe_arg
= Fcons (Qafter_change_functions
, Qnil
);
2267 /* Mark after-change-functions to be reset to nil in case of error. */
2268 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2270 /* Actually run the hook functions. */
2271 args
[0] = Qafter_change_functions
;
2272 XSETFASTINT (args
[1], charpos
);
2273 XSETFASTINT (args
[2], charpos
+ lenins
);
2274 XSETFASTINT (args
[3], lendel
);
2275 Frun_hook_with_args (4, args
);
2277 /* There was no error: unarm the reset_on_error. */
2278 XSETCDR (rvoe_arg
, Qt
);
2281 if (current_buffer
->overlays_before
|| current_buffer
->overlays_after
)
2282 report_overlay_modification (make_number (charpos
),
2283 make_number (charpos
+ lenins
),
2285 make_number (charpos
),
2286 make_number (charpos
+ lenins
),
2287 make_number (lendel
));
2289 /* After an insertion, call the text properties
2290 insert-behind-hooks or insert-in-front-hooks. */
2292 report_interval_modification (make_number (charpos
),
2293 make_number (charpos
+ lenins
));
2295 unbind_to (count
, Qnil
);
2299 Fcombine_after_change_execute_1 (val
)
2302 Vcombine_after_change_calls
= val
;
2306 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2307 Scombine_after_change_execute
, 0, 0, 0,
2308 doc
: /* This function is for use internally in `combine-after-change-calls'. */)
2311 int count
= SPECPDL_INDEX ();
2312 EMACS_INT beg
, end
, change
;
2313 EMACS_INT begpos
, endpos
;
2316 if (NILP (combine_after_change_list
))
2319 /* It is rare for combine_after_change_buffer to be invalid, but
2320 possible. It can happen when combine-after-change-calls is
2321 non-nil, and insertion calls a file handler (e.g. through
2322 lock_file) which scribbles into a temp file -- cyd */
2323 if (!BUFFERP (combine_after_change_buffer
)
2324 || NILP (XBUFFER (combine_after_change_buffer
)->name
))
2326 combine_after_change_list
= Qnil
;
2330 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2332 Fset_buffer (combine_after_change_buffer
);
2334 /* # chars unchanged at beginning of buffer. */
2336 /* # chars unchanged at end of buffer. */
2338 /* Total amount of insertion (negative for deletion). */
2341 /* Scan the various individual changes,
2342 accumulating the range info in BEG, END and CHANGE. */
2343 for (tail
= combine_after_change_list
; CONSP (tail
);
2347 EMACS_INT thisbeg
, thisend
, thischange
;
2349 /* Extract the info from the next element. */
2353 thisbeg
= XINT (XCAR (elt
));
2358 thisend
= XINT (XCAR (elt
));
2363 thischange
= XINT (XCAR (elt
));
2365 /* Merge this range into the accumulated range. */
2366 change
+= thischange
;
2373 /* Get the current start and end positions of the range
2374 that was changed. */
2378 /* We are about to handle these, so discard them. */
2379 combine_after_change_list
= Qnil
;
2381 /* Now run the after-change functions for real.
2382 Turn off the flag that defers them. */
2383 record_unwind_protect (Fcombine_after_change_execute_1
,
2384 Vcombine_after_change_calls
);
2385 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2386 update_compositions (begpos
, endpos
, CHECK_ALL
);
2388 return unbind_to (count
, Qnil
);
2394 staticpro (&combine_after_change_list
);
2395 staticpro (&combine_after_change_buffer
);
2396 combine_after_change_list
= Qnil
;
2397 combine_after_change_buffer
= Qnil
;
2399 DEFVAR_BOOL ("check-markers-debug-flag", &check_markers_debug_flag
,
2400 doc
: /* Non-nil means enable debugging checks for invalid marker positions. */);
2401 check_markers_debug_flag
= 0;
2402 DEFVAR_LISP ("combine-after-change-calls", &Vcombine_after_change_calls
,
2403 doc
: /* Used internally by the `combine-after-change-calls' macro. */);
2404 Vcombine_after_change_calls
= Qnil
;
2406 DEFVAR_BOOL ("inhibit-modification-hooks", &inhibit_modification_hooks
,
2407 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2408 This affects `before-change-functions' and `after-change-functions',
2409 as well as hooks attached to text properties and overlays. */);
2410 inhibit_modification_hooks
= 0;
2411 Qinhibit_modification_hooks
= intern ("inhibit-modification-hooks");
2412 staticpro (&Qinhibit_modification_hooks
);
2414 defsubr (&Scombine_after_change_execute
);
2417 /* arch-tag: 9b34b886-47d7-465e-a234-299af411b23d
2418 (do not change this comment) */