1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2015 Free Software
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
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 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
56 /* Also used in marker.c to enable expensive marker checks. */
63 struct Lisp_Marker
*tail
;
64 bool multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
66 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
68 if (tail
->buffer
->text
!= current_buffer
->text
)
70 if (tail
->charpos
> Z
)
72 if (tail
->bytepos
> Z_BYTE
)
74 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (tail
->bytepos
)))
79 #else /* not MARKER_DEBUG */
81 #define check_markers() do { } while (0)
83 #endif /* MARKER_DEBUG */
85 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
86 Note that this can quit! */
89 move_gap_both (ptrdiff_t charpos
, ptrdiff_t bytepos
)
91 eassert (charpos
== BYTE_TO_CHAR (bytepos
)
92 && bytepos
== CHAR_TO_BYTE (charpos
));
93 if (bytepos
< GPT_BYTE
)
94 gap_left (charpos
, bytepos
, 0);
95 else if (bytepos
> GPT_BYTE
)
96 gap_right (charpos
, bytepos
);
99 /* Move the gap to a position less than the current GPT.
100 BYTEPOS describes the new position as a byte position,
101 and CHARPOS is the corresponding char position.
102 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
105 gap_left (ptrdiff_t charpos
, ptrdiff_t bytepos
, bool newgap
)
107 unsigned char *to
, *from
;
112 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
119 /* Now copy the characters. To move the gap down,
120 copy characters up. */
124 /* I gets number of characters left to copy. */
125 i
= new_s1
- bytepos
;
128 /* If a quit is requested, stop copying now.
129 Change BYTEPOS to be where we have actually moved the gap to. */
133 charpos
= BYTE_TO_CHAR (bytepos
);
136 /* Move at most 32000 chars before checking again for a quit. */
141 memmove (to
, from
, i
);
144 /* Adjust buffer data structure, to put the gap at BYTEPOS.
145 BYTEPOS is where the loop above stopped, which may be what
146 was specified or may be where a quit was detected. */
149 eassert (charpos
<= bytepos
);
150 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
154 /* Move the gap to a position greater than the current GPT.
155 BYTEPOS describes the new position as a byte position,
156 and CHARPOS is the corresponding char position. */
159 gap_right (ptrdiff_t charpos
, ptrdiff_t bytepos
)
161 register unsigned char *to
, *from
;
162 register ptrdiff_t i
;
165 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
172 /* Now copy the characters. To move the gap up,
173 copy characters down. */
177 /* I gets number of characters left to copy. */
178 i
= bytepos
- new_s1
;
181 /* If a quit is requested, stop copying now.
182 Change BYTEPOS to be where we have actually moved the gap to. */
186 charpos
= BYTE_TO_CHAR (bytepos
);
189 /* Move at most 32000 chars before checking again for a quit. */
193 memmove (to
, from
, i
);
199 eassert (charpos
<= bytepos
);
200 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
204 /* If the selected window's old pointm is adjacent or covered by the
205 region from FROM to TO, unsuspend auto hscroll in that window. */
208 adjust_suspend_auto_hscroll (ptrdiff_t from
, ptrdiff_t to
)
210 if (WINDOWP (selected_window
))
212 struct window
*w
= XWINDOW (selected_window
);
214 if (BUFFERP (w
->contents
)
215 && XBUFFER (w
->contents
) == current_buffer
216 && XMARKER (w
->old_pointm
)->charpos
>= from
217 && XMARKER (w
->old_pointm
)->charpos
<= to
)
218 w
->suspend_auto_hscroll
= 0;
223 /* Adjust all markers for a deletion
224 whose range in bytes is FROM_BYTE to TO_BYTE.
225 The range in charpos is FROM to TO.
227 This function assumes that the gap is adjacent to
228 or inside of the range being deleted. */
231 adjust_markers_for_delete (ptrdiff_t from
, ptrdiff_t from_byte
,
232 ptrdiff_t to
, ptrdiff_t to_byte
)
234 struct Lisp_Marker
*m
;
237 adjust_suspend_auto_hscroll (from
, to
);
238 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
240 charpos
= m
->charpos
;
241 eassert (charpos
<= Z
);
243 /* If the marker is after the deletion,
244 relocate by number of chars / bytes deleted. */
247 m
->charpos
-= to
- from
;
248 m
->bytepos
-= to_byte
- from_byte
;
250 /* Here's the case where a marker is inside text being deleted. */
251 else if (charpos
> from
)
254 m
->bytepos
= from_byte
;
260 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
261 to TO / TO_BYTE. We have to relocate the charpos of every marker
262 that points after the insertion (but not their bytepos).
264 When a marker points at the insertion point,
265 we advance it if either its insertion-type is t
266 or BEFORE_MARKERS is true. */
269 adjust_markers_for_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
270 ptrdiff_t to
, ptrdiff_t to_byte
, bool before_markers
)
272 struct Lisp_Marker
*m
;
274 ptrdiff_t nchars
= to
- from
;
275 ptrdiff_t nbytes
= to_byte
- from_byte
;
277 adjust_suspend_auto_hscroll (from
, to
);
278 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
280 eassert (m
->bytepos
>= m
->charpos
281 && m
->bytepos
- m
->charpos
<= Z_BYTE
- Z
);
283 if (m
->bytepos
== from_byte
)
285 if (m
->insertion_type
|| before_markers
)
287 m
->bytepos
= to_byte
;
289 if (m
->insertion_type
)
293 else if (m
->bytepos
> from_byte
)
295 m
->bytepos
+= nbytes
;
296 m
->charpos
+= nchars
;
300 /* Adjusting only markers whose insertion-type is t may result in
301 - disordered start and end in overlays, and
302 - disordered overlays in the slot `overlays_before' of current_buffer. */
305 fix_start_end_in_overlays (from
, to
);
306 fix_overlays_before (current_buffer
, from
, to
);
310 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
312 This is used only when the value of point changes due to an insert
313 or delete; it does not represent a conceptual change in point as a
314 marker. In particular, point is not crossing any interval
315 boundaries, so there's no need to use the usual SET_PT macro. In
316 fact it would be incorrect to do so, because either the old or the
317 new value of point is out of sync with the current set of
321 adjust_point (ptrdiff_t nchars
, ptrdiff_t nbytes
)
323 SET_BUF_PT_BOTH (current_buffer
, PT
+ nchars
, PT_BYTE
+ nbytes
);
324 /* In a single-byte buffer, the two positions must be equal. */
325 eassert (PT_BYTE
>= PT
&& PT_BYTE
- PT
<= ZV_BYTE
- ZV
);
328 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
329 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
330 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
334 adjust_markers_for_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
335 ptrdiff_t old_chars
, ptrdiff_t old_bytes
,
336 ptrdiff_t new_chars
, ptrdiff_t new_bytes
)
338 register struct Lisp_Marker
*m
;
339 ptrdiff_t prev_to_byte
= from_byte
+ old_bytes
;
340 ptrdiff_t diff_chars
= new_chars
- old_chars
;
341 ptrdiff_t diff_bytes
= new_bytes
- old_bytes
;
343 adjust_suspend_auto_hscroll (from
, from
+ old_chars
);
344 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
346 if (m
->bytepos
>= prev_to_byte
)
348 m
->charpos
+= diff_chars
;
349 m
->bytepos
+= diff_bytes
;
351 else if (m
->bytepos
> from_byte
)
354 m
->bytepos
= from_byte
;
363 buffer_overflow (void)
365 error ("Maximum buffer size exceeded");
368 /* Make the gap NBYTES_ADDED bytes longer. */
371 make_gap_larger (ptrdiff_t nbytes_added
)
374 ptrdiff_t real_gap_loc
;
375 ptrdiff_t real_gap_loc_byte
;
376 ptrdiff_t old_gap_size
;
377 ptrdiff_t current_size
= Z_BYTE
- BEG_BYTE
+ GAP_SIZE
;
379 if (BUF_BYTES_MAX
- current_size
< nbytes_added
)
382 /* If we have to get more space, get enough to last a while;
383 but do not exceed the maximum buffer size. */
384 nbytes_added
= min (nbytes_added
+ GAP_BYTES_DFL
,
385 BUF_BYTES_MAX
- current_size
);
387 enlarge_buffer_text (current_buffer
, nbytes_added
);
389 /* Prevent quitting in move_gap. */
394 real_gap_loc_byte
= GPT_BYTE
;
395 old_gap_size
= GAP_SIZE
;
397 /* Call the newly allocated space a gap at the end of the whole space. */
399 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
400 GAP_SIZE
= nbytes_added
;
402 /* Move the new gap down to be consecutive with the end of the old one. */
403 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
405 /* Now combine the two into one large gap. */
406 GAP_SIZE
+= old_gap_size
;
408 GPT_BYTE
= real_gap_loc_byte
;
416 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
418 /* Make the gap NBYTES_REMOVED bytes shorter. */
421 make_gap_smaller (ptrdiff_t nbytes_removed
)
424 ptrdiff_t real_gap_loc
;
425 ptrdiff_t real_gap_loc_byte
;
427 ptrdiff_t real_Z_byte
;
428 ptrdiff_t real_beg_unchanged
;
429 ptrdiff_t new_gap_size
;
431 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
432 if (GAP_SIZE
- nbytes_removed
< GAP_BYTES_MIN
)
433 nbytes_removed
= GAP_SIZE
- GAP_BYTES_MIN
;
435 /* Prevent quitting in move_gap. */
440 real_gap_loc_byte
= GPT_BYTE
;
441 new_gap_size
= GAP_SIZE
- nbytes_removed
;
443 real_Z_byte
= Z_BYTE
;
444 real_beg_unchanged
= BEG_UNCHANGED
;
446 /* Pretend that the last unwanted part of the gap is the entire gap,
447 and that the first desired part of the gap is part of the buffer
449 memset (GPT_ADDR
, 0, new_gap_size
);
451 GPT_BYTE
+= new_gap_size
;
453 Z_BYTE
+= new_gap_size
;
454 GAP_SIZE
= nbytes_removed
;
456 /* Move the unwanted pretend gap to the end of the buffer. */
457 gap_right (Z
, Z_BYTE
);
459 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
461 /* Now restore the desired gap. */
462 GAP_SIZE
= new_gap_size
;
464 GPT_BYTE
= real_gap_loc_byte
;
466 Z_BYTE
= real_Z_byte
;
467 BEG_UNCHANGED
= real_beg_unchanged
;
475 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
478 make_gap (ptrdiff_t nbytes_added
)
480 if (nbytes_added
>= 0)
481 make_gap_larger (nbytes_added
);
482 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
484 make_gap_smaller (-nbytes_added
);
488 /* Add NBYTES to B's gap. It's enough to temporarily
489 fake current_buffer and avoid real switch to B. */
492 make_gap_1 (struct buffer
*b
, ptrdiff_t nbytes
)
494 struct buffer
*oldb
= current_buffer
;
498 current_buffer
= oldb
;
501 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
502 FROM_MULTIBYTE says whether the incoming text is multibyte.
503 TO_MULTIBYTE says whether to store the text as multibyte.
504 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
506 Return the number of bytes stored at TO_ADDR. */
509 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
510 ptrdiff_t nbytes
, bool from_multibyte
, bool to_multibyte
)
512 if (from_multibyte
== to_multibyte
)
514 memcpy (to_addr
, from_addr
, nbytes
);
517 else if (from_multibyte
)
519 ptrdiff_t nchars
= 0;
520 ptrdiff_t bytes_left
= nbytes
;
522 while (bytes_left
> 0)
525 c
= STRING_CHAR_AND_LENGTH (from_addr
, thislen
);
526 if (! ASCII_CHAR_P (c
))
529 from_addr
+= thislen
;
530 bytes_left
-= thislen
;
537 unsigned char *initial_to_addr
= to_addr
;
539 /* Convert single-byte to multibyte. */
542 int c
= *from_addr
++;
544 if (!ASCII_CHAR_P (c
))
546 c
= BYTE8_TO_CHAR (c
);
547 to_addr
+= CHAR_STRING (c
, to_addr
);
551 /* Special case for speed. */
552 *to_addr
++ = c
, nbytes
--;
554 return to_addr
- initial_to_addr
;
558 /* Insert a string of specified length before point.
559 This function judges multibyteness based on
560 enable_multibyte_characters in the current buffer;
561 it never converts between single-byte and multibyte.
563 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
564 prepare_to_modify_buffer could relocate the text. */
567 insert (const char *string
, ptrdiff_t nbytes
)
571 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
572 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
574 signal_after_change (opoint
, 0, len
);
575 update_compositions (opoint
, PT
, CHECK_BORDER
);
579 /* Likewise, but inherit text properties from neighboring characters. */
582 insert_and_inherit (const char *string
, ptrdiff_t nbytes
)
586 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
587 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
589 signal_after_change (opoint
, 0, len
);
590 update_compositions (opoint
, PT
, CHECK_BORDER
);
594 /* Insert the character C before point. Do not inherit text properties. */
599 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
602 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
603 len
= CHAR_STRING (c
, str
);
610 insert ((char *) str
, len
);
613 /* Insert the null-terminated string S before point. */
616 insert_string (const char *s
)
618 insert (s
, strlen (s
));
621 /* Like `insert' except that all markers pointing at the place where
622 the insertion happens are adjusted to point after it.
623 Don't use this function to insert part of a Lisp string,
624 since gc could happen and relocate it. */
627 insert_before_markers (const char *string
, ptrdiff_t nbytes
)
631 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
632 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
634 signal_after_change (opoint
, 0, len
);
635 update_compositions (opoint
, PT
, CHECK_BORDER
);
639 /* Likewise, but inherit text properties from neighboring characters. */
642 insert_before_markers_and_inherit (const char *string
,
647 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
648 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
650 signal_after_change (opoint
, 0, len
);
651 update_compositions (opoint
, PT
, CHECK_BORDER
);
655 #ifdef BYTE_COMBINING_DEBUG
657 /* See if the bytes before POS/POS_BYTE combine with bytes
658 at the start of STRING to form a single character.
659 If so, return the number of bytes at the start of STRING
660 which combine in this way. Otherwise, return 0. */
663 count_combining_before (const unsigned char *string
, ptrdiff_t length
,
664 ptrdiff_t pos
, ptrdiff_t pos_byte
)
666 int len
, combining_bytes
;
667 const unsigned char *p
;
669 if (NILP (current_buffer
->enable_multibyte_characters
))
672 /* At first, we can exclude the following cases:
673 (1) STRING[0] can't be a following byte of multibyte sequence.
674 (2) POS is the start of the current buffer.
675 (3) A character before POS is not a multibyte character. */
676 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
678 if (pos_byte
== BEG_BYTE
) /* case (2) */
681 p
= BYTE_POS_ADDR (pos_byte
- 1);
682 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
683 if (! LEADING_CODE_P (*p
)) /* case (3) */
686 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
687 if (combining_bytes
<= 0)
688 /* The character preceding POS is, complete and no room for
689 combining bytes (combining_bytes == 0), or an independent 8-bit
690 character (combining_bytes < 0). */
693 /* We have a combination situation. Count the bytes at STRING that
696 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
699 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
702 /* See if the bytes after POS/POS_BYTE combine with bytes
703 at the end of STRING to form a single character.
704 If so, return the number of bytes after POS/POS_BYTE
705 which combine in this way. Otherwise, return 0. */
708 count_combining_after (const unsigned char *string
,
709 ptrdiff_t length
, ptrdiff_t pos
, ptrdiff_t pos_byte
)
711 ptrdiff_t opos_byte
= pos_byte
;
716 if (NILP (current_buffer
->enable_multibyte_characters
))
719 /* At first, we can exclude the following cases:
720 (1) The last byte of STRING is an ASCII.
721 (2) POS is the last of the current buffer.
722 (3) A character at POS can't be a following byte of multibyte
724 if (length
> 0 && ASCII_CHAR_P (string
[length
- 1])) /* case (1) */
726 if (pos_byte
== Z_BYTE
) /* case (2) */
728 bufp
= BYTE_POS_ADDR (pos_byte
);
729 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
733 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
739 /* All characters in STRING are not character head. We must
740 check also preceding bytes at POS. We are sure that the gap
742 unsigned char *p
= BEG_ADDR
;
744 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
746 if (i
< 0 || !LEADING_CODE_P (p
[i
]))
749 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
750 return (bytes
<= pos_byte
- 1 - i
+ length
752 : bytes
- (pos_byte
- 1 - i
+ length
));
754 if (!LEADING_CODE_P (string
[i
]))
757 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
759 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
761 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
767 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
768 starting at STRING. INHERIT non-zero means inherit the text
769 properties from neighboring characters; zero means inserted text
770 will have no text properties. PREPARE non-zero means call
771 prepare_to_modify_buffer, which checks that the region is not
772 read-only, and calls before-change-function and any modification
773 properties the text may have. BEFORE_MARKERS non-zero means adjust
774 all markers that point at the insertion place to point after it. */
777 insert_1_both (const char *string
,
778 ptrdiff_t nchars
, ptrdiff_t nbytes
,
779 bool inherit
, bool prepare
, bool before_markers
)
784 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
788 /* Do this before moving and increasing the gap,
789 because the before-change hooks might move the gap
790 or make it smaller. */
791 prepare_to_modify_buffer (PT
, PT
, NULL
);
794 move_gap_both (PT
, PT_BYTE
);
795 if (GAP_SIZE
< nbytes
)
796 make_gap (nbytes
- GAP_SIZE
);
798 #ifdef BYTE_COMBINING_DEBUG
799 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
800 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
804 /* Record deletion of the surrounding text that combines with
805 the insertion. This, together with recording the insertion,
806 will add up to the right stuff in the undo list. */
807 record_insert (PT
, nchars
);
809 CHARS_MODIFF
= MODIFF
;
811 memcpy (GPT_ADDR
, string
, nbytes
);
820 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
822 eassert (GPT
<= GPT_BYTE
);
824 /* The insert may have been in the unchanged region, so check again. */
825 if (Z
- GPT
< END_UNCHANGED
)
826 END_UNCHANGED
= Z
- GPT
;
828 adjust_overlays_for_insert (PT
, nchars
);
829 adjust_markers_for_insert (PT
, PT_BYTE
,
830 PT
+ nchars
, PT_BYTE
+ nbytes
,
833 offset_intervals (current_buffer
, PT
, nchars
);
835 if (!inherit
&& buffer_intervals (current_buffer
))
836 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
839 adjust_point (nchars
, nbytes
);
844 /* Insert the part of the text of STRING, a Lisp object assumed to be
845 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
846 starting at position POS / POS_BYTE. If the text of STRING has properties,
847 copy them into the buffer.
849 It does not work to use `insert' for this, because a GC could happen
850 before we copy the stuff into the buffer, and relocate the string
851 without insert noticing. */
854 insert_from_string (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
855 ptrdiff_t length
, ptrdiff_t length_byte
, bool inherit
)
857 ptrdiff_t opoint
= PT
;
859 if (SCHARS (string
) == 0)
862 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
864 signal_after_change (opoint
, 0, PT
- opoint
);
865 update_compositions (opoint
, PT
, CHECK_BORDER
);
868 /* Like `insert_from_string' except that all markers pointing
869 at the place where the insertion happens are adjusted to point after it. */
872 insert_from_string_before_markers (Lisp_Object string
,
873 ptrdiff_t pos
, ptrdiff_t pos_byte
,
874 ptrdiff_t length
, ptrdiff_t length_byte
,
877 ptrdiff_t opoint
= PT
;
879 if (SCHARS (string
) == 0)
882 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
884 signal_after_change (opoint
, 0, PT
- opoint
);
885 update_compositions (opoint
, PT
, CHECK_BORDER
);
888 /* Subroutine of the insertion functions above. */
891 insert_from_string_1 (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
892 ptrdiff_t nchars
, ptrdiff_t nbytes
,
893 bool inherit
, bool before_markers
)
896 ptrdiff_t outgoing_nbytes
= nbytes
;
899 /* Make OUTGOING_NBYTES describe the text
900 as it will be inserted in this buffer. */
902 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
903 outgoing_nbytes
= nchars
;
904 else if (! STRING_MULTIBYTE (string
))
906 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
910 /* Do this before moving and increasing the gap,
911 because the before-change hooks might move the gap
912 or make it smaller. */
913 prepare_to_modify_buffer (PT
, PT
, NULL
);
916 move_gap_both (PT
, PT_BYTE
);
917 if (GAP_SIZE
< outgoing_nbytes
)
918 make_gap (outgoing_nbytes
- GAP_SIZE
);
921 /* Copy the string text into the buffer, perhaps converting
922 between single-byte and multibyte. */
923 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
924 STRING_MULTIBYTE (string
),
925 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
927 #ifdef BYTE_COMBINING_DEBUG
928 /* We have copied text into the gap, but we have not altered
929 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
930 to these functions and get the same results as we would
931 have got earlier on. Meanwhile, PT_ADDR does point to
932 the text that has been stored by copy_text. */
933 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
934 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
938 record_insert (PT
, nchars
);
940 CHARS_MODIFF
= MODIFF
;
942 GAP_SIZE
-= outgoing_nbytes
;
946 GPT_BYTE
+= outgoing_nbytes
;
947 ZV_BYTE
+= outgoing_nbytes
;
948 Z_BYTE
+= outgoing_nbytes
;
949 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
951 eassert (GPT
<= GPT_BYTE
);
953 /* The insert may have been in the unchanged region, so check again. */
954 if (Z
- GPT
< END_UNCHANGED
)
955 END_UNCHANGED
= Z
- GPT
;
957 adjust_overlays_for_insert (PT
, nchars
);
958 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
959 PT_BYTE
+ outgoing_nbytes
,
962 offset_intervals (current_buffer
, PT
, nchars
);
964 intervals
= string_intervals (string
);
965 /* Get the intervals for the part of the string we are inserting. */
966 if (nbytes
< SBYTES (string
))
967 intervals
= copy_intervals (intervals
, pos
, nchars
);
969 /* Insert those intervals. */
970 graft_intervals_into_buffer (intervals
, PT
, nchars
,
971 current_buffer
, inherit
);
973 adjust_point (nchars
, outgoing_nbytes
);
978 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
979 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
980 GPT_ADDR (if not text_at_gap_tail). */
983 insert_from_gap (ptrdiff_t nchars
, ptrdiff_t nbytes
, bool text_at_gap_tail
)
985 ptrdiff_t ins_charpos
= GPT
, ins_bytepos
= GPT_BYTE
;
987 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
990 /* No need to call prepare_to_modify_buffer, since this is called
991 from places that replace some region with a different text, so
992 prepare_to_modify_buffer was already called by the deletion part
994 invalidate_buffer_caches (current_buffer
, GPT
, GPT
);
995 record_insert (GPT
, nchars
);
999 if (! text_at_gap_tail
)
1008 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1010 eassert (GPT
<= GPT_BYTE
);
1012 adjust_overlays_for_insert (ins_charpos
, nchars
);
1013 adjust_markers_for_insert (ins_charpos
, ins_bytepos
,
1014 ins_charpos
+ nchars
, ins_bytepos
+ nbytes
, 0);
1016 if (buffer_intervals (current_buffer
))
1018 offset_intervals (current_buffer
, ins_charpos
, nchars
);
1019 graft_intervals_into_buffer (NULL
, ins_charpos
, nchars
,
1023 if (ins_charpos
< PT
)
1024 adjust_point (nchars
, nbytes
);
1029 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1030 current buffer. If the text in BUF has properties, they are absorbed
1031 into the current buffer.
1033 It does not work to use `insert' for this, because a malloc could happen
1034 and relocate BUF's text before the copy happens. */
1037 insert_from_buffer (struct buffer
*buf
,
1038 ptrdiff_t charpos
, ptrdiff_t nchars
, bool inherit
)
1040 ptrdiff_t opoint
= PT
;
1042 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1043 signal_after_change (opoint
, 0, PT
- opoint
);
1044 update_compositions (opoint
, PT
, CHECK_BORDER
);
1048 insert_from_buffer_1 (struct buffer
*buf
,
1049 ptrdiff_t from
, ptrdiff_t nchars
, bool inherit
)
1051 ptrdiff_t chunk
, chunk_expanded
;
1052 ptrdiff_t from_byte
= buf_charpos_to_bytepos (buf
, from
);
1053 ptrdiff_t to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1054 ptrdiff_t incoming_nbytes
= to_byte
- from_byte
;
1055 ptrdiff_t outgoing_nbytes
= incoming_nbytes
;
1061 /* Make OUTGOING_NBYTES describe the text
1062 as it will be inserted in this buffer. */
1064 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1065 outgoing_nbytes
= nchars
;
1066 else if (NILP (BVAR (buf
, enable_multibyte_characters
)))
1068 ptrdiff_t outgoing_before_gap
= 0;
1069 ptrdiff_t outgoing_after_gap
= 0;
1071 if (from
< BUF_GPT (buf
))
1073 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1074 if (chunk
> incoming_nbytes
)
1075 chunk
= incoming_nbytes
;
1077 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1083 if (chunk
< incoming_nbytes
)
1085 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1087 incoming_nbytes
- chunk
);
1089 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1092 /* Do this before moving and increasing the gap,
1093 because the before-change hooks might move the gap
1094 or make it smaller. */
1095 prepare_to_modify_buffer (PT
, PT
, NULL
);
1098 move_gap_both (PT
, PT_BYTE
);
1099 if (GAP_SIZE
< outgoing_nbytes
)
1100 make_gap (outgoing_nbytes
- GAP_SIZE
);
1102 if (from
< BUF_GPT (buf
))
1104 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1105 if (chunk
> incoming_nbytes
)
1106 chunk
= incoming_nbytes
;
1107 /* Record number of output bytes, so we know where
1108 to put the output from the second copy_text. */
1110 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1112 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1113 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1116 chunk_expanded
= chunk
= 0;
1118 if (chunk
< incoming_nbytes
)
1119 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1120 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1121 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1122 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1124 #ifdef BYTE_COMBINING_DEBUG
1125 /* We have copied text into the gap, but we have not altered
1126 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1127 to these functions and get the same results as we would
1128 have got earlier on. Meanwhile, GPT_ADDR does point to
1129 the text that has been stored by copy_text. */
1130 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1131 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1135 record_insert (PT
, nchars
);
1137 CHARS_MODIFF
= MODIFF
;
1139 GAP_SIZE
-= outgoing_nbytes
;
1143 GPT_BYTE
+= outgoing_nbytes
;
1144 ZV_BYTE
+= outgoing_nbytes
;
1145 Z_BYTE
+= outgoing_nbytes
;
1146 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1148 eassert (GPT
<= GPT_BYTE
);
1150 /* The insert may have been in the unchanged region, so check again. */
1151 if (Z
- GPT
< END_UNCHANGED
)
1152 END_UNCHANGED
= Z
- GPT
;
1154 adjust_overlays_for_insert (PT
, nchars
);
1155 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1156 PT_BYTE
+ outgoing_nbytes
,
1159 offset_intervals (current_buffer
, PT
, nchars
);
1161 /* Get the intervals for the part of the string we are inserting. */
1162 intervals
= buffer_intervals (buf
);
1163 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1165 if (buf
== current_buffer
&& PT
<= from
)
1167 intervals
= copy_intervals (intervals
, from
, nchars
);
1170 /* Insert those intervals. */
1171 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1173 adjust_point (nchars
, outgoing_nbytes
);
1176 /* Record undo information and adjust markers and position keepers for
1177 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1178 chars (LEN_BYTE bytes) which resides in the gap just after
1181 PREV_TEXT nil means the new text was just inserted. */
1184 adjust_after_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
1185 Lisp_Object prev_text
, ptrdiff_t len
, ptrdiff_t len_byte
)
1187 ptrdiff_t nchars_del
= 0, nbytes_del
= 0;
1189 #ifdef BYTE_COMBINING_DEBUG
1190 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1191 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1195 if (STRINGP (prev_text
))
1197 nchars_del
= SCHARS (prev_text
);
1198 nbytes_del
= SBYTES (prev_text
);
1201 /* Update various buffer positions for the new text. */
1202 GAP_SIZE
-= len_byte
;
1203 ZV
+= len
; Z
+= len
;
1204 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1205 GPT
+= len
; GPT_BYTE
+= len_byte
;
1206 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1209 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1212 adjust_markers_for_insert (from
, from_byte
,
1213 from
+ len
, from_byte
+ len_byte
, 0);
1216 record_delete (from
, prev_text
, false);
1217 record_insert (from
, len
);
1219 if (len
> nchars_del
)
1220 adjust_overlays_for_insert (from
, len
- nchars_del
);
1221 else if (len
< nchars_del
)
1222 adjust_overlays_for_delete (from
, nchars_del
- len
);
1224 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1227 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1229 /* As byte combining will decrease Z, we must check this again. */
1230 if (Z
- GPT
< END_UNCHANGED
)
1231 END_UNCHANGED
= Z
- GPT
;
1236 evaporate_overlays (from
);
1238 CHARS_MODIFF
= MODIFF
;
1241 /* Record undo information, adjust markers and position keepers for an
1242 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1243 text already exists in the current buffer but character length (TO
1244 - FROM) may be incorrect, the correct length is NEWLEN. */
1247 adjust_after_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
1248 ptrdiff_t to
, ptrdiff_t to_byte
, ptrdiff_t newlen
)
1250 ptrdiff_t len
= to
- from
, len_byte
= to_byte
- from_byte
;
1253 move_gap_both (to
, to_byte
);
1254 GAP_SIZE
+= len_byte
;
1255 GPT
-= len
; GPT_BYTE
-= len_byte
;
1256 ZV
-= len
; ZV_BYTE
-= len_byte
;
1257 Z
-= len
; Z_BYTE
-= len_byte
;
1258 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1261 /* Replace the text from character positions FROM to TO with NEW,
1262 If PREPARE, call prepare_to_modify_buffer.
1263 If INHERIT, the newly inserted text should inherit text properties
1264 from the surrounding non-deleted text. */
1266 /* Note that this does not yet handle markers quite right.
1267 Also it needs to record a single undo-entry that does a replacement
1268 rather than a separate delete and insert.
1269 That way, undo will also handle markers properly.
1271 But if MARKERS is 0, don't relocate markers. */
1274 replace_range (ptrdiff_t from
, ptrdiff_t to
, Lisp_Object
new,
1275 bool prepare
, bool inherit
, bool markers
)
1277 ptrdiff_t inschars
= SCHARS (new);
1278 ptrdiff_t insbytes
= SBYTES (new);
1279 ptrdiff_t from_byte
, to_byte
;
1280 ptrdiff_t nbytes_del
, nchars_del
;
1281 struct gcpro gcpro1
;
1283 ptrdiff_t outgoing_insbytes
= insbytes
;
1284 Lisp_Object deletion
;
1293 ptrdiff_t range_length
= to
- from
;
1294 prepare_to_modify_buffer (from
, to
, &from
);
1295 to
= from
+ range_length
;
1300 /* Make args be valid. */
1306 from_byte
= CHAR_TO_BYTE (from
);
1307 to_byte
= CHAR_TO_BYTE (to
);
1309 nchars_del
= to
- from
;
1310 nbytes_del
= to_byte
- from_byte
;
1312 if (nbytes_del
<= 0 && insbytes
== 0)
1315 /* Make OUTGOING_INSBYTES describe the text
1316 as it will be inserted in this buffer. */
1318 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1319 outgoing_insbytes
= inschars
;
1320 else if (! STRING_MULTIBYTE (new))
1322 = count_size_as_multibyte (SDATA (new), insbytes
);
1326 /* Make sure the gap is somewhere in or next to what we are deleting. */
1328 gap_right (from
, from_byte
);
1330 gap_left (to
, to_byte
, 0);
1332 /* Even if we don't record for undo, we must keep the original text
1333 because we may have to recover it because of inappropriate byte
1335 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1336 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1338 GAP_SIZE
+= nbytes_del
;
1341 ZV_BYTE
-= nbytes_del
;
1342 Z_BYTE
-= nbytes_del
;
1344 GPT_BYTE
= from_byte
;
1345 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1347 eassert (GPT
<= GPT_BYTE
);
1349 if (GPT
- BEG
< BEG_UNCHANGED
)
1350 BEG_UNCHANGED
= GPT
- BEG
;
1351 if (Z
- GPT
< END_UNCHANGED
)
1352 END_UNCHANGED
= Z
- GPT
;
1354 if (GAP_SIZE
< outgoing_insbytes
)
1355 make_gap (outgoing_insbytes
- GAP_SIZE
);
1357 /* Copy the string text into the buffer, perhaps converting
1358 between single-byte and multibyte. */
1359 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1360 STRING_MULTIBYTE (new),
1361 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1363 #ifdef BYTE_COMBINING_DEBUG
1364 /* We have copied text into the gap, but we have not marked
1365 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1366 here, for both the previous text and the following text.
1367 Meanwhile, GPT_ADDR does point to
1368 the text that has been stored by copy_text. */
1369 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1370 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
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 if (!NILP (deletion
))
1380 record_insert (from
+ SCHARS (deletion
), inschars
);
1381 record_delete (from
, deletion
, false);
1384 GAP_SIZE
-= outgoing_insbytes
;
1388 GPT_BYTE
+= outgoing_insbytes
;
1389 ZV_BYTE
+= outgoing_insbytes
;
1390 Z_BYTE
+= outgoing_insbytes
;
1391 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1393 eassert (GPT
<= GPT_BYTE
);
1395 /* Adjust markers for the deletion and the insertion. */
1397 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1398 inschars
, outgoing_insbytes
);
1400 /* Adjust the overlay center as needed. This must be done after
1401 adjusting the markers that bound the overlays. */
1402 adjust_overlays_for_delete (from
, nchars_del
);
1403 adjust_overlays_for_insert (from
, inschars
);
1405 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1407 /* Get the intervals for the part of the string we are inserting--
1408 not including the combined-before bytes. */
1409 intervals
= string_intervals (new);
1410 /* Insert those intervals. */
1411 graft_intervals_into_buffer (intervals
, from
, inschars
,
1412 current_buffer
, inherit
);
1414 /* Relocate point as if it were a marker. */
1416 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1417 (from_byte
+ outgoing_insbytes
1418 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1420 if (outgoing_insbytes
== 0)
1421 evaporate_overlays (from
);
1426 CHARS_MODIFF
= MODIFF
;
1429 signal_after_change (from
, nchars_del
, GPT
- from
);
1430 update_compositions (from
, GPT
, CHECK_BORDER
);
1433 /* Replace the text from character positions FROM to TO with
1434 the text in INS of length INSCHARS.
1435 Keep the text properties that applied to the old characters
1436 (extending them to all the new chars if there are more new chars).
1438 Note that this does not yet handle markers quite right.
1440 If MARKERS, relocate markers.
1442 Unlike most functions at this level, never call
1443 prepare_to_modify_buffer and never call signal_after_change. */
1446 replace_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1447 ptrdiff_t to
, ptrdiff_t to_byte
,
1448 const char *ins
, ptrdiff_t inschars
, ptrdiff_t insbytes
,
1451 ptrdiff_t nbytes_del
, nchars_del
;
1455 nchars_del
= to
- from
;
1456 nbytes_del
= to_byte
- from_byte
;
1458 if (nbytes_del
<= 0 && insbytes
== 0)
1461 /* Make sure the gap is somewhere in or next to what we are deleting. */
1463 gap_right (from
, from_byte
);
1465 gap_left (to
, to_byte
, 0);
1467 GAP_SIZE
+= nbytes_del
;
1470 ZV_BYTE
-= nbytes_del
;
1471 Z_BYTE
-= nbytes_del
;
1473 GPT_BYTE
= from_byte
;
1474 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1476 eassert (GPT
<= GPT_BYTE
);
1478 if (GPT
- BEG
< BEG_UNCHANGED
)
1479 BEG_UNCHANGED
= GPT
- BEG
;
1480 if (Z
- GPT
< END_UNCHANGED
)
1481 END_UNCHANGED
= Z
- GPT
;
1483 if (GAP_SIZE
< insbytes
)
1484 make_gap (insbytes
- GAP_SIZE
);
1486 /* Copy the replacement text into the buffer. */
1487 memcpy (GPT_ADDR
, ins
, insbytes
);
1489 #ifdef BYTE_COMBINING_DEBUG
1490 /* We have copied text into the gap, but we have not marked
1491 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1492 here, for both the previous text and the following text.
1493 Meanwhile, GPT_ADDR does point to
1494 the text that has been stored by copy_text. */
1495 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1496 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1500 GAP_SIZE
-= insbytes
;
1504 GPT_BYTE
+= insbytes
;
1505 ZV_BYTE
+= insbytes
;
1507 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1509 eassert (GPT
<= GPT_BYTE
);
1511 /* Adjust markers for the deletion and the insertion. */
1513 && ! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1514 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1515 inschars
, insbytes
);
1517 /* Adjust the overlay center as needed. This must be done after
1518 adjusting the markers that bound the overlays. */
1519 if (nchars_del
!= inschars
)
1521 adjust_overlays_for_insert (from
, inschars
);
1522 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1525 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1527 /* Relocate point as if it were a marker. */
1528 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1531 /* PT was within the deleted text. Move it to FROM. */
1532 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1534 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1538 evaporate_overlays (from
);
1543 CHARS_MODIFF
= MODIFF
;
1546 /* Delete characters in current buffer
1547 from FROM up to (but not including) TO.
1548 If TO comes before FROM, we delete nothing. */
1551 del_range (ptrdiff_t from
, ptrdiff_t to
)
1553 del_range_1 (from
, to
, 1, 0);
1556 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1557 RET_STRING says to return the deleted text. */
1560 del_range_1 (ptrdiff_t from
, ptrdiff_t to
, bool prepare
, bool ret_string
)
1562 ptrdiff_t from_byte
, to_byte
;
1563 Lisp_Object deletion
;
1564 struct gcpro gcpro1
;
1566 /* Make args be valid */
1577 ptrdiff_t range_length
= to
- from
;
1578 prepare_to_modify_buffer (from
, to
, &from
);
1579 to
= min (ZV
, from
+ range_length
);
1582 from_byte
= CHAR_TO_BYTE (from
);
1583 to_byte
= CHAR_TO_BYTE (to
);
1585 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1587 signal_after_change (from
, to
- from
, 0);
1588 update_compositions (from
, from
, CHECK_HEAD
);
1593 /* Like del_range_1 but args are byte positions, not char positions. */
1596 del_range_byte (ptrdiff_t from_byte
, ptrdiff_t to_byte
, bool prepare
)
1600 /* Make args be valid. */
1601 if (from_byte
< BEGV_BYTE
)
1602 from_byte
= BEGV_BYTE
;
1603 if (to_byte
> ZV_BYTE
)
1606 if (to_byte
<= from_byte
)
1609 from
= BYTE_TO_CHAR (from_byte
);
1610 to
= BYTE_TO_CHAR (to_byte
);
1614 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1615 ptrdiff_t range_length
= to
- from
;
1616 prepare_to_modify_buffer (from
, to
, &from
);
1617 to
= from
+ range_length
;
1619 if (old_from
!= from
)
1620 from_byte
= CHAR_TO_BYTE (from
);
1626 else if (old_to
== Z
- to
)
1627 to_byte
= CHAR_TO_BYTE (to
);
1630 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1631 signal_after_change (from
, to
- from
, 0);
1632 update_compositions (from
, from
, CHECK_HEAD
);
1635 /* Like del_range_1, but positions are specified both as charpos
1639 del_range_both (ptrdiff_t from
, ptrdiff_t from_byte
,
1640 ptrdiff_t to
, ptrdiff_t to_byte
, bool prepare
)
1642 /* Make args be valid */
1643 if (from_byte
< BEGV_BYTE
)
1644 from_byte
= BEGV_BYTE
;
1645 if (to_byte
> ZV_BYTE
)
1648 if (to_byte
<= from_byte
)
1658 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1659 ptrdiff_t range_length
= to
- from
;
1660 prepare_to_modify_buffer (from
, to
, &from
);
1661 to
= from
+ range_length
;
1663 if (old_from
!= from
)
1664 from_byte
= CHAR_TO_BYTE (from
);
1670 else if (old_to
== Z
- to
)
1671 to_byte
= CHAR_TO_BYTE (to
);
1674 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1675 signal_after_change (from
, to
- from
, 0);
1676 update_compositions (from
, from
, CHECK_HEAD
);
1679 /* Delete a range of text, specified both as character positions
1680 and byte positions. FROM and TO are character positions,
1681 while FROM_BYTE and TO_BYTE are byte positions.
1682 If RET_STRING, the deleted area is returned as a string. */
1685 del_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1686 ptrdiff_t to
, ptrdiff_t to_byte
, bool ret_string
)
1688 ptrdiff_t nbytes_del
, nchars_del
;
1689 Lisp_Object deletion
;
1693 nchars_del
= to
- from
;
1694 nbytes_del
= to_byte
- from_byte
;
1696 /* Make sure the gap is somewhere in or next to what we are deleting. */
1698 gap_right (from
, from_byte
);
1700 gap_left (to
, to_byte
, 0);
1702 #ifdef BYTE_COMBINING_DEBUG
1703 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1704 Z_BYTE
- to_byte
, from
, from_byte
))
1708 if (ret_string
|| ! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1709 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1713 /* Record marker adjustments, and text deletion into undo
1715 record_delete (from
, deletion
, true);
1717 /* Relocate all markers pointing into the new, larger gap to point
1718 at the end of the text before the gap. */
1719 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1722 CHARS_MODIFF
= MODIFF
;
1724 /* Relocate point as if it were a marker. */
1726 adjust_point (from
- (PT
< to
? PT
: to
),
1727 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1729 offset_intervals (current_buffer
, from
, - nchars_del
);
1731 /* Adjust the overlay center as needed. This must be done after
1732 adjusting the markers that bound the overlays. */
1733 adjust_overlays_for_delete (from
, nchars_del
);
1735 GAP_SIZE
+= nbytes_del
;
1736 ZV_BYTE
-= nbytes_del
;
1737 Z_BYTE
-= nbytes_del
;
1741 GPT_BYTE
= from_byte
;
1742 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1743 /* Put an anchor, unless called from decode_coding_object which
1744 needs to access the previous gap contents. */
1747 eassert (GPT
<= GPT_BYTE
);
1749 if (GPT
- BEG
< BEG_UNCHANGED
)
1750 BEG_UNCHANGED
= GPT
- BEG
;
1751 if (Z
- GPT
< END_UNCHANGED
)
1752 END_UNCHANGED
= Z
- GPT
;
1756 evaporate_overlays (from
);
1761 /* Call this if you're about to change the text of current buffer
1762 from character positions START to END. This checks the read-only
1763 properties of the region, calls the necessary modification hooks,
1764 and warns the next redisplay that it should pay attention to that
1768 modify_text (ptrdiff_t start
, ptrdiff_t end
)
1770 prepare_to_modify_buffer (start
, end
, NULL
);
1772 BUF_COMPUTE_UNCHANGED (current_buffer
, start
- 1, end
);
1773 if (MODIFF
<= SAVE_MODIFF
)
1774 record_first_change ();
1776 CHARS_MODIFF
= MODIFF
;
1778 bset_point_before_scroll (current_buffer
, Qnil
);
1781 /* Check that it is okay to modify the buffer between START and END,
1782 which are char positions.
1784 Run the before-change-function, if any. If intervals are in use,
1785 verify that the text to be modified is not read-only, and call
1786 any modification properties the text may have.
1788 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1789 by holding its value temporarily in a marker. */
1792 prepare_to_modify_buffer_1 (ptrdiff_t start
, ptrdiff_t end
,
1793 ptrdiff_t *preserve_ptr
)
1795 struct buffer
*base_buffer
;
1798 XSETFASTINT (temp
, start
);
1799 if (!NILP (BVAR (current_buffer
, read_only
)))
1800 Fbarf_if_buffer_read_only (temp
);
1802 bset_redisplay (current_buffer
);
1804 if (buffer_intervals (current_buffer
))
1808 Lisp_Object preserve_marker
;
1809 struct gcpro gcpro1
;
1810 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1811 GCPRO1 (preserve_marker
);
1812 verify_interval_modification (current_buffer
, start
, end
);
1813 *preserve_ptr
= marker_position (preserve_marker
);
1814 unchain_marker (XMARKER (preserve_marker
));
1818 verify_interval_modification (current_buffer
, start
, end
);
1821 /* For indirect buffers, use the base buffer to check clashes. */
1822 if (current_buffer
->base_buffer
!= 0)
1823 base_buffer
= current_buffer
->base_buffer
;
1825 base_buffer
= current_buffer
;
1827 if (inhibit_modification_hooks
)
1830 if (!NILP (BVAR (base_buffer
, file_truename
))
1831 /* Make binding buffer-file-name to nil effective. */
1832 && !NILP (BVAR (base_buffer
, filename
))
1833 && SAVE_MODIFF
>= MODIFF
)
1834 lock_file (BVAR (base_buffer
, file_truename
));
1836 /* If `select-active-regions' is non-nil, save the region text. */
1837 /* FIXME: Move this to Elisp (via before-change-functions). */
1838 if (!NILP (BVAR (current_buffer
, mark_active
))
1839 && XMARKER (BVAR (current_buffer
, mark
))->buffer
1840 && NILP (Vsaved_region_selection
)
1841 && (EQ (Vselect_active_regions
, Qonly
)
1842 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
1843 : (!NILP (Vselect_active_regions
)
1844 && !NILP (Vtransient_mark_mode
))))
1845 Vsaved_region_selection
1846 = call1 (Fsymbol_value (Qregion_extract_function
), Qnil
);
1848 signal_before_change (start
, end
, preserve_ptr
);
1849 Fset (Qdeactivate_mark
, Qt
);
1852 /* Like above, but called when we know that the buffer text
1853 will be modified and region caches should be invalidated. */
1856 prepare_to_modify_buffer (ptrdiff_t start
, ptrdiff_t end
,
1857 ptrdiff_t *preserve_ptr
)
1859 prepare_to_modify_buffer_1 (start
, end
, preserve_ptr
);
1860 invalidate_buffer_caches (current_buffer
, start
, end
);
1863 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1864 region between buffer positions START and END. */
1866 invalidate_buffer_caches (struct buffer
*buf
, ptrdiff_t start
, ptrdiff_t end
)
1868 /* Indirect buffers usually have their caches set to NULL, but we
1869 need to consider the caches of their base buffer. */
1870 if (buf
->base_buffer
)
1871 buf
= buf
->base_buffer
;
1872 /* The bidi_paragraph_cache must be invalidated first, because doing
1873 so might need to use the newline_cache (via find_newline_no_quit,
1875 if (buf
->bidi_paragraph_cache
)
1878 && start
> BUF_BEG (buf
))
1880 /* If we are deleting or replacing characters, we could
1881 create a paragraph start, because all of the characters
1882 from START to the beginning of START's line are
1883 whitespace. Therefore, we must extend the region to be
1884 invalidated up to the newline before START. */
1885 ptrdiff_t line_beg
= start
;
1886 ptrdiff_t start_byte
= buf_charpos_to_bytepos (buf
, start
);
1888 if (BUF_FETCH_BYTE (buf
, start_byte
- 1) != '\n')
1890 struct buffer
*old
= current_buffer
;
1892 set_buffer_internal (buf
);
1894 line_beg
= find_newline_no_quit (start
, start_byte
, -1,
1896 set_buffer_internal (old
);
1898 start
= line_beg
- (line_beg
> BUF_BEG (buf
));
1900 invalidate_region_cache (buf
,
1901 buf
->bidi_paragraph_cache
,
1902 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
1904 if (buf
->newline_cache
)
1905 invalidate_region_cache (buf
,
1907 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
1908 if (buf
->width_run_cache
)
1909 invalidate_region_cache (buf
,
1910 buf
->width_run_cache
,
1911 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
1914 /* These macros work with an argument named `preserve_ptr'
1915 and a local variable named `preserve_marker'. */
1917 #define PRESERVE_VALUE \
1918 if (preserve_ptr && NILP (preserve_marker)) \
1919 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1921 #define RESTORE_VALUE \
1922 if (! NILP (preserve_marker)) \
1924 *preserve_ptr = marker_position (preserve_marker); \
1925 unchain_marker (XMARKER (preserve_marker)); \
1928 #define PRESERVE_START_END \
1929 if (NILP (start_marker)) \
1930 start_marker = Fcopy_marker (start, Qnil); \
1931 if (NILP (end_marker)) \
1932 end_marker = Fcopy_marker (end, Qnil);
1934 #define FETCH_START \
1935 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1938 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1940 /* Set a variable to nil if an error occurred.
1941 Don't change the variable if there was no error.
1942 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1943 VARIABLE is the variable to maybe set to nil.
1944 NO-ERROR-FLAG is nil if there was an error,
1945 anything else meaning no error (so this function does nothing). */
1948 Lisp_Object
*location
;
1953 reset_var_on_error (void *ptr
)
1955 struct rvoe_arg
*p
= ptr
;
1957 *p
->location
= Qnil
;
1960 /* Signal a change to the buffer immediately before it happens.
1961 START_INT and END_INT are the bounds of the text to be changed.
1963 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1964 by holding its value temporarily in a marker. */
1967 signal_before_change (ptrdiff_t start_int
, ptrdiff_t end_int
,
1968 ptrdiff_t *preserve_ptr
)
1970 Lisp_Object start
, end
;
1971 Lisp_Object start_marker
, end_marker
;
1972 Lisp_Object preserve_marker
;
1973 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1974 ptrdiff_t count
= SPECPDL_INDEX ();
1975 struct rvoe_arg rvoe_arg
;
1977 start
= make_number (start_int
);
1978 end
= make_number (end_int
);
1979 preserve_marker
= Qnil
;
1980 start_marker
= Qnil
;
1982 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
1984 specbind (Qinhibit_modification_hooks
, Qt
);
1986 /* If buffer is unmodified, run a special hook for that case. The
1987 check for Vfirst_change_hook is just a minor optimization. */
1988 if (SAVE_MODIFF
>= MODIFF
1989 && !NILP (Vfirst_change_hook
))
1993 run_hook (Qfirst_change_hook
);
1996 /* Now run the before-change-functions if any. */
1997 if (!NILP (Vbefore_change_functions
))
1999 rvoe_arg
.location
= &Vbefore_change_functions
;
2000 rvoe_arg
.errorp
= 1;
2005 /* Mark before-change-functions to be reset to nil in case of error. */
2006 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2008 /* Actually run the hook functions. */
2009 CALLN (Frun_hook_with_args
, Qbefore_change_functions
,
2010 FETCH_START
, FETCH_END
);
2012 /* There was no error: unarm the reset_on_error. */
2013 rvoe_arg
.errorp
= 0;
2016 if (buffer_has_overlays ())
2019 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2020 FETCH_START
, FETCH_END
, Qnil
);
2023 if (! NILP (start_marker
))
2024 free_marker (start_marker
);
2025 if (! NILP (end_marker
))
2026 free_marker (end_marker
);
2030 unbind_to (count
, Qnil
);
2033 /* Signal a change immediately after it happens.
2034 CHARPOS is the character position of the start of the changed text.
2035 LENDEL is the number of characters of the text before the change.
2036 (Not the whole buffer; just the part that was changed.)
2037 LENINS is the number of characters in that part of the text
2038 after the change. */
2041 signal_after_change (ptrdiff_t charpos
, ptrdiff_t lendel
, ptrdiff_t lenins
)
2043 ptrdiff_t count
= SPECPDL_INDEX ();
2044 struct rvoe_arg rvoe_arg
;
2046 if (inhibit_modification_hooks
)
2049 /* If we are deferring calls to the after-change functions
2050 and there are no before-change functions,
2051 just record the args that we were going to use. */
2052 if (! NILP (Vcombine_after_change_calls
)
2053 && NILP (Vbefore_change_functions
)
2054 && !buffer_has_overlays ())
2058 if (!NILP (combine_after_change_list
)
2059 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2060 Fcombine_after_change_execute ();
2062 elt
= list3i (charpos
- BEG
, Z
- (charpos
- lendel
+ lenins
),
2064 combine_after_change_list
2065 = Fcons (elt
, combine_after_change_list
);
2066 combine_after_change_buffer
= Fcurrent_buffer ();
2071 if (!NILP (combine_after_change_list
))
2072 Fcombine_after_change_execute ();
2074 specbind (Qinhibit_modification_hooks
, Qt
);
2076 if (!NILP (Vafter_change_functions
))
2078 rvoe_arg
.location
= &Vafter_change_functions
;
2079 rvoe_arg
.errorp
= 1;
2081 /* Mark after-change-functions to be reset to nil in case of error. */
2082 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2084 /* Actually run the hook functions. */
2085 CALLN (Frun_hook_with_args
, Qafter_change_functions
,
2086 make_number (charpos
), make_number (charpos
+ lenins
),
2087 make_number (lendel
));
2089 /* There was no error: unarm the reset_on_error. */
2090 rvoe_arg
.errorp
= 0;
2093 if (buffer_has_overlays ())
2094 report_overlay_modification (make_number (charpos
),
2095 make_number (charpos
+ lenins
),
2097 make_number (charpos
),
2098 make_number (charpos
+ lenins
),
2099 make_number (lendel
));
2101 /* After an insertion, call the text properties
2102 insert-behind-hooks or insert-in-front-hooks. */
2104 report_interval_modification (make_number (charpos
),
2105 make_number (charpos
+ lenins
));
2107 unbind_to (count
, Qnil
);
2111 Fcombine_after_change_execute_1 (Lisp_Object val
)
2113 Vcombine_after_change_calls
= val
;
2116 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2117 Scombine_after_change_execute
, 0, 0, 0,
2118 doc
: /* This function is for use internally in the function `combine-after-change-calls'. */)
2121 ptrdiff_t count
= SPECPDL_INDEX ();
2122 ptrdiff_t beg
, end
, change
;
2123 ptrdiff_t begpos
, endpos
;
2126 if (NILP (combine_after_change_list
))
2129 /* It is rare for combine_after_change_buffer to be invalid, but
2130 possible. It can happen when combine-after-change-calls is
2131 non-nil, and insertion calls a file handler (e.g. through
2132 lock_file) which scribbles into a temp file -- cyd */
2133 if (!BUFFERP (combine_after_change_buffer
)
2134 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer
)))
2136 combine_after_change_list
= Qnil
;
2140 record_unwind_current_buffer ();
2142 Fset_buffer (combine_after_change_buffer
);
2144 /* # chars unchanged at beginning of buffer. */
2146 /* # chars unchanged at end of buffer. */
2148 /* Total amount of insertion (negative for deletion). */
2151 /* Scan the various individual changes,
2152 accumulating the range info in BEG, END and CHANGE. */
2153 for (tail
= combine_after_change_list
; CONSP (tail
);
2157 ptrdiff_t thisbeg
, thisend
, thischange
;
2159 /* Extract the info from the next element. */
2163 thisbeg
= XINT (XCAR (elt
));
2168 thisend
= XINT (XCAR (elt
));
2173 thischange
= XINT (XCAR (elt
));
2175 /* Merge this range into the accumulated range. */
2176 change
+= thischange
;
2183 /* Get the current start and end positions of the range
2184 that was changed. */
2188 /* We are about to handle these, so discard them. */
2189 combine_after_change_list
= Qnil
;
2191 /* Now run the after-change functions for real.
2192 Turn off the flag that defers them. */
2193 record_unwind_protect (Fcombine_after_change_execute_1
,
2194 Vcombine_after_change_calls
);
2195 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2196 update_compositions (begpos
, endpos
, CHECK_ALL
);
2198 return unbind_to (count
, Qnil
);
2202 syms_of_insdel (void)
2204 staticpro (&combine_after_change_list
);
2205 staticpro (&combine_after_change_buffer
);
2206 combine_after_change_list
= Qnil
;
2207 combine_after_change_buffer
= Qnil
;
2209 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls
,
2210 doc
: /* Used internally by the function `combine-after-change-calls' macro. */);
2211 Vcombine_after_change_calls
= Qnil
;
2213 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks
,
2214 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2215 This affects `before-change-functions' and `after-change-functions',
2216 as well as hooks attached to text properties and overlays. */);
2217 inhibit_modification_hooks
= 0;
2218 DEFSYM (Qinhibit_modification_hooks
, "inhibit-modification-hooks");
2220 DEFSYM (Qregion_extract_function
, "region-extract-function");
2222 defsubr (&Scombine_after_change_execute
);