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, 2011
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 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
57 describing changes which happened while combine_after_change_calls
58 was nonzero. We use this to decide how to call them
59 once the deferral ends.
62 BEG-UNCHANGED is the number of chars before the changed range.
63 END-UNCHANGED is the number of chars after the changed range,
64 and CHANGE-AMOUNT is the number of characters inserted by the change
65 (negative for a deletion). */
66 Lisp_Object combine_after_change_list
;
68 /* Buffer which combine_after_change_list is about. */
69 Lisp_Object combine_after_change_buffer
;
71 Lisp_Object Qinhibit_modification_hooks
;
73 #define CHECK_MARKERS() \
74 if (check_markers_debug_flag) \
81 register struct Lisp_Marker
*tail
;
82 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
84 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
86 if (tail
->buffer
->text
!= current_buffer
->text
)
88 if (tail
->charpos
> Z
)
90 if (tail
->bytepos
> Z_BYTE
)
92 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (tail
->bytepos
)))
97 /* Move gap to position CHARPOS.
98 Note that this can quit! */
101 move_gap (EMACS_INT charpos
)
103 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
106 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
107 Note that this can quit! */
110 move_gap_both (EMACS_INT charpos
, EMACS_INT bytepos
)
112 if (bytepos
< GPT_BYTE
)
113 gap_left (charpos
, bytepos
, 0);
114 else if (bytepos
> GPT_BYTE
)
115 gap_right (charpos
, bytepos
);
118 /* Move the gap to a position less than the current GPT.
119 BYTEPOS describes the new position as a byte position,
120 and CHARPOS is the corresponding char position.
121 If NEWGAP is nonzero, then don't update beg_unchanged and end_unchanged. */
124 gap_left (EMACS_INT charpos
, EMACS_INT bytepos
, int newgap
)
126 register unsigned char *to
, *from
;
127 register EMACS_INT i
;
131 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
138 /* Now copy the characters. To move the gap down,
139 copy characters up. */
143 /* I gets number of characters left to copy. */
144 i
= new_s1
- bytepos
;
147 /* If a quit is requested, stop copying now.
148 Change BYTEPOS to be where we have actually moved the gap to. */
152 charpos
= BYTE_TO_CHAR (bytepos
);
155 /* Move at most 32000 chars before checking again for a quit. */
160 memmove (to
, from
, i
);
163 /* Adjust markers, and buffer data structure, to put the gap at BYTEPOS.
164 BYTEPOS is where the loop above stopped, which may be what was specified
165 or may be where a quit was detected. */
166 adjust_markers_gap_motion (bytepos
, GPT_BYTE
, GAP_SIZE
);
169 if (bytepos
< charpos
)
171 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
175 /* Move the gap to a position greater than the current GPT.
176 BYTEPOS describes the new position as a byte position,
177 and CHARPOS is the corresponding char position. */
180 gap_right (EMACS_INT charpos
, EMACS_INT bytepos
)
182 register unsigned char *to
, *from
;
183 register EMACS_INT i
;
186 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
193 /* Now copy the characters. To move the gap up,
194 copy characters down. */
198 /* I gets number of characters left to copy. */
199 i
= bytepos
- new_s1
;
202 /* If a quit is requested, stop copying now.
203 Change BYTEPOS to be where we have actually moved the gap to. */
207 charpos
= BYTE_TO_CHAR (bytepos
);
210 /* Move at most 32000 chars before checking again for a quit. */
214 memmove (to
, from
, i
);
218 adjust_markers_gap_motion (GPT_BYTE
+ GAP_SIZE
, bytepos
+ GAP_SIZE
,
222 if (bytepos
< charpos
)
224 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
228 /* Add AMOUNT to the byte position of every marker in the current buffer
229 whose current byte position is between FROM (exclusive) and TO (inclusive).
231 Also, any markers past the outside of that interval, in the direction
232 of adjustment, are first moved back to the near end of the interval
233 and then adjusted by AMOUNT.
235 When the latter adjustment is done, if AMOUNT is negative,
236 we record the adjustment for undo. (This case happens only for
239 The markers' character positions are not altered,
240 because gap motion does not affect character positions. */
242 int adjust_markers_test
;
245 adjust_markers_gap_motion (EMACS_INT from
, EMACS_INT to
, EMACS_INT amount
)
247 /* Now that a marker has a bytepos, not counting the gap,
248 nothing needs to be done here. */
251 register struct Lisp_Marker
*m
;
252 register EMACS_INT mpos
;
254 marker
= BUF_MARKERS (current_buffer
);
256 while (!NILP (marker
))
258 m
= XMARKER (marker
);
262 if (mpos
> to
&& mpos
< to
+ amount
)
264 if (adjust_markers_test
)
271 /* Here's the case where a marker is inside text being deleted.
272 AMOUNT can be negative for gap motion, too,
273 but then this range contains no markers. */
274 if (mpos
> from
+ amount
&& mpos
<= from
)
276 if (adjust_markers_test
)
278 mpos
= from
+ amount
;
281 if (mpos
> from
&& mpos
<= to
)
289 /* Adjust all markers for a deletion
290 whose range in bytes is FROM_BYTE to TO_BYTE.
291 The range in charpos is FROM to TO.
293 This function assumes that the gap is adjacent to
294 or inside of the range being deleted. */
297 adjust_markers_for_delete (EMACS_INT from
, EMACS_INT from_byte
,
298 EMACS_INT to
, EMACS_INT to_byte
)
301 register struct Lisp_Marker
*m
;
302 register EMACS_INT charpos
;
304 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
306 charpos
= m
->charpos
;
311 /* If the marker is after the deletion,
312 relocate by number of chars / bytes deleted. */
315 m
->charpos
-= to
- from
;
316 m
->bytepos
-= to_byte
- from_byte
;
318 /* Here's the case where a marker is inside text being deleted. */
319 else if (charpos
> from
)
321 if (! m
->insertion_type
)
322 { /* Normal markers will end up at the beginning of the
323 re-inserted text after undoing a deletion, and must be
324 adjusted to move them to the correct place. */
325 XSETMISC (marker
, m
);
326 record_marker_adjustment (marker
, from
- charpos
);
328 else if (charpos
< to
)
329 { /* Before-insertion markers will automatically move forward
330 upon re-inserting the deleted text, so we have to arrange
331 for them to move backward to the correct position. */
332 XSETMISC (marker
, m
);
333 record_marker_adjustment (marker
, to
- charpos
);
336 m
->bytepos
= from_byte
;
338 /* Here's the case where a before-insertion marker is immediately
339 before the deleted region. */
340 else if (charpos
== from
&& m
->insertion_type
)
342 /* Undoing the change uses normal insertion, which will
343 incorrectly make MARKER move forward, so we arrange for it
344 to then move backward to the correct place at the beginning
345 of the deleted region. */
346 XSETMISC (marker
, m
);
347 record_marker_adjustment (marker
, to
- from
);
353 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
354 to TO / TO_BYTE. We have to relocate the charpos of every marker
355 that points after the insertion (but not their bytepos).
357 When a marker points at the insertion point,
358 we advance it if either its insertion-type is t
359 or BEFORE_MARKERS is true. */
362 adjust_markers_for_insert (EMACS_INT from
, EMACS_INT from_byte
,
363 EMACS_INT to
, EMACS_INT to_byte
, int before_markers
)
365 struct Lisp_Marker
*m
;
367 EMACS_INT nchars
= to
- from
;
368 EMACS_INT nbytes
= to_byte
- from_byte
;
370 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
372 eassert (m
->bytepos
>= m
->charpos
373 && m
->bytepos
- m
->charpos
<= Z_BYTE
- Z
);
375 if (m
->bytepos
== from_byte
)
377 if (m
->insertion_type
|| before_markers
)
379 m
->bytepos
= to_byte
;
381 if (m
->insertion_type
)
385 else if (m
->bytepos
> from_byte
)
387 m
->bytepos
+= nbytes
;
388 m
->charpos
+= nchars
;
392 /* Adjusting only markers whose insertion-type is t may result in
393 - disordered start and end in overlays, and
394 - disordered overlays in the slot `overlays_before' of current_buffer. */
397 fix_start_end_in_overlays(from
, to
);
398 fix_overlays_before (current_buffer
, from
, to
);
402 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
404 This is used only when the value of point changes due to an insert
405 or delete; it does not represent a conceptual change in point as a
406 marker. In particular, point is not crossing any interval
407 boundaries, so there's no need to use the usual SET_PT macro. In
408 fact it would be incorrect to do so, because either the old or the
409 new value of point is out of sync with the current set of
413 adjust_point (EMACS_INT nchars
, EMACS_INT nbytes
)
415 BUF_PT (current_buffer
) += nchars
;
416 BUF_PT_BYTE (current_buffer
) += nbytes
;
418 /* In a single-byte buffer, the two positions must be equal. */
419 eassert (PT_BYTE
>= PT
&& PT_BYTE
- PT
<= ZV_BYTE
- ZV
);
422 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
423 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
424 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
428 adjust_markers_for_replace (EMACS_INT from
, EMACS_INT from_byte
,
429 EMACS_INT old_chars
, EMACS_INT old_bytes
,
430 EMACS_INT new_chars
, EMACS_INT new_bytes
)
432 register struct Lisp_Marker
*m
;
433 EMACS_INT prev_to_byte
= from_byte
+ old_bytes
;
434 EMACS_INT diff_chars
= new_chars
- old_chars
;
435 EMACS_INT diff_bytes
= new_bytes
- old_bytes
;
437 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
439 if (m
->bytepos
>= prev_to_byte
)
441 m
->charpos
+= diff_chars
;
442 m
->bytepos
+= diff_bytes
;
444 else if (m
->bytepos
> from_byte
)
447 m
->bytepos
= from_byte
;
455 /* Make the gap NBYTES_ADDED bytes longer. */
458 make_gap_larger (EMACS_INT nbytes_added
)
461 EMACS_INT real_gap_loc
;
462 EMACS_INT real_gap_loc_byte
;
463 EMACS_INT old_gap_size
;
465 /* If we have to get more space, get enough to last a while. */
466 nbytes_added
+= 2000;
468 { EMACS_INT total_size
= Z_BYTE
- BEG_BYTE
+ GAP_SIZE
+ nbytes_added
;
470 /* Don't allow a buffer size that won't fit in a Lisp integer. */
471 || total_size
!= XINT (make_number (total_size
))
472 /* Don't allow a buffer size that won't fit in an int
473 even if it will fit in a Lisp integer.
474 That won't work because so many places still use `int'. */
475 || total_size
!= (EMACS_INT
) (int) total_size
)
476 error ("Buffer exceeds maximum size");
479 enlarge_buffer_text (current_buffer
, nbytes_added
);
481 /* Prevent quitting in move_gap. */
486 real_gap_loc_byte
= GPT_BYTE
;
487 old_gap_size
= GAP_SIZE
;
489 /* Call the newly allocated space a gap at the end of the whole space. */
491 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
492 GAP_SIZE
= nbytes_added
;
494 /* Move the new gap down to be consecutive with the end of the old one.
495 This adjusts the markers properly too. */
496 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
498 /* Now combine the two into one large gap. */
499 GAP_SIZE
+= old_gap_size
;
501 GPT_BYTE
= real_gap_loc_byte
;
510 /* Make the gap NBYTES_REMOVED bytes shorter. */
513 make_gap_smaller (EMACS_INT nbytes_removed
)
516 EMACS_INT real_gap_loc
;
517 EMACS_INT real_gap_loc_byte
;
519 EMACS_INT real_Z_byte
;
520 EMACS_INT real_beg_unchanged
;
521 EMACS_INT new_gap_size
;
523 /* Make sure the gap is at least 20 bytes. */
524 if (GAP_SIZE
- nbytes_removed
< 20)
525 nbytes_removed
= GAP_SIZE
- 20;
527 /* Prevent quitting in move_gap. */
532 real_gap_loc_byte
= GPT_BYTE
;
533 new_gap_size
= GAP_SIZE
- nbytes_removed
;
535 real_Z_byte
= Z_BYTE
;
536 real_beg_unchanged
= BEG_UNCHANGED
;
538 /* Pretend that the last unwanted part of the gap is the entire gap,
539 and that the first desired part of the gap is part of the buffer
541 memset (GPT_ADDR
, 0, new_gap_size
);
543 GPT_BYTE
+= new_gap_size
;
545 Z_BYTE
+= new_gap_size
;
546 GAP_SIZE
= nbytes_removed
;
548 /* Move the unwanted pretend gap to the end of the buffer. This
549 adjusts the markers properly too. */
550 gap_right (Z
, Z_BYTE
);
552 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
554 /* Now restore the desired gap. */
555 GAP_SIZE
= new_gap_size
;
557 GPT_BYTE
= real_gap_loc_byte
;
559 Z_BYTE
= real_Z_byte
;
560 BEG_UNCHANGED
= real_beg_unchanged
;
569 make_gap (EMACS_INT nbytes_added
)
571 if (nbytes_added
>= 0)
572 make_gap_larger (nbytes_added
);
573 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
575 make_gap_smaller (-nbytes_added
);
579 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
580 FROM_MULTIBYTE says whether the incoming text is multibyte.
581 TO_MULTIBYTE says whether to store the text as multibyte.
582 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
584 Return the number of bytes stored at TO_ADDR. */
587 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
588 EMACS_INT nbytes
, int from_multibyte
, int to_multibyte
)
590 if (from_multibyte
== to_multibyte
)
592 memcpy (to_addr
, from_addr
, nbytes
);
595 else if (from_multibyte
)
597 EMACS_INT nchars
= 0;
598 EMACS_INT bytes_left
= nbytes
;
599 Lisp_Object tbl
= Qnil
;
601 while (bytes_left
> 0)
604 c
= STRING_CHAR_AND_LENGTH (from_addr
, thislen
);
605 if (! ASCII_CHAR_P (c
))
608 from_addr
+= thislen
;
609 bytes_left
-= thislen
;
616 unsigned char *initial_to_addr
= to_addr
;
618 /* Convert single-byte to multibyte. */
621 int c
= *from_addr
++;
623 if (!ASCII_CHAR_P (c
))
625 c
= BYTE8_TO_CHAR (c
);
626 to_addr
+= CHAR_STRING (c
, to_addr
);
630 /* Special case for speed. */
631 *to_addr
++ = c
, nbytes
--;
633 return to_addr
- initial_to_addr
;
637 /* Return the number of bytes it would take
638 to convert some single-byte text to multibyte.
639 The single-byte text consists of NBYTES bytes at PTR. */
642 count_size_as_multibyte (const unsigned char *ptr
, EMACS_INT nbytes
)
645 EMACS_INT outgoing_nbytes
= 0;
647 for (i
= 0; i
< nbytes
; i
++)
649 unsigned int c
= *ptr
++;
651 if (ASCII_CHAR_P (c
))
655 c
= BYTE8_TO_CHAR (c
);
656 outgoing_nbytes
+= CHAR_BYTES (c
);
660 return outgoing_nbytes
;
663 /* Insert a string of specified length before point.
664 This function judges multibyteness based on
665 enable_multibyte_characters in the current buffer;
666 it never converts between single-byte and multibyte.
668 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
669 prepare_to_modify_buffer could relocate the text. */
672 insert (const unsigned char *string
, EMACS_INT nbytes
)
676 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
677 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
679 signal_after_change (opoint
, 0, len
);
680 update_compositions (opoint
, PT
, CHECK_BORDER
);
684 /* Likewise, but inherit text properties from neighboring characters. */
687 insert_and_inherit (const unsigned char *string
, EMACS_INT nbytes
)
691 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
692 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
694 signal_after_change (opoint
, 0, len
);
695 update_compositions (opoint
, PT
, CHECK_BORDER
);
699 /* Insert the character C before point. Do not inherit text properties. */
704 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
707 if (! NILP (current_buffer
->enable_multibyte_characters
))
708 len
= CHAR_STRING (c
, str
);
718 /* Insert the null-terminated string S before point. */
721 insert_string (const char *s
)
723 insert (s
, strlen (s
));
726 /* Like `insert' except that all markers pointing at the place where
727 the insertion happens are adjusted to point after it.
728 Don't use this function to insert part of a Lisp string,
729 since gc could happen and relocate it. */
732 insert_before_markers (const unsigned char *string
, EMACS_INT nbytes
)
736 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
737 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
739 signal_after_change (opoint
, 0, len
);
740 update_compositions (opoint
, PT
, CHECK_BORDER
);
744 /* Likewise, but inherit text properties from neighboring characters. */
747 insert_before_markers_and_inherit (const unsigned char *string
,
752 EMACS_INT len
= chars_in_text (string
, nbytes
), opoint
;
753 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
755 signal_after_change (opoint
, 0, len
);
756 update_compositions (opoint
, PT
, CHECK_BORDER
);
760 /* Subroutine used by the insert functions above. */
763 insert_1 (const unsigned char *string
, EMACS_INT nbytes
,
764 int inherit
, int prepare
, int before_markers
)
766 insert_1_both (string
, chars_in_text (string
, nbytes
), nbytes
,
767 inherit
, prepare
, before_markers
);
771 #ifdef BYTE_COMBINING_DEBUG
773 /* See if the bytes before POS/POS_BYTE combine with bytes
774 at the start of STRING to form a single character.
775 If so, return the number of bytes at the start of STRING
776 which combine in this way. Otherwise, return 0. */
779 count_combining_before (const unsigned char *string
, EMACS_INT length
,
780 EMACS_INT pos
, EMACS_INT pos_byte
)
782 int len
, combining_bytes
;
783 const unsigned char *p
;
785 if (NILP (current_buffer
->enable_multibyte_characters
))
788 /* At first, we can exclude the following cases:
789 (1) STRING[0] can't be a following byte of multibyte sequence.
790 (2) POS is the start of the current buffer.
791 (3) A character before POS is not a multibyte character. */
792 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
794 if (pos_byte
== BEG_BYTE
) /* case (2) */
797 p
= BYTE_POS_ADDR (pos_byte
- 1);
798 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
799 if (! LEADING_CODE_P (*p
)) /* case (3) */
802 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
803 if (combining_bytes
<= 0)
804 /* The character preceding POS is, complete and no room for
805 combining bytes (combining_bytes == 0), or an independent 8-bit
806 character (combining_bytes < 0). */
809 /* We have a combination situation. Count the bytes at STRING that
812 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
815 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
818 /* See if the bytes after POS/POS_BYTE combine with bytes
819 at the end of STRING to form a single character.
820 If so, return the number of bytes after POS/POS_BYTE
821 which combine in this way. Otherwise, return 0. */
824 count_combining_after (const unsigned char *string
,
825 EMACS_INT length
, EMACS_INT pos
, EMACS_INT pos_byte
)
827 EMACS_INT opos_byte
= pos_byte
;
832 if (NILP (current_buffer
->enable_multibyte_characters
))
835 /* At first, we can exclude the following cases:
836 (1) The last byte of STRING is an ASCII.
837 (2) POS is the last of the current buffer.
838 (3) A character at POS can't be a following byte of multibyte
840 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1])) /* case (1) */
842 if (pos_byte
== Z_BYTE
) /* case (2) */
844 bufp
= BYTE_POS_ADDR (pos_byte
);
845 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
849 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
855 /* All characters in STRING are not character head. We must
856 check also preceding bytes at POS. We are sure that the gap
858 unsigned char *p
= BEG_ADDR
;
860 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
862 if (i
< 0 || !LEADING_CODE_P (p
[i
]))
865 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
866 return (bytes
<= pos_byte
- 1 - i
+ length
868 : bytes
- (pos_byte
- 1 - i
+ length
));
870 if (!LEADING_CODE_P (string
[i
]))
873 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
875 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
877 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
883 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
884 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
885 are the same as in insert_1. */
888 insert_1_both (const unsigned char *string
,
889 EMACS_INT nchars
, EMACS_INT nbytes
,
890 int inherit
, int prepare
, int before_markers
)
895 if (NILP (current_buffer
->enable_multibyte_characters
))
899 /* Do this before moving and increasing the gap,
900 because the before-change hooks might move the gap
901 or make it smaller. */
902 prepare_to_modify_buffer (PT
, PT
, NULL
);
905 move_gap_both (PT
, PT_BYTE
);
906 if (GAP_SIZE
< nbytes
)
907 make_gap (nbytes
- GAP_SIZE
);
909 #ifdef BYTE_COMBINING_DEBUG
910 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
911 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
915 /* Record deletion of the surrounding text that combines with
916 the insertion. This, together with recording the insertion,
917 will add up to the right stuff in the undo list. */
918 record_insert (PT
, nchars
);
920 CHARS_MODIFF
= MODIFF
;
922 memcpy (GPT_ADDR
, string
, nbytes
);
931 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
936 /* The insert may have been in the unchanged region, so check again. */
937 if (Z
- GPT
< END_UNCHANGED
)
938 END_UNCHANGED
= Z
- GPT
;
940 adjust_overlays_for_insert (PT
, nchars
);
941 adjust_markers_for_insert (PT
, PT_BYTE
,
942 PT
+ nchars
, PT_BYTE
+ nbytes
,
945 if (BUF_INTERVALS (current_buffer
) != 0)
946 offset_intervals (current_buffer
, PT
, nchars
);
948 if (!inherit
&& BUF_INTERVALS (current_buffer
) != 0)
949 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
952 adjust_point (nchars
, nbytes
);
957 /* Insert the part of the text of STRING, a Lisp object assumed to be
958 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
959 starting at position POS / POS_BYTE. If the text of STRING has properties,
960 copy them into the buffer.
962 It does not work to use `insert' for this, because a GC could happen
963 before we copy the stuff into the buffer, and relocate the string
964 without insert noticing. */
967 insert_from_string (Lisp_Object string
, EMACS_INT pos
, EMACS_INT pos_byte
,
968 EMACS_INT length
, EMACS_INT length_byte
, int inherit
)
970 EMACS_INT opoint
= PT
;
972 if (SCHARS (string
) == 0)
975 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
977 signal_after_change (opoint
, 0, PT
- opoint
);
978 update_compositions (opoint
, PT
, CHECK_BORDER
);
981 /* Like `insert_from_string' except that all markers pointing
982 at the place where the insertion happens are adjusted to point after it. */
985 insert_from_string_before_markers (Lisp_Object string
,
986 EMACS_INT pos
, EMACS_INT pos_byte
,
987 EMACS_INT length
, EMACS_INT length_byte
,
990 EMACS_INT opoint
= PT
;
992 if (SCHARS (string
) == 0)
995 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
997 signal_after_change (opoint
, 0, PT
- opoint
);
998 update_compositions (opoint
, PT
, CHECK_BORDER
);
1001 /* Subroutine of the insertion functions above. */
1004 insert_from_string_1 (Lisp_Object string
, EMACS_INT pos
, EMACS_INT pos_byte
,
1005 EMACS_INT nchars
, EMACS_INT nbytes
,
1006 int inherit
, int before_markers
)
1008 struct gcpro gcpro1
;
1009 EMACS_INT outgoing_nbytes
= nbytes
;
1012 /* Make OUTGOING_NBYTES describe the text
1013 as it will be inserted in this buffer. */
1015 if (NILP (current_buffer
->enable_multibyte_characters
))
1016 outgoing_nbytes
= nchars
;
1017 else if (! STRING_MULTIBYTE (string
))
1019 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
1023 /* Do this before moving and increasing the gap,
1024 because the before-change hooks might move the gap
1025 or make it smaller. */
1026 prepare_to_modify_buffer (PT
, PT
, NULL
);
1029 move_gap_both (PT
, PT_BYTE
);
1030 if (GAP_SIZE
< outgoing_nbytes
)
1031 make_gap (outgoing_nbytes
- GAP_SIZE
);
1034 /* Copy the string text into the buffer, perhaps converting
1035 between single-byte and multibyte. */
1036 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
1037 STRING_MULTIBYTE (string
),
1038 ! NILP (current_buffer
->enable_multibyte_characters
));
1040 #ifdef BYTE_COMBINING_DEBUG
1041 /* We have copied text into the gap, but we have not altered
1042 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1043 to these functions and get the same results as we would
1044 have got earlier on. Meanwhile, PT_ADDR does point to
1045 the text that has been stored by copy_text. */
1046 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1047 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1051 record_insert (PT
, nchars
);
1053 CHARS_MODIFF
= MODIFF
;
1055 GAP_SIZE
-= outgoing_nbytes
;
1059 GPT_BYTE
+= outgoing_nbytes
;
1060 ZV_BYTE
+= outgoing_nbytes
;
1061 Z_BYTE
+= outgoing_nbytes
;
1062 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1067 /* The insert may have been in the unchanged region, so check again. */
1068 if (Z
- GPT
< END_UNCHANGED
)
1069 END_UNCHANGED
= Z
- GPT
;
1071 adjust_overlays_for_insert (PT
, nchars
);
1072 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1073 PT_BYTE
+ outgoing_nbytes
,
1076 offset_intervals (current_buffer
, PT
, nchars
);
1078 intervals
= STRING_INTERVALS (string
);
1079 /* Get the intervals for the part of the string we are inserting. */
1080 if (nbytes
< SBYTES (string
))
1081 intervals
= copy_intervals (intervals
, pos
, nchars
);
1083 /* Insert those intervals. */
1084 graft_intervals_into_buffer (intervals
, PT
, nchars
,
1085 current_buffer
, inherit
);
1087 adjust_point (nchars
, outgoing_nbytes
);
1092 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
1093 starting at GPT_ADDR. */
1096 insert_from_gap (EMACS_INT nchars
, EMACS_INT nbytes
)
1098 if (NILP (current_buffer
->enable_multibyte_characters
))
1101 record_insert (GPT
, nchars
);
1111 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1116 adjust_overlays_for_insert (GPT
- nchars
, nchars
);
1117 adjust_markers_for_insert (GPT
- nchars
, GPT_BYTE
- nbytes
,
1120 if (BUF_INTERVALS (current_buffer
) != 0)
1122 offset_intervals (current_buffer
, GPT
- nchars
, nchars
);
1123 graft_intervals_into_buffer (NULL_INTERVAL
, GPT
- nchars
, nchars
,
1127 if (GPT
- nchars
< PT
)
1128 adjust_point (nchars
, nbytes
);
1133 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1134 current buffer. If the text in BUF has properties, they are absorbed
1135 into the current buffer.
1137 It does not work to use `insert' for this, because a malloc could happen
1138 and relocate BUF's text before the copy happens. */
1141 insert_from_buffer (struct buffer
*buf
,
1142 EMACS_INT charpos
, EMACS_INT nchars
, int inherit
)
1144 EMACS_INT opoint
= PT
;
1146 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1147 signal_after_change (opoint
, 0, PT
- opoint
);
1148 update_compositions (opoint
, PT
, CHECK_BORDER
);
1152 insert_from_buffer_1 (struct buffer
*buf
,
1153 EMACS_INT from
, EMACS_INT nchars
, int inherit
)
1155 register Lisp_Object temp
;
1156 EMACS_INT chunk
, chunk_expanded
;
1157 EMACS_INT from_byte
= buf_charpos_to_bytepos (buf
, from
);
1158 EMACS_INT to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1159 EMACS_INT incoming_nbytes
= to_byte
- from_byte
;
1160 EMACS_INT outgoing_nbytes
= incoming_nbytes
;
1163 /* Make OUTGOING_NBYTES describe the text
1164 as it will be inserted in this buffer. */
1166 if (NILP (current_buffer
->enable_multibyte_characters
))
1167 outgoing_nbytes
= nchars
;
1168 else if (NILP (buf
->enable_multibyte_characters
))
1170 EMACS_INT outgoing_before_gap
= 0;
1171 EMACS_INT outgoing_after_gap
= 0;
1173 if (from
< BUF_GPT (buf
))
1175 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1176 if (chunk
> incoming_nbytes
)
1177 chunk
= incoming_nbytes
;
1179 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1185 if (chunk
< incoming_nbytes
)
1187 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1189 incoming_nbytes
- chunk
);
1191 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1194 /* Make sure point-max won't overflow after this insertion. */
1195 XSETINT (temp
, outgoing_nbytes
+ Z
);
1196 if (outgoing_nbytes
+ Z
!= XINT (temp
))
1197 error ("Maximum buffer size exceeded");
1199 /* Do this before moving and increasing the gap,
1200 because the before-change hooks might move the gap
1201 or make it smaller. */
1202 prepare_to_modify_buffer (PT
, PT
, NULL
);
1205 move_gap_both (PT
, PT_BYTE
);
1206 if (GAP_SIZE
< outgoing_nbytes
)
1207 make_gap (outgoing_nbytes
- GAP_SIZE
);
1209 if (from
< BUF_GPT (buf
))
1211 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1212 if (chunk
> incoming_nbytes
)
1213 chunk
= incoming_nbytes
;
1214 /* Record number of output bytes, so we know where
1215 to put the output from the second copy_text. */
1217 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1219 ! NILP (buf
->enable_multibyte_characters
),
1220 ! NILP (current_buffer
->enable_multibyte_characters
));
1223 chunk_expanded
= chunk
= 0;
1225 if (chunk
< incoming_nbytes
)
1226 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1227 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1228 ! NILP (buf
->enable_multibyte_characters
),
1229 ! NILP (current_buffer
->enable_multibyte_characters
));
1231 #ifdef BYTE_COMBINING_DEBUG
1232 /* We have copied text into the gap, but we have not altered
1233 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1234 to these functions and get the same results as we would
1235 have got earlier on. Meanwhile, GPT_ADDR does point to
1236 the text that has been stored by copy_text. */
1237 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1238 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1242 record_insert (PT
, nchars
);
1244 CHARS_MODIFF
= MODIFF
;
1246 GAP_SIZE
-= outgoing_nbytes
;
1250 GPT_BYTE
+= outgoing_nbytes
;
1251 ZV_BYTE
+= outgoing_nbytes
;
1252 Z_BYTE
+= outgoing_nbytes
;
1253 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1258 /* The insert may have been in the unchanged region, so check again. */
1259 if (Z
- GPT
< END_UNCHANGED
)
1260 END_UNCHANGED
= Z
- GPT
;
1262 adjust_overlays_for_insert (PT
, nchars
);
1263 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1264 PT_BYTE
+ outgoing_nbytes
,
1267 if (BUF_INTERVALS (current_buffer
) != 0)
1268 offset_intervals (current_buffer
, PT
, nchars
);
1270 /* Get the intervals for the part of the string we are inserting. */
1271 intervals
= BUF_INTERVALS (buf
);
1272 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1274 if (buf
== current_buffer
&& PT
<= from
)
1276 intervals
= copy_intervals (intervals
, from
, nchars
);
1279 /* Insert those intervals. */
1280 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1282 adjust_point (nchars
, outgoing_nbytes
);
1285 /* Record undo information and adjust markers and position keepers for
1286 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1287 chars (LEN_BYTE bytes) which resides in the gap just after
1290 PREV_TEXT nil means the new text was just inserted. */
1293 adjust_after_replace (EMACS_INT from
, EMACS_INT from_byte
,
1294 Lisp_Object prev_text
, EMACS_INT len
, EMACS_INT len_byte
)
1296 EMACS_INT nchars_del
= 0, nbytes_del
= 0;
1298 #ifdef BYTE_COMBINING_DEBUG
1299 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1300 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1304 if (STRINGP (prev_text
))
1306 nchars_del
= SCHARS (prev_text
);
1307 nbytes_del
= SBYTES (prev_text
);
1310 /* Update various buffer positions for the new text. */
1311 GAP_SIZE
-= len_byte
;
1313 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1314 GPT
+= len
; GPT_BYTE
+= len_byte
;
1315 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1318 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1321 adjust_markers_for_insert (from
, from_byte
,
1322 from
+ len
, from_byte
+ len_byte
, 0);
1324 if (! EQ (current_buffer
->undo_list
, Qt
))
1327 record_delete (from
, prev_text
);
1328 record_insert (from
, len
);
1331 if (len
> nchars_del
)
1332 adjust_overlays_for_insert (from
, len
- nchars_del
);
1333 else if (len
< nchars_del
)
1334 adjust_overlays_for_delete (from
, nchars_del
- len
);
1335 if (BUF_INTERVALS (current_buffer
) != 0)
1337 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1341 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1343 /* As byte combining will decrease Z, we must check this again. */
1344 if (Z
- GPT
< END_UNCHANGED
)
1345 END_UNCHANGED
= Z
- GPT
;
1350 evaporate_overlays (from
);
1352 CHARS_MODIFF
= MODIFF
;
1355 /* Like adjust_after_replace, but doesn't require PREV_TEXT.
1356 This is for use when undo is not enabled in the current buffer. */
1359 adjust_after_replace_noundo (EMACS_INT from
, EMACS_INT from_byte
,
1360 EMACS_INT nchars_del
, EMACS_INT nbytes_del
,
1361 EMACS_INT len
, EMACS_INT len_byte
)
1363 #ifdef BYTE_COMBINING_DEBUG
1364 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1365 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1369 /* Update various buffer positions for the new text. */
1370 GAP_SIZE
-= len_byte
;
1372 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1373 GPT
+= len
; GPT_BYTE
+= len_byte
;
1374 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1377 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1380 adjust_markers_for_insert (from
, from_byte
,
1381 from
+ len
, from_byte
+ len_byte
, 0);
1383 if (len
> nchars_del
)
1384 adjust_overlays_for_insert (from
, len
- nchars_del
);
1385 else if (len
< nchars_del
)
1386 adjust_overlays_for_delete (from
, nchars_del
- len
);
1387 if (BUF_INTERVALS (current_buffer
) != 0)
1389 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1393 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1395 /* As byte combining will decrease Z, we must check this again. */
1396 if (Z
- GPT
< END_UNCHANGED
)
1397 END_UNCHANGED
= Z
- GPT
;
1402 evaporate_overlays (from
);
1404 CHARS_MODIFF
= MODIFF
;
1407 /* Record undo information, adjust markers and position keepers for an
1408 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1409 text already exists in the current buffer but character length (TO
1410 - FROM) may be incorrect, the correct length is NEWLEN. */
1413 adjust_after_insert (EMACS_INT from
, EMACS_INT from_byte
,
1414 EMACS_INT to
, EMACS_INT to_byte
, EMACS_INT newlen
)
1416 EMACS_INT len
= to
- from
, len_byte
= to_byte
- from_byte
;
1419 move_gap_both (to
, to_byte
);
1420 GAP_SIZE
+= len_byte
;
1421 GPT
-= len
; GPT_BYTE
-= len_byte
;
1422 ZV
-= len
; ZV_BYTE
-= len_byte
;
1423 Z
-= len
; Z_BYTE
-= len_byte
;
1424 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1427 /* Replace the text from character positions FROM to TO with NEW,
1428 If PREPARE is nonzero, call prepare_to_modify_buffer.
1429 If INHERIT, the newly inserted text should inherit text properties
1430 from the surrounding non-deleted text. */
1432 /* Note that this does not yet handle markers quite right.
1433 Also it needs to record a single undo-entry that does a replacement
1434 rather than a separate delete and insert.
1435 That way, undo will also handle markers properly.
1437 But if MARKERS is 0, don't relocate markers. */
1440 replace_range (EMACS_INT from
, EMACS_INT to
, Lisp_Object
new,
1441 int prepare
, int inherit
, int markers
)
1443 EMACS_INT inschars
= SCHARS (new);
1444 EMACS_INT insbytes
= SBYTES (new);
1445 EMACS_INT from_byte
, to_byte
;
1446 EMACS_INT nbytes_del
, nchars_del
;
1447 register Lisp_Object temp
;
1448 struct gcpro gcpro1
;
1450 EMACS_INT outgoing_insbytes
= insbytes
;
1451 Lisp_Object deletion
;
1460 EMACS_INT range_length
= to
- from
;
1461 prepare_to_modify_buffer (from
, to
, &from
);
1462 to
= from
+ range_length
;
1467 /* Make args be valid */
1473 from_byte
= CHAR_TO_BYTE (from
);
1474 to_byte
= CHAR_TO_BYTE (to
);
1476 nchars_del
= to
- from
;
1477 nbytes_del
= to_byte
- from_byte
;
1479 if (nbytes_del
<= 0 && insbytes
== 0)
1482 /* Make OUTGOING_INSBYTES describe the text
1483 as it will be inserted in this buffer. */
1485 if (NILP (current_buffer
->enable_multibyte_characters
))
1486 outgoing_insbytes
= inschars
;
1487 else if (! STRING_MULTIBYTE (new))
1489 = count_size_as_multibyte (SDATA (new), insbytes
);
1491 /* Make sure point-max won't overflow after this insertion. */
1492 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1493 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1494 error ("Maximum buffer size exceeded");
1498 /* Make sure the gap is somewhere in or next to what we are deleting. */
1500 gap_right (from
, from_byte
);
1502 gap_left (to
, to_byte
, 0);
1504 /* Even if we don't record for undo, we must keep the original text
1505 because we may have to recover it because of inappropriate byte
1507 if (! EQ (current_buffer
->undo_list
, Qt
))
1508 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1510 GAP_SIZE
+= nbytes_del
;
1513 ZV_BYTE
-= nbytes_del
;
1514 Z_BYTE
-= nbytes_del
;
1516 GPT_BYTE
= from_byte
;
1517 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1522 if (GPT
- BEG
< BEG_UNCHANGED
)
1523 BEG_UNCHANGED
= GPT
- BEG
;
1524 if (Z
- GPT
< END_UNCHANGED
)
1525 END_UNCHANGED
= Z
- GPT
;
1527 if (GAP_SIZE
< insbytes
)
1528 make_gap (insbytes
- GAP_SIZE
);
1530 /* Copy the string text into the buffer, perhaps converting
1531 between single-byte and multibyte. */
1532 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1533 STRING_MULTIBYTE (new),
1534 ! NILP (current_buffer
->enable_multibyte_characters
));
1536 #ifdef BYTE_COMBINING_DEBUG
1537 /* We have copied text into the gap, but we have not marked
1538 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1539 here, for both the previous text and the following text.
1540 Meanwhile, GPT_ADDR does point to
1541 the text that has been stored by copy_text. */
1542 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1543 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1547 if (! EQ (current_buffer
->undo_list
, Qt
))
1549 /* Record the insertion first, so that when we undo,
1550 the deletion will be undone first. Thus, undo
1551 will insert before deleting, and thus will keep
1552 the markers before and after this text separate. */
1553 record_insert (from
+ SCHARS (deletion
), inschars
);
1554 record_delete (from
, deletion
);
1557 GAP_SIZE
-= outgoing_insbytes
;
1561 GPT_BYTE
+= outgoing_insbytes
;
1562 ZV_BYTE
+= outgoing_insbytes
;
1563 Z_BYTE
+= outgoing_insbytes
;
1564 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1569 /* Adjust the overlay center as needed. This must be done after
1570 adjusting the markers that bound the overlays. */
1571 adjust_overlays_for_delete (from
, nchars_del
);
1572 adjust_overlays_for_insert (from
, inschars
);
1574 /* Adjust markers for the deletion and the insertion. */
1576 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1577 inschars
, outgoing_insbytes
);
1579 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1581 /* Get the intervals for the part of the string we are inserting--
1582 not including the combined-before bytes. */
1583 intervals
= STRING_INTERVALS (new);
1584 /* Insert those intervals. */
1585 graft_intervals_into_buffer (intervals
, from
, inschars
,
1586 current_buffer
, inherit
);
1588 /* Relocate point as if it were a marker. */
1590 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1591 (from_byte
+ outgoing_insbytes
1592 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1594 if (outgoing_insbytes
== 0)
1595 evaporate_overlays (from
);
1600 CHARS_MODIFF
= MODIFF
;
1603 signal_after_change (from
, nchars_del
, GPT
- from
);
1604 update_compositions (from
, GPT
, CHECK_BORDER
);
1607 /* Replace the text from character positions FROM to TO with
1608 the text in INS of length INSCHARS.
1609 Keep the text properties that applied to the old characters
1610 (extending them to all the new chars if there are more new chars).
1612 Note that this does not yet handle markers quite right.
1614 If MARKERS is nonzero, relocate markers.
1616 Unlike most functions at this level, never call
1617 prepare_to_modify_buffer and never call signal_after_change. */
1620 replace_range_2 (EMACS_INT from
, EMACS_INT from_byte
,
1621 EMACS_INT to
, EMACS_INT to_byte
,
1622 const char *ins
, EMACS_INT inschars
, EMACS_INT insbytes
,
1625 EMACS_INT nbytes_del
, nchars_del
;
1630 nchars_del
= to
- from
;
1631 nbytes_del
= to_byte
- from_byte
;
1633 if (nbytes_del
<= 0 && insbytes
== 0)
1636 /* Make sure point-max won't overflow after this insertion. */
1637 XSETINT (temp
, Z_BYTE
- nbytes_del
+ insbytes
);
1638 if (Z_BYTE
- nbytes_del
+ insbytes
!= XINT (temp
))
1639 error ("Maximum buffer size exceeded");
1641 /* Make sure the gap is somewhere in or next to what we are deleting. */
1643 gap_right (from
, from_byte
);
1645 gap_left (to
, to_byte
, 0);
1647 GAP_SIZE
+= nbytes_del
;
1650 ZV_BYTE
-= nbytes_del
;
1651 Z_BYTE
-= nbytes_del
;
1653 GPT_BYTE
= from_byte
;
1654 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1659 if (GPT
- BEG
< BEG_UNCHANGED
)
1660 BEG_UNCHANGED
= GPT
- BEG
;
1661 if (Z
- GPT
< END_UNCHANGED
)
1662 END_UNCHANGED
= Z
- GPT
;
1664 if (GAP_SIZE
< insbytes
)
1665 make_gap (insbytes
- GAP_SIZE
);
1667 /* Copy the replacement text into the buffer. */
1668 memcpy (GPT_ADDR
, ins
, insbytes
);
1670 #ifdef BYTE_COMBINING_DEBUG
1671 /* We have copied text into the gap, but we have not marked
1672 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1673 here, for both the previous text and the following text.
1674 Meanwhile, GPT_ADDR does point to
1675 the text that has been stored by copy_text. */
1676 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1677 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1681 GAP_SIZE
-= insbytes
;
1685 GPT_BYTE
+= insbytes
;
1686 ZV_BYTE
+= insbytes
;
1688 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1693 /* Adjust the overlay center as needed. This must be done after
1694 adjusting the markers that bound the overlays. */
1695 if (nchars_del
!= inschars
)
1697 adjust_overlays_for_insert (from
, inschars
);
1698 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1701 /* Adjust markers for the deletion and the insertion. */
1703 && ! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1704 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1705 inschars
, insbytes
);
1707 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1709 /* Relocate point as if it were a marker. */
1710 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1713 /* PT was within the deleted text. Move it to FROM. */
1714 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1716 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1720 evaporate_overlays (from
);
1725 CHARS_MODIFF
= MODIFF
;
1728 /* Delete characters in current buffer
1729 from FROM up to (but not including) TO.
1730 If TO comes before FROM, we delete nothing. */
1733 del_range (EMACS_INT from
, EMACS_INT to
)
1735 del_range_1 (from
, to
, 1, 0);
1738 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1739 RET_STRING says to return the deleted text. */
1742 del_range_1 (EMACS_INT from
, EMACS_INT to
, int prepare
, int ret_string
)
1744 EMACS_INT from_byte
, to_byte
;
1745 Lisp_Object deletion
;
1746 struct gcpro gcpro1
;
1748 /* Make args be valid */
1759 EMACS_INT range_length
= to
- from
;
1760 prepare_to_modify_buffer (from
, to
, &from
);
1761 to
= min (ZV
, from
+ range_length
);
1764 from_byte
= CHAR_TO_BYTE (from
);
1765 to_byte
= CHAR_TO_BYTE (to
);
1767 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1769 signal_after_change (from
, to
- from
, 0);
1770 update_compositions (from
, from
, CHECK_HEAD
);
1775 /* Like del_range_1 but args are byte positions, not char positions. */
1778 del_range_byte (EMACS_INT from_byte
, EMACS_INT to_byte
, int prepare
)
1782 /* Make args be valid */
1783 if (from_byte
< BEGV_BYTE
)
1784 from_byte
= BEGV_BYTE
;
1785 if (to_byte
> ZV_BYTE
)
1788 if (to_byte
<= from_byte
)
1791 from
= BYTE_TO_CHAR (from_byte
);
1792 to
= BYTE_TO_CHAR (to_byte
);
1796 EMACS_INT old_from
= from
, old_to
= Z
- to
;
1797 EMACS_INT range_length
= to
- from
;
1798 prepare_to_modify_buffer (from
, to
, &from
);
1799 to
= from
+ range_length
;
1801 if (old_from
!= from
)
1802 from_byte
= CHAR_TO_BYTE (from
);
1808 else if (old_to
== Z
- to
)
1809 to_byte
= CHAR_TO_BYTE (to
);
1812 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1813 signal_after_change (from
, to
- from
, 0);
1814 update_compositions (from
, from
, CHECK_HEAD
);
1817 /* Like del_range_1, but positions are specified both as charpos
1821 del_range_both (EMACS_INT from
, EMACS_INT from_byte
,
1822 EMACS_INT to
, EMACS_INT to_byte
, int prepare
)
1824 /* Make args be valid */
1825 if (from_byte
< BEGV_BYTE
)
1826 from_byte
= BEGV_BYTE
;
1827 if (to_byte
> ZV_BYTE
)
1830 if (to_byte
<= from_byte
)
1840 EMACS_INT old_from
= from
, old_to
= Z
- to
;
1841 EMACS_INT range_length
= to
- from
;
1842 prepare_to_modify_buffer (from
, to
, &from
);
1843 to
= from
+ range_length
;
1845 if (old_from
!= from
)
1846 from_byte
= CHAR_TO_BYTE (from
);
1852 else if (old_to
== Z
- to
)
1853 to_byte
= CHAR_TO_BYTE (to
);
1856 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1857 signal_after_change (from
, to
- from
, 0);
1858 update_compositions (from
, from
, CHECK_HEAD
);
1861 /* Delete a range of text, specified both as character positions
1862 and byte positions. FROM and TO are character positions,
1863 while FROM_BYTE and TO_BYTE are byte positions.
1864 If RET_STRING is true, the deleted area is returned as a string. */
1867 del_range_2 (EMACS_INT from
, EMACS_INT from_byte
,
1868 EMACS_INT to
, EMACS_INT to_byte
, int ret_string
)
1870 register EMACS_INT nbytes_del
, nchars_del
;
1871 Lisp_Object deletion
;
1875 nchars_del
= to
- from
;
1876 nbytes_del
= to_byte
- from_byte
;
1878 /* Make sure the gap is somewhere in or next to what we are deleting. */
1880 gap_right (from
, from_byte
);
1882 gap_left (to
, to_byte
, 0);
1884 #ifdef BYTE_COMBINING_DEBUG
1885 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1886 Z_BYTE
- to_byte
, from
, from_byte
))
1890 if (ret_string
|| ! EQ (current_buffer
->undo_list
, Qt
))
1891 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1895 /* Relocate all markers pointing into the new, larger gap
1896 to point at the end of the text before the gap.
1897 Do this before recording the deletion,
1898 so that undo handles this after reinserting the text. */
1899 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1901 if (! EQ (current_buffer
->undo_list
, Qt
))
1902 record_delete (from
, deletion
);
1904 CHARS_MODIFF
= MODIFF
;
1906 /* Relocate point as if it were a marker. */
1908 adjust_point (from
- (PT
< to
? PT
: to
),
1909 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1911 offset_intervals (current_buffer
, from
, - nchars_del
);
1913 /* Adjust the overlay center as needed. This must be done after
1914 adjusting the markers that bound the overlays. */
1915 adjust_overlays_for_delete (from
, nchars_del
);
1917 GAP_SIZE
+= nbytes_del
;
1918 ZV_BYTE
-= nbytes_del
;
1919 Z_BYTE
-= nbytes_del
;
1923 GPT_BYTE
= from_byte
;
1924 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1925 /* Put an anchor, unless called from decode_coding_object which
1926 needs to access the previous gap contents. */
1932 if (GPT
- BEG
< BEG_UNCHANGED
)
1933 BEG_UNCHANGED
= GPT
- BEG
;
1934 if (Z
- GPT
< END_UNCHANGED
)
1935 END_UNCHANGED
= Z
- GPT
;
1939 evaporate_overlays (from
);
1944 /* Call this if you're about to change the region of BUFFER from
1945 character positions START to END. This checks the read-only
1946 properties of the region, calls the necessary modification hooks,
1947 and warns the next redisplay that it should pay attention to that
1950 If PRESERVE_CHARS_MODIFF is non-zero, do not update CHARS_MODIFF.
1951 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1954 modify_region (struct buffer
*buffer
, EMACS_INT start
, EMACS_INT end
,
1955 int preserve_chars_modiff
)
1957 struct buffer
*old_buffer
= current_buffer
;
1959 if (buffer
!= old_buffer
)
1960 set_buffer_internal (buffer
);
1962 prepare_to_modify_buffer (start
, end
, NULL
);
1964 BUF_COMPUTE_UNCHANGED (buffer
, start
- 1, end
);
1966 if (MODIFF
<= SAVE_MODIFF
)
1967 record_first_change ();
1969 if (! preserve_chars_modiff
)
1970 CHARS_MODIFF
= MODIFF
;
1972 buffer
->point_before_scroll
= Qnil
;
1974 if (buffer
!= old_buffer
)
1975 set_buffer_internal (old_buffer
);
1978 /* Check that it is okay to modify the buffer between START and END,
1979 which are char positions.
1981 Run the before-change-function, if any. If intervals are in use,
1982 verify that the text to be modified is not read-only, and call
1983 any modification properties the text may have.
1985 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1986 by holding its value temporarily in a marker. */
1989 prepare_to_modify_buffer (EMACS_INT start
, EMACS_INT end
,
1990 EMACS_INT
*preserve_ptr
)
1992 struct buffer
*base_buffer
;
1994 if (!NILP (current_buffer
->read_only
))
1995 Fbarf_if_buffer_read_only ();
1997 /* Let redisplay consider other windows than selected_window
1998 if modifying another buffer. */
1999 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
2000 ++windows_or_buffers_changed
;
2002 if (BUF_INTERVALS (current_buffer
) != 0)
2006 Lisp_Object preserve_marker
;
2007 struct gcpro gcpro1
;
2008 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
2009 GCPRO1 (preserve_marker
);
2010 verify_interval_modification (current_buffer
, start
, end
);
2011 *preserve_ptr
= marker_position (preserve_marker
);
2012 unchain_marker (XMARKER (preserve_marker
));
2016 verify_interval_modification (current_buffer
, start
, end
);
2019 /* For indirect buffers, use the base buffer to check clashes. */
2020 if (current_buffer
->base_buffer
!= 0)
2021 base_buffer
= current_buffer
->base_buffer
;
2023 base_buffer
= current_buffer
;
2025 #ifdef CLASH_DETECTION
2026 if (!NILP (base_buffer
->file_truename
)
2027 /* Make binding buffer-file-name to nil effective. */
2028 && !NILP (base_buffer
->filename
)
2029 && SAVE_MODIFF
>= MODIFF
)
2030 lock_file (base_buffer
->file_truename
);
2032 /* At least warn if this file has changed on disk since it was visited. */
2033 if (!NILP (base_buffer
->filename
)
2034 && SAVE_MODIFF
>= MODIFF
2035 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
2036 && !NILP (Ffile_exists_p (base_buffer
->filename
)))
2037 call1 (intern ("ask-user-about-supersession-threat"),
2038 base_buffer
->filename
);
2039 #endif /* not CLASH_DETECTION */
2041 /* If `select-active-regions' is non-nil, save the region text. */
2042 if (!NILP (current_buffer
->mark_active
)
2043 && !inhibit_modification_hooks
2044 && XMARKER (current_buffer
->mark
)->buffer
2045 && NILP (Vsaved_region_selection
)
2046 && (EQ (Vselect_active_regions
, Qonly
)
2047 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
2048 : (!NILP (Vselect_active_regions
)
2049 && !NILP (Vtransient_mark_mode
))))
2051 EMACS_INT b
= XMARKER (current_buffer
->mark
)->charpos
;
2054 Vsaved_region_selection
= make_buffer_string (b
, e
, 0);
2056 Vsaved_region_selection
= make_buffer_string (e
, b
, 0);
2059 signal_before_change (start
, end
, preserve_ptr
);
2061 if (current_buffer
->newline_cache
)
2062 invalidate_region_cache (current_buffer
,
2063 current_buffer
->newline_cache
,
2064 start
- BEG
, Z
- end
);
2065 if (current_buffer
->width_run_cache
)
2066 invalidate_region_cache (current_buffer
,
2067 current_buffer
->width_run_cache
,
2068 start
- BEG
, Z
- end
);
2070 Vdeactivate_mark
= Qt
;
2073 /* These macros work with an argument named `preserve_ptr'
2074 and a local variable named `preserve_marker'. */
2076 #define PRESERVE_VALUE \
2077 if (preserve_ptr && NILP (preserve_marker)) \
2078 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
2080 #define RESTORE_VALUE \
2081 if (! NILP (preserve_marker)) \
2083 *preserve_ptr = marker_position (preserve_marker); \
2084 unchain_marker (XMARKER (preserve_marker)); \
2087 #define PRESERVE_START_END \
2088 if (NILP (start_marker)) \
2089 start_marker = Fcopy_marker (start, Qnil); \
2090 if (NILP (end_marker)) \
2091 end_marker = Fcopy_marker (end, Qnil);
2093 #define FETCH_START \
2094 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
2097 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
2099 /* Set a variable to nil if an error occurred.
2100 Don't change the variable if there was no error.
2101 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
2102 VARIABLE is the variable to maybe set to nil.
2103 NO-ERROR-FLAG is nil if there was an error,
2104 anything else meaning no error (so this function does nothing). */
2106 reset_var_on_error (Lisp_Object val
)
2108 if (NILP (XCDR (val
)))
2109 Fset (XCAR (val
), Qnil
);
2113 /* Signal a change to the buffer immediately before it happens.
2114 START_INT and END_INT are the bounds of the text to be changed.
2116 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
2117 by holding its value temporarily in a marker. */
2120 signal_before_change (EMACS_INT start_int
, EMACS_INT end_int
,
2121 EMACS_INT
*preserve_ptr
)
2123 Lisp_Object start
, end
;
2124 Lisp_Object start_marker
, end_marker
;
2125 Lisp_Object preserve_marker
;
2126 struct gcpro gcpro1
, gcpro2
, gcpro3
;
2127 int count
= SPECPDL_INDEX ();
2129 if (inhibit_modification_hooks
)
2132 start
= make_number (start_int
);
2133 end
= make_number (end_int
);
2134 preserve_marker
= Qnil
;
2135 start_marker
= Qnil
;
2137 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
2139 specbind (Qinhibit_modification_hooks
, Qt
);
2141 /* If buffer is unmodified, run a special hook for that case. */
2142 if (SAVE_MODIFF
>= MODIFF
2143 && !NILP (Vfirst_change_hook
)
2144 && !NILP (Vrun_hooks
))
2148 call1 (Vrun_hooks
, Qfirst_change_hook
);
2151 /* Now run the before-change-functions if any. */
2152 if (!NILP (Vbefore_change_functions
))
2154 Lisp_Object args
[3];
2155 Lisp_Object rvoe_arg
= Fcons (Qbefore_change_functions
, Qnil
);
2160 /* Mark before-change-functions to be reset to nil in case of error. */
2161 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2163 /* Actually run the hook functions. */
2164 args
[0] = Qbefore_change_functions
;
2165 args
[1] = FETCH_START
;
2166 args
[2] = FETCH_END
;
2167 Frun_hook_with_args (3, args
);
2169 /* There was no error: unarm the reset_on_error. */
2170 XSETCDR (rvoe_arg
, Qt
);
2173 if (current_buffer
->overlays_before
|| current_buffer
->overlays_after
)
2176 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2177 FETCH_START
, FETCH_END
, Qnil
);
2180 if (! NILP (start_marker
))
2181 free_marker (start_marker
);
2182 if (! NILP (end_marker
))
2183 free_marker (end_marker
);
2187 unbind_to (count
, Qnil
);
2190 /* Signal a change immediately after it happens.
2191 CHARPOS is the character position of the start of the changed text.
2192 LENDEL is the number of characters of the text before the change.
2193 (Not the whole buffer; just the part that was changed.)
2194 LENINS is the number of characters in that part of the text
2195 after the change. */
2198 signal_after_change (EMACS_INT charpos
, EMACS_INT lendel
, EMACS_INT lenins
)
2200 int count
= SPECPDL_INDEX ();
2201 if (inhibit_modification_hooks
)
2204 /* If we are deferring calls to the after-change functions
2205 and there are no before-change functions,
2206 just record the args that we were going to use. */
2207 if (! NILP (Vcombine_after_change_calls
)
2208 && NILP (Vbefore_change_functions
)
2209 && !current_buffer
->overlays_before
2210 && !current_buffer
->overlays_after
)
2214 if (!NILP (combine_after_change_list
)
2215 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2216 Fcombine_after_change_execute ();
2218 elt
= Fcons (make_number (charpos
- BEG
),
2219 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2220 Fcons (make_number (lenins
- lendel
), Qnil
)));
2221 combine_after_change_list
2222 = Fcons (elt
, combine_after_change_list
);
2223 combine_after_change_buffer
= Fcurrent_buffer ();
2228 if (!NILP (combine_after_change_list
))
2229 Fcombine_after_change_execute ();
2231 specbind (Qinhibit_modification_hooks
, Qt
);
2233 if (!NILP (Vafter_change_functions
))
2235 Lisp_Object args
[4];
2236 Lisp_Object rvoe_arg
= Fcons (Qafter_change_functions
, Qnil
);
2238 /* Mark after-change-functions to be reset to nil in case of error. */
2239 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2241 /* Actually run the hook functions. */
2242 args
[0] = Qafter_change_functions
;
2243 XSETFASTINT (args
[1], charpos
);
2244 XSETFASTINT (args
[2], charpos
+ lenins
);
2245 XSETFASTINT (args
[3], lendel
);
2246 Frun_hook_with_args (4, args
);
2248 /* There was no error: unarm the reset_on_error. */
2249 XSETCDR (rvoe_arg
, Qt
);
2252 if (current_buffer
->overlays_before
|| current_buffer
->overlays_after
)
2253 report_overlay_modification (make_number (charpos
),
2254 make_number (charpos
+ lenins
),
2256 make_number (charpos
),
2257 make_number (charpos
+ lenins
),
2258 make_number (lendel
));
2260 /* After an insertion, call the text properties
2261 insert-behind-hooks or insert-in-front-hooks. */
2263 report_interval_modification (make_number (charpos
),
2264 make_number (charpos
+ lenins
));
2266 unbind_to (count
, Qnil
);
2270 Fcombine_after_change_execute_1 (Lisp_Object val
)
2272 Vcombine_after_change_calls
= val
;
2276 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2277 Scombine_after_change_execute
, 0, 0, 0,
2278 doc
: /* This function is for use internally in `combine-after-change-calls'. */)
2281 int count
= SPECPDL_INDEX ();
2282 EMACS_INT beg
, end
, change
;
2283 EMACS_INT begpos
, endpos
;
2286 if (NILP (combine_after_change_list
))
2289 /* It is rare for combine_after_change_buffer to be invalid, but
2290 possible. It can happen when combine-after-change-calls is
2291 non-nil, and insertion calls a file handler (e.g. through
2292 lock_file) which scribbles into a temp file -- cyd */
2293 if (!BUFFERP (combine_after_change_buffer
)
2294 || NILP (XBUFFER (combine_after_change_buffer
)->name
))
2296 combine_after_change_list
= Qnil
;
2300 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2302 Fset_buffer (combine_after_change_buffer
);
2304 /* # chars unchanged at beginning of buffer. */
2306 /* # chars unchanged at end of buffer. */
2308 /* Total amount of insertion (negative for deletion). */
2311 /* Scan the various individual changes,
2312 accumulating the range info in BEG, END and CHANGE. */
2313 for (tail
= combine_after_change_list
; CONSP (tail
);
2317 EMACS_INT thisbeg
, thisend
, thischange
;
2319 /* Extract the info from the next element. */
2323 thisbeg
= XINT (XCAR (elt
));
2328 thisend
= XINT (XCAR (elt
));
2333 thischange
= XINT (XCAR (elt
));
2335 /* Merge this range into the accumulated range. */
2336 change
+= thischange
;
2343 /* Get the current start and end positions of the range
2344 that was changed. */
2348 /* We are about to handle these, so discard them. */
2349 combine_after_change_list
= Qnil
;
2351 /* Now run the after-change functions for real.
2352 Turn off the flag that defers them. */
2353 record_unwind_protect (Fcombine_after_change_execute_1
,
2354 Vcombine_after_change_calls
);
2355 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2356 update_compositions (begpos
, endpos
, CHECK_ALL
);
2358 return unbind_to (count
, Qnil
);
2362 syms_of_insdel (void)
2364 staticpro (&combine_after_change_list
);
2365 staticpro (&combine_after_change_buffer
);
2366 combine_after_change_list
= Qnil
;
2367 combine_after_change_buffer
= Qnil
;
2369 DEFVAR_BOOL ("check-markers-debug-flag", check_markers_debug_flag
,
2370 doc
: /* Non-nil means enable debugging checks for invalid marker positions. */);
2371 check_markers_debug_flag
= 0;
2372 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls
,
2373 doc
: /* Used internally by the `combine-after-change-calls' macro. */);
2374 Vcombine_after_change_calls
= Qnil
;
2376 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks
,
2377 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2378 This affects `before-change-functions' and `after-change-functions',
2379 as well as hooks attached to text properties and overlays. */);
2380 inhibit_modification_hooks
= 0;
2381 Qinhibit_modification_hooks
= intern_c_string ("inhibit-modification-hooks");
2382 staticpro (&Qinhibit_modification_hooks
);
2384 defsubr (&Scombine_after_change_execute
);