1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2012
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 #include "intervals.h"
27 #include "character.h"
30 #include "blockinput.h"
31 #include "region-cache.h"
33 static void insert_from_string_1 (Lisp_Object
, ptrdiff_t, ptrdiff_t, ptrdiff_t,
34 ptrdiff_t, bool, bool);
35 static void insert_from_buffer_1 (struct buffer
*, ptrdiff_t, ptrdiff_t, bool);
36 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
37 static void gap_right (ptrdiff_t, ptrdiff_t);
39 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
40 describing changes which happened while combine_after_change_calls
41 was non-nil. We use this to decide how to call them
42 once the deferral ends.
45 BEG-UNCHANGED is the number of chars before the changed range.
46 END-UNCHANGED is the number of chars after the changed range,
47 and CHANGE-AMOUNT is the number of characters inserted by the change
48 (negative for a deletion). */
49 static Lisp_Object combine_after_change_list
;
51 /* Buffer which combine_after_change_list is about. */
52 static Lisp_Object combine_after_change_buffer
;
54 Lisp_Object Qinhibit_modification_hooks
;
56 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
58 /* Also used in marker.c to enable expensive marker checks. */
65 struct Lisp_Marker
*tail
;
66 bool multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
68 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
70 if (tail
->buffer
->text
!= current_buffer
->text
)
72 if (tail
->charpos
> Z
)
74 if (tail
->bytepos
> Z_BYTE
)
76 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (tail
->bytepos
)))
81 #else /* not MARKER_DEBUG */
83 #define check_markers() do { } while (0)
85 #endif /* MARKER_DEBUG */
87 /* Move gap to position CHARPOS.
88 Note that this can quit! */
91 move_gap (ptrdiff_t charpos
)
93 move_gap_both (charpos
, charpos_to_bytepos (charpos
));
96 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
97 Note that this can quit! */
100 move_gap_both (ptrdiff_t charpos
, ptrdiff_t bytepos
)
102 if (bytepos
< GPT_BYTE
)
103 gap_left (charpos
, bytepos
, 0);
104 else if (bytepos
> GPT_BYTE
)
105 gap_right (charpos
, bytepos
);
108 /* Move the gap to a position less than the current GPT.
109 BYTEPOS describes the new position as a byte position,
110 and CHARPOS is the corresponding char position.
111 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
114 gap_left (ptrdiff_t charpos
, ptrdiff_t bytepos
, bool newgap
)
116 unsigned char *to
, *from
;
121 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
128 /* Now copy the characters. To move the gap down,
129 copy characters up. */
133 /* I gets number of characters left to copy. */
134 i
= new_s1
- bytepos
;
137 /* If a quit is requested, stop copying now.
138 Change BYTEPOS to be where we have actually moved the gap to. */
142 charpos
= BYTE_TO_CHAR (bytepos
);
145 /* Move at most 32000 chars before checking again for a quit. */
150 memmove (to
, from
, i
);
153 /* Adjust buffer data structure, to put the gap at BYTEPOS.
154 BYTEPOS is where the loop above stopped, which may be what
155 was specified or may be where a quit was detected. */
158 eassert (charpos
<= bytepos
);
159 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
163 /* Move the gap to a position greater than the current GPT.
164 BYTEPOS describes the new position as a byte position,
165 and CHARPOS is the corresponding char position. */
168 gap_right (ptrdiff_t charpos
, ptrdiff_t bytepos
)
170 register unsigned char *to
, *from
;
171 register ptrdiff_t i
;
174 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
181 /* Now copy the characters. To move the gap up,
182 copy characters down. */
186 /* I gets number of characters left to copy. */
187 i
= bytepos
- new_s1
;
190 /* If a quit is requested, stop copying now.
191 Change BYTEPOS to be where we have actually moved the gap to. */
195 charpos
= BYTE_TO_CHAR (bytepos
);
198 /* Move at most 32000 chars before checking again for a quit. */
202 memmove (to
, from
, i
);
208 eassert (charpos
<= bytepos
);
209 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
213 /* Adjust all markers for a deletion
214 whose range in bytes is FROM_BYTE to TO_BYTE.
215 The range in charpos is FROM to TO.
217 This function assumes that the gap is adjacent to
218 or inside of the range being deleted. */
221 adjust_markers_for_delete (ptrdiff_t from
, ptrdiff_t from_byte
,
222 ptrdiff_t to
, ptrdiff_t to_byte
)
225 register struct Lisp_Marker
*m
;
226 register ptrdiff_t charpos
;
228 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
230 charpos
= m
->charpos
;
231 eassert (charpos
<= Z
);
233 /* If the marker is after the deletion,
234 relocate by number of chars / bytes deleted. */
237 m
->charpos
-= to
- from
;
238 m
->bytepos
-= to_byte
- from_byte
;
240 /* Here's the case where a marker is inside text being deleted. */
241 else if (charpos
> from
)
243 if (! m
->insertion_type
)
244 { /* Normal markers will end up at the beginning of the
245 re-inserted text after undoing a deletion, and must be
246 adjusted to move them to the correct place. */
247 XSETMISC (marker
, m
);
248 record_marker_adjustment (marker
, from
- charpos
);
250 else if (charpos
< to
)
251 { /* Before-insertion markers will automatically move forward
252 upon re-inserting the deleted text, so we have to arrange
253 for them to move backward to the correct position. */
254 XSETMISC (marker
, m
);
255 record_marker_adjustment (marker
, to
- charpos
);
258 m
->bytepos
= from_byte
;
260 /* Here's the case where a before-insertion marker is immediately
261 before the deleted region. */
262 else if (charpos
== from
&& m
->insertion_type
)
264 /* Undoing the change uses normal insertion, which will
265 incorrectly make MARKER move forward, so we arrange for it
266 to then move backward to the correct place at the beginning
267 of the deleted region. */
268 XSETMISC (marker
, m
);
269 record_marker_adjustment (marker
, to
- from
);
275 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
276 to TO / TO_BYTE. We have to relocate the charpos of every marker
277 that points after the insertion (but not their bytepos).
279 When a marker points at the insertion point,
280 we advance it if either its insertion-type is t
281 or BEFORE_MARKERS is true. */
284 adjust_markers_for_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
285 ptrdiff_t to
, ptrdiff_t to_byte
, bool before_markers
)
287 struct Lisp_Marker
*m
;
289 ptrdiff_t nchars
= to
- from
;
290 ptrdiff_t nbytes
= to_byte
- from_byte
;
292 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
294 eassert (m
->bytepos
>= m
->charpos
295 && m
->bytepos
- m
->charpos
<= Z_BYTE
- Z
);
297 if (m
->bytepos
== from_byte
)
299 if (m
->insertion_type
|| before_markers
)
301 m
->bytepos
= to_byte
;
303 if (m
->insertion_type
)
307 else if (m
->bytepos
> from_byte
)
309 m
->bytepos
+= nbytes
;
310 m
->charpos
+= nchars
;
314 /* Adjusting only markers whose insertion-type is t may result in
315 - disordered start and end in overlays, and
316 - disordered overlays in the slot `overlays_before' of current_buffer. */
319 fix_start_end_in_overlays (from
, to
);
320 fix_overlays_before (current_buffer
, from
, to
);
324 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
326 This is used only when the value of point changes due to an insert
327 or delete; it does not represent a conceptual change in point as a
328 marker. In particular, point is not crossing any interval
329 boundaries, so there's no need to use the usual SET_PT macro. In
330 fact it would be incorrect to do so, because either the old or the
331 new value of point is out of sync with the current set of
335 adjust_point (ptrdiff_t nchars
, ptrdiff_t nbytes
)
337 SET_BUF_PT_BOTH (current_buffer
, PT
+ nchars
, PT_BYTE
+ nbytes
);
338 /* In a single-byte buffer, the two positions must be equal. */
339 eassert (PT_BYTE
>= PT
&& PT_BYTE
- PT
<= ZV_BYTE
- ZV
);
342 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
343 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
344 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
348 adjust_markers_for_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
349 ptrdiff_t old_chars
, ptrdiff_t old_bytes
,
350 ptrdiff_t new_chars
, ptrdiff_t new_bytes
)
352 register struct Lisp_Marker
*m
;
353 ptrdiff_t prev_to_byte
= from_byte
+ old_bytes
;
354 ptrdiff_t diff_chars
= new_chars
- old_chars
;
355 ptrdiff_t diff_bytes
= new_bytes
- old_bytes
;
357 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
359 if (m
->bytepos
>= prev_to_byte
)
361 m
->charpos
+= diff_chars
;
362 m
->bytepos
+= diff_bytes
;
364 else if (m
->bytepos
> from_byte
)
367 m
->bytepos
= from_byte
;
376 buffer_overflow (void)
378 error ("Maximum buffer size exceeded");
381 /* Make the gap NBYTES_ADDED bytes longer. */
384 make_gap_larger (ptrdiff_t nbytes_added
)
387 ptrdiff_t real_gap_loc
;
388 ptrdiff_t real_gap_loc_byte
;
389 ptrdiff_t old_gap_size
;
390 ptrdiff_t current_size
= Z_BYTE
- BEG_BYTE
+ GAP_SIZE
;
391 enum { enough_for_a_while
= 2000 };
393 if (BUF_BYTES_MAX
- current_size
< nbytes_added
)
396 /* If we have to get more space, get enough to last a while;
397 but do not exceed the maximum buffer size. */
398 nbytes_added
= min (nbytes_added
+ enough_for_a_while
,
399 BUF_BYTES_MAX
- current_size
);
401 enlarge_buffer_text (current_buffer
, nbytes_added
);
403 /* Prevent quitting in move_gap. */
408 real_gap_loc_byte
= GPT_BYTE
;
409 old_gap_size
= GAP_SIZE
;
411 /* Call the newly allocated space a gap at the end of the whole space. */
413 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
414 GAP_SIZE
= nbytes_added
;
416 /* Move the new gap down to be consecutive with the end of the old one.
417 This adjusts the markers properly too. */
418 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
420 /* Now combine the two into one large gap. */
421 GAP_SIZE
+= old_gap_size
;
423 GPT_BYTE
= real_gap_loc_byte
;
431 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
433 /* Make the gap NBYTES_REMOVED bytes shorter. */
436 make_gap_smaller (ptrdiff_t nbytes_removed
)
439 ptrdiff_t real_gap_loc
;
440 ptrdiff_t real_gap_loc_byte
;
442 ptrdiff_t real_Z_byte
;
443 ptrdiff_t real_beg_unchanged
;
444 ptrdiff_t new_gap_size
;
446 /* Make sure the gap is at least 20 bytes. */
447 if (GAP_SIZE
- nbytes_removed
< 20)
448 nbytes_removed
= GAP_SIZE
- 20;
450 /* Prevent quitting in move_gap. */
455 real_gap_loc_byte
= GPT_BYTE
;
456 new_gap_size
= GAP_SIZE
- nbytes_removed
;
458 real_Z_byte
= Z_BYTE
;
459 real_beg_unchanged
= BEG_UNCHANGED
;
461 /* Pretend that the last unwanted part of the gap is the entire gap,
462 and that the first desired part of the gap is part of the buffer
464 memset (GPT_ADDR
, 0, new_gap_size
);
466 GPT_BYTE
+= new_gap_size
;
468 Z_BYTE
+= new_gap_size
;
469 GAP_SIZE
= nbytes_removed
;
471 /* Move the unwanted pretend gap to the end of the buffer. This
472 adjusts the markers properly too. */
473 gap_right (Z
, Z_BYTE
);
475 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
477 /* Now restore the desired gap. */
478 GAP_SIZE
= new_gap_size
;
480 GPT_BYTE
= real_gap_loc_byte
;
482 Z_BYTE
= real_Z_byte
;
483 BEG_UNCHANGED
= real_beg_unchanged
;
491 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
494 make_gap (ptrdiff_t nbytes_added
)
496 if (nbytes_added
>= 0)
497 make_gap_larger (nbytes_added
);
498 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
500 make_gap_smaller (-nbytes_added
);
504 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
505 FROM_MULTIBYTE says whether the incoming text is multibyte.
506 TO_MULTIBYTE says whether to store the text as multibyte.
507 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
509 Return the number of bytes stored at TO_ADDR. */
512 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
513 ptrdiff_t nbytes
, bool from_multibyte
, bool to_multibyte
)
515 if (from_multibyte
== to_multibyte
)
517 memcpy (to_addr
, from_addr
, nbytes
);
520 else if (from_multibyte
)
522 ptrdiff_t nchars
= 0;
523 ptrdiff_t bytes_left
= nbytes
;
525 while (bytes_left
> 0)
528 c
= STRING_CHAR_AND_LENGTH (from_addr
, thislen
);
529 if (! ASCII_CHAR_P (c
))
532 from_addr
+= thislen
;
533 bytes_left
-= thislen
;
540 unsigned char *initial_to_addr
= to_addr
;
542 /* Convert single-byte to multibyte. */
545 int c
= *from_addr
++;
547 if (!ASCII_CHAR_P (c
))
549 c
= BYTE8_TO_CHAR (c
);
550 to_addr
+= CHAR_STRING (c
, to_addr
);
554 /* Special case for speed. */
555 *to_addr
++ = c
, nbytes
--;
557 return to_addr
- initial_to_addr
;
561 /* Insert a string of specified length before point.
562 This function judges multibyteness based on
563 enable_multibyte_characters in the current buffer;
564 it never converts between single-byte and multibyte.
566 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
567 prepare_to_modify_buffer could relocate the text. */
570 insert (const char *string
, ptrdiff_t nbytes
)
574 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
575 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
577 signal_after_change (opoint
, 0, len
);
578 update_compositions (opoint
, PT
, CHECK_BORDER
);
582 /* Likewise, but inherit text properties from neighboring characters. */
585 insert_and_inherit (const char *string
, ptrdiff_t nbytes
)
589 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
590 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
592 signal_after_change (opoint
, 0, len
);
593 update_compositions (opoint
, PT
, CHECK_BORDER
);
597 /* Insert the character C before point. Do not inherit text properties. */
602 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
605 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
606 len
= CHAR_STRING (c
, str
);
613 insert ((char *) str
, len
);
616 /* Insert the null-terminated string S before point. */
619 insert_string (const char *s
)
621 insert (s
, strlen (s
));
624 /* Like `insert' except that all markers pointing at the place where
625 the insertion happens are adjusted to point after it.
626 Don't use this function to insert part of a Lisp string,
627 since gc could happen and relocate it. */
630 insert_before_markers (const char *string
, ptrdiff_t nbytes
)
634 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
635 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
637 signal_after_change (opoint
, 0, len
);
638 update_compositions (opoint
, PT
, CHECK_BORDER
);
642 /* Likewise, but inherit text properties from neighboring characters. */
645 insert_before_markers_and_inherit (const char *string
,
650 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
651 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
653 signal_after_change (opoint
, 0, len
);
654 update_compositions (opoint
, PT
, CHECK_BORDER
);
658 /* Subroutine used by the insert functions above. */
661 insert_1 (const char *string
, ptrdiff_t nbytes
,
662 bool inherit
, bool prepare
, bool before_markers
)
664 insert_1_both (string
, chars_in_text ((unsigned char *) string
, nbytes
),
665 nbytes
, inherit
, prepare
, before_markers
);
669 #ifdef BYTE_COMBINING_DEBUG
671 /* See if the bytes before POS/POS_BYTE combine with bytes
672 at the start of STRING to form a single character.
673 If so, return the number of bytes at the start of STRING
674 which combine in this way. Otherwise, return 0. */
677 count_combining_before (const unsigned char *string
, ptrdiff_t length
,
678 ptrdiff_t pos
, ptrdiff_t pos_byte
)
680 int len
, combining_bytes
;
681 const unsigned char *p
;
683 if (NILP (current_buffer
->enable_multibyte_characters
))
686 /* At first, we can exclude the following cases:
687 (1) STRING[0] can't be a following byte of multibyte sequence.
688 (2) POS is the start of the current buffer.
689 (3) A character before POS is not a multibyte character. */
690 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
692 if (pos_byte
== BEG_BYTE
) /* case (2) */
695 p
= BYTE_POS_ADDR (pos_byte
- 1);
696 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
697 if (! LEADING_CODE_P (*p
)) /* case (3) */
700 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
701 if (combining_bytes
<= 0)
702 /* The character preceding POS is, complete and no room for
703 combining bytes (combining_bytes == 0), or an independent 8-bit
704 character (combining_bytes < 0). */
707 /* We have a combination situation. Count the bytes at STRING that
710 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
713 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
716 /* See if the bytes after POS/POS_BYTE combine with bytes
717 at the end of STRING to form a single character.
718 If so, return the number of bytes after POS/POS_BYTE
719 which combine in this way. Otherwise, return 0. */
722 count_combining_after (const unsigned char *string
,
723 ptrdiff_t length
, ptrdiff_t pos
, ptrdiff_t pos_byte
)
725 ptrdiff_t opos_byte
= pos_byte
;
730 if (NILP (current_buffer
->enable_multibyte_characters
))
733 /* At first, we can exclude the following cases:
734 (1) The last byte of STRING is an ASCII.
735 (2) POS is the last of the current buffer.
736 (3) A character at POS can't be a following byte of multibyte
738 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1])) /* case (1) */
740 if (pos_byte
== Z_BYTE
) /* case (2) */
742 bufp
= BYTE_POS_ADDR (pos_byte
);
743 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
747 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
753 /* All characters in STRING are not character head. We must
754 check also preceding bytes at POS. We are sure that the gap
756 unsigned char *p
= BEG_ADDR
;
758 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
760 if (i
< 0 || !LEADING_CODE_P (p
[i
]))
763 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
764 return (bytes
<= pos_byte
- 1 - i
+ length
766 : bytes
- (pos_byte
- 1 - i
+ length
));
768 if (!LEADING_CODE_P (string
[i
]))
771 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
773 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
775 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
781 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
782 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
783 are the same as in insert_1. */
786 insert_1_both (const char *string
,
787 ptrdiff_t nchars
, ptrdiff_t nbytes
,
788 bool inherit
, bool prepare
, bool before_markers
)
793 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
797 /* Do this before moving and increasing the gap,
798 because the before-change hooks might move the gap
799 or make it smaller. */
800 prepare_to_modify_buffer (PT
, PT
, NULL
);
803 move_gap_both (PT
, PT_BYTE
);
804 if (GAP_SIZE
< nbytes
)
805 make_gap (nbytes
- GAP_SIZE
);
807 #ifdef BYTE_COMBINING_DEBUG
808 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
809 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
813 /* Record deletion of the surrounding text that combines with
814 the insertion. This, together with recording the insertion,
815 will add up to the right stuff in the undo list. */
816 record_insert (PT
, nchars
);
818 CHARS_MODIFF
= MODIFF
;
820 memcpy (GPT_ADDR
, string
, nbytes
);
829 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
831 eassert (GPT
<= GPT_BYTE
);
833 /* The insert may have been in the unchanged region, so check again. */
834 if (Z
- GPT
< END_UNCHANGED
)
835 END_UNCHANGED
= Z
- GPT
;
837 adjust_overlays_for_insert (PT
, nchars
);
838 adjust_markers_for_insert (PT
, PT_BYTE
,
839 PT
+ nchars
, PT_BYTE
+ nbytes
,
842 offset_intervals (current_buffer
, PT
, nchars
);
844 if (!inherit
&& buffer_intervals (current_buffer
))
845 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
848 adjust_point (nchars
, nbytes
);
853 /* Insert the part of the text of STRING, a Lisp object assumed to be
854 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
855 starting at position POS / POS_BYTE. If the text of STRING has properties,
856 copy them into the buffer.
858 It does not work to use `insert' for this, because a GC could happen
859 before we copy the stuff into the buffer, and relocate the string
860 without insert noticing. */
863 insert_from_string (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
864 ptrdiff_t length
, ptrdiff_t length_byte
, bool inherit
)
866 ptrdiff_t opoint
= PT
;
868 if (SCHARS (string
) == 0)
871 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
873 signal_after_change (opoint
, 0, PT
- opoint
);
874 update_compositions (opoint
, PT
, CHECK_BORDER
);
877 /* Like `insert_from_string' except that all markers pointing
878 at the place where the insertion happens are adjusted to point after it. */
881 insert_from_string_before_markers (Lisp_Object string
,
882 ptrdiff_t pos
, ptrdiff_t pos_byte
,
883 ptrdiff_t length
, ptrdiff_t length_byte
,
886 ptrdiff_t opoint
= PT
;
888 if (SCHARS (string
) == 0)
891 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
893 signal_after_change (opoint
, 0, PT
- opoint
);
894 update_compositions (opoint
, PT
, CHECK_BORDER
);
897 /* Subroutine of the insertion functions above. */
900 insert_from_string_1 (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
901 ptrdiff_t nchars
, ptrdiff_t nbytes
,
902 bool inherit
, bool before_markers
)
905 ptrdiff_t outgoing_nbytes
= nbytes
;
908 /* Make OUTGOING_NBYTES describe the text
909 as it will be inserted in this buffer. */
911 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
912 outgoing_nbytes
= nchars
;
913 else if (! STRING_MULTIBYTE (string
))
915 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
919 /* Do this before moving and increasing the gap,
920 because the before-change hooks might move the gap
921 or make it smaller. */
922 prepare_to_modify_buffer (PT
, PT
, NULL
);
925 move_gap_both (PT
, PT_BYTE
);
926 if (GAP_SIZE
< outgoing_nbytes
)
927 make_gap (outgoing_nbytes
- GAP_SIZE
);
930 /* Copy the string text into the buffer, perhaps converting
931 between single-byte and multibyte. */
932 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
933 STRING_MULTIBYTE (string
),
934 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
936 #ifdef BYTE_COMBINING_DEBUG
937 /* We have copied text into the gap, but we have not altered
938 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
939 to these functions and get the same results as we would
940 have got earlier on. Meanwhile, PT_ADDR does point to
941 the text that has been stored by copy_text. */
942 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
943 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
947 record_insert (PT
, nchars
);
949 CHARS_MODIFF
= MODIFF
;
951 GAP_SIZE
-= outgoing_nbytes
;
955 GPT_BYTE
+= outgoing_nbytes
;
956 ZV_BYTE
+= outgoing_nbytes
;
957 Z_BYTE
+= outgoing_nbytes
;
958 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
960 eassert (GPT
<= GPT_BYTE
);
962 /* The insert may have been in the unchanged region, so check again. */
963 if (Z
- GPT
< END_UNCHANGED
)
964 END_UNCHANGED
= Z
- GPT
;
966 adjust_overlays_for_insert (PT
, nchars
);
967 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
968 PT_BYTE
+ outgoing_nbytes
,
971 offset_intervals (current_buffer
, PT
, nchars
);
973 intervals
= string_intervals (string
);
974 /* Get the intervals for the part of the string we are inserting. */
975 if (nbytes
< SBYTES (string
))
976 intervals
= copy_intervals (intervals
, pos
, nchars
);
978 /* Insert those intervals. */
979 graft_intervals_into_buffer (intervals
, PT
, nchars
,
980 current_buffer
, inherit
);
982 adjust_point (nchars
, outgoing_nbytes
);
987 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
988 starting at GPT_ADDR. */
991 insert_from_gap (ptrdiff_t nchars
, ptrdiff_t nbytes
)
993 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
996 record_insert (GPT
, nchars
);
1006 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1008 eassert (GPT
<= GPT_BYTE
);
1010 adjust_overlays_for_insert (GPT
- nchars
, nchars
);
1011 adjust_markers_for_insert (GPT
- nchars
, GPT_BYTE
- nbytes
,
1014 if (buffer_intervals (current_buffer
))
1016 offset_intervals (current_buffer
, GPT
- nchars
, nchars
);
1017 graft_intervals_into_buffer (NULL
, GPT
- nchars
, nchars
,
1021 if (GPT
- nchars
< PT
)
1022 adjust_point (nchars
, nbytes
);
1027 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1028 current buffer. If the text in BUF has properties, they are absorbed
1029 into the current buffer.
1031 It does not work to use `insert' for this, because a malloc could happen
1032 and relocate BUF's text before the copy happens. */
1035 insert_from_buffer (struct buffer
*buf
,
1036 ptrdiff_t charpos
, ptrdiff_t nchars
, bool inherit
)
1038 ptrdiff_t opoint
= PT
;
1040 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1041 signal_after_change (opoint
, 0, PT
- opoint
);
1042 update_compositions (opoint
, PT
, CHECK_BORDER
);
1046 insert_from_buffer_1 (struct buffer
*buf
,
1047 ptrdiff_t from
, ptrdiff_t nchars
, bool inherit
)
1049 ptrdiff_t chunk
, chunk_expanded
;
1050 ptrdiff_t from_byte
= buf_charpos_to_bytepos (buf
, from
);
1051 ptrdiff_t to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1052 ptrdiff_t incoming_nbytes
= to_byte
- from_byte
;
1053 ptrdiff_t outgoing_nbytes
= incoming_nbytes
;
1056 /* Make OUTGOING_NBYTES describe the text
1057 as it will be inserted in this buffer. */
1059 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1060 outgoing_nbytes
= nchars
;
1061 else if (NILP (BVAR (buf
, enable_multibyte_characters
)))
1063 ptrdiff_t outgoing_before_gap
= 0;
1064 ptrdiff_t outgoing_after_gap
= 0;
1066 if (from
< BUF_GPT (buf
))
1068 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1069 if (chunk
> incoming_nbytes
)
1070 chunk
= incoming_nbytes
;
1072 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1078 if (chunk
< incoming_nbytes
)
1080 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1082 incoming_nbytes
- chunk
);
1084 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1087 /* Do this before moving and increasing the gap,
1088 because the before-change hooks might move the gap
1089 or make it smaller. */
1090 prepare_to_modify_buffer (PT
, PT
, NULL
);
1093 move_gap_both (PT
, PT_BYTE
);
1094 if (GAP_SIZE
< outgoing_nbytes
)
1095 make_gap (outgoing_nbytes
- GAP_SIZE
);
1097 if (from
< BUF_GPT (buf
))
1099 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1100 if (chunk
> incoming_nbytes
)
1101 chunk
= incoming_nbytes
;
1102 /* Record number of output bytes, so we know where
1103 to put the output from the second copy_text. */
1105 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1107 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1108 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1111 chunk_expanded
= chunk
= 0;
1113 if (chunk
< incoming_nbytes
)
1114 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1115 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1116 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1117 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1119 #ifdef BYTE_COMBINING_DEBUG
1120 /* We have copied text into the gap, but we have not altered
1121 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1122 to these functions and get the same results as we would
1123 have got earlier on. Meanwhile, GPT_ADDR does point to
1124 the text that has been stored by copy_text. */
1125 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1126 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1130 record_insert (PT
, nchars
);
1132 CHARS_MODIFF
= MODIFF
;
1134 GAP_SIZE
-= outgoing_nbytes
;
1138 GPT_BYTE
+= outgoing_nbytes
;
1139 ZV_BYTE
+= outgoing_nbytes
;
1140 Z_BYTE
+= outgoing_nbytes
;
1141 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1143 eassert (GPT
<= GPT_BYTE
);
1145 /* The insert may have been in the unchanged region, so check again. */
1146 if (Z
- GPT
< END_UNCHANGED
)
1147 END_UNCHANGED
= Z
- GPT
;
1149 adjust_overlays_for_insert (PT
, nchars
);
1150 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1151 PT_BYTE
+ outgoing_nbytes
,
1154 offset_intervals (current_buffer
, PT
, nchars
);
1156 /* Get the intervals for the part of the string we are inserting. */
1157 intervals
= buffer_intervals (buf
);
1158 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1160 if (buf
== current_buffer
&& PT
<= from
)
1162 intervals
= copy_intervals (intervals
, from
, nchars
);
1165 /* Insert those intervals. */
1166 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1168 adjust_point (nchars
, outgoing_nbytes
);
1171 /* Record undo information and adjust markers and position keepers for
1172 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1173 chars (LEN_BYTE bytes) which resides in the gap just after
1176 PREV_TEXT nil means the new text was just inserted. */
1179 adjust_after_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
1180 Lisp_Object prev_text
, ptrdiff_t len
, ptrdiff_t len_byte
)
1182 ptrdiff_t nchars_del
= 0, nbytes_del
= 0;
1184 #ifdef BYTE_COMBINING_DEBUG
1185 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1186 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1190 if (STRINGP (prev_text
))
1192 nchars_del
= SCHARS (prev_text
);
1193 nbytes_del
= SBYTES (prev_text
);
1196 /* Update various buffer positions for the new text. */
1197 GAP_SIZE
-= len_byte
;
1199 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1200 GPT
+= len
; GPT_BYTE
+= len_byte
;
1201 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1204 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1207 adjust_markers_for_insert (from
, from_byte
,
1208 from
+ len
, from_byte
+ len_byte
, 0);
1210 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1213 record_delete (from
, prev_text
);
1214 record_insert (from
, len
);
1217 if (len
> nchars_del
)
1218 adjust_overlays_for_insert (from
, len
- nchars_del
);
1219 else if (len
< nchars_del
)
1220 adjust_overlays_for_delete (from
, nchars_del
- len
);
1222 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1225 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1227 /* As byte combining will decrease Z, we must check this again. */
1228 if (Z
- GPT
< END_UNCHANGED
)
1229 END_UNCHANGED
= Z
- GPT
;
1234 evaporate_overlays (from
);
1236 CHARS_MODIFF
= MODIFF
;
1239 /* Record undo information, adjust markers and position keepers for an
1240 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1241 text already exists in the current buffer but character length (TO
1242 - FROM) may be incorrect, the correct length is NEWLEN. */
1245 adjust_after_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
1246 ptrdiff_t to
, ptrdiff_t to_byte
, ptrdiff_t newlen
)
1248 ptrdiff_t len
= to
- from
, len_byte
= to_byte
- from_byte
;
1251 move_gap_both (to
, to_byte
);
1252 GAP_SIZE
+= len_byte
;
1253 GPT
-= len
; GPT_BYTE
-= len_byte
;
1254 ZV
-= len
; ZV_BYTE
-= len_byte
;
1255 Z
-= len
; Z_BYTE
-= len_byte
;
1256 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1259 /* Replace the text from character positions FROM to TO with NEW,
1260 If PREPARE, call prepare_to_modify_buffer.
1261 If INHERIT, the newly inserted text should inherit text properties
1262 from the surrounding non-deleted text. */
1264 /* Note that this does not yet handle markers quite right.
1265 Also it needs to record a single undo-entry that does a replacement
1266 rather than a separate delete and insert.
1267 That way, undo will also handle markers properly.
1269 But if MARKERS is 0, don't relocate markers. */
1272 replace_range (ptrdiff_t from
, ptrdiff_t to
, Lisp_Object
new,
1273 bool prepare
, bool inherit
, bool markers
)
1275 ptrdiff_t inschars
= SCHARS (new);
1276 ptrdiff_t insbytes
= SBYTES (new);
1277 ptrdiff_t from_byte
, to_byte
;
1278 ptrdiff_t nbytes_del
, nchars_del
;
1279 struct gcpro gcpro1
;
1281 ptrdiff_t outgoing_insbytes
= insbytes
;
1282 Lisp_Object deletion
;
1291 ptrdiff_t range_length
= to
- from
;
1292 prepare_to_modify_buffer (from
, to
, &from
);
1293 to
= from
+ range_length
;
1298 /* Make args be valid. */
1304 from_byte
= CHAR_TO_BYTE (from
);
1305 to_byte
= CHAR_TO_BYTE (to
);
1307 nchars_del
= to
- from
;
1308 nbytes_del
= to_byte
- from_byte
;
1310 if (nbytes_del
<= 0 && insbytes
== 0)
1313 /* Make OUTGOING_INSBYTES describe the text
1314 as it will be inserted in this buffer. */
1316 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1317 outgoing_insbytes
= inschars
;
1318 else if (! STRING_MULTIBYTE (new))
1320 = count_size_as_multibyte (SDATA (new), insbytes
);
1324 /* Make sure the gap is somewhere in or next to what we are deleting. */
1326 gap_right (from
, from_byte
);
1328 gap_left (to
, to_byte
, 0);
1330 /* Even if we don't record for undo, we must keep the original text
1331 because we may have to recover it because of inappropriate byte
1333 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1334 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1336 GAP_SIZE
+= nbytes_del
;
1339 ZV_BYTE
-= nbytes_del
;
1340 Z_BYTE
-= nbytes_del
;
1342 GPT_BYTE
= from_byte
;
1343 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1345 eassert (GPT
<= GPT_BYTE
);
1347 if (GPT
- BEG
< BEG_UNCHANGED
)
1348 BEG_UNCHANGED
= GPT
- BEG
;
1349 if (Z
- GPT
< END_UNCHANGED
)
1350 END_UNCHANGED
= Z
- GPT
;
1352 if (GAP_SIZE
< outgoing_insbytes
)
1353 make_gap (outgoing_insbytes
- GAP_SIZE
);
1355 /* Copy the string text into the buffer, perhaps converting
1356 between single-byte and multibyte. */
1357 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1358 STRING_MULTIBYTE (new),
1359 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1361 #ifdef BYTE_COMBINING_DEBUG
1362 /* We have copied text into the gap, but we have not marked
1363 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1364 here, for both the previous text and the following text.
1365 Meanwhile, GPT_ADDR does point to
1366 the text that has been stored by copy_text. */
1367 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1368 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1372 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1374 /* Record the insertion first, so that when we undo,
1375 the deletion will be undone first. Thus, undo
1376 will insert before deleting, and thus will keep
1377 the markers before and after this text separate. */
1378 record_insert (from
+ SCHARS (deletion
), inschars
);
1379 record_delete (from
, deletion
);
1382 GAP_SIZE
-= outgoing_insbytes
;
1386 GPT_BYTE
+= outgoing_insbytes
;
1387 ZV_BYTE
+= outgoing_insbytes
;
1388 Z_BYTE
+= outgoing_insbytes
;
1389 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1391 eassert (GPT
<= GPT_BYTE
);
1393 /* Adjust markers for the deletion and the insertion. */
1395 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1396 inschars
, outgoing_insbytes
);
1398 /* Adjust the overlay center as needed. This must be done after
1399 adjusting the markers that bound the overlays. */
1400 adjust_overlays_for_delete (from
, nchars_del
);
1401 adjust_overlays_for_insert (from
, inschars
);
1403 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1405 /* Get the intervals for the part of the string we are inserting--
1406 not including the combined-before bytes. */
1407 intervals
= string_intervals (new);
1408 /* Insert those intervals. */
1409 graft_intervals_into_buffer (intervals
, from
, inschars
,
1410 current_buffer
, inherit
);
1412 /* Relocate point as if it were a marker. */
1414 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1415 (from_byte
+ outgoing_insbytes
1416 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1418 if (outgoing_insbytes
== 0)
1419 evaporate_overlays (from
);
1424 CHARS_MODIFF
= MODIFF
;
1427 signal_after_change (from
, nchars_del
, GPT
- from
);
1428 update_compositions (from
, GPT
, CHECK_BORDER
);
1431 /* Replace the text from character positions FROM to TO with
1432 the text in INS of length INSCHARS.
1433 Keep the text properties that applied to the old characters
1434 (extending them to all the new chars if there are more new chars).
1436 Note that this does not yet handle markers quite right.
1438 If MARKERS, relocate markers.
1440 Unlike most functions at this level, never call
1441 prepare_to_modify_buffer and never call signal_after_change. */
1444 replace_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1445 ptrdiff_t to
, ptrdiff_t to_byte
,
1446 const char *ins
, ptrdiff_t inschars
, ptrdiff_t insbytes
,
1449 ptrdiff_t nbytes_del
, nchars_del
;
1453 nchars_del
= to
- from
;
1454 nbytes_del
= to_byte
- from_byte
;
1456 if (nbytes_del
<= 0 && insbytes
== 0)
1459 /* Make sure the gap is somewhere in or next to what we are deleting. */
1461 gap_right (from
, from_byte
);
1463 gap_left (to
, to_byte
, 0);
1465 GAP_SIZE
+= nbytes_del
;
1468 ZV_BYTE
-= nbytes_del
;
1469 Z_BYTE
-= nbytes_del
;
1471 GPT_BYTE
= from_byte
;
1472 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1474 eassert (GPT
<= GPT_BYTE
);
1476 if (GPT
- BEG
< BEG_UNCHANGED
)
1477 BEG_UNCHANGED
= GPT
- BEG
;
1478 if (Z
- GPT
< END_UNCHANGED
)
1479 END_UNCHANGED
= Z
- GPT
;
1481 if (GAP_SIZE
< insbytes
)
1482 make_gap (insbytes
- GAP_SIZE
);
1484 /* Copy the replacement text into the buffer. */
1485 memcpy (GPT_ADDR
, ins
, insbytes
);
1487 #ifdef BYTE_COMBINING_DEBUG
1488 /* We have copied text into the gap, but we have not marked
1489 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1490 here, for both the previous text and the following text.
1491 Meanwhile, GPT_ADDR does point to
1492 the text that has been stored by copy_text. */
1493 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1494 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1498 GAP_SIZE
-= insbytes
;
1502 GPT_BYTE
+= insbytes
;
1503 ZV_BYTE
+= insbytes
;
1505 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1507 eassert (GPT
<= GPT_BYTE
);
1509 /* Adjust markers for the deletion and the insertion. */
1511 && ! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1512 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1513 inschars
, insbytes
);
1515 /* Adjust the overlay center as needed. This must be done after
1516 adjusting the markers that bound the overlays. */
1517 if (nchars_del
!= inschars
)
1519 adjust_overlays_for_insert (from
, inschars
);
1520 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1523 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1525 /* Relocate point as if it were a marker. */
1526 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1529 /* PT was within the deleted text. Move it to FROM. */
1530 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1532 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1536 evaporate_overlays (from
);
1541 CHARS_MODIFF
= MODIFF
;
1544 /* Delete characters in current buffer
1545 from FROM up to (but not including) TO.
1546 If TO comes before FROM, we delete nothing. */
1549 del_range (ptrdiff_t from
, ptrdiff_t to
)
1551 del_range_1 (from
, to
, 1, 0);
1554 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1555 RET_STRING says to return the deleted text. */
1558 del_range_1 (ptrdiff_t from
, ptrdiff_t to
, bool prepare
, bool ret_string
)
1560 ptrdiff_t from_byte
, to_byte
;
1561 Lisp_Object deletion
;
1562 struct gcpro gcpro1
;
1564 /* Make args be valid */
1575 ptrdiff_t range_length
= to
- from
;
1576 prepare_to_modify_buffer (from
, to
, &from
);
1577 to
= min (ZV
, from
+ range_length
);
1580 from_byte
= CHAR_TO_BYTE (from
);
1581 to_byte
= CHAR_TO_BYTE (to
);
1583 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1585 signal_after_change (from
, to
- from
, 0);
1586 update_compositions (from
, from
, CHECK_HEAD
);
1591 /* Like del_range_1 but args are byte positions, not char positions. */
1594 del_range_byte (ptrdiff_t from_byte
, ptrdiff_t to_byte
, bool prepare
)
1598 /* Make args be valid */
1599 if (from_byte
< BEGV_BYTE
)
1600 from_byte
= BEGV_BYTE
;
1601 if (to_byte
> ZV_BYTE
)
1604 if (to_byte
<= from_byte
)
1607 from
= BYTE_TO_CHAR (from_byte
);
1608 to
= BYTE_TO_CHAR (to_byte
);
1612 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1613 ptrdiff_t range_length
= to
- from
;
1614 prepare_to_modify_buffer (from
, to
, &from
);
1615 to
= from
+ range_length
;
1617 if (old_from
!= from
)
1618 from_byte
= CHAR_TO_BYTE (from
);
1624 else if (old_to
== Z
- to
)
1625 to_byte
= CHAR_TO_BYTE (to
);
1628 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1629 signal_after_change (from
, to
- from
, 0);
1630 update_compositions (from
, from
, CHECK_HEAD
);
1633 /* Like del_range_1, but positions are specified both as charpos
1637 del_range_both (ptrdiff_t from
, ptrdiff_t from_byte
,
1638 ptrdiff_t to
, ptrdiff_t to_byte
, bool prepare
)
1640 /* Make args be valid */
1641 if (from_byte
< BEGV_BYTE
)
1642 from_byte
= BEGV_BYTE
;
1643 if (to_byte
> ZV_BYTE
)
1646 if (to_byte
<= from_byte
)
1656 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1657 ptrdiff_t range_length
= to
- from
;
1658 prepare_to_modify_buffer (from
, to
, &from
);
1659 to
= from
+ range_length
;
1661 if (old_from
!= from
)
1662 from_byte
= CHAR_TO_BYTE (from
);
1668 else if (old_to
== Z
- to
)
1669 to_byte
= CHAR_TO_BYTE (to
);
1672 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1673 signal_after_change (from
, to
- from
, 0);
1674 update_compositions (from
, from
, CHECK_HEAD
);
1677 /* Delete a range of text, specified both as character positions
1678 and byte positions. FROM and TO are character positions,
1679 while FROM_BYTE and TO_BYTE are byte positions.
1680 If RET_STRING, the deleted area is returned as a string. */
1683 del_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1684 ptrdiff_t to
, ptrdiff_t to_byte
, bool ret_string
)
1686 ptrdiff_t nbytes_del
, nchars_del
;
1687 Lisp_Object deletion
;
1691 nchars_del
= to
- from
;
1692 nbytes_del
= to_byte
- from_byte
;
1694 /* Make sure the gap is somewhere in or next to what we are deleting. */
1696 gap_right (from
, from_byte
);
1698 gap_left (to
, to_byte
, 0);
1700 #ifdef BYTE_COMBINING_DEBUG
1701 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1702 Z_BYTE
- to_byte
, from
, from_byte
))
1706 if (ret_string
|| ! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1707 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1711 /* Relocate all markers pointing into the new, larger gap
1712 to point at the end of the text before the gap.
1713 Do this before recording the deletion,
1714 so that undo handles this after reinserting the text. */
1715 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1717 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1718 record_delete (from
, deletion
);
1720 CHARS_MODIFF
= MODIFF
;
1722 /* Relocate point as if it were a marker. */
1724 adjust_point (from
- (PT
< to
? PT
: to
),
1725 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1727 offset_intervals (current_buffer
, from
, - nchars_del
);
1729 /* Adjust the overlay center as needed. This must be done after
1730 adjusting the markers that bound the overlays. */
1731 adjust_overlays_for_delete (from
, nchars_del
);
1733 GAP_SIZE
+= nbytes_del
;
1734 ZV_BYTE
-= nbytes_del
;
1735 Z_BYTE
-= nbytes_del
;
1739 GPT_BYTE
= from_byte
;
1740 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1741 /* Put an anchor, unless called from decode_coding_object which
1742 needs to access the previous gap contents. */
1745 eassert (GPT
<= GPT_BYTE
);
1747 if (GPT
- BEG
< BEG_UNCHANGED
)
1748 BEG_UNCHANGED
= GPT
- BEG
;
1749 if (Z
- GPT
< END_UNCHANGED
)
1750 END_UNCHANGED
= Z
- GPT
;
1754 evaporate_overlays (from
);
1759 /* Call this if you're about to change the region of BUFFER from
1760 character positions START to END. This checks the read-only
1761 properties of the region, calls the necessary modification hooks,
1762 and warns the next redisplay that it should pay attention to that
1765 If PRESERVE_CHARS_MODIFF, do not update CHARS_MODIFF.
1766 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1769 modify_region (struct buffer
*buffer
, ptrdiff_t start
, ptrdiff_t end
,
1770 bool preserve_chars_modiff
)
1772 struct buffer
*old_buffer
= current_buffer
;
1774 set_buffer_internal (buffer
);
1776 prepare_to_modify_buffer (start
, end
, NULL
);
1778 BUF_COMPUTE_UNCHANGED (buffer
, start
- 1, end
);
1780 if (MODIFF
<= SAVE_MODIFF
)
1781 record_first_change ();
1783 if (! preserve_chars_modiff
)
1784 CHARS_MODIFF
= MODIFF
;
1786 bset_point_before_scroll (buffer
, Qnil
);
1788 set_buffer_internal (old_buffer
);
1791 /* Check that it is okay to modify the buffer between START and END,
1792 which are char positions.
1794 Run the before-change-function, if any. If intervals are in use,
1795 verify that the text to be modified is not read-only, and call
1796 any modification properties the text may have.
1798 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1799 by holding its value temporarily in a marker. */
1802 prepare_to_modify_buffer (ptrdiff_t start
, ptrdiff_t end
,
1803 ptrdiff_t *preserve_ptr
)
1805 struct buffer
*base_buffer
;
1807 if (!NILP (BVAR (current_buffer
, read_only
)))
1808 Fbarf_if_buffer_read_only ();
1810 /* Let redisplay consider other windows than selected_window
1811 if modifying another buffer. */
1812 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
1813 ++windows_or_buffers_changed
;
1815 if (buffer_intervals (current_buffer
))
1819 Lisp_Object preserve_marker
;
1820 struct gcpro gcpro1
;
1821 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1822 GCPRO1 (preserve_marker
);
1823 verify_interval_modification (current_buffer
, start
, end
);
1824 *preserve_ptr
= marker_position (preserve_marker
);
1825 unchain_marker (XMARKER (preserve_marker
));
1829 verify_interval_modification (current_buffer
, start
, end
);
1832 /* For indirect buffers, use the base buffer to check clashes. */
1833 if (current_buffer
->base_buffer
!= 0)
1834 base_buffer
= current_buffer
->base_buffer
;
1836 base_buffer
= current_buffer
;
1838 #ifdef CLASH_DETECTION
1839 if (!NILP (BVAR (base_buffer
, file_truename
))
1840 /* Make binding buffer-file-name to nil effective. */
1841 && !NILP (BVAR (base_buffer
, filename
))
1842 && SAVE_MODIFF
>= MODIFF
)
1843 lock_file (BVAR (base_buffer
, file_truename
));
1845 /* At least warn if this file has changed on disk since it was visited. */
1846 if (!NILP (BVAR (base_buffer
, filename
))
1847 && SAVE_MODIFF
>= MODIFF
1848 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1849 && !NILP (Ffile_exists_p (BVAR (base_buffer
, filename
))))
1850 call1 (intern ("ask-user-about-supersession-threat"),
1851 BVAR (base_buffer
,filename
));
1852 #endif /* not CLASH_DETECTION */
1854 /* If `select-active-regions' is non-nil, save the region text. */
1855 if (!NILP (BVAR (current_buffer
, mark_active
))
1856 && !inhibit_modification_hooks
1857 && XMARKER (BVAR (current_buffer
, mark
))->buffer
1858 && NILP (Vsaved_region_selection
)
1859 && (EQ (Vselect_active_regions
, Qonly
)
1860 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
1861 : (!NILP (Vselect_active_regions
)
1862 && !NILP (Vtransient_mark_mode
))))
1864 ptrdiff_t b
= XMARKER (BVAR (current_buffer
, mark
))->charpos
;
1867 Vsaved_region_selection
= make_buffer_string (b
, e
, 0);
1869 Vsaved_region_selection
= make_buffer_string (e
, b
, 0);
1872 signal_before_change (start
, end
, preserve_ptr
);
1874 if (current_buffer
->newline_cache
)
1875 invalidate_region_cache (current_buffer
,
1876 current_buffer
->newline_cache
,
1877 start
- BEG
, Z
- end
);
1878 if (current_buffer
->width_run_cache
)
1879 invalidate_region_cache (current_buffer
,
1880 current_buffer
->width_run_cache
,
1881 start
- BEG
, Z
- end
);
1883 Vdeactivate_mark
= Qt
;
1886 /* These macros work with an argument named `preserve_ptr'
1887 and a local variable named `preserve_marker'. */
1889 #define PRESERVE_VALUE \
1890 if (preserve_ptr && NILP (preserve_marker)) \
1891 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1893 #define RESTORE_VALUE \
1894 if (! NILP (preserve_marker)) \
1896 *preserve_ptr = marker_position (preserve_marker); \
1897 unchain_marker (XMARKER (preserve_marker)); \
1900 #define PRESERVE_START_END \
1901 if (NILP (start_marker)) \
1902 start_marker = Fcopy_marker (start, Qnil); \
1903 if (NILP (end_marker)) \
1904 end_marker = Fcopy_marker (end, Qnil);
1906 #define FETCH_START \
1907 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1910 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1912 /* Set a variable to nil if an error occurred.
1913 Don't change the variable if there was no error.
1914 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1915 VARIABLE is the variable to maybe set to nil.
1916 NO-ERROR-FLAG is nil if there was an error,
1917 anything else meaning no error (so this function does nothing). */
1919 reset_var_on_error (Lisp_Object val
)
1921 if (NILP (XCDR (val
)))
1922 Fset (XCAR (val
), Qnil
);
1926 /* Signal a change to the buffer immediately before it happens.
1927 START_INT and END_INT are the bounds of the text to be changed.
1929 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1930 by holding its value temporarily in a marker. */
1933 signal_before_change (ptrdiff_t start_int
, ptrdiff_t end_int
,
1934 ptrdiff_t *preserve_ptr
)
1936 Lisp_Object start
, end
;
1937 Lisp_Object start_marker
, end_marker
;
1938 Lisp_Object preserve_marker
;
1939 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1940 ptrdiff_t count
= SPECPDL_INDEX ();
1942 if (inhibit_modification_hooks
)
1945 start
= make_number (start_int
);
1946 end
= make_number (end_int
);
1947 preserve_marker
= Qnil
;
1948 start_marker
= Qnil
;
1950 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
1952 specbind (Qinhibit_modification_hooks
, Qt
);
1954 /* If buffer is unmodified, run a special hook for that case. The
1955 check for Vfirst_change_hook is just a minor optimization. */
1956 if (SAVE_MODIFF
>= MODIFF
1957 && !NILP (Vfirst_change_hook
))
1961 Frun_hooks (1, &Qfirst_change_hook
);
1964 /* Now run the before-change-functions if any. */
1965 if (!NILP (Vbefore_change_functions
))
1967 Lisp_Object args
[3];
1968 Lisp_Object rvoe_arg
= Fcons (Qbefore_change_functions
, Qnil
);
1973 /* Mark before-change-functions to be reset to nil in case of error. */
1974 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
1976 /* Actually run the hook functions. */
1977 args
[0] = Qbefore_change_functions
;
1978 args
[1] = FETCH_START
;
1979 args
[2] = FETCH_END
;
1980 Frun_hook_with_args (3, args
);
1982 /* There was no error: unarm the reset_on_error. */
1983 XSETCDR (rvoe_arg
, Qt
);
1986 if (buffer_has_overlays ())
1989 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
1990 FETCH_START
, FETCH_END
, Qnil
);
1993 if (! NILP (start_marker
))
1994 free_marker (start_marker
);
1995 if (! NILP (end_marker
))
1996 free_marker (end_marker
);
2000 unbind_to (count
, Qnil
);
2003 /* Signal a change immediately after it happens.
2004 CHARPOS is the character position of the start of the changed text.
2005 LENDEL is the number of characters of the text before the change.
2006 (Not the whole buffer; just the part that was changed.)
2007 LENINS is the number of characters in that part of the text
2008 after the change. */
2011 signal_after_change (ptrdiff_t charpos
, ptrdiff_t lendel
, ptrdiff_t lenins
)
2013 ptrdiff_t count
= SPECPDL_INDEX ();
2014 if (inhibit_modification_hooks
)
2017 /* If we are deferring calls to the after-change functions
2018 and there are no before-change functions,
2019 just record the args that we were going to use. */
2020 if (! NILP (Vcombine_after_change_calls
)
2021 && NILP (Vbefore_change_functions
)
2022 && !buffer_has_overlays ())
2026 if (!NILP (combine_after_change_list
)
2027 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2028 Fcombine_after_change_execute ();
2030 elt
= Fcons (make_number (charpos
- BEG
),
2031 Fcons (make_number (Z
- (charpos
- lendel
+ lenins
)),
2032 Fcons (make_number (lenins
- lendel
), Qnil
)));
2033 combine_after_change_list
2034 = Fcons (elt
, combine_after_change_list
);
2035 combine_after_change_buffer
= Fcurrent_buffer ();
2040 if (!NILP (combine_after_change_list
))
2041 Fcombine_after_change_execute ();
2043 specbind (Qinhibit_modification_hooks
, Qt
);
2045 if (!NILP (Vafter_change_functions
))
2047 Lisp_Object args
[4];
2048 Lisp_Object rvoe_arg
= Fcons (Qafter_change_functions
, Qnil
);
2050 /* Mark after-change-functions to be reset to nil in case of error. */
2051 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2053 /* Actually run the hook functions. */
2054 args
[0] = Qafter_change_functions
;
2055 XSETFASTINT (args
[1], charpos
);
2056 XSETFASTINT (args
[2], charpos
+ lenins
);
2057 XSETFASTINT (args
[3], lendel
);
2058 Frun_hook_with_args (4, args
);
2060 /* There was no error: unarm the reset_on_error. */
2061 XSETCDR (rvoe_arg
, Qt
);
2064 if (buffer_has_overlays ())
2065 report_overlay_modification (make_number (charpos
),
2066 make_number (charpos
+ lenins
),
2068 make_number (charpos
),
2069 make_number (charpos
+ lenins
),
2070 make_number (lendel
));
2072 /* After an insertion, call the text properties
2073 insert-behind-hooks or insert-in-front-hooks. */
2075 report_interval_modification (make_number (charpos
),
2076 make_number (charpos
+ lenins
));
2078 unbind_to (count
, Qnil
);
2082 Fcombine_after_change_execute_1 (Lisp_Object val
)
2084 Vcombine_after_change_calls
= val
;
2088 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2089 Scombine_after_change_execute
, 0, 0, 0,
2090 doc
: /* This function is for use internally in `combine-after-change-calls'. */)
2093 ptrdiff_t count
= SPECPDL_INDEX ();
2094 ptrdiff_t beg
, end
, change
;
2095 ptrdiff_t begpos
, endpos
;
2098 if (NILP (combine_after_change_list
))
2101 /* It is rare for combine_after_change_buffer to be invalid, but
2102 possible. It can happen when combine-after-change-calls is
2103 non-nil, and insertion calls a file handler (e.g. through
2104 lock_file) which scribbles into a temp file -- cyd */
2105 if (!BUFFERP (combine_after_change_buffer
)
2106 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer
)))
2108 combine_after_change_list
= Qnil
;
2112 record_unwind_current_buffer ();
2114 Fset_buffer (combine_after_change_buffer
);
2116 /* # chars unchanged at beginning of buffer. */
2118 /* # chars unchanged at end of buffer. */
2120 /* Total amount of insertion (negative for deletion). */
2123 /* Scan the various individual changes,
2124 accumulating the range info in BEG, END and CHANGE. */
2125 for (tail
= combine_after_change_list
; CONSP (tail
);
2129 ptrdiff_t thisbeg
, thisend
, thischange
;
2131 /* Extract the info from the next element. */
2135 thisbeg
= XINT (XCAR (elt
));
2140 thisend
= XINT (XCAR (elt
));
2145 thischange
= XINT (XCAR (elt
));
2147 /* Merge this range into the accumulated range. */
2148 change
+= thischange
;
2155 /* Get the current start and end positions of the range
2156 that was changed. */
2160 /* We are about to handle these, so discard them. */
2161 combine_after_change_list
= Qnil
;
2163 /* Now run the after-change functions for real.
2164 Turn off the flag that defers them. */
2165 record_unwind_protect (Fcombine_after_change_execute_1
,
2166 Vcombine_after_change_calls
);
2167 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2168 update_compositions (begpos
, endpos
, CHECK_ALL
);
2170 return unbind_to (count
, Qnil
);
2174 syms_of_insdel (void)
2176 staticpro (&combine_after_change_list
);
2177 staticpro (&combine_after_change_buffer
);
2178 combine_after_change_list
= Qnil
;
2179 combine_after_change_buffer
= Qnil
;
2181 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls
,
2182 doc
: /* Used internally by the `combine-after-change-calls' macro. */);
2183 Vcombine_after_change_calls
= Qnil
;
2185 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks
,
2186 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2187 This affects `before-change-functions' and `after-change-functions',
2188 as well as hooks attached to text properties and overlays. */);
2189 inhibit_modification_hooks
= 0;
2190 DEFSYM (Qinhibit_modification_hooks
, "inhibit-modification-hooks");
2192 defsubr (&Scombine_after_change_execute
);