1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2013 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 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 byte position BYTEPOS, which is also char position CHARPOS.
88 Note that this can quit! */
91 move_gap_both (ptrdiff_t charpos
, ptrdiff_t bytepos
)
93 eassert (charpos
== BYTE_TO_CHAR (bytepos
)
94 && bytepos
== CHAR_TO_BYTE (charpos
));
95 if (bytepos
< GPT_BYTE
)
96 gap_left (charpos
, bytepos
, 0);
97 else if (bytepos
> GPT_BYTE
)
98 gap_right (charpos
, bytepos
);
101 /* Move the gap to a position less than the current GPT.
102 BYTEPOS describes the new position as a byte position,
103 and CHARPOS is the corresponding char position.
104 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
107 gap_left (ptrdiff_t charpos
, ptrdiff_t bytepos
, bool newgap
)
109 unsigned char *to
, *from
;
114 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
121 /* Now copy the characters. To move the gap down,
122 copy characters up. */
126 /* I gets number of characters left to copy. */
127 i
= new_s1
- bytepos
;
130 /* If a quit is requested, stop copying now.
131 Change BYTEPOS to be where we have actually moved the gap to. */
135 charpos
= BYTE_TO_CHAR (bytepos
);
138 /* Move at most 32000 chars before checking again for a quit. */
143 memmove (to
, from
, i
);
146 /* Adjust buffer data structure, to put the gap at BYTEPOS.
147 BYTEPOS is where the loop above stopped, which may be what
148 was specified or may be where a quit was detected. */
151 eassert (charpos
<= bytepos
);
152 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
156 /* Move the gap to a position greater than the current GPT.
157 BYTEPOS describes the new position as a byte position,
158 and CHARPOS is the corresponding char position. */
161 gap_right (ptrdiff_t charpos
, ptrdiff_t bytepos
)
163 register unsigned char *to
, *from
;
164 register ptrdiff_t i
;
167 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
174 /* Now copy the characters. To move the gap up,
175 copy characters down. */
179 /* I gets number of characters left to copy. */
180 i
= bytepos
- new_s1
;
183 /* If a quit is requested, stop copying now.
184 Change BYTEPOS to be where we have actually moved the gap to. */
188 charpos
= BYTE_TO_CHAR (bytepos
);
191 /* Move at most 32000 chars before checking again for a quit. */
195 memmove (to
, from
, i
);
201 eassert (charpos
<= bytepos
);
202 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
206 /* Adjust all markers for a deletion
207 whose range in bytes is FROM_BYTE to TO_BYTE.
208 The range in charpos is FROM to TO.
210 This function assumes that the gap is adjacent to
211 or inside of the range being deleted. */
214 adjust_markers_for_delete (ptrdiff_t from
, ptrdiff_t from_byte
,
215 ptrdiff_t to
, ptrdiff_t to_byte
)
218 register struct Lisp_Marker
*m
;
219 register ptrdiff_t charpos
;
221 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
223 charpos
= m
->charpos
;
224 eassert (charpos
<= Z
);
226 /* If the marker is after the deletion,
227 relocate by number of chars / bytes deleted. */
230 m
->charpos
-= to
- from
;
231 m
->bytepos
-= to_byte
- from_byte
;
233 /* Here's the case where a marker is inside text being deleted. */
234 else if (charpos
> from
)
236 if (! m
->insertion_type
)
237 { /* Normal markers will end up at the beginning of the
238 re-inserted text after undoing a deletion, and must be
239 adjusted to move them to the correct place. */
240 XSETMISC (marker
, m
);
241 record_marker_adjustment (marker
, from
- charpos
);
243 else if (charpos
< to
)
244 { /* Before-insertion markers will automatically move forward
245 upon re-inserting the deleted text, so we have to arrange
246 for them to move backward to the correct position. */
247 XSETMISC (marker
, m
);
248 record_marker_adjustment (marker
, to
- charpos
);
251 m
->bytepos
= from_byte
;
253 /* Here's the case where a before-insertion marker is immediately
254 before the deleted region. */
255 else if (charpos
== from
&& m
->insertion_type
)
257 /* Undoing the change uses normal insertion, which will
258 incorrectly make MARKER move forward, so we arrange for it
259 to then move backward to the correct place at the beginning
260 of the deleted region. */
261 XSETMISC (marker
, m
);
262 record_marker_adjustment (marker
, to
- from
);
268 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
269 to TO / TO_BYTE. We have to relocate the charpos of every marker
270 that points after the insertion (but not their bytepos).
272 When a marker points at the insertion point,
273 we advance it if either its insertion-type is t
274 or BEFORE_MARKERS is true. */
277 adjust_markers_for_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
278 ptrdiff_t to
, ptrdiff_t to_byte
, bool before_markers
)
280 struct Lisp_Marker
*m
;
282 ptrdiff_t nchars
= to
- from
;
283 ptrdiff_t nbytes
= to_byte
- from_byte
;
285 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
287 eassert (m
->bytepos
>= m
->charpos
288 && m
->bytepos
- m
->charpos
<= Z_BYTE
- Z
);
290 if (m
->bytepos
== from_byte
)
292 if (m
->insertion_type
|| before_markers
)
294 m
->bytepos
= to_byte
;
296 if (m
->insertion_type
)
300 else if (m
->bytepos
> from_byte
)
302 m
->bytepos
+= nbytes
;
303 m
->charpos
+= nchars
;
307 /* Adjusting only markers whose insertion-type is t may result in
308 - disordered start and end in overlays, and
309 - disordered overlays in the slot `overlays_before' of current_buffer. */
312 fix_start_end_in_overlays (from
, to
);
313 fix_overlays_before (current_buffer
, from
, to
);
317 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
319 This is used only when the value of point changes due to an insert
320 or delete; it does not represent a conceptual change in point as a
321 marker. In particular, point is not crossing any interval
322 boundaries, so there's no need to use the usual SET_PT macro. In
323 fact it would be incorrect to do so, because either the old or the
324 new value of point is out of sync with the current set of
328 adjust_point (ptrdiff_t nchars
, ptrdiff_t nbytes
)
330 SET_BUF_PT_BOTH (current_buffer
, PT
+ nchars
, PT_BYTE
+ nbytes
);
331 /* In a single-byte buffer, the two positions must be equal. */
332 eassert (PT_BYTE
>= PT
&& PT_BYTE
- PT
<= ZV_BYTE
- ZV
);
335 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
336 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
337 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
341 adjust_markers_for_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
342 ptrdiff_t old_chars
, ptrdiff_t old_bytes
,
343 ptrdiff_t new_chars
, ptrdiff_t new_bytes
)
345 register struct Lisp_Marker
*m
;
346 ptrdiff_t prev_to_byte
= from_byte
+ old_bytes
;
347 ptrdiff_t diff_chars
= new_chars
- old_chars
;
348 ptrdiff_t diff_bytes
= new_bytes
- old_bytes
;
350 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
352 if (m
->bytepos
>= prev_to_byte
)
354 m
->charpos
+= diff_chars
;
355 m
->bytepos
+= diff_bytes
;
357 else if (m
->bytepos
> from_byte
)
360 m
->bytepos
= from_byte
;
369 buffer_overflow (void)
371 error ("Maximum buffer size exceeded");
374 /* Make the gap NBYTES_ADDED bytes longer. */
377 make_gap_larger (ptrdiff_t nbytes_added
)
380 ptrdiff_t real_gap_loc
;
381 ptrdiff_t real_gap_loc_byte
;
382 ptrdiff_t old_gap_size
;
383 ptrdiff_t current_size
= Z_BYTE
- BEG_BYTE
+ GAP_SIZE
;
385 if (BUF_BYTES_MAX
- current_size
< nbytes_added
)
388 /* If we have to get more space, get enough to last a while;
389 but do not exceed the maximum buffer size. */
390 nbytes_added
= min (nbytes_added
+ GAP_BYTES_DFL
,
391 BUF_BYTES_MAX
- current_size
);
393 enlarge_buffer_text (current_buffer
, nbytes_added
);
395 /* Prevent quitting in move_gap. */
400 real_gap_loc_byte
= GPT_BYTE
;
401 old_gap_size
= GAP_SIZE
;
403 /* Call the newly allocated space a gap at the end of the whole space. */
405 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
406 GAP_SIZE
= nbytes_added
;
408 /* Move the new gap down to be consecutive with the end of the old one. */
409 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
411 /* Now combine the two into one large gap. */
412 GAP_SIZE
+= old_gap_size
;
414 GPT_BYTE
= real_gap_loc_byte
;
422 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
424 /* Make the gap NBYTES_REMOVED bytes shorter. */
427 make_gap_smaller (ptrdiff_t nbytes_removed
)
430 ptrdiff_t real_gap_loc
;
431 ptrdiff_t real_gap_loc_byte
;
433 ptrdiff_t real_Z_byte
;
434 ptrdiff_t real_beg_unchanged
;
435 ptrdiff_t new_gap_size
;
437 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
438 if (GAP_SIZE
- nbytes_removed
< GAP_BYTES_MIN
)
439 nbytes_removed
= GAP_SIZE
- GAP_BYTES_MIN
;
441 /* Prevent quitting in move_gap. */
446 real_gap_loc_byte
= GPT_BYTE
;
447 new_gap_size
= GAP_SIZE
- nbytes_removed
;
449 real_Z_byte
= Z_BYTE
;
450 real_beg_unchanged
= BEG_UNCHANGED
;
452 /* Pretend that the last unwanted part of the gap is the entire gap,
453 and that the first desired part of the gap is part of the buffer
455 memset (GPT_ADDR
, 0, new_gap_size
);
457 GPT_BYTE
+= new_gap_size
;
459 Z_BYTE
+= new_gap_size
;
460 GAP_SIZE
= nbytes_removed
;
462 /* Move the unwanted pretend gap to the end of the buffer. */
463 gap_right (Z
, Z_BYTE
);
465 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
467 /* Now restore the desired gap. */
468 GAP_SIZE
= new_gap_size
;
470 GPT_BYTE
= real_gap_loc_byte
;
472 Z_BYTE
= real_Z_byte
;
473 BEG_UNCHANGED
= real_beg_unchanged
;
481 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
484 make_gap (ptrdiff_t nbytes_added
)
486 if (nbytes_added
>= 0)
487 make_gap_larger (nbytes_added
);
488 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
490 make_gap_smaller (-nbytes_added
);
494 /* Add NBYTES to B's gap. It's enough to temporarily
495 fake current_buffer and avoid real switch to B. */
498 make_gap_1 (struct buffer
*b
, ptrdiff_t nbytes
)
500 struct buffer
*oldb
= current_buffer
;
504 current_buffer
= oldb
;
507 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
508 FROM_MULTIBYTE says whether the incoming text is multibyte.
509 TO_MULTIBYTE says whether to store the text as multibyte.
510 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
512 Return the number of bytes stored at TO_ADDR. */
515 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
516 ptrdiff_t nbytes
, bool from_multibyte
, bool to_multibyte
)
518 if (from_multibyte
== to_multibyte
)
520 memcpy (to_addr
, from_addr
, nbytes
);
523 else if (from_multibyte
)
525 ptrdiff_t nchars
= 0;
526 ptrdiff_t bytes_left
= nbytes
;
528 while (bytes_left
> 0)
531 c
= STRING_CHAR_AND_LENGTH (from_addr
, thislen
);
532 if (! ASCII_CHAR_P (c
))
535 from_addr
+= thislen
;
536 bytes_left
-= thislen
;
543 unsigned char *initial_to_addr
= to_addr
;
545 /* Convert single-byte to multibyte. */
548 int c
= *from_addr
++;
550 if (!ASCII_CHAR_P (c
))
552 c
= BYTE8_TO_CHAR (c
);
553 to_addr
+= CHAR_STRING (c
, to_addr
);
557 /* Special case for speed. */
558 *to_addr
++ = c
, nbytes
--;
560 return to_addr
- initial_to_addr
;
564 /* Insert a string of specified length before point.
565 This function judges multibyteness based on
566 enable_multibyte_characters in the current buffer;
567 it never converts between single-byte and multibyte.
569 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
570 prepare_to_modify_buffer could relocate the text. */
573 insert (const char *string
, ptrdiff_t nbytes
)
577 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
578 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
580 signal_after_change (opoint
, 0, len
);
581 update_compositions (opoint
, PT
, CHECK_BORDER
);
585 /* Likewise, but inherit text properties from neighboring characters. */
588 insert_and_inherit (const char *string
, ptrdiff_t nbytes
)
592 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
593 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
595 signal_after_change (opoint
, 0, len
);
596 update_compositions (opoint
, PT
, CHECK_BORDER
);
600 /* Insert the character C before point. Do not inherit text properties. */
605 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
608 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
609 len
= CHAR_STRING (c
, str
);
616 insert ((char *) str
, len
);
619 /* Insert the null-terminated string S before point. */
622 insert_string (const char *s
)
624 insert (s
, strlen (s
));
627 /* Like `insert' except that all markers pointing at the place where
628 the insertion happens are adjusted to point after it.
629 Don't use this function to insert part of a Lisp string,
630 since gc could happen and relocate it. */
633 insert_before_markers (const char *string
, ptrdiff_t nbytes
)
637 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
638 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
640 signal_after_change (opoint
, 0, len
);
641 update_compositions (opoint
, PT
, CHECK_BORDER
);
645 /* Likewise, but inherit text properties from neighboring characters. */
648 insert_before_markers_and_inherit (const char *string
,
653 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
654 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
656 signal_after_change (opoint
, 0, len
);
657 update_compositions (opoint
, PT
, CHECK_BORDER
);
661 #ifdef BYTE_COMBINING_DEBUG
663 /* See if the bytes before POS/POS_BYTE combine with bytes
664 at the start of STRING to form a single character.
665 If so, return the number of bytes at the start of STRING
666 which combine in this way. Otherwise, return 0. */
669 count_combining_before (const unsigned char *string
, ptrdiff_t length
,
670 ptrdiff_t pos
, ptrdiff_t pos_byte
)
672 int len
, combining_bytes
;
673 const unsigned char *p
;
675 if (NILP (current_buffer
->enable_multibyte_characters
))
678 /* At first, we can exclude the following cases:
679 (1) STRING[0] can't be a following byte of multibyte sequence.
680 (2) POS is the start of the current buffer.
681 (3) A character before POS is not a multibyte character. */
682 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
684 if (pos_byte
== BEG_BYTE
) /* case (2) */
687 p
= BYTE_POS_ADDR (pos_byte
- 1);
688 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
689 if (! LEADING_CODE_P (*p
)) /* case (3) */
692 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
693 if (combining_bytes
<= 0)
694 /* The character preceding POS is, complete and no room for
695 combining bytes (combining_bytes == 0), or an independent 8-bit
696 character (combining_bytes < 0). */
699 /* We have a combination situation. Count the bytes at STRING that
702 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
705 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
708 /* See if the bytes after POS/POS_BYTE combine with bytes
709 at the end of STRING to form a single character.
710 If so, return the number of bytes after POS/POS_BYTE
711 which combine in this way. Otherwise, return 0. */
714 count_combining_after (const unsigned char *string
,
715 ptrdiff_t length
, ptrdiff_t pos
, ptrdiff_t pos_byte
)
717 ptrdiff_t opos_byte
= pos_byte
;
722 if (NILP (current_buffer
->enable_multibyte_characters
))
725 /* At first, we can exclude the following cases:
726 (1) The last byte of STRING is an ASCII.
727 (2) POS is the last of the current buffer.
728 (3) A character at POS can't be a following byte of multibyte
730 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1])) /* case (1) */
732 if (pos_byte
== Z_BYTE
) /* case (2) */
734 bufp
= BYTE_POS_ADDR (pos_byte
);
735 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
739 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
745 /* All characters in STRING are not character head. We must
746 check also preceding bytes at POS. We are sure that the gap
748 unsigned char *p
= BEG_ADDR
;
750 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
752 if (i
< 0 || !LEADING_CODE_P (p
[i
]))
755 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
756 return (bytes
<= pos_byte
- 1 - i
+ length
758 : bytes
- (pos_byte
- 1 - i
+ length
));
760 if (!LEADING_CODE_P (string
[i
]))
763 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
765 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
767 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
773 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
774 starting at STRING. INHERIT, PREPARE and BEFORE_MARKERS
775 are the same as in insert_1. */
778 insert_1_both (const char *string
,
779 ptrdiff_t nchars
, ptrdiff_t nbytes
,
780 bool inherit
, bool prepare
, bool before_markers
)
785 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
789 /* Do this before moving and increasing the gap,
790 because the before-change hooks might move the gap
791 or make it smaller. */
792 prepare_to_modify_buffer (PT
, PT
, NULL
);
795 move_gap_both (PT
, PT_BYTE
);
796 if (GAP_SIZE
< nbytes
)
797 make_gap (nbytes
- GAP_SIZE
);
799 #ifdef BYTE_COMBINING_DEBUG
800 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
801 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
805 /* Record deletion of the surrounding text that combines with
806 the insertion. This, together with recording the insertion,
807 will add up to the right stuff in the undo list. */
808 record_insert (PT
, nchars
);
810 CHARS_MODIFF
= MODIFF
;
812 memcpy (GPT_ADDR
, string
, nbytes
);
821 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
823 eassert (GPT
<= GPT_BYTE
);
825 /* The insert may have been in the unchanged region, so check again. */
826 if (Z
- GPT
< END_UNCHANGED
)
827 END_UNCHANGED
= Z
- GPT
;
829 adjust_overlays_for_insert (PT
, nchars
);
830 adjust_markers_for_insert (PT
, PT_BYTE
,
831 PT
+ nchars
, PT_BYTE
+ nbytes
,
834 offset_intervals (current_buffer
, PT
, nchars
);
836 if (!inherit
&& buffer_intervals (current_buffer
))
837 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
840 adjust_point (nchars
, nbytes
);
845 /* Insert the part of the text of STRING, a Lisp object assumed to be
846 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
847 starting at position POS / POS_BYTE. If the text of STRING has properties,
848 copy them into the buffer.
850 It does not work to use `insert' for this, because a GC could happen
851 before we copy the stuff into the buffer, and relocate the string
852 without insert noticing. */
855 insert_from_string (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
856 ptrdiff_t length
, ptrdiff_t length_byte
, bool inherit
)
858 ptrdiff_t opoint
= PT
;
860 if (SCHARS (string
) == 0)
863 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
865 signal_after_change (opoint
, 0, PT
- opoint
);
866 update_compositions (opoint
, PT
, CHECK_BORDER
);
869 /* Like `insert_from_string' except that all markers pointing
870 at the place where the insertion happens are adjusted to point after it. */
873 insert_from_string_before_markers (Lisp_Object string
,
874 ptrdiff_t pos
, ptrdiff_t pos_byte
,
875 ptrdiff_t length
, ptrdiff_t length_byte
,
878 ptrdiff_t opoint
= PT
;
880 if (SCHARS (string
) == 0)
883 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
885 signal_after_change (opoint
, 0, PT
- opoint
);
886 update_compositions (opoint
, PT
, CHECK_BORDER
);
889 /* Subroutine of the insertion functions above. */
892 insert_from_string_1 (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
893 ptrdiff_t nchars
, ptrdiff_t nbytes
,
894 bool inherit
, bool before_markers
)
897 ptrdiff_t outgoing_nbytes
= nbytes
;
900 /* Make OUTGOING_NBYTES describe the text
901 as it will be inserted in this buffer. */
903 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
904 outgoing_nbytes
= nchars
;
905 else if (! STRING_MULTIBYTE (string
))
907 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
911 /* Do this before moving and increasing the gap,
912 because the before-change hooks might move the gap
913 or make it smaller. */
914 prepare_to_modify_buffer (PT
, PT
, NULL
);
917 move_gap_both (PT
, PT_BYTE
);
918 if (GAP_SIZE
< outgoing_nbytes
)
919 make_gap (outgoing_nbytes
- GAP_SIZE
);
922 /* Copy the string text into the buffer, perhaps converting
923 between single-byte and multibyte. */
924 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
925 STRING_MULTIBYTE (string
),
926 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
928 #ifdef BYTE_COMBINING_DEBUG
929 /* We have copied text into the gap, but we have not altered
930 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
931 to these functions and get the same results as we would
932 have got earlier on. Meanwhile, PT_ADDR does point to
933 the text that has been stored by copy_text. */
934 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
935 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
939 record_insert (PT
, nchars
);
941 CHARS_MODIFF
= MODIFF
;
943 GAP_SIZE
-= outgoing_nbytes
;
947 GPT_BYTE
+= outgoing_nbytes
;
948 ZV_BYTE
+= outgoing_nbytes
;
949 Z_BYTE
+= outgoing_nbytes
;
950 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
952 eassert (GPT
<= GPT_BYTE
);
954 /* The insert may have been in the unchanged region, so check again. */
955 if (Z
- GPT
< END_UNCHANGED
)
956 END_UNCHANGED
= Z
- GPT
;
958 adjust_overlays_for_insert (PT
, nchars
);
959 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
960 PT_BYTE
+ outgoing_nbytes
,
963 offset_intervals (current_buffer
, PT
, nchars
);
965 intervals
= string_intervals (string
);
966 /* Get the intervals for the part of the string we are inserting. */
967 if (nbytes
< SBYTES (string
))
968 intervals
= copy_intervals (intervals
, pos
, nchars
);
970 /* Insert those intervals. */
971 graft_intervals_into_buffer (intervals
, PT
, nchars
,
972 current_buffer
, inherit
);
974 adjust_point (nchars
, outgoing_nbytes
);
979 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
980 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
981 GPT_ADDR (if not text_at_gap_tail). */
984 insert_from_gap (ptrdiff_t nchars
, ptrdiff_t nbytes
, bool text_at_gap_tail
)
986 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
989 record_insert (GPT
, nchars
);
993 if (! text_at_gap_tail
)
1002 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1004 eassert (GPT
<= GPT_BYTE
);
1006 adjust_overlays_for_insert (GPT
- nchars
, nchars
);
1007 adjust_markers_for_insert (GPT
- nchars
, GPT_BYTE
- nbytes
,
1010 if (buffer_intervals (current_buffer
))
1012 offset_intervals (current_buffer
, GPT
- nchars
, nchars
);
1013 graft_intervals_into_buffer (NULL
, GPT
- nchars
, nchars
,
1017 if (! text_at_gap_tail
&& GPT
- nchars
< PT
)
1018 adjust_point (nchars
, nbytes
);
1023 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1024 current buffer. If the text in BUF has properties, they are absorbed
1025 into the current buffer.
1027 It does not work to use `insert' for this, because a malloc could happen
1028 and relocate BUF's text before the copy happens. */
1031 insert_from_buffer (struct buffer
*buf
,
1032 ptrdiff_t charpos
, ptrdiff_t nchars
, bool inherit
)
1034 ptrdiff_t opoint
= PT
;
1036 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1037 signal_after_change (opoint
, 0, PT
- opoint
);
1038 update_compositions (opoint
, PT
, CHECK_BORDER
);
1042 insert_from_buffer_1 (struct buffer
*buf
,
1043 ptrdiff_t from
, ptrdiff_t nchars
, bool inherit
)
1045 ptrdiff_t chunk
, chunk_expanded
;
1046 ptrdiff_t from_byte
= buf_charpos_to_bytepos (buf
, from
);
1047 ptrdiff_t to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1048 ptrdiff_t incoming_nbytes
= to_byte
- from_byte
;
1049 ptrdiff_t outgoing_nbytes
= incoming_nbytes
;
1052 /* Make OUTGOING_NBYTES describe the text
1053 as it will be inserted in this buffer. */
1055 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1056 outgoing_nbytes
= nchars
;
1057 else if (NILP (BVAR (buf
, enable_multibyte_characters
)))
1059 ptrdiff_t outgoing_before_gap
= 0;
1060 ptrdiff_t outgoing_after_gap
= 0;
1062 if (from
< BUF_GPT (buf
))
1064 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1065 if (chunk
> incoming_nbytes
)
1066 chunk
= incoming_nbytes
;
1068 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1074 if (chunk
< incoming_nbytes
)
1076 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1078 incoming_nbytes
- chunk
);
1080 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1083 /* Do this before moving and increasing the gap,
1084 because the before-change hooks might move the gap
1085 or make it smaller. */
1086 prepare_to_modify_buffer (PT
, PT
, NULL
);
1089 move_gap_both (PT
, PT_BYTE
);
1090 if (GAP_SIZE
< outgoing_nbytes
)
1091 make_gap (outgoing_nbytes
- GAP_SIZE
);
1093 if (from
< BUF_GPT (buf
))
1095 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1096 if (chunk
> incoming_nbytes
)
1097 chunk
= incoming_nbytes
;
1098 /* Record number of output bytes, so we know where
1099 to put the output from the second copy_text. */
1101 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1103 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1104 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1107 chunk_expanded
= chunk
= 0;
1109 if (chunk
< incoming_nbytes
)
1110 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1111 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1112 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1113 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1115 #ifdef BYTE_COMBINING_DEBUG
1116 /* We have copied text into the gap, but we have not altered
1117 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1118 to these functions and get the same results as we would
1119 have got earlier on. Meanwhile, GPT_ADDR does point to
1120 the text that has been stored by copy_text. */
1121 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1122 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1126 record_insert (PT
, nchars
);
1128 CHARS_MODIFF
= MODIFF
;
1130 GAP_SIZE
-= outgoing_nbytes
;
1134 GPT_BYTE
+= outgoing_nbytes
;
1135 ZV_BYTE
+= outgoing_nbytes
;
1136 Z_BYTE
+= outgoing_nbytes
;
1137 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1139 eassert (GPT
<= GPT_BYTE
);
1141 /* The insert may have been in the unchanged region, so check again. */
1142 if (Z
- GPT
< END_UNCHANGED
)
1143 END_UNCHANGED
= Z
- GPT
;
1145 adjust_overlays_for_insert (PT
, nchars
);
1146 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1147 PT_BYTE
+ outgoing_nbytes
,
1150 offset_intervals (current_buffer
, PT
, nchars
);
1152 /* Get the intervals for the part of the string we are inserting. */
1153 intervals
= buffer_intervals (buf
);
1154 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1156 if (buf
== current_buffer
&& PT
<= from
)
1158 intervals
= copy_intervals (intervals
, from
, nchars
);
1161 /* Insert those intervals. */
1162 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1164 adjust_point (nchars
, outgoing_nbytes
);
1167 /* Record undo information and adjust markers and position keepers for
1168 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1169 chars (LEN_BYTE bytes) which resides in the gap just after
1172 PREV_TEXT nil means the new text was just inserted. */
1175 adjust_after_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
1176 Lisp_Object prev_text
, ptrdiff_t len
, ptrdiff_t len_byte
)
1178 ptrdiff_t nchars_del
= 0, nbytes_del
= 0;
1180 #ifdef BYTE_COMBINING_DEBUG
1181 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1182 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1186 if (STRINGP (prev_text
))
1188 nchars_del
= SCHARS (prev_text
);
1189 nbytes_del
= SBYTES (prev_text
);
1192 /* Update various buffer positions for the new text. */
1193 GAP_SIZE
-= len_byte
;
1195 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1196 GPT
+= len
; GPT_BYTE
+= len_byte
;
1197 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1200 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1203 adjust_markers_for_insert (from
, from_byte
,
1204 from
+ len
, from_byte
+ len_byte
, 0);
1206 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1209 record_delete (from
, prev_text
);
1210 record_insert (from
, len
);
1213 if (len
> nchars_del
)
1214 adjust_overlays_for_insert (from
, len
- nchars_del
);
1215 else if (len
< nchars_del
)
1216 adjust_overlays_for_delete (from
, nchars_del
- len
);
1218 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1221 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1223 /* As byte combining will decrease Z, we must check this again. */
1224 if (Z
- GPT
< END_UNCHANGED
)
1225 END_UNCHANGED
= Z
- GPT
;
1230 evaporate_overlays (from
);
1232 CHARS_MODIFF
= MODIFF
;
1235 /* Record undo information, adjust markers and position keepers for an
1236 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1237 text already exists in the current buffer but character length (TO
1238 - FROM) may be incorrect, the correct length is NEWLEN. */
1241 adjust_after_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
1242 ptrdiff_t to
, ptrdiff_t to_byte
, ptrdiff_t newlen
)
1244 ptrdiff_t len
= to
- from
, len_byte
= to_byte
- from_byte
;
1247 move_gap_both (to
, to_byte
);
1248 GAP_SIZE
+= len_byte
;
1249 GPT
-= len
; GPT_BYTE
-= len_byte
;
1250 ZV
-= len
; ZV_BYTE
-= len_byte
;
1251 Z
-= len
; Z_BYTE
-= len_byte
;
1252 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1255 /* Replace the text from character positions FROM to TO with NEW,
1256 If PREPARE, call prepare_to_modify_buffer.
1257 If INHERIT, the newly inserted text should inherit text properties
1258 from the surrounding non-deleted text. */
1260 /* Note that this does not yet handle markers quite right.
1261 Also it needs to record a single undo-entry that does a replacement
1262 rather than a separate delete and insert.
1263 That way, undo will also handle markers properly.
1265 But if MARKERS is 0, don't relocate markers. */
1268 replace_range (ptrdiff_t from
, ptrdiff_t to
, Lisp_Object
new,
1269 bool prepare
, bool inherit
, bool markers
)
1271 ptrdiff_t inschars
= SCHARS (new);
1272 ptrdiff_t insbytes
= SBYTES (new);
1273 ptrdiff_t from_byte
, to_byte
;
1274 ptrdiff_t nbytes_del
, nchars_del
;
1275 struct gcpro gcpro1
;
1277 ptrdiff_t outgoing_insbytes
= insbytes
;
1278 Lisp_Object deletion
;
1287 ptrdiff_t range_length
= to
- from
;
1288 prepare_to_modify_buffer (from
, to
, &from
);
1289 to
= from
+ range_length
;
1294 /* Make args be valid. */
1300 from_byte
= CHAR_TO_BYTE (from
);
1301 to_byte
= CHAR_TO_BYTE (to
);
1303 nchars_del
= to
- from
;
1304 nbytes_del
= to_byte
- from_byte
;
1306 if (nbytes_del
<= 0 && insbytes
== 0)
1309 /* Make OUTGOING_INSBYTES describe the text
1310 as it will be inserted in this buffer. */
1312 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1313 outgoing_insbytes
= inschars
;
1314 else if (! STRING_MULTIBYTE (new))
1316 = count_size_as_multibyte (SDATA (new), insbytes
);
1320 /* Make sure the gap is somewhere in or next to what we are deleting. */
1322 gap_right (from
, from_byte
);
1324 gap_left (to
, to_byte
, 0);
1326 /* Even if we don't record for undo, we must keep the original text
1327 because we may have to recover it because of inappropriate byte
1329 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1330 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1332 GAP_SIZE
+= nbytes_del
;
1335 ZV_BYTE
-= nbytes_del
;
1336 Z_BYTE
-= nbytes_del
;
1338 GPT_BYTE
= from_byte
;
1339 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1341 eassert (GPT
<= GPT_BYTE
);
1343 if (GPT
- BEG
< BEG_UNCHANGED
)
1344 BEG_UNCHANGED
= GPT
- BEG
;
1345 if (Z
- GPT
< END_UNCHANGED
)
1346 END_UNCHANGED
= Z
- GPT
;
1348 if (GAP_SIZE
< outgoing_insbytes
)
1349 make_gap (outgoing_insbytes
- GAP_SIZE
);
1351 /* Copy the string text into the buffer, perhaps converting
1352 between single-byte and multibyte. */
1353 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1354 STRING_MULTIBYTE (new),
1355 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1357 #ifdef BYTE_COMBINING_DEBUG
1358 /* We have copied text into the gap, but we have not marked
1359 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1360 here, for both the previous text and the following text.
1361 Meanwhile, GPT_ADDR does point to
1362 the text that has been stored by copy_text. */
1363 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1364 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1368 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1370 /* Record the insertion first, so that when we undo,
1371 the deletion will be undone first. Thus, undo
1372 will insert before deleting, and thus will keep
1373 the markers before and after this text separate. */
1374 record_insert (from
+ SCHARS (deletion
), inschars
);
1375 record_delete (from
, deletion
);
1378 GAP_SIZE
-= outgoing_insbytes
;
1382 GPT_BYTE
+= outgoing_insbytes
;
1383 ZV_BYTE
+= outgoing_insbytes
;
1384 Z_BYTE
+= outgoing_insbytes
;
1385 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1387 eassert (GPT
<= GPT_BYTE
);
1389 /* Adjust markers for the deletion and the insertion. */
1391 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1392 inschars
, outgoing_insbytes
);
1394 /* Adjust the overlay center as needed. This must be done after
1395 adjusting the markers that bound the overlays. */
1396 adjust_overlays_for_delete (from
, nchars_del
);
1397 adjust_overlays_for_insert (from
, inschars
);
1399 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1401 /* Get the intervals for the part of the string we are inserting--
1402 not including the combined-before bytes. */
1403 intervals
= string_intervals (new);
1404 /* Insert those intervals. */
1405 graft_intervals_into_buffer (intervals
, from
, inschars
,
1406 current_buffer
, inherit
);
1408 /* Relocate point as if it were a marker. */
1410 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1411 (from_byte
+ outgoing_insbytes
1412 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1414 if (outgoing_insbytes
== 0)
1415 evaporate_overlays (from
);
1420 CHARS_MODIFF
= MODIFF
;
1423 signal_after_change (from
, nchars_del
, GPT
- from
);
1424 update_compositions (from
, GPT
, CHECK_BORDER
);
1427 /* Replace the text from character positions FROM to TO with
1428 the text in INS of length INSCHARS.
1429 Keep the text properties that applied to the old characters
1430 (extending them to all the new chars if there are more new chars).
1432 Note that this does not yet handle markers quite right.
1434 If MARKERS, relocate markers.
1436 Unlike most functions at this level, never call
1437 prepare_to_modify_buffer and never call signal_after_change. */
1440 replace_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1441 ptrdiff_t to
, ptrdiff_t to_byte
,
1442 const char *ins
, ptrdiff_t inschars
, ptrdiff_t insbytes
,
1445 ptrdiff_t nbytes_del
, nchars_del
;
1449 nchars_del
= to
- from
;
1450 nbytes_del
= to_byte
- from_byte
;
1452 if (nbytes_del
<= 0 && insbytes
== 0)
1455 /* Make sure the gap is somewhere in or next to what we are deleting. */
1457 gap_right (from
, from_byte
);
1459 gap_left (to
, to_byte
, 0);
1461 GAP_SIZE
+= nbytes_del
;
1464 ZV_BYTE
-= nbytes_del
;
1465 Z_BYTE
-= nbytes_del
;
1467 GPT_BYTE
= from_byte
;
1468 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1470 eassert (GPT
<= GPT_BYTE
);
1472 if (GPT
- BEG
< BEG_UNCHANGED
)
1473 BEG_UNCHANGED
= GPT
- BEG
;
1474 if (Z
- GPT
< END_UNCHANGED
)
1475 END_UNCHANGED
= Z
- GPT
;
1477 if (GAP_SIZE
< insbytes
)
1478 make_gap (insbytes
- GAP_SIZE
);
1480 /* Copy the replacement text into the buffer. */
1481 memcpy (GPT_ADDR
, ins
, insbytes
);
1483 #ifdef BYTE_COMBINING_DEBUG
1484 /* We have copied text into the gap, but we have not marked
1485 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1486 here, for both the previous text and the following text.
1487 Meanwhile, GPT_ADDR does point to
1488 the text that has been stored by copy_text. */
1489 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1490 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1494 GAP_SIZE
-= insbytes
;
1498 GPT_BYTE
+= insbytes
;
1499 ZV_BYTE
+= insbytes
;
1501 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1503 eassert (GPT
<= GPT_BYTE
);
1505 /* Adjust markers for the deletion and the insertion. */
1507 && ! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1508 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1509 inschars
, insbytes
);
1511 /* Adjust the overlay center as needed. This must be done after
1512 adjusting the markers that bound the overlays. */
1513 if (nchars_del
!= inschars
)
1515 adjust_overlays_for_insert (from
, inschars
);
1516 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1519 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1521 /* Relocate point as if it were a marker. */
1522 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1525 /* PT was within the deleted text. Move it to FROM. */
1526 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1528 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1532 evaporate_overlays (from
);
1537 CHARS_MODIFF
= MODIFF
;
1540 /* Delete characters in current buffer
1541 from FROM up to (but not including) TO.
1542 If TO comes before FROM, we delete nothing. */
1545 del_range (ptrdiff_t from
, ptrdiff_t to
)
1547 del_range_1 (from
, to
, 1, 0);
1550 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1551 RET_STRING says to return the deleted text. */
1554 del_range_1 (ptrdiff_t from
, ptrdiff_t to
, bool prepare
, bool ret_string
)
1556 ptrdiff_t from_byte
, to_byte
;
1557 Lisp_Object deletion
;
1558 struct gcpro gcpro1
;
1560 /* Make args be valid */
1571 ptrdiff_t range_length
= to
- from
;
1572 prepare_to_modify_buffer (from
, to
, &from
);
1573 to
= min (ZV
, from
+ range_length
);
1576 from_byte
= CHAR_TO_BYTE (from
);
1577 to_byte
= CHAR_TO_BYTE (to
);
1579 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1581 signal_after_change (from
, to
- from
, 0);
1582 update_compositions (from
, from
, CHECK_HEAD
);
1587 /* Like del_range_1 but args are byte positions, not char positions. */
1590 del_range_byte (ptrdiff_t from_byte
, ptrdiff_t to_byte
, bool prepare
)
1594 /* Make args be valid */
1595 if (from_byte
< BEGV_BYTE
)
1596 from_byte
= BEGV_BYTE
;
1597 if (to_byte
> ZV_BYTE
)
1600 if (to_byte
<= from_byte
)
1603 from
= BYTE_TO_CHAR (from_byte
);
1604 to
= BYTE_TO_CHAR (to_byte
);
1608 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1609 ptrdiff_t range_length
= to
- from
;
1610 prepare_to_modify_buffer (from
, to
, &from
);
1611 to
= from
+ range_length
;
1613 if (old_from
!= from
)
1614 from_byte
= CHAR_TO_BYTE (from
);
1620 else if (old_to
== Z
- to
)
1621 to_byte
= CHAR_TO_BYTE (to
);
1624 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1625 signal_after_change (from
, to
- from
, 0);
1626 update_compositions (from
, from
, CHECK_HEAD
);
1629 /* Like del_range_1, but positions are specified both as charpos
1633 del_range_both (ptrdiff_t from
, ptrdiff_t from_byte
,
1634 ptrdiff_t to
, ptrdiff_t to_byte
, bool prepare
)
1636 /* Make args be valid */
1637 if (from_byte
< BEGV_BYTE
)
1638 from_byte
= BEGV_BYTE
;
1639 if (to_byte
> ZV_BYTE
)
1642 if (to_byte
<= from_byte
)
1652 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1653 ptrdiff_t range_length
= to
- from
;
1654 prepare_to_modify_buffer (from
, to
, &from
);
1655 to
= from
+ range_length
;
1657 if (old_from
!= from
)
1658 from_byte
= CHAR_TO_BYTE (from
);
1664 else if (old_to
== Z
- to
)
1665 to_byte
= CHAR_TO_BYTE (to
);
1668 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1669 signal_after_change (from
, to
- from
, 0);
1670 update_compositions (from
, from
, CHECK_HEAD
);
1673 /* Delete a range of text, specified both as character positions
1674 and byte positions. FROM and TO are character positions,
1675 while FROM_BYTE and TO_BYTE are byte positions.
1676 If RET_STRING, the deleted area is returned as a string. */
1679 del_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1680 ptrdiff_t to
, ptrdiff_t to_byte
, bool ret_string
)
1682 ptrdiff_t nbytes_del
, nchars_del
;
1683 Lisp_Object deletion
;
1687 nchars_del
= to
- from
;
1688 nbytes_del
= to_byte
- from_byte
;
1690 /* Make sure the gap is somewhere in or next to what we are deleting. */
1692 gap_right (from
, from_byte
);
1694 gap_left (to
, to_byte
, 0);
1696 #ifdef BYTE_COMBINING_DEBUG
1697 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1698 Z_BYTE
- to_byte
, from
, from_byte
))
1702 if (ret_string
|| ! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1703 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1707 /* Relocate all markers pointing into the new, larger gap
1708 to point at the end of the text before the gap.
1709 Do this before recording the deletion,
1710 so that undo handles this after reinserting the text. */
1711 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1713 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1714 record_delete (from
, deletion
);
1716 CHARS_MODIFF
= MODIFF
;
1718 /* Relocate point as if it were a marker. */
1720 adjust_point (from
- (PT
< to
? PT
: to
),
1721 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1723 offset_intervals (current_buffer
, from
, - nchars_del
);
1725 /* Adjust the overlay center as needed. This must be done after
1726 adjusting the markers that bound the overlays. */
1727 adjust_overlays_for_delete (from
, nchars_del
);
1729 GAP_SIZE
+= nbytes_del
;
1730 ZV_BYTE
-= nbytes_del
;
1731 Z_BYTE
-= nbytes_del
;
1735 GPT_BYTE
= from_byte
;
1736 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1737 /* Put an anchor, unless called from decode_coding_object which
1738 needs to access the previous gap contents. */
1741 eassert (GPT
<= GPT_BYTE
);
1743 if (GPT
- BEG
< BEG_UNCHANGED
)
1744 BEG_UNCHANGED
= GPT
- BEG
;
1745 if (Z
- GPT
< END_UNCHANGED
)
1746 END_UNCHANGED
= Z
- GPT
;
1750 evaporate_overlays (from
);
1755 /* Call this if you're about to change the region of current buffer
1756 from character positions START to END. This checks the read-only
1757 properties of the region, calls the necessary modification hooks,
1758 and warns the next redisplay that it should pay attention to that
1761 If PRESERVE_CHARS_MODIFF, do not update CHARS_MODIFF.
1762 Otherwise set CHARS_MODIFF to the new value of MODIFF. */
1765 modify_region_1 (ptrdiff_t start
, ptrdiff_t end
, bool preserve_chars_modiff
)
1767 prepare_to_modify_buffer (start
, end
, NULL
);
1769 BUF_COMPUTE_UNCHANGED (current_buffer
, start
- 1, end
);
1771 if (MODIFF
<= SAVE_MODIFF
)
1772 record_first_change ();
1774 if (! preserve_chars_modiff
)
1775 CHARS_MODIFF
= MODIFF
;
1777 bset_point_before_scroll (current_buffer
, Qnil
);
1780 /* Check that it is okay to modify the buffer between START and END,
1781 which are char positions.
1783 Run the before-change-function, if any. If intervals are in use,
1784 verify that the text to be modified is not read-only, and call
1785 any modification properties the text may have.
1787 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1788 by holding its value temporarily in a marker. */
1791 prepare_to_modify_buffer (ptrdiff_t start
, ptrdiff_t end
,
1792 ptrdiff_t *preserve_ptr
)
1794 struct buffer
*base_buffer
;
1796 if (!NILP (BVAR (current_buffer
, read_only
)))
1797 Fbarf_if_buffer_read_only ();
1799 /* If we're modifying the buffer other than shown in a selected window,
1800 let redisplay consider other windows if this buffer is visible. */
1801 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
1802 && buffer_window_count (current_buffer
))
1803 ++windows_or_buffers_changed
;
1805 if (buffer_intervals (current_buffer
))
1809 Lisp_Object preserve_marker
;
1810 struct gcpro gcpro1
;
1811 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1812 GCPRO1 (preserve_marker
);
1813 verify_interval_modification (current_buffer
, start
, end
);
1814 *preserve_ptr
= marker_position (preserve_marker
);
1815 unchain_marker (XMARKER (preserve_marker
));
1819 verify_interval_modification (current_buffer
, start
, end
);
1822 /* For indirect buffers, use the base buffer to check clashes. */
1823 if (current_buffer
->base_buffer
!= 0)
1824 base_buffer
= current_buffer
->base_buffer
;
1826 base_buffer
= current_buffer
;
1828 #ifdef CLASH_DETECTION
1829 if (!NILP (BVAR (base_buffer
, file_truename
))
1830 /* Make binding buffer-file-name to nil effective. */
1831 && !NILP (BVAR (base_buffer
, filename
))
1832 && SAVE_MODIFF
>= MODIFF
)
1833 lock_file (BVAR (base_buffer
, file_truename
));
1835 /* At least warn if this file has changed on disk since it was visited. */
1836 if (!NILP (BVAR (base_buffer
, filename
))
1837 && SAVE_MODIFF
>= MODIFF
1838 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1839 && !NILP (Ffile_exists_p (BVAR (base_buffer
, filename
))))
1840 call1 (intern ("ask-user-about-supersession-threat"),
1841 BVAR (base_buffer
,filename
));
1842 #endif /* not CLASH_DETECTION */
1844 /* If `select-active-regions' is non-nil, save the region text. */
1845 if (!NILP (BVAR (current_buffer
, mark_active
))
1846 && !inhibit_modification_hooks
1847 && XMARKER (BVAR (current_buffer
, mark
))->buffer
1848 && NILP (Vsaved_region_selection
)
1849 && (EQ (Vselect_active_regions
, Qonly
)
1850 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
1851 : (!NILP (Vselect_active_regions
)
1852 && !NILP (Vtransient_mark_mode
))))
1854 ptrdiff_t b
= marker_position (BVAR (current_buffer
, mark
));
1857 Vsaved_region_selection
= make_buffer_string (b
, e
, 0);
1859 Vsaved_region_selection
= make_buffer_string (e
, b
, 0);
1862 signal_before_change (start
, end
, preserve_ptr
);
1864 if (current_buffer
->newline_cache
)
1865 invalidate_region_cache (current_buffer
,
1866 current_buffer
->newline_cache
,
1867 start
- BEG
, Z
- end
);
1868 if (current_buffer
->width_run_cache
)
1869 invalidate_region_cache (current_buffer
,
1870 current_buffer
->width_run_cache
,
1871 start
- BEG
, Z
- end
);
1873 Vdeactivate_mark
= Qt
;
1876 /* These macros work with an argument named `preserve_ptr'
1877 and a local variable named `preserve_marker'. */
1879 #define PRESERVE_VALUE \
1880 if (preserve_ptr && NILP (preserve_marker)) \
1881 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1883 #define RESTORE_VALUE \
1884 if (! NILP (preserve_marker)) \
1886 *preserve_ptr = marker_position (preserve_marker); \
1887 unchain_marker (XMARKER (preserve_marker)); \
1890 #define PRESERVE_START_END \
1891 if (NILP (start_marker)) \
1892 start_marker = Fcopy_marker (start, Qnil); \
1893 if (NILP (end_marker)) \
1894 end_marker = Fcopy_marker (end, Qnil);
1896 #define FETCH_START \
1897 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1900 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1902 /* Set a variable to nil if an error occurred.
1903 Don't change the variable if there was no error.
1904 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1905 VARIABLE is the variable to maybe set to nil.
1906 NO-ERROR-FLAG is nil if there was an error,
1907 anything else meaning no error (so this function does nothing). */
1909 reset_var_on_error (Lisp_Object val
)
1911 if (NILP (XCDR (val
)))
1912 Fset (XCAR (val
), Qnil
);
1916 /* Signal a change to the buffer immediately before it happens.
1917 START_INT and END_INT are the bounds of the text to be changed.
1919 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1920 by holding its value temporarily in a marker. */
1923 signal_before_change (ptrdiff_t start_int
, ptrdiff_t end_int
,
1924 ptrdiff_t *preserve_ptr
)
1926 Lisp_Object start
, end
;
1927 Lisp_Object start_marker
, end_marker
;
1928 Lisp_Object preserve_marker
;
1929 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1930 ptrdiff_t count
= SPECPDL_INDEX ();
1932 if (inhibit_modification_hooks
)
1935 start
= make_number (start_int
);
1936 end
= make_number (end_int
);
1937 preserve_marker
= Qnil
;
1938 start_marker
= Qnil
;
1940 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
1942 specbind (Qinhibit_modification_hooks
, Qt
);
1944 /* If buffer is unmodified, run a special hook for that case. The
1945 check for Vfirst_change_hook is just a minor optimization. */
1946 if (SAVE_MODIFF
>= MODIFF
1947 && !NILP (Vfirst_change_hook
))
1951 Frun_hooks (1, &Qfirst_change_hook
);
1954 /* Now run the before-change-functions if any. */
1955 if (!NILP (Vbefore_change_functions
))
1957 Lisp_Object args
[3];
1958 Lisp_Object rvoe_arg
= Fcons (Qbefore_change_functions
, Qnil
);
1963 /* Mark before-change-functions to be reset to nil in case of error. */
1964 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
1966 /* Actually run the hook functions. */
1967 args
[0] = Qbefore_change_functions
;
1968 args
[1] = FETCH_START
;
1969 args
[2] = FETCH_END
;
1970 Frun_hook_with_args (3, args
);
1972 /* There was no error: unarm the reset_on_error. */
1973 XSETCDR (rvoe_arg
, Qt
);
1976 if (buffer_has_overlays ())
1979 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
1980 FETCH_START
, FETCH_END
, Qnil
);
1983 if (! NILP (start_marker
))
1984 free_marker (start_marker
);
1985 if (! NILP (end_marker
))
1986 free_marker (end_marker
);
1990 unbind_to (count
, Qnil
);
1993 /* Signal a change immediately after it happens.
1994 CHARPOS is the character position of the start of the changed text.
1995 LENDEL is the number of characters of the text before the change.
1996 (Not the whole buffer; just the part that was changed.)
1997 LENINS is the number of characters in that part of the text
1998 after the change. */
2001 signal_after_change (ptrdiff_t charpos
, ptrdiff_t lendel
, ptrdiff_t lenins
)
2003 ptrdiff_t count
= SPECPDL_INDEX ();
2004 if (inhibit_modification_hooks
)
2007 /* If we are deferring calls to the after-change functions
2008 and there are no before-change functions,
2009 just record the args that we were going to use. */
2010 if (! NILP (Vcombine_after_change_calls
)
2011 && NILP (Vbefore_change_functions
)
2012 && !buffer_has_overlays ())
2016 if (!NILP (combine_after_change_list
)
2017 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2018 Fcombine_after_change_execute ();
2020 elt
= list3i (charpos
- BEG
, Z
- (charpos
- lendel
+ lenins
),
2022 combine_after_change_list
2023 = Fcons (elt
, combine_after_change_list
);
2024 combine_after_change_buffer
= Fcurrent_buffer ();
2029 if (!NILP (combine_after_change_list
))
2030 Fcombine_after_change_execute ();
2032 specbind (Qinhibit_modification_hooks
, Qt
);
2034 if (!NILP (Vafter_change_functions
))
2036 Lisp_Object args
[4];
2037 Lisp_Object rvoe_arg
= Fcons (Qafter_change_functions
, Qnil
);
2039 /* Mark after-change-functions to be reset to nil in case of error. */
2040 record_unwind_protect (reset_var_on_error
, rvoe_arg
);
2042 /* Actually run the hook functions. */
2043 args
[0] = Qafter_change_functions
;
2044 XSETFASTINT (args
[1], charpos
);
2045 XSETFASTINT (args
[2], charpos
+ lenins
);
2046 XSETFASTINT (args
[3], lendel
);
2047 Frun_hook_with_args (4, args
);
2049 /* There was no error: unarm the reset_on_error. */
2050 XSETCDR (rvoe_arg
, Qt
);
2053 if (buffer_has_overlays ())
2054 report_overlay_modification (make_number (charpos
),
2055 make_number (charpos
+ lenins
),
2057 make_number (charpos
),
2058 make_number (charpos
+ lenins
),
2059 make_number (lendel
));
2061 /* After an insertion, call the text properties
2062 insert-behind-hooks or insert-in-front-hooks. */
2064 report_interval_modification (make_number (charpos
),
2065 make_number (charpos
+ lenins
));
2067 unbind_to (count
, Qnil
);
2071 Fcombine_after_change_execute_1 (Lisp_Object val
)
2073 Vcombine_after_change_calls
= val
;
2077 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2078 Scombine_after_change_execute
, 0, 0, 0,
2079 doc
: /* This function is for use internally in the function `combine-after-change-calls'. */)
2082 ptrdiff_t count
= SPECPDL_INDEX ();
2083 ptrdiff_t beg
, end
, change
;
2084 ptrdiff_t begpos
, endpos
;
2087 if (NILP (combine_after_change_list
))
2090 /* It is rare for combine_after_change_buffer to be invalid, but
2091 possible. It can happen when combine-after-change-calls is
2092 non-nil, and insertion calls a file handler (e.g. through
2093 lock_file) which scribbles into a temp file -- cyd */
2094 if (!BUFFERP (combine_after_change_buffer
)
2095 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer
)))
2097 combine_after_change_list
= Qnil
;
2101 record_unwind_current_buffer ();
2103 Fset_buffer (combine_after_change_buffer
);
2105 /* # chars unchanged at beginning of buffer. */
2107 /* # chars unchanged at end of buffer. */
2109 /* Total amount of insertion (negative for deletion). */
2112 /* Scan the various individual changes,
2113 accumulating the range info in BEG, END and CHANGE. */
2114 for (tail
= combine_after_change_list
; CONSP (tail
);
2118 ptrdiff_t thisbeg
, thisend
, thischange
;
2120 /* Extract the info from the next element. */
2124 thisbeg
= XINT (XCAR (elt
));
2129 thisend
= XINT (XCAR (elt
));
2134 thischange
= XINT (XCAR (elt
));
2136 /* Merge this range into the accumulated range. */
2137 change
+= thischange
;
2144 /* Get the current start and end positions of the range
2145 that was changed. */
2149 /* We are about to handle these, so discard them. */
2150 combine_after_change_list
= Qnil
;
2152 /* Now run the after-change functions for real.
2153 Turn off the flag that defers them. */
2154 record_unwind_protect (Fcombine_after_change_execute_1
,
2155 Vcombine_after_change_calls
);
2156 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2157 update_compositions (begpos
, endpos
, CHECK_ALL
);
2159 return unbind_to (count
, Qnil
);
2163 syms_of_insdel (void)
2165 staticpro (&combine_after_change_list
);
2166 staticpro (&combine_after_change_buffer
);
2167 combine_after_change_list
= Qnil
;
2168 combine_after_change_buffer
= Qnil
;
2170 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls
,
2171 doc
: /* Used internally by the function `combine-after-change-calls' macro. */);
2172 Vcombine_after_change_calls
= Qnil
;
2174 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks
,
2175 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2176 This affects `before-change-functions' and `after-change-functions',
2177 as well as hooks attached to text properties and overlays. */);
2178 inhibit_modification_hooks
= 0;
2179 DEFSYM (Qinhibit_modification_hooks
, "inhibit-modification-hooks");
2181 defsubr (&Scombine_after_change_execute
);