1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
2 Copyright (C) 1985-1986, 1993-1995, 1997-2014 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
)
217 struct Lisp_Marker
*m
;
220 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
222 charpos
= m
->charpos
;
223 eassert (charpos
<= Z
);
225 /* If the marker is after the deletion,
226 relocate by number of chars / bytes deleted. */
229 m
->charpos
-= to
- from
;
230 m
->bytepos
-= to_byte
- from_byte
;
232 /* Here's the case where a marker is inside text being deleted. */
233 else if (charpos
> from
)
236 m
->bytepos
= from_byte
;
242 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
243 to TO / TO_BYTE. We have to relocate the charpos of every marker
244 that points after the insertion (but not their bytepos).
246 When a marker points at the insertion point,
247 we advance it if either its insertion-type is t
248 or BEFORE_MARKERS is true. */
251 adjust_markers_for_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
252 ptrdiff_t to
, ptrdiff_t to_byte
, bool before_markers
)
254 struct Lisp_Marker
*m
;
256 ptrdiff_t nchars
= to
- from
;
257 ptrdiff_t nbytes
= to_byte
- from_byte
;
259 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
261 eassert (m
->bytepos
>= m
->charpos
262 && m
->bytepos
- m
->charpos
<= Z_BYTE
- Z
);
264 if (m
->bytepos
== from_byte
)
266 if (m
->insertion_type
|| before_markers
)
268 m
->bytepos
= to_byte
;
270 if (m
->insertion_type
)
274 else if (m
->bytepos
> from_byte
)
276 m
->bytepos
+= nbytes
;
277 m
->charpos
+= nchars
;
281 /* Adjusting only markers whose insertion-type is t may result in
282 - disordered start and end in overlays, and
283 - disordered overlays in the slot `overlays_before' of current_buffer. */
286 fix_start_end_in_overlays (from
, to
);
287 fix_overlays_before (current_buffer
, from
, to
);
291 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
293 This is used only when the value of point changes due to an insert
294 or delete; it does not represent a conceptual change in point as a
295 marker. In particular, point is not crossing any interval
296 boundaries, so there's no need to use the usual SET_PT macro. In
297 fact it would be incorrect to do so, because either the old or the
298 new value of point is out of sync with the current set of
302 adjust_point (ptrdiff_t nchars
, ptrdiff_t nbytes
)
304 SET_BUF_PT_BOTH (current_buffer
, PT
+ nchars
, PT_BYTE
+ nbytes
);
305 /* In a single-byte buffer, the two positions must be equal. */
306 eassert (PT_BYTE
>= PT
&& PT_BYTE
- PT
<= ZV_BYTE
- ZV
);
309 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
310 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
311 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
315 adjust_markers_for_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
316 ptrdiff_t old_chars
, ptrdiff_t old_bytes
,
317 ptrdiff_t new_chars
, ptrdiff_t new_bytes
)
319 register struct Lisp_Marker
*m
;
320 ptrdiff_t prev_to_byte
= from_byte
+ old_bytes
;
321 ptrdiff_t diff_chars
= new_chars
- old_chars
;
322 ptrdiff_t diff_bytes
= new_bytes
- old_bytes
;
324 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
326 if (m
->bytepos
>= prev_to_byte
)
328 m
->charpos
+= diff_chars
;
329 m
->bytepos
+= diff_bytes
;
331 else if (m
->bytepos
> from_byte
)
334 m
->bytepos
= from_byte
;
343 buffer_overflow (void)
345 error ("Maximum buffer size exceeded");
348 /* Make the gap NBYTES_ADDED bytes longer. */
351 make_gap_larger (ptrdiff_t nbytes_added
)
354 ptrdiff_t real_gap_loc
;
355 ptrdiff_t real_gap_loc_byte
;
356 ptrdiff_t old_gap_size
;
357 ptrdiff_t current_size
= Z_BYTE
- BEG_BYTE
+ GAP_SIZE
;
359 if (BUF_BYTES_MAX
- current_size
< nbytes_added
)
362 /* If we have to get more space, get enough to last a while;
363 but do not exceed the maximum buffer size. */
364 nbytes_added
= min (nbytes_added
+ GAP_BYTES_DFL
,
365 BUF_BYTES_MAX
- current_size
);
367 enlarge_buffer_text (current_buffer
, nbytes_added
);
369 /* Prevent quitting in move_gap. */
374 real_gap_loc_byte
= GPT_BYTE
;
375 old_gap_size
= GAP_SIZE
;
377 /* Call the newly allocated space a gap at the end of the whole space. */
379 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
380 GAP_SIZE
= nbytes_added
;
382 /* Move the new gap down to be consecutive with the end of the old one. */
383 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
385 /* Now combine the two into one large gap. */
386 GAP_SIZE
+= old_gap_size
;
388 GPT_BYTE
= real_gap_loc_byte
;
396 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
398 /* Make the gap NBYTES_REMOVED bytes shorter. */
401 make_gap_smaller (ptrdiff_t nbytes_removed
)
404 ptrdiff_t real_gap_loc
;
405 ptrdiff_t real_gap_loc_byte
;
407 ptrdiff_t real_Z_byte
;
408 ptrdiff_t real_beg_unchanged
;
409 ptrdiff_t new_gap_size
;
411 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
412 if (GAP_SIZE
- nbytes_removed
< GAP_BYTES_MIN
)
413 nbytes_removed
= GAP_SIZE
- GAP_BYTES_MIN
;
415 /* Prevent quitting in move_gap. */
420 real_gap_loc_byte
= GPT_BYTE
;
421 new_gap_size
= GAP_SIZE
- nbytes_removed
;
423 real_Z_byte
= Z_BYTE
;
424 real_beg_unchanged
= BEG_UNCHANGED
;
426 /* Pretend that the last unwanted part of the gap is the entire gap,
427 and that the first desired part of the gap is part of the buffer
429 memset (GPT_ADDR
, 0, new_gap_size
);
431 GPT_BYTE
+= new_gap_size
;
433 Z_BYTE
+= new_gap_size
;
434 GAP_SIZE
= nbytes_removed
;
436 /* Move the unwanted pretend gap to the end of the buffer. */
437 gap_right (Z
, Z_BYTE
);
439 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
441 /* Now restore the desired gap. */
442 GAP_SIZE
= new_gap_size
;
444 GPT_BYTE
= real_gap_loc_byte
;
446 Z_BYTE
= real_Z_byte
;
447 BEG_UNCHANGED
= real_beg_unchanged
;
455 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
458 make_gap (ptrdiff_t nbytes_added
)
460 if (nbytes_added
>= 0)
461 make_gap_larger (nbytes_added
);
462 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
464 make_gap_smaller (-nbytes_added
);
468 /* Add NBYTES to B's gap. It's enough to temporarily
469 fake current_buffer and avoid real switch to B. */
472 make_gap_1 (struct buffer
*b
, ptrdiff_t nbytes
)
474 struct buffer
*oldb
= current_buffer
;
478 current_buffer
= oldb
;
481 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
482 FROM_MULTIBYTE says whether the incoming text is multibyte.
483 TO_MULTIBYTE says whether to store the text as multibyte.
484 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
486 Return the number of bytes stored at TO_ADDR. */
489 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
490 ptrdiff_t nbytes
, bool from_multibyte
, bool to_multibyte
)
492 if (from_multibyte
== to_multibyte
)
494 memcpy (to_addr
, from_addr
, nbytes
);
497 else if (from_multibyte
)
499 ptrdiff_t nchars
= 0;
500 ptrdiff_t bytes_left
= nbytes
;
502 while (bytes_left
> 0)
505 c
= STRING_CHAR_AND_LENGTH (from_addr
, thislen
);
506 if (! ASCII_CHAR_P (c
))
509 from_addr
+= thislen
;
510 bytes_left
-= thislen
;
517 unsigned char *initial_to_addr
= to_addr
;
519 /* Convert single-byte to multibyte. */
522 int c
= *from_addr
++;
524 if (!ASCII_CHAR_P (c
))
526 c
= BYTE8_TO_CHAR (c
);
527 to_addr
+= CHAR_STRING (c
, to_addr
);
531 /* Special case for speed. */
532 *to_addr
++ = c
, nbytes
--;
534 return to_addr
- initial_to_addr
;
538 /* Insert a string of specified length before point.
539 This function judges multibyteness based on
540 enable_multibyte_characters in the current buffer;
541 it never converts between single-byte and multibyte.
543 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
544 prepare_to_modify_buffer could relocate the text. */
547 insert (const char *string
, ptrdiff_t nbytes
)
551 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
552 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
554 signal_after_change (opoint
, 0, len
);
555 update_compositions (opoint
, PT
, CHECK_BORDER
);
559 /* Likewise, but inherit text properties from neighboring characters. */
562 insert_and_inherit (const char *string
, ptrdiff_t nbytes
)
566 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
567 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
569 signal_after_change (opoint
, 0, len
);
570 update_compositions (opoint
, PT
, CHECK_BORDER
);
574 /* Insert the character C before point. Do not inherit text properties. */
579 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
582 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
583 len
= CHAR_STRING (c
, str
);
590 insert ((char *) str
, len
);
593 /* Insert the null-terminated string S before point. */
596 insert_string (const char *s
)
598 insert (s
, strlen (s
));
601 /* Like `insert' except that all markers pointing at the place where
602 the insertion happens are adjusted to point after it.
603 Don't use this function to insert part of a Lisp string,
604 since gc could happen and relocate it. */
607 insert_before_markers (const char *string
, ptrdiff_t nbytes
)
611 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
612 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
614 signal_after_change (opoint
, 0, len
);
615 update_compositions (opoint
, PT
, CHECK_BORDER
);
619 /* Likewise, but inherit text properties from neighboring characters. */
622 insert_before_markers_and_inherit (const char *string
,
627 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
628 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
630 signal_after_change (opoint
, 0, len
);
631 update_compositions (opoint
, PT
, CHECK_BORDER
);
635 #ifdef BYTE_COMBINING_DEBUG
637 /* See if the bytes before POS/POS_BYTE combine with bytes
638 at the start of STRING to form a single character.
639 If so, return the number of bytes at the start of STRING
640 which combine in this way. Otherwise, return 0. */
643 count_combining_before (const unsigned char *string
, ptrdiff_t length
,
644 ptrdiff_t pos
, ptrdiff_t pos_byte
)
646 int len
, combining_bytes
;
647 const unsigned char *p
;
649 if (NILP (current_buffer
->enable_multibyte_characters
))
652 /* At first, we can exclude the following cases:
653 (1) STRING[0] can't be a following byte of multibyte sequence.
654 (2) POS is the start of the current buffer.
655 (3) A character before POS is not a multibyte character. */
656 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
658 if (pos_byte
== BEG_BYTE
) /* case (2) */
661 p
= BYTE_POS_ADDR (pos_byte
- 1);
662 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
663 if (! LEADING_CODE_P (*p
)) /* case (3) */
666 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
667 if (combining_bytes
<= 0)
668 /* The character preceding POS is, complete and no room for
669 combining bytes (combining_bytes == 0), or an independent 8-bit
670 character (combining_bytes < 0). */
673 /* We have a combination situation. Count the bytes at STRING that
676 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
679 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
682 /* See if the bytes after POS/POS_BYTE combine with bytes
683 at the end of STRING to form a single character.
684 If so, return the number of bytes after POS/POS_BYTE
685 which combine in this way. Otherwise, return 0. */
688 count_combining_after (const unsigned char *string
,
689 ptrdiff_t length
, ptrdiff_t pos
, ptrdiff_t pos_byte
)
691 ptrdiff_t opos_byte
= pos_byte
;
696 if (NILP (current_buffer
->enable_multibyte_characters
))
699 /* At first, we can exclude the following cases:
700 (1) The last byte of STRING is an ASCII.
701 (2) POS is the last of the current buffer.
702 (3) A character at POS can't be a following byte of multibyte
704 if (length
> 0 && ASCII_BYTE_P (string
[length
- 1])) /* case (1) */
706 if (pos_byte
== Z_BYTE
) /* case (2) */
708 bufp
= BYTE_POS_ADDR (pos_byte
);
709 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
713 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
719 /* All characters in STRING are not character head. We must
720 check also preceding bytes at POS. We are sure that the gap
722 unsigned char *p
= BEG_ADDR
;
724 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
726 if (i
< 0 || !LEADING_CODE_P (p
[i
]))
729 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
730 return (bytes
<= pos_byte
- 1 - i
+ length
732 : bytes
- (pos_byte
- 1 - i
+ length
));
734 if (!LEADING_CODE_P (string
[i
]))
737 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
739 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
741 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
747 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
748 starting at STRING. INHERIT non-zero means inherit the text
749 properties from neighboring characters; zero means inserted text
750 will have no text properties. PREPARE non-zero means call
751 prepare_to_modify_buffer, which checks that the region is not
752 read-only, and calls before-change-function and any modification
753 properties the text may have. BEFORE_MARKERS non-zero means adjust
754 all markers that point at the insertion place to point after it. */
757 insert_1_both (const char *string
,
758 ptrdiff_t nchars
, ptrdiff_t nbytes
,
759 bool inherit
, bool prepare
, bool before_markers
)
764 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
768 /* Do this before moving and increasing the gap,
769 because the before-change hooks might move the gap
770 or make it smaller. */
771 prepare_to_modify_buffer (PT
, PT
, NULL
);
774 move_gap_both (PT
, PT_BYTE
);
775 if (GAP_SIZE
< nbytes
)
776 make_gap (nbytes
- GAP_SIZE
);
778 #ifdef BYTE_COMBINING_DEBUG
779 if (count_combining_before (string
, nbytes
, PT
, PT_BYTE
)
780 || count_combining_after (string
, nbytes
, PT
, PT_BYTE
))
784 /* Record deletion of the surrounding text that combines with
785 the insertion. This, together with recording the insertion,
786 will add up to the right stuff in the undo list. */
787 record_insert (PT
, nchars
);
789 CHARS_MODIFF
= MODIFF
;
791 memcpy (GPT_ADDR
, string
, nbytes
);
800 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
802 eassert (GPT
<= GPT_BYTE
);
804 /* The insert may have been in the unchanged region, so check again. */
805 if (Z
- GPT
< END_UNCHANGED
)
806 END_UNCHANGED
= Z
- GPT
;
808 adjust_overlays_for_insert (PT
, nchars
);
809 adjust_markers_for_insert (PT
, PT_BYTE
,
810 PT
+ nchars
, PT_BYTE
+ nbytes
,
813 offset_intervals (current_buffer
, PT
, nchars
);
815 if (!inherit
&& buffer_intervals (current_buffer
))
816 set_text_properties (make_number (PT
), make_number (PT
+ nchars
),
819 adjust_point (nchars
, nbytes
);
824 /* Insert the part of the text of STRING, a Lisp object assumed to be
825 of type string, consisting of the LENGTH characters (LENGTH_BYTE bytes)
826 starting at position POS / POS_BYTE. If the text of STRING has properties,
827 copy them into the buffer.
829 It does not work to use `insert' for this, because a GC could happen
830 before we copy the stuff into the buffer, and relocate the string
831 without insert noticing. */
834 insert_from_string (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
835 ptrdiff_t length
, ptrdiff_t length_byte
, bool inherit
)
837 ptrdiff_t opoint
= PT
;
839 if (SCHARS (string
) == 0)
842 insert_from_string_1 (string
, pos
, pos_byte
, length
, length_byte
,
844 signal_after_change (opoint
, 0, PT
- opoint
);
845 update_compositions (opoint
, PT
, CHECK_BORDER
);
848 /* Like `insert_from_string' except that all markers pointing
849 at the place where the insertion happens are adjusted to point after it. */
852 insert_from_string_before_markers (Lisp_Object string
,
853 ptrdiff_t pos
, ptrdiff_t pos_byte
,
854 ptrdiff_t length
, ptrdiff_t length_byte
,
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 /* Subroutine of the insertion functions above. */
871 insert_from_string_1 (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
872 ptrdiff_t nchars
, ptrdiff_t nbytes
,
873 bool inherit
, bool before_markers
)
876 ptrdiff_t outgoing_nbytes
= nbytes
;
879 /* Make OUTGOING_NBYTES describe the text
880 as it will be inserted in this buffer. */
882 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
883 outgoing_nbytes
= nchars
;
884 else if (! STRING_MULTIBYTE (string
))
886 = count_size_as_multibyte (SDATA (string
) + pos_byte
,
890 /* Do this before moving and increasing the gap,
891 because the before-change hooks might move the gap
892 or make it smaller. */
893 prepare_to_modify_buffer (PT
, PT
, NULL
);
896 move_gap_both (PT
, PT_BYTE
);
897 if (GAP_SIZE
< outgoing_nbytes
)
898 make_gap (outgoing_nbytes
- GAP_SIZE
);
901 /* Copy the string text into the buffer, perhaps converting
902 between single-byte and multibyte. */
903 copy_text (SDATA (string
) + pos_byte
, GPT_ADDR
, nbytes
,
904 STRING_MULTIBYTE (string
),
905 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
907 #ifdef BYTE_COMBINING_DEBUG
908 /* We have copied text into the gap, but we have not altered
909 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
910 to these functions and get the same results as we would
911 have got earlier on. Meanwhile, PT_ADDR does point to
912 the text that has been stored by copy_text. */
913 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
914 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
918 record_insert (PT
, nchars
);
920 CHARS_MODIFF
= MODIFF
;
922 GAP_SIZE
-= outgoing_nbytes
;
926 GPT_BYTE
+= outgoing_nbytes
;
927 ZV_BYTE
+= outgoing_nbytes
;
928 Z_BYTE
+= outgoing_nbytes
;
929 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
931 eassert (GPT
<= GPT_BYTE
);
933 /* The insert may have been in the unchanged region, so check again. */
934 if (Z
- GPT
< END_UNCHANGED
)
935 END_UNCHANGED
= Z
- GPT
;
937 adjust_overlays_for_insert (PT
, nchars
);
938 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
939 PT_BYTE
+ outgoing_nbytes
,
942 offset_intervals (current_buffer
, PT
, nchars
);
944 intervals
= string_intervals (string
);
945 /* Get the intervals for the part of the string we are inserting. */
946 if (nbytes
< SBYTES (string
))
947 intervals
= copy_intervals (intervals
, pos
, nchars
);
949 /* Insert those intervals. */
950 graft_intervals_into_buffer (intervals
, PT
, nchars
,
951 current_buffer
, inherit
);
953 adjust_point (nchars
, outgoing_nbytes
);
958 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
959 starting at GAP_END_ADDR - NBYTES (if text_at_gap_tail) and at
960 GPT_ADDR (if not text_at_gap_tail). */
963 insert_from_gap (ptrdiff_t nchars
, ptrdiff_t nbytes
, bool text_at_gap_tail
)
965 ptrdiff_t ins_charpos
= GPT
, ins_bytepos
= GPT_BYTE
;
967 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
970 /* No need to call prepare_to_modify_buffer, since this is called
971 from places that replace some region with a different text, so
972 prepare_to_modify_buffer was already called by the deletion part
974 invalidate_buffer_caches (current_buffer
, GPT
, GPT
);
975 record_insert (GPT
, nchars
);
979 if (! text_at_gap_tail
)
988 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
990 eassert (GPT
<= GPT_BYTE
);
992 adjust_overlays_for_insert (ins_charpos
, nchars
);
993 adjust_markers_for_insert (ins_charpos
, ins_bytepos
,
994 ins_charpos
+ nchars
, ins_bytepos
+ nbytes
, 0);
996 if (buffer_intervals (current_buffer
))
998 offset_intervals (current_buffer
, ins_charpos
, nchars
);
999 graft_intervals_into_buffer (NULL
, ins_charpos
, nchars
,
1003 if (ins_charpos
< PT
)
1004 adjust_point (nchars
, nbytes
);
1009 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1010 current buffer. If the text in BUF has properties, they are absorbed
1011 into the current buffer.
1013 It does not work to use `insert' for this, because a malloc could happen
1014 and relocate BUF's text before the copy happens. */
1017 insert_from_buffer (struct buffer
*buf
,
1018 ptrdiff_t charpos
, ptrdiff_t nchars
, bool inherit
)
1020 ptrdiff_t opoint
= PT
;
1022 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1023 signal_after_change (opoint
, 0, PT
- opoint
);
1024 update_compositions (opoint
, PT
, CHECK_BORDER
);
1028 insert_from_buffer_1 (struct buffer
*buf
,
1029 ptrdiff_t from
, ptrdiff_t nchars
, bool inherit
)
1031 ptrdiff_t chunk
, chunk_expanded
;
1032 ptrdiff_t from_byte
= buf_charpos_to_bytepos (buf
, from
);
1033 ptrdiff_t to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1034 ptrdiff_t incoming_nbytes
= to_byte
- from_byte
;
1035 ptrdiff_t outgoing_nbytes
= incoming_nbytes
;
1041 /* Make OUTGOING_NBYTES describe the text
1042 as it will be inserted in this buffer. */
1044 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1045 outgoing_nbytes
= nchars
;
1046 else if (NILP (BVAR (buf
, enable_multibyte_characters
)))
1048 ptrdiff_t outgoing_before_gap
= 0;
1049 ptrdiff_t outgoing_after_gap
= 0;
1051 if (from
< BUF_GPT (buf
))
1053 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1054 if (chunk
> incoming_nbytes
)
1055 chunk
= incoming_nbytes
;
1057 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1063 if (chunk
< incoming_nbytes
)
1065 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1067 incoming_nbytes
- chunk
);
1069 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1072 /* Do this before moving and increasing the gap,
1073 because the before-change hooks might move the gap
1074 or make it smaller. */
1075 prepare_to_modify_buffer (PT
, PT
, NULL
);
1078 move_gap_both (PT
, PT_BYTE
);
1079 if (GAP_SIZE
< outgoing_nbytes
)
1080 make_gap (outgoing_nbytes
- GAP_SIZE
);
1082 if (from
< BUF_GPT (buf
))
1084 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1085 if (chunk
> incoming_nbytes
)
1086 chunk
= incoming_nbytes
;
1087 /* Record number of output bytes, so we know where
1088 to put the output from the second copy_text. */
1090 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1092 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1093 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1096 chunk_expanded
= chunk
= 0;
1098 if (chunk
< incoming_nbytes
)
1099 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1100 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1101 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1102 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1104 #ifdef BYTE_COMBINING_DEBUG
1105 /* We have copied text into the gap, but we have not altered
1106 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1107 to these functions and get the same results as we would
1108 have got earlier on. Meanwhile, GPT_ADDR does point to
1109 the text that has been stored by copy_text. */
1110 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1111 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1115 record_insert (PT
, nchars
);
1117 CHARS_MODIFF
= MODIFF
;
1119 GAP_SIZE
-= outgoing_nbytes
;
1123 GPT_BYTE
+= outgoing_nbytes
;
1124 ZV_BYTE
+= outgoing_nbytes
;
1125 Z_BYTE
+= outgoing_nbytes
;
1126 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1128 eassert (GPT
<= GPT_BYTE
);
1130 /* The insert may have been in the unchanged region, so check again. */
1131 if (Z
- GPT
< END_UNCHANGED
)
1132 END_UNCHANGED
= Z
- GPT
;
1134 adjust_overlays_for_insert (PT
, nchars
);
1135 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1136 PT_BYTE
+ outgoing_nbytes
,
1139 offset_intervals (current_buffer
, PT
, nchars
);
1141 /* Get the intervals for the part of the string we are inserting. */
1142 intervals
= buffer_intervals (buf
);
1143 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1145 if (buf
== current_buffer
&& PT
<= from
)
1147 intervals
= copy_intervals (intervals
, from
, nchars
);
1150 /* Insert those intervals. */
1151 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1153 adjust_point (nchars
, outgoing_nbytes
);
1156 /* Record undo information and adjust markers and position keepers for
1157 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1158 chars (LEN_BYTE bytes) which resides in the gap just after
1161 PREV_TEXT nil means the new text was just inserted. */
1164 adjust_after_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
1165 Lisp_Object prev_text
, ptrdiff_t len
, ptrdiff_t len_byte
)
1167 ptrdiff_t nchars_del
= 0, nbytes_del
= 0;
1169 #ifdef BYTE_COMBINING_DEBUG
1170 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1171 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1175 if (STRINGP (prev_text
))
1177 nchars_del
= SCHARS (prev_text
);
1178 nbytes_del
= SBYTES (prev_text
);
1181 /* Update various buffer positions for the new text. */
1182 GAP_SIZE
-= len_byte
;
1184 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1185 GPT
+= len
; GPT_BYTE
+= len_byte
;
1186 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1189 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1192 adjust_markers_for_insert (from
, from_byte
,
1193 from
+ len
, from_byte
+ len_byte
, 0);
1196 record_delete (from
, prev_text
, false);
1197 record_insert (from
, len
);
1199 if (len
> nchars_del
)
1200 adjust_overlays_for_insert (from
, len
- nchars_del
);
1201 else if (len
< nchars_del
)
1202 adjust_overlays_for_delete (from
, nchars_del
- len
);
1204 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1207 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1209 /* As byte combining will decrease Z, we must check this again. */
1210 if (Z
- GPT
< END_UNCHANGED
)
1211 END_UNCHANGED
= Z
- GPT
;
1216 evaporate_overlays (from
);
1218 CHARS_MODIFF
= MODIFF
;
1221 /* Record undo information, adjust markers and position keepers for an
1222 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1223 text already exists in the current buffer but character length (TO
1224 - FROM) may be incorrect, the correct length is NEWLEN. */
1227 adjust_after_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
1228 ptrdiff_t to
, ptrdiff_t to_byte
, ptrdiff_t newlen
)
1230 ptrdiff_t len
= to
- from
, len_byte
= to_byte
- from_byte
;
1233 move_gap_both (to
, to_byte
);
1234 GAP_SIZE
+= len_byte
;
1235 GPT
-= len
; GPT_BYTE
-= len_byte
;
1236 ZV
-= len
; ZV_BYTE
-= len_byte
;
1237 Z
-= len
; Z_BYTE
-= len_byte
;
1238 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1241 /* Replace the text from character positions FROM to TO with NEW,
1242 If PREPARE, call prepare_to_modify_buffer.
1243 If INHERIT, the newly inserted text should inherit text properties
1244 from the surrounding non-deleted text. */
1246 /* Note that this does not yet handle markers quite right.
1247 Also it needs to record a single undo-entry that does a replacement
1248 rather than a separate delete and insert.
1249 That way, undo will also handle markers properly.
1251 But if MARKERS is 0, don't relocate markers. */
1254 replace_range (ptrdiff_t from
, ptrdiff_t to
, Lisp_Object
new,
1255 bool prepare
, bool inherit
, bool markers
)
1257 ptrdiff_t inschars
= SCHARS (new);
1258 ptrdiff_t insbytes
= SBYTES (new);
1259 ptrdiff_t from_byte
, to_byte
;
1260 ptrdiff_t nbytes_del
, nchars_del
;
1261 struct gcpro gcpro1
;
1263 ptrdiff_t outgoing_insbytes
= insbytes
;
1264 Lisp_Object deletion
;
1273 ptrdiff_t range_length
= to
- from
;
1274 prepare_to_modify_buffer (from
, to
, &from
);
1275 to
= from
+ range_length
;
1280 /* Make args be valid. */
1286 from_byte
= CHAR_TO_BYTE (from
);
1287 to_byte
= CHAR_TO_BYTE (to
);
1289 nchars_del
= to
- from
;
1290 nbytes_del
= to_byte
- from_byte
;
1292 if (nbytes_del
<= 0 && insbytes
== 0)
1295 /* Make OUTGOING_INSBYTES describe the text
1296 as it will be inserted in this buffer. */
1298 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1299 outgoing_insbytes
= inschars
;
1300 else if (! STRING_MULTIBYTE (new))
1302 = count_size_as_multibyte (SDATA (new), insbytes
);
1306 /* Make sure the gap is somewhere in or next to what we are deleting. */
1308 gap_right (from
, from_byte
);
1310 gap_left (to
, to_byte
, 0);
1312 /* Even if we don't record for undo, we must keep the original text
1313 because we may have to recover it because of inappropriate byte
1315 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1316 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1318 GAP_SIZE
+= nbytes_del
;
1321 ZV_BYTE
-= nbytes_del
;
1322 Z_BYTE
-= nbytes_del
;
1324 GPT_BYTE
= from_byte
;
1325 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1327 eassert (GPT
<= GPT_BYTE
);
1329 if (GPT
- BEG
< BEG_UNCHANGED
)
1330 BEG_UNCHANGED
= GPT
- BEG
;
1331 if (Z
- GPT
< END_UNCHANGED
)
1332 END_UNCHANGED
= Z
- GPT
;
1334 if (GAP_SIZE
< outgoing_insbytes
)
1335 make_gap (outgoing_insbytes
- GAP_SIZE
);
1337 /* Copy the string text into the buffer, perhaps converting
1338 between single-byte and multibyte. */
1339 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1340 STRING_MULTIBYTE (new),
1341 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1343 #ifdef BYTE_COMBINING_DEBUG
1344 /* We have copied text into the gap, but we have not marked
1345 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1346 here, for both the previous text and the following text.
1347 Meanwhile, GPT_ADDR does point to
1348 the text that has been stored by copy_text. */
1349 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1350 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1354 /* Record the insertion first, so that when we undo,
1355 the deletion will be undone first. Thus, undo
1356 will insert before deleting, and thus will keep
1357 the markers before and after this text separate. */
1358 if (!NILP (deletion
))
1360 record_insert (from
+ SCHARS (deletion
), inschars
);
1361 record_delete (from
, deletion
, false);
1364 GAP_SIZE
-= outgoing_insbytes
;
1368 GPT_BYTE
+= outgoing_insbytes
;
1369 ZV_BYTE
+= outgoing_insbytes
;
1370 Z_BYTE
+= outgoing_insbytes
;
1371 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1373 eassert (GPT
<= GPT_BYTE
);
1375 /* Adjust markers for the deletion and the insertion. */
1377 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1378 inschars
, outgoing_insbytes
);
1380 /* Adjust the overlay center as needed. This must be done after
1381 adjusting the markers that bound the overlays. */
1382 adjust_overlays_for_delete (from
, nchars_del
);
1383 adjust_overlays_for_insert (from
, inschars
);
1385 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1387 /* Get the intervals for the part of the string we are inserting--
1388 not including the combined-before bytes. */
1389 intervals
= string_intervals (new);
1390 /* Insert those intervals. */
1391 graft_intervals_into_buffer (intervals
, from
, inschars
,
1392 current_buffer
, inherit
);
1394 /* Relocate point as if it were a marker. */
1396 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1397 (from_byte
+ outgoing_insbytes
1398 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1400 if (outgoing_insbytes
== 0)
1401 evaporate_overlays (from
);
1406 CHARS_MODIFF
= MODIFF
;
1409 signal_after_change (from
, nchars_del
, GPT
- from
);
1410 update_compositions (from
, GPT
, CHECK_BORDER
);
1413 /* Replace the text from character positions FROM to TO with
1414 the text in INS of length INSCHARS.
1415 Keep the text properties that applied to the old characters
1416 (extending them to all the new chars if there are more new chars).
1418 Note that this does not yet handle markers quite right.
1420 If MARKERS, relocate markers.
1422 Unlike most functions at this level, never call
1423 prepare_to_modify_buffer and never call signal_after_change. */
1426 replace_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1427 ptrdiff_t to
, ptrdiff_t to_byte
,
1428 const char *ins
, ptrdiff_t inschars
, ptrdiff_t insbytes
,
1431 ptrdiff_t nbytes_del
, nchars_del
;
1435 nchars_del
= to
- from
;
1436 nbytes_del
= to_byte
- from_byte
;
1438 if (nbytes_del
<= 0 && insbytes
== 0)
1441 /* Make sure the gap is somewhere in or next to what we are deleting. */
1443 gap_right (from
, from_byte
);
1445 gap_left (to
, to_byte
, 0);
1447 GAP_SIZE
+= nbytes_del
;
1450 ZV_BYTE
-= nbytes_del
;
1451 Z_BYTE
-= nbytes_del
;
1453 GPT_BYTE
= from_byte
;
1454 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1456 eassert (GPT
<= GPT_BYTE
);
1458 if (GPT
- BEG
< BEG_UNCHANGED
)
1459 BEG_UNCHANGED
= GPT
- BEG
;
1460 if (Z
- GPT
< END_UNCHANGED
)
1461 END_UNCHANGED
= Z
- GPT
;
1463 if (GAP_SIZE
< insbytes
)
1464 make_gap (insbytes
- GAP_SIZE
);
1466 /* Copy the replacement text into the buffer. */
1467 memcpy (GPT_ADDR
, ins
, insbytes
);
1469 #ifdef BYTE_COMBINING_DEBUG
1470 /* We have copied text into the gap, but we have not marked
1471 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1472 here, for both the previous text and the following text.
1473 Meanwhile, GPT_ADDR does point to
1474 the text that has been stored by copy_text. */
1475 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1476 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1480 GAP_SIZE
-= insbytes
;
1484 GPT_BYTE
+= insbytes
;
1485 ZV_BYTE
+= insbytes
;
1487 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1489 eassert (GPT
<= GPT_BYTE
);
1491 /* Adjust markers for the deletion and the insertion. */
1493 && ! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1494 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1495 inschars
, insbytes
);
1497 /* Adjust the overlay center as needed. This must be done after
1498 adjusting the markers that bound the overlays. */
1499 if (nchars_del
!= inschars
)
1501 adjust_overlays_for_insert (from
, inschars
);
1502 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1505 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1507 /* Relocate point as if it were a marker. */
1508 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1511 /* PT was within the deleted text. Move it to FROM. */
1512 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1514 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1518 evaporate_overlays (from
);
1523 CHARS_MODIFF
= MODIFF
;
1526 /* Delete characters in current buffer
1527 from FROM up to (but not including) TO.
1528 If TO comes before FROM, we delete nothing. */
1531 del_range (ptrdiff_t from
, ptrdiff_t to
)
1533 del_range_1 (from
, to
, 1, 0);
1536 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1537 RET_STRING says to return the deleted text. */
1540 del_range_1 (ptrdiff_t from
, ptrdiff_t to
, bool prepare
, bool ret_string
)
1542 ptrdiff_t from_byte
, to_byte
;
1543 Lisp_Object deletion
;
1544 struct gcpro gcpro1
;
1546 /* Make args be valid */
1557 ptrdiff_t range_length
= to
- from
;
1558 prepare_to_modify_buffer (from
, to
, &from
);
1559 to
= min (ZV
, from
+ range_length
);
1562 from_byte
= CHAR_TO_BYTE (from
);
1563 to_byte
= CHAR_TO_BYTE (to
);
1565 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1567 signal_after_change (from
, to
- from
, 0);
1568 update_compositions (from
, from
, CHECK_HEAD
);
1573 /* Like del_range_1 but args are byte positions, not char positions. */
1576 del_range_byte (ptrdiff_t from_byte
, ptrdiff_t to_byte
, bool prepare
)
1580 /* Make args be valid */
1581 if (from_byte
< BEGV_BYTE
)
1582 from_byte
= BEGV_BYTE
;
1583 if (to_byte
> ZV_BYTE
)
1586 if (to_byte
<= from_byte
)
1589 from
= BYTE_TO_CHAR (from_byte
);
1590 to
= BYTE_TO_CHAR (to_byte
);
1594 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1595 ptrdiff_t range_length
= to
- from
;
1596 prepare_to_modify_buffer (from
, to
, &from
);
1597 to
= from
+ range_length
;
1599 if (old_from
!= from
)
1600 from_byte
= CHAR_TO_BYTE (from
);
1606 else if (old_to
== Z
- to
)
1607 to_byte
= CHAR_TO_BYTE (to
);
1610 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1611 signal_after_change (from
, to
- from
, 0);
1612 update_compositions (from
, from
, CHECK_HEAD
);
1615 /* Like del_range_1, but positions are specified both as charpos
1619 del_range_both (ptrdiff_t from
, ptrdiff_t from_byte
,
1620 ptrdiff_t to
, ptrdiff_t to_byte
, bool prepare
)
1622 /* Make args be valid */
1623 if (from_byte
< BEGV_BYTE
)
1624 from_byte
= BEGV_BYTE
;
1625 if (to_byte
> ZV_BYTE
)
1628 if (to_byte
<= from_byte
)
1638 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1639 ptrdiff_t range_length
= to
- from
;
1640 prepare_to_modify_buffer (from
, to
, &from
);
1641 to
= from
+ range_length
;
1643 if (old_from
!= from
)
1644 from_byte
= CHAR_TO_BYTE (from
);
1650 else if (old_to
== Z
- to
)
1651 to_byte
= CHAR_TO_BYTE (to
);
1654 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1655 signal_after_change (from
, to
- from
, 0);
1656 update_compositions (from
, from
, CHECK_HEAD
);
1659 /* Delete a range of text, specified both as character positions
1660 and byte positions. FROM and TO are character positions,
1661 while FROM_BYTE and TO_BYTE are byte positions.
1662 If RET_STRING, the deleted area is returned as a string. */
1665 del_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1666 ptrdiff_t to
, ptrdiff_t to_byte
, bool ret_string
)
1668 ptrdiff_t nbytes_del
, nchars_del
;
1669 Lisp_Object deletion
;
1673 nchars_del
= to
- from
;
1674 nbytes_del
= to_byte
- from_byte
;
1676 /* Make sure the gap is somewhere in or next to what we are deleting. */
1678 gap_right (from
, from_byte
);
1680 gap_left (to
, to_byte
, 0);
1682 #ifdef BYTE_COMBINING_DEBUG
1683 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1684 Z_BYTE
- to_byte
, from
, from_byte
))
1688 if (ret_string
|| ! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1689 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1693 /* Record marker adjustments, and text deletion into undo
1695 record_delete (from
, deletion
, true);
1697 /* Relocate all markers pointing into the new, larger gap to point
1698 at the end of the text before the gap. */
1699 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1702 CHARS_MODIFF
= MODIFF
;
1704 /* Relocate point as if it were a marker. */
1706 adjust_point (from
- (PT
< to
? PT
: to
),
1707 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1709 offset_intervals (current_buffer
, from
, - nchars_del
);
1711 /* Adjust the overlay center as needed. This must be done after
1712 adjusting the markers that bound the overlays. */
1713 adjust_overlays_for_delete (from
, nchars_del
);
1715 GAP_SIZE
+= nbytes_del
;
1716 ZV_BYTE
-= nbytes_del
;
1717 Z_BYTE
-= nbytes_del
;
1721 GPT_BYTE
= from_byte
;
1722 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1723 /* Put an anchor, unless called from decode_coding_object which
1724 needs to access the previous gap contents. */
1727 eassert (GPT
<= GPT_BYTE
);
1729 if (GPT
- BEG
< BEG_UNCHANGED
)
1730 BEG_UNCHANGED
= GPT
- BEG
;
1731 if (Z
- GPT
< END_UNCHANGED
)
1732 END_UNCHANGED
= Z
- GPT
;
1736 evaporate_overlays (from
);
1741 /* Call this if you're about to change the text of current buffer
1742 from character positions START to END. This checks the read-only
1743 properties of the region, calls the necessary modification hooks,
1744 and warns the next redisplay that it should pay attention to that
1748 modify_text (ptrdiff_t start
, ptrdiff_t end
)
1750 prepare_to_modify_buffer (start
, end
, NULL
);
1752 BUF_COMPUTE_UNCHANGED (current_buffer
, start
- 1, end
);
1753 if (MODIFF
<= SAVE_MODIFF
)
1754 record_first_change ();
1756 CHARS_MODIFF
= MODIFF
;
1758 bset_point_before_scroll (current_buffer
, Qnil
);
1761 Lisp_Object Qregion_extract_function
;
1763 /* Check that it is okay to modify the buffer between START and END,
1764 which are char positions.
1766 Run the before-change-function, if any. If intervals are in use,
1767 verify that the text to be modified is not read-only, and call
1768 any modification properties the text may have.
1770 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1771 by holding its value temporarily in a marker. */
1774 prepare_to_modify_buffer_1 (ptrdiff_t start
, ptrdiff_t end
,
1775 ptrdiff_t *preserve_ptr
)
1777 struct buffer
*base_buffer
;
1779 if (!NILP (BVAR (current_buffer
, read_only
)))
1780 Fbarf_if_buffer_read_only ();
1782 bset_redisplay (current_buffer
);
1784 if (buffer_intervals (current_buffer
))
1788 Lisp_Object preserve_marker
;
1789 struct gcpro gcpro1
;
1790 preserve_marker
= Fcopy_marker (make_number (*preserve_ptr
), Qnil
);
1791 GCPRO1 (preserve_marker
);
1792 verify_interval_modification (current_buffer
, start
, end
);
1793 *preserve_ptr
= marker_position (preserve_marker
);
1794 unchain_marker (XMARKER (preserve_marker
));
1798 verify_interval_modification (current_buffer
, start
, end
);
1801 /* For indirect buffers, use the base buffer to check clashes. */
1802 if (current_buffer
->base_buffer
!= 0)
1803 base_buffer
= current_buffer
->base_buffer
;
1805 base_buffer
= current_buffer
;
1807 #ifdef CLASH_DETECTION
1808 if (!NILP (BVAR (base_buffer
, file_truename
))
1809 /* Make binding buffer-file-name to nil effective. */
1810 && !NILP (BVAR (base_buffer
, filename
))
1811 && SAVE_MODIFF
>= MODIFF
)
1812 lock_file (BVAR (base_buffer
, file_truename
));
1814 /* At least warn if this file has changed on disk since it was visited. */
1815 if (!NILP (BVAR (base_buffer
, filename
))
1816 && SAVE_MODIFF
>= MODIFF
1817 && NILP (Fverify_visited_file_modtime (Fcurrent_buffer ()))
1818 && !NILP (Ffile_exists_p (BVAR (base_buffer
, filename
))))
1819 call1 (intern ("ask-user-about-supersession-threat"),
1820 BVAR (base_buffer
,filename
));
1821 #endif /* not CLASH_DETECTION */
1823 /* If `select-active-regions' is non-nil, save the region text. */
1824 /* FIXME: Move this to Elisp (via before-change-functions). */
1825 if (!NILP (BVAR (current_buffer
, mark_active
))
1826 && !inhibit_modification_hooks
1827 && XMARKER (BVAR (current_buffer
, mark
))->buffer
1828 && NILP (Vsaved_region_selection
)
1829 && (EQ (Vselect_active_regions
, Qonly
)
1830 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
1831 : (!NILP (Vselect_active_regions
)
1832 && !NILP (Vtransient_mark_mode
))))
1833 Vsaved_region_selection
1834 = call1 (Fsymbol_value (Qregion_extract_function
), Qnil
);
1836 signal_before_change (start
, end
, preserve_ptr
);
1837 Vdeactivate_mark
= Qt
;
1840 /* Like above, but called when we know that the buffer text
1841 will be modified and region caches should be invalidated. */
1844 prepare_to_modify_buffer (ptrdiff_t start
, ptrdiff_t end
,
1845 ptrdiff_t *preserve_ptr
)
1847 prepare_to_modify_buffer_1 (start
, end
, preserve_ptr
);
1848 invalidate_buffer_caches (current_buffer
, start
, end
);
1851 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1852 region between buffer positions START and END. */
1854 invalidate_buffer_caches (struct buffer
*buf
, ptrdiff_t start
, ptrdiff_t end
)
1856 /* Indirect buffers usually have their caches set to NULL, but we
1857 need to consider the caches of their base buffer. */
1858 if (buf
->base_buffer
)
1859 buf
= buf
->base_buffer
;
1860 if (buf
->newline_cache
)
1861 invalidate_region_cache (buf
,
1863 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
1864 if (buf
->width_run_cache
)
1865 invalidate_region_cache (buf
,
1866 buf
->width_run_cache
,
1867 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
1868 if (buf
->bidi_paragraph_cache
)
1871 && start
> BUF_BEG (buf
))
1873 /* If we are deleting or replacing characters, we could
1874 create a paragraph start, because all of the characters
1875 from START to the beginning of START's line are
1876 whitespace. Therefore, we must extend the region to be
1877 invalidated up to the newline before START. */
1878 ptrdiff_t line_beg
= start
;
1879 ptrdiff_t start_byte
= buf_charpos_to_bytepos (buf
, start
);
1881 if (BUF_FETCH_BYTE (buf
, start_byte
- 1) != '\n')
1883 struct buffer
*old
= current_buffer
;
1885 set_buffer_internal (buf
);
1887 line_beg
= find_newline_no_quit (start
, start_byte
, -1,
1889 set_buffer_internal (old
);
1891 if (line_beg
> BUF_BEG (buf
))
1892 start
= line_beg
- 1;
1894 invalidate_region_cache (buf
,
1895 buf
->bidi_paragraph_cache
,
1896 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
1900 /* These macros work with an argument named `preserve_ptr'
1901 and a local variable named `preserve_marker'. */
1903 #define PRESERVE_VALUE \
1904 if (preserve_ptr && NILP (preserve_marker)) \
1905 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1907 #define RESTORE_VALUE \
1908 if (! NILP (preserve_marker)) \
1910 *preserve_ptr = marker_position (preserve_marker); \
1911 unchain_marker (XMARKER (preserve_marker)); \
1914 #define PRESERVE_START_END \
1915 if (NILP (start_marker)) \
1916 start_marker = Fcopy_marker (start, Qnil); \
1917 if (NILP (end_marker)) \
1918 end_marker = Fcopy_marker (end, Qnil);
1920 #define FETCH_START \
1921 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1924 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1926 /* Set a variable to nil if an error occurred.
1927 Don't change the variable if there was no error.
1928 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1929 VARIABLE is the variable to maybe set to nil.
1930 NO-ERROR-FLAG is nil if there was an error,
1931 anything else meaning no error (so this function does nothing). */
1934 Lisp_Object
*location
;
1939 reset_var_on_error (void *ptr
)
1941 struct rvoe_arg
*p
= ptr
;
1943 *p
->location
= Qnil
;
1946 /* Signal a change to the buffer immediately before it happens.
1947 START_INT and END_INT are the bounds of the text to be changed.
1949 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1950 by holding its value temporarily in a marker. */
1953 signal_before_change (ptrdiff_t start_int
, ptrdiff_t end_int
,
1954 ptrdiff_t *preserve_ptr
)
1956 Lisp_Object start
, end
;
1957 Lisp_Object start_marker
, end_marker
;
1958 Lisp_Object preserve_marker
;
1959 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1960 ptrdiff_t count
= SPECPDL_INDEX ();
1961 struct rvoe_arg rvoe_arg
;
1963 if (inhibit_modification_hooks
)
1966 start
= make_number (start_int
);
1967 end
= make_number (end_int
);
1968 preserve_marker
= Qnil
;
1969 start_marker
= Qnil
;
1971 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
1973 specbind (Qinhibit_modification_hooks
, Qt
);
1975 /* If buffer is unmodified, run a special hook for that case. The
1976 check for Vfirst_change_hook is just a minor optimization. */
1977 if (SAVE_MODIFF
>= MODIFF
1978 && !NILP (Vfirst_change_hook
))
1982 Frun_hooks (1, &Qfirst_change_hook
);
1985 /* Now run the before-change-functions if any. */
1986 if (!NILP (Vbefore_change_functions
))
1988 Lisp_Object args
[3];
1989 rvoe_arg
.location
= &Vbefore_change_functions
;
1990 rvoe_arg
.errorp
= 1;
1995 /* Mark before-change-functions to be reset to nil in case of error. */
1996 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
1998 /* Actually run the hook functions. */
1999 args
[0] = Qbefore_change_functions
;
2000 args
[1] = FETCH_START
;
2001 args
[2] = FETCH_END
;
2002 Frun_hook_with_args (3, args
);
2004 /* There was no error: unarm the reset_on_error. */
2005 rvoe_arg
.errorp
= 0;
2008 if (buffer_has_overlays ())
2011 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2012 FETCH_START
, FETCH_END
, Qnil
);
2015 if (! NILP (start_marker
))
2016 free_marker (start_marker
);
2017 if (! NILP (end_marker
))
2018 free_marker (end_marker
);
2022 unbind_to (count
, Qnil
);
2025 /* Signal a change immediately after it happens.
2026 CHARPOS is the character position of the start of the changed text.
2027 LENDEL is the number of characters of the text before the change.
2028 (Not the whole buffer; just the part that was changed.)
2029 LENINS is the number of characters in that part of the text
2030 after the change. */
2033 signal_after_change (ptrdiff_t charpos
, ptrdiff_t lendel
, ptrdiff_t lenins
)
2035 ptrdiff_t count
= SPECPDL_INDEX ();
2036 struct rvoe_arg rvoe_arg
;
2038 if (inhibit_modification_hooks
)
2041 /* If we are deferring calls to the after-change functions
2042 and there are no before-change functions,
2043 just record the args that we were going to use. */
2044 if (! NILP (Vcombine_after_change_calls
)
2045 && NILP (Vbefore_change_functions
)
2046 && !buffer_has_overlays ())
2050 if (!NILP (combine_after_change_list
)
2051 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2052 Fcombine_after_change_execute ();
2054 elt
= list3i (charpos
- BEG
, Z
- (charpos
- lendel
+ lenins
),
2056 combine_after_change_list
2057 = Fcons (elt
, combine_after_change_list
);
2058 combine_after_change_buffer
= Fcurrent_buffer ();
2063 if (!NILP (combine_after_change_list
))
2064 Fcombine_after_change_execute ();
2066 specbind (Qinhibit_modification_hooks
, Qt
);
2068 if (!NILP (Vafter_change_functions
))
2070 Lisp_Object args
[4];
2071 rvoe_arg
.location
= &Vafter_change_functions
;
2072 rvoe_arg
.errorp
= 1;
2074 /* Mark after-change-functions to be reset to nil in case of error. */
2075 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2077 /* Actually run the hook functions. */
2078 args
[0] = Qafter_change_functions
;
2079 XSETFASTINT (args
[1], charpos
);
2080 XSETFASTINT (args
[2], charpos
+ lenins
);
2081 XSETFASTINT (args
[3], lendel
);
2082 Frun_hook_with_args (4, args
);
2084 /* There was no error: unarm the reset_on_error. */
2085 rvoe_arg
.errorp
= 0;
2088 if (buffer_has_overlays ())
2089 report_overlay_modification (make_number (charpos
),
2090 make_number (charpos
+ lenins
),
2092 make_number (charpos
),
2093 make_number (charpos
+ lenins
),
2094 make_number (lendel
));
2096 /* After an insertion, call the text properties
2097 insert-behind-hooks or insert-in-front-hooks. */
2099 report_interval_modification (make_number (charpos
),
2100 make_number (charpos
+ lenins
));
2102 unbind_to (count
, Qnil
);
2106 Fcombine_after_change_execute_1 (Lisp_Object val
)
2108 Vcombine_after_change_calls
= val
;
2111 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2112 Scombine_after_change_execute
, 0, 0, 0,
2113 doc
: /* This function is for use internally in the function `combine-after-change-calls'. */)
2116 ptrdiff_t count
= SPECPDL_INDEX ();
2117 ptrdiff_t beg
, end
, change
;
2118 ptrdiff_t begpos
, endpos
;
2121 if (NILP (combine_after_change_list
))
2124 /* It is rare for combine_after_change_buffer to be invalid, but
2125 possible. It can happen when combine-after-change-calls is
2126 non-nil, and insertion calls a file handler (e.g. through
2127 lock_file) which scribbles into a temp file -- cyd */
2128 if (!BUFFERP (combine_after_change_buffer
)
2129 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer
)))
2131 combine_after_change_list
= Qnil
;
2135 record_unwind_current_buffer ();
2137 Fset_buffer (combine_after_change_buffer
);
2139 /* # chars unchanged at beginning of buffer. */
2141 /* # chars unchanged at end of buffer. */
2143 /* Total amount of insertion (negative for deletion). */
2146 /* Scan the various individual changes,
2147 accumulating the range info in BEG, END and CHANGE. */
2148 for (tail
= combine_after_change_list
; CONSP (tail
);
2152 ptrdiff_t thisbeg
, thisend
, thischange
;
2154 /* Extract the info from the next element. */
2158 thisbeg
= XINT (XCAR (elt
));
2163 thisend
= XINT (XCAR (elt
));
2168 thischange
= XINT (XCAR (elt
));
2170 /* Merge this range into the accumulated range. */
2171 change
+= thischange
;
2178 /* Get the current start and end positions of the range
2179 that was changed. */
2183 /* We are about to handle these, so discard them. */
2184 combine_after_change_list
= Qnil
;
2186 /* Now run the after-change functions for real.
2187 Turn off the flag that defers them. */
2188 record_unwind_protect (Fcombine_after_change_execute_1
,
2189 Vcombine_after_change_calls
);
2190 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2191 update_compositions (begpos
, endpos
, CHECK_ALL
);
2193 return unbind_to (count
, Qnil
);
2197 syms_of_insdel (void)
2199 staticpro (&combine_after_change_list
);
2200 staticpro (&combine_after_change_buffer
);
2201 combine_after_change_list
= Qnil
;
2202 combine_after_change_buffer
= Qnil
;
2204 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls
,
2205 doc
: /* Used internally by the function `combine-after-change-calls' macro. */);
2206 Vcombine_after_change_calls
= Qnil
;
2208 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks
,
2209 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2210 This affects `before-change-functions' and `after-change-functions',
2211 as well as hooks attached to text properties and overlays. */);
2212 inhibit_modification_hooks
= 0;
2213 DEFSYM (Qinhibit_modification_hooks
, "inhibit-modification-hooks");
2215 DEFSYM (Qregion_extract_function
, "region-extract-function");
2217 defsubr (&Scombine_after_change_execute
);