1 /* Buffer insertion/deletion and gap motion for GNU Emacs.
3 Copyright (C) 1985-1986, 1993-1995, 1997-2015 Free Software Foundation,
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 #include "intervals.h"
28 #include "character.h"
31 #include "blockinput.h"
32 #include "region-cache.h"
34 static void insert_from_string_1 (Lisp_Object
, ptrdiff_t, ptrdiff_t, ptrdiff_t,
35 ptrdiff_t, bool, bool);
36 static void insert_from_buffer_1 (struct buffer
*, ptrdiff_t, ptrdiff_t, bool);
37 static void gap_left (ptrdiff_t, ptrdiff_t, bool);
38 static void gap_right (ptrdiff_t, ptrdiff_t);
40 /* List of elements of the form (BEG-UNCHANGED END-UNCHANGED CHANGE-AMOUNT)
41 describing changes which happened while combine_after_change_calls
42 was non-nil. We use this to decide how to call them
43 once the deferral ends.
46 BEG-UNCHANGED is the number of chars before the changed range.
47 END-UNCHANGED is the number of chars after the changed range,
48 and CHANGE-AMOUNT is the number of characters inserted by the change
49 (negative for a deletion). */
50 static Lisp_Object combine_after_change_list
;
52 /* Buffer which combine_after_change_list is about. */
53 static Lisp_Object combine_after_change_buffer
;
55 static void signal_before_change (ptrdiff_t, ptrdiff_t, ptrdiff_t *);
57 /* Also used in marker.c to enable expensive marker checks. */
64 struct Lisp_Marker
*tail
;
65 bool multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
67 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
69 if (tail
->buffer
->text
!= current_buffer
->text
)
71 if (tail
->charpos
> Z
)
73 if (tail
->bytepos
> Z_BYTE
)
75 if (multibyte
&& ! CHAR_HEAD_P (FETCH_BYTE (tail
->bytepos
)))
80 #else /* not MARKER_DEBUG */
82 #define check_markers() do { } while (0)
84 #endif /* MARKER_DEBUG */
86 /* Move gap to byte position BYTEPOS, which is also char position CHARPOS.
87 Note that this can quit! */
90 move_gap_both (ptrdiff_t charpos
, ptrdiff_t bytepos
)
92 eassert (charpos
== BYTE_TO_CHAR (bytepos
)
93 && bytepos
== CHAR_TO_BYTE (charpos
));
94 if (bytepos
< GPT_BYTE
)
95 gap_left (charpos
, bytepos
, 0);
96 else if (bytepos
> GPT_BYTE
)
97 gap_right (charpos
, bytepos
);
100 /* Move the gap to a position less than the current GPT.
101 BYTEPOS describes the new position as a byte position,
102 and CHARPOS is the corresponding char position.
103 If NEWGAP, then don't update beg_unchanged and end_unchanged. */
106 gap_left (ptrdiff_t charpos
, ptrdiff_t bytepos
, bool newgap
)
108 unsigned char *to
, *from
;
113 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
120 /* Now copy the characters. To move the gap down,
121 copy characters up. */
125 /* I gets number of characters left to copy. */
126 i
= new_s1
- bytepos
;
129 /* If a quit is requested, stop copying now.
130 Change BYTEPOS to be where we have actually moved the gap to. */
134 charpos
= BYTE_TO_CHAR (bytepos
);
137 /* Move at most 32000 chars before checking again for a quit. */
142 memmove (to
, from
, i
);
145 /* Adjust buffer data structure, to put the gap at BYTEPOS.
146 BYTEPOS is where the loop above stopped, which may be what
147 was specified or may be where a quit was detected. */
150 eassert (charpos
<= bytepos
);
151 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
155 /* Move the gap to a position greater than the current GPT.
156 BYTEPOS describes the new position as a byte position,
157 and CHARPOS is the corresponding char position. */
160 gap_right (ptrdiff_t charpos
, ptrdiff_t bytepos
)
162 register unsigned char *to
, *from
;
163 register ptrdiff_t i
;
166 BUF_COMPUTE_UNCHANGED (current_buffer
, charpos
, GPT
);
173 /* Now copy the characters. To move the gap up,
174 copy characters down. */
178 /* I gets number of characters left to copy. */
179 i
= bytepos
- new_s1
;
182 /* If a quit is requested, stop copying now.
183 Change BYTEPOS to be where we have actually moved the gap to. */
187 charpos
= BYTE_TO_CHAR (bytepos
);
190 /* Move at most 32000 chars before checking again for a quit. */
194 memmove (to
, from
, i
);
200 eassert (charpos
<= bytepos
);
201 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
205 /* If the selected window's old pointm is adjacent or covered by the
206 region from FROM to TO, unsuspend auto hscroll in that window. */
209 adjust_suspend_auto_hscroll (ptrdiff_t from
, ptrdiff_t to
)
211 if (WINDOWP (selected_window
))
213 struct window
*w
= XWINDOW (selected_window
);
215 if (BUFFERP (w
->contents
)
216 && XBUFFER (w
->contents
) == current_buffer
217 && XMARKER (w
->old_pointm
)->charpos
>= from
218 && XMARKER (w
->old_pointm
)->charpos
<= to
)
219 w
->suspend_auto_hscroll
= 0;
224 /* Adjust all markers for a deletion
225 whose range in bytes is FROM_BYTE to TO_BYTE.
226 The range in charpos is FROM to TO.
228 This function assumes that the gap is adjacent to
229 or inside of the range being deleted. */
232 adjust_markers_for_delete (ptrdiff_t from
, ptrdiff_t from_byte
,
233 ptrdiff_t to
, ptrdiff_t to_byte
)
235 struct Lisp_Marker
*m
;
238 adjust_suspend_auto_hscroll (from
, to
);
239 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
241 charpos
= m
->charpos
;
242 eassert (charpos
<= Z
);
244 /* If the marker is after the deletion,
245 relocate by number of chars / bytes deleted. */
248 m
->charpos
-= to
- from
;
249 m
->bytepos
-= to_byte
- from_byte
;
251 /* Here's the case where a marker is inside text being deleted. */
252 else if (charpos
> from
)
255 m
->bytepos
= from_byte
;
261 /* Adjust markers for an insertion that stretches from FROM / FROM_BYTE
262 to TO / TO_BYTE. We have to relocate the charpos of every marker
263 that points after the insertion (but not their bytepos).
265 When a marker points at the insertion point,
266 we advance it if either its insertion-type is t
267 or BEFORE_MARKERS is true. */
270 adjust_markers_for_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
271 ptrdiff_t to
, ptrdiff_t to_byte
, bool before_markers
)
273 struct Lisp_Marker
*m
;
275 ptrdiff_t nchars
= to
- from
;
276 ptrdiff_t nbytes
= to_byte
- from_byte
;
278 adjust_suspend_auto_hscroll (from
, to
);
279 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
281 eassert (m
->bytepos
>= m
->charpos
282 && m
->bytepos
- m
->charpos
<= Z_BYTE
- Z
);
284 if (m
->bytepos
== from_byte
)
286 if (m
->insertion_type
|| before_markers
)
288 m
->bytepos
= to_byte
;
290 if (m
->insertion_type
)
294 else if (m
->bytepos
> from_byte
)
296 m
->bytepos
+= nbytes
;
297 m
->charpos
+= nchars
;
301 /* Adjusting only markers whose insertion-type is t may result in
302 - disordered start and end in overlays, and
303 - disordered overlays in the slot `overlays_before' of current_buffer. */
306 fix_start_end_in_overlays (from
, to
);
307 fix_overlays_before (current_buffer
, from
, to
);
311 /* Adjust point for an insertion of NBYTES bytes, which are NCHARS characters.
313 This is used only when the value of point changes due to an insert
314 or delete; it does not represent a conceptual change in point as a
315 marker. In particular, point is not crossing any interval
316 boundaries, so there's no need to use the usual SET_PT macro. In
317 fact it would be incorrect to do so, because either the old or the
318 new value of point is out of sync with the current set of
322 adjust_point (ptrdiff_t nchars
, ptrdiff_t nbytes
)
324 SET_BUF_PT_BOTH (current_buffer
, PT
+ nchars
, PT_BYTE
+ nbytes
);
325 /* In a single-byte buffer, the two positions must be equal. */
326 eassert (PT_BYTE
>= PT
&& PT_BYTE
- PT
<= ZV_BYTE
- ZV
);
329 /* Adjust markers for a replacement of a text at FROM (FROM_BYTE) of
330 length OLD_CHARS (OLD_BYTES) to a new text of length NEW_CHARS
331 (NEW_BYTES). It is assumed that OLD_CHARS > 0, i.e., this is not
335 adjust_markers_for_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
336 ptrdiff_t old_chars
, ptrdiff_t old_bytes
,
337 ptrdiff_t new_chars
, ptrdiff_t new_bytes
)
339 register struct Lisp_Marker
*m
;
340 ptrdiff_t prev_to_byte
= from_byte
+ old_bytes
;
341 ptrdiff_t diff_chars
= new_chars
- old_chars
;
342 ptrdiff_t diff_bytes
= new_bytes
- old_bytes
;
344 adjust_suspend_auto_hscroll (from
, from
+ old_chars
);
345 for (m
= BUF_MARKERS (current_buffer
); m
; m
= m
->next
)
347 if (m
->bytepos
>= prev_to_byte
)
349 m
->charpos
+= diff_chars
;
350 m
->bytepos
+= diff_bytes
;
352 else if (m
->bytepos
> from_byte
)
355 m
->bytepos
= from_byte
;
364 buffer_overflow (void)
366 error ("Maximum buffer size exceeded");
369 /* Make the gap NBYTES_ADDED bytes longer. */
372 make_gap_larger (ptrdiff_t nbytes_added
)
375 ptrdiff_t real_gap_loc
;
376 ptrdiff_t real_gap_loc_byte
;
377 ptrdiff_t old_gap_size
;
378 ptrdiff_t current_size
= Z_BYTE
- BEG_BYTE
+ GAP_SIZE
;
380 if (BUF_BYTES_MAX
- current_size
< nbytes_added
)
383 /* If we have to get more space, get enough to last a while;
384 but do not exceed the maximum buffer size. */
385 nbytes_added
= min (nbytes_added
+ GAP_BYTES_DFL
,
386 BUF_BYTES_MAX
- current_size
);
388 enlarge_buffer_text (current_buffer
, nbytes_added
);
390 /* Prevent quitting in move_gap. */
395 real_gap_loc_byte
= GPT_BYTE
;
396 old_gap_size
= GAP_SIZE
;
398 /* Call the newly allocated space a gap at the end of the whole space. */
400 GPT_BYTE
= Z_BYTE
+ GAP_SIZE
;
401 GAP_SIZE
= nbytes_added
;
403 /* Move the new gap down to be consecutive with the end of the old one. */
404 gap_left (real_gap_loc
+ old_gap_size
, real_gap_loc_byte
+ old_gap_size
, 1);
406 /* Now combine the two into one large gap. */
407 GAP_SIZE
+= old_gap_size
;
409 GPT_BYTE
= real_gap_loc_byte
;
417 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
419 /* Make the gap NBYTES_REMOVED bytes shorter. */
422 make_gap_smaller (ptrdiff_t nbytes_removed
)
425 ptrdiff_t real_gap_loc
;
426 ptrdiff_t real_gap_loc_byte
;
428 ptrdiff_t real_Z_byte
;
429 ptrdiff_t real_beg_unchanged
;
430 ptrdiff_t new_gap_size
;
432 /* Make sure the gap is at least GAP_BYTES_MIN bytes. */
433 if (GAP_SIZE
- nbytes_removed
< GAP_BYTES_MIN
)
434 nbytes_removed
= GAP_SIZE
- GAP_BYTES_MIN
;
436 /* Prevent quitting in move_gap. */
441 real_gap_loc_byte
= GPT_BYTE
;
442 new_gap_size
= GAP_SIZE
- nbytes_removed
;
444 real_Z_byte
= Z_BYTE
;
445 real_beg_unchanged
= BEG_UNCHANGED
;
447 /* Pretend that the last unwanted part of the gap is the entire gap,
448 and that the first desired part of the gap is part of the buffer
450 memset (GPT_ADDR
, 0, new_gap_size
);
452 GPT_BYTE
+= new_gap_size
;
454 Z_BYTE
+= new_gap_size
;
455 GAP_SIZE
= nbytes_removed
;
457 /* Move the unwanted pretend gap to the end of the buffer. */
458 gap_right (Z
, Z_BYTE
);
460 enlarge_buffer_text (current_buffer
, -nbytes_removed
);
462 /* Now restore the desired gap. */
463 GAP_SIZE
= new_gap_size
;
465 GPT_BYTE
= real_gap_loc_byte
;
467 Z_BYTE
= real_Z_byte
;
468 BEG_UNCHANGED
= real_beg_unchanged
;
476 #endif /* USE_MMAP_FOR_BUFFERS || REL_ALLOC || DOUG_LEA_MALLOC */
479 make_gap (ptrdiff_t nbytes_added
)
481 if (nbytes_added
>= 0)
482 make_gap_larger (nbytes_added
);
483 #if defined USE_MMAP_FOR_BUFFERS || defined REL_ALLOC || defined DOUG_LEA_MALLOC
485 make_gap_smaller (-nbytes_added
);
489 /* Add NBYTES to B's gap. It's enough to temporarily
490 fake current_buffer and avoid real switch to B. */
493 make_gap_1 (struct buffer
*b
, ptrdiff_t nbytes
)
495 struct buffer
*oldb
= current_buffer
;
499 current_buffer
= oldb
;
502 /* Copy NBYTES bytes of text from FROM_ADDR to TO_ADDR.
503 FROM_MULTIBYTE says whether the incoming text is multibyte.
504 TO_MULTIBYTE says whether to store the text as multibyte.
505 If FROM_MULTIBYTE != TO_MULTIBYTE, we convert.
507 Return the number of bytes stored at TO_ADDR. */
510 copy_text (const unsigned char *from_addr
, unsigned char *to_addr
,
511 ptrdiff_t nbytes
, bool from_multibyte
, bool to_multibyte
)
513 if (from_multibyte
== to_multibyte
)
515 memcpy (to_addr
, from_addr
, nbytes
);
518 else if (from_multibyte
)
520 ptrdiff_t nchars
= 0;
521 ptrdiff_t bytes_left
= nbytes
;
523 while (bytes_left
> 0)
526 c
= STRING_CHAR_AND_LENGTH (from_addr
, thislen
);
527 if (! ASCII_CHAR_P (c
))
530 from_addr
+= thislen
;
531 bytes_left
-= thislen
;
538 unsigned char *initial_to_addr
= to_addr
;
540 /* Convert single-byte to multibyte. */
543 int c
= *from_addr
++;
545 if (!ASCII_CHAR_P (c
))
547 c
= BYTE8_TO_CHAR (c
);
548 to_addr
+= CHAR_STRING (c
, to_addr
);
552 /* Special case for speed. */
553 *to_addr
++ = c
, nbytes
--;
555 return to_addr
- initial_to_addr
;
559 /* Insert a string of specified length before point.
560 This function judges multibyteness based on
561 enable_multibyte_characters in the current buffer;
562 it never converts between single-byte and multibyte.
564 DO NOT use this for the contents of a Lisp string or a Lisp buffer!
565 prepare_to_modify_buffer could relocate the text. */
568 insert (const char *string
, ptrdiff_t nbytes
)
572 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
573 insert_1_both (string
, len
, nbytes
, 0, 1, 0);
575 signal_after_change (opoint
, 0, len
);
576 update_compositions (opoint
, PT
, CHECK_BORDER
);
580 /* Likewise, but inherit text properties from neighboring characters. */
583 insert_and_inherit (const char *string
, ptrdiff_t nbytes
)
587 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
588 insert_1_both (string
, len
, nbytes
, 1, 1, 0);
590 signal_after_change (opoint
, 0, len
);
591 update_compositions (opoint
, PT
, CHECK_BORDER
);
595 /* Insert the character C before point. Do not inherit text properties. */
600 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
603 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
604 len
= CHAR_STRING (c
, str
);
611 insert ((char *) str
, len
);
614 /* Insert the null-terminated string S before point. */
617 insert_string (const char *s
)
619 insert (s
, strlen (s
));
622 /* Like `insert' except that all markers pointing at the place where
623 the insertion happens are adjusted to point after it.
624 Don't use this function to insert part of a Lisp string,
625 since gc could happen and relocate it. */
628 insert_before_markers (const char *string
, ptrdiff_t nbytes
)
632 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
633 insert_1_both (string
, len
, nbytes
, 0, 1, 1);
635 signal_after_change (opoint
, 0, len
);
636 update_compositions (opoint
, PT
, CHECK_BORDER
);
640 /* Likewise, but inherit text properties from neighboring characters. */
643 insert_before_markers_and_inherit (const char *string
,
648 ptrdiff_t len
= chars_in_text ((unsigned char *) string
, nbytes
), opoint
;
649 insert_1_both (string
, len
, nbytes
, 1, 1, 1);
651 signal_after_change (opoint
, 0, len
);
652 update_compositions (opoint
, PT
, CHECK_BORDER
);
656 #ifdef BYTE_COMBINING_DEBUG
658 /* See if the bytes before POS/POS_BYTE combine with bytes
659 at the start of STRING to form a single character.
660 If so, return the number of bytes at the start of STRING
661 which combine in this way. Otherwise, return 0. */
664 count_combining_before (const unsigned char *string
, ptrdiff_t length
,
665 ptrdiff_t pos
, ptrdiff_t pos_byte
)
667 int len
, combining_bytes
;
668 const unsigned char *p
;
670 if (NILP (current_buffer
->enable_multibyte_characters
))
673 /* At first, we can exclude the following cases:
674 (1) STRING[0] can't be a following byte of multibyte sequence.
675 (2) POS is the start of the current buffer.
676 (3) A character before POS is not a multibyte character. */
677 if (length
== 0 || CHAR_HEAD_P (*string
)) /* case (1) */
679 if (pos_byte
== BEG_BYTE
) /* case (2) */
682 p
= BYTE_POS_ADDR (pos_byte
- 1);
683 while (! CHAR_HEAD_P (*p
)) p
--, len
++;
684 if (! LEADING_CODE_P (*p
)) /* case (3) */
687 combining_bytes
= BYTES_BY_CHAR_HEAD (*p
) - len
;
688 if (combining_bytes
<= 0)
689 /* The character preceding POS is, complete and no room for
690 combining bytes (combining_bytes == 0), or an independent 8-bit
691 character (combining_bytes < 0). */
694 /* We have a combination situation. Count the bytes at STRING that
697 while (!CHAR_HEAD_P (*p
) && p
< string
+ length
)
700 return (combining_bytes
< p
- string
? combining_bytes
: p
- string
);
703 /* See if the bytes after POS/POS_BYTE combine with bytes
704 at the end of STRING to form a single character.
705 If so, return the number of bytes after POS/POS_BYTE
706 which combine in this way. Otherwise, return 0. */
709 count_combining_after (const unsigned char *string
,
710 ptrdiff_t length
, ptrdiff_t pos
, ptrdiff_t pos_byte
)
712 ptrdiff_t opos_byte
= pos_byte
;
717 if (NILP (current_buffer
->enable_multibyte_characters
))
720 /* At first, we can exclude the following cases:
721 (1) The last byte of STRING is an ASCII.
722 (2) POS is the last of the current buffer.
723 (3) A character at POS can't be a following byte of multibyte
725 if (length
> 0 && ASCII_CHAR_P (string
[length
- 1])) /* case (1) */
727 if (pos_byte
== Z_BYTE
) /* case (2) */
729 bufp
= BYTE_POS_ADDR (pos_byte
);
730 if (CHAR_HEAD_P (*bufp
)) /* case (3) */
734 while (i
>= 0 && ! CHAR_HEAD_P (string
[i
]))
740 /* All characters in STRING are not character head. We must
741 check also preceding bytes at POS. We are sure that the gap
743 unsigned char *p
= BEG_ADDR
;
745 while (i
>= 0 && ! CHAR_HEAD_P (p
[i
]))
747 if (i
< 0 || !LEADING_CODE_P (p
[i
]))
750 bytes
= BYTES_BY_CHAR_HEAD (p
[i
]);
751 return (bytes
<= pos_byte
- 1 - i
+ length
753 : bytes
- (pos_byte
- 1 - i
+ length
));
755 if (!LEADING_CODE_P (string
[i
]))
758 bytes
= BYTES_BY_CHAR_HEAD (string
[i
]) - (length
- i
);
760 while (!CHAR_HEAD_P (*bufp
)) bufp
++, pos_byte
++;
762 return (bytes
<= pos_byte
- opos_byte
? bytes
: pos_byte
- opos_byte
);
768 /* Insert a sequence of NCHARS chars which occupy NBYTES bytes
769 starting at STRING. INHERIT non-zero means inherit the text
770 properties from neighboring characters; zero means inserted text
771 will have no text properties. PREPARE non-zero means call
772 prepare_to_modify_buffer, which checks that the region is not
773 read-only, and calls before-change-function and any modification
774 properties the text may have. BEFORE_MARKERS non-zero means adjust
775 all markers that point at the insertion place to point after it. */
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 ptrdiff_t ins_charpos
= GPT
, ins_bytepos
= GPT_BYTE
;
988 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
991 /* No need to call prepare_to_modify_buffer, since this is called
992 from places that replace some region with a different text, so
993 prepare_to_modify_buffer was already called by the deletion part
995 invalidate_buffer_caches (current_buffer
, GPT
, GPT
);
996 record_insert (GPT
, nchars
);
1000 if (! text_at_gap_tail
)
1009 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1011 eassert (GPT
<= GPT_BYTE
);
1013 adjust_overlays_for_insert (ins_charpos
, nchars
);
1014 adjust_markers_for_insert (ins_charpos
, ins_bytepos
,
1015 ins_charpos
+ nchars
, ins_bytepos
+ nbytes
, 0);
1017 if (buffer_intervals (current_buffer
))
1019 offset_intervals (current_buffer
, ins_charpos
, nchars
);
1020 graft_intervals_into_buffer (NULL
, ins_charpos
, nchars
,
1024 if (ins_charpos
< PT
)
1025 adjust_point (nchars
, nbytes
);
1030 /* Insert text from BUF, NCHARS characters starting at CHARPOS, into the
1031 current buffer. If the text in BUF has properties, they are absorbed
1032 into the current buffer.
1034 It does not work to use `insert' for this, because a malloc could happen
1035 and relocate BUF's text before the copy happens. */
1038 insert_from_buffer (struct buffer
*buf
,
1039 ptrdiff_t charpos
, ptrdiff_t nchars
, bool inherit
)
1041 ptrdiff_t opoint
= PT
;
1043 insert_from_buffer_1 (buf
, charpos
, nchars
, inherit
);
1044 signal_after_change (opoint
, 0, PT
- opoint
);
1045 update_compositions (opoint
, PT
, CHECK_BORDER
);
1049 insert_from_buffer_1 (struct buffer
*buf
,
1050 ptrdiff_t from
, ptrdiff_t nchars
, bool inherit
)
1052 ptrdiff_t chunk
, chunk_expanded
;
1053 ptrdiff_t from_byte
= buf_charpos_to_bytepos (buf
, from
);
1054 ptrdiff_t to_byte
= buf_charpos_to_bytepos (buf
, from
+ nchars
);
1055 ptrdiff_t incoming_nbytes
= to_byte
- from_byte
;
1056 ptrdiff_t outgoing_nbytes
= incoming_nbytes
;
1062 /* Make OUTGOING_NBYTES describe the text
1063 as it will be inserted in this buffer. */
1065 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1066 outgoing_nbytes
= nchars
;
1067 else if (NILP (BVAR (buf
, enable_multibyte_characters
)))
1069 ptrdiff_t outgoing_before_gap
= 0;
1070 ptrdiff_t outgoing_after_gap
= 0;
1072 if (from
< BUF_GPT (buf
))
1074 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1075 if (chunk
> incoming_nbytes
)
1076 chunk
= incoming_nbytes
;
1078 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
, from_byte
),
1084 if (chunk
< incoming_nbytes
)
1086 = count_size_as_multibyte (BUF_BYTE_ADDRESS (buf
,
1088 incoming_nbytes
- chunk
);
1090 outgoing_nbytes
= outgoing_before_gap
+ outgoing_after_gap
;
1093 /* Do this before moving and increasing the gap,
1094 because the before-change hooks might move the gap
1095 or make it smaller. */
1096 prepare_to_modify_buffer (PT
, PT
, NULL
);
1099 move_gap_both (PT
, PT_BYTE
);
1100 if (GAP_SIZE
< outgoing_nbytes
)
1101 make_gap (outgoing_nbytes
- GAP_SIZE
);
1103 if (from
< BUF_GPT (buf
))
1105 chunk
= BUF_GPT_BYTE (buf
) - from_byte
;
1106 if (chunk
> incoming_nbytes
)
1107 chunk
= incoming_nbytes
;
1108 /* Record number of output bytes, so we know where
1109 to put the output from the second copy_text. */
1111 = copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
),
1113 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1114 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1117 chunk_expanded
= chunk
= 0;
1119 if (chunk
< incoming_nbytes
)
1120 copy_text (BUF_BYTE_ADDRESS (buf
, from_byte
+ chunk
),
1121 GPT_ADDR
+ chunk_expanded
, incoming_nbytes
- chunk
,
1122 ! NILP (BVAR (buf
, enable_multibyte_characters
)),
1123 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1125 #ifdef BYTE_COMBINING_DEBUG
1126 /* We have copied text into the gap, but we have not altered
1127 PT or PT_BYTE yet. So we can pass PT and PT_BYTE
1128 to these functions and get the same results as we would
1129 have got earlier on. Meanwhile, GPT_ADDR does point to
1130 the text that has been stored by copy_text. */
1131 if (count_combining_before (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
)
1132 || count_combining_after (GPT_ADDR
, outgoing_nbytes
, PT
, PT_BYTE
))
1136 record_insert (PT
, nchars
);
1138 CHARS_MODIFF
= MODIFF
;
1140 GAP_SIZE
-= outgoing_nbytes
;
1144 GPT_BYTE
+= outgoing_nbytes
;
1145 ZV_BYTE
+= outgoing_nbytes
;
1146 Z_BYTE
+= outgoing_nbytes
;
1147 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1149 eassert (GPT
<= GPT_BYTE
);
1151 /* The insert may have been in the unchanged region, so check again. */
1152 if (Z
- GPT
< END_UNCHANGED
)
1153 END_UNCHANGED
= Z
- GPT
;
1155 adjust_overlays_for_insert (PT
, nchars
);
1156 adjust_markers_for_insert (PT
, PT_BYTE
, PT
+ nchars
,
1157 PT_BYTE
+ outgoing_nbytes
,
1160 offset_intervals (current_buffer
, PT
, nchars
);
1162 /* Get the intervals for the part of the string we are inserting. */
1163 intervals
= buffer_intervals (buf
);
1164 if (nchars
< BUF_Z (buf
) - BUF_BEG (buf
))
1166 if (buf
== current_buffer
&& PT
<= from
)
1168 intervals
= copy_intervals (intervals
, from
, nchars
);
1171 /* Insert those intervals. */
1172 graft_intervals_into_buffer (intervals
, PT
, nchars
, current_buffer
, inherit
);
1174 adjust_point (nchars
, outgoing_nbytes
);
1177 /* Record undo information and adjust markers and position keepers for
1178 a replacement of a text PREV_TEXT at FROM to a new text of LEN
1179 chars (LEN_BYTE bytes) which resides in the gap just after
1182 PREV_TEXT nil means the new text was just inserted. */
1185 adjust_after_replace (ptrdiff_t from
, ptrdiff_t from_byte
,
1186 Lisp_Object prev_text
, ptrdiff_t len
, ptrdiff_t len_byte
)
1188 ptrdiff_t nchars_del
= 0, nbytes_del
= 0;
1190 #ifdef BYTE_COMBINING_DEBUG
1191 if (count_combining_before (GPT_ADDR
, len_byte
, from
, from_byte
)
1192 || count_combining_after (GPT_ADDR
, len_byte
, from
, from_byte
))
1196 if (STRINGP (prev_text
))
1198 nchars_del
= SCHARS (prev_text
);
1199 nbytes_del
= SBYTES (prev_text
);
1202 /* Update various buffer positions for the new text. */
1203 GAP_SIZE
-= len_byte
;
1204 ZV
+= len
; Z
+= len
;
1205 ZV_BYTE
+= len_byte
; Z_BYTE
+= len_byte
;
1206 GPT
+= len
; GPT_BYTE
+= len_byte
;
1207 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1210 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1213 adjust_markers_for_insert (from
, from_byte
,
1214 from
+ len
, from_byte
+ len_byte
, 0);
1217 record_delete (from
, prev_text
, false);
1218 record_insert (from
, len
);
1220 if (len
> nchars_del
)
1221 adjust_overlays_for_insert (from
, len
- nchars_del
);
1222 else if (len
< nchars_del
)
1223 adjust_overlays_for_delete (from
, nchars_del
- len
);
1225 offset_intervals (current_buffer
, from
, len
- nchars_del
);
1228 adjust_point (len
- nchars_del
, len_byte
- nbytes_del
);
1230 /* As byte combining will decrease Z, we must check this again. */
1231 if (Z
- GPT
< END_UNCHANGED
)
1232 END_UNCHANGED
= Z
- GPT
;
1237 evaporate_overlays (from
);
1239 CHARS_MODIFF
= MODIFF
;
1242 /* Record undo information, adjust markers and position keepers for an
1243 insertion of a text from FROM (FROM_BYTE) to TO (TO_BYTE). The
1244 text already exists in the current buffer but character length (TO
1245 - FROM) may be incorrect, the correct length is NEWLEN. */
1248 adjust_after_insert (ptrdiff_t from
, ptrdiff_t from_byte
,
1249 ptrdiff_t to
, ptrdiff_t to_byte
, ptrdiff_t newlen
)
1251 ptrdiff_t len
= to
- from
, len_byte
= to_byte
- from_byte
;
1254 move_gap_both (to
, to_byte
);
1255 GAP_SIZE
+= len_byte
;
1256 GPT
-= len
; GPT_BYTE
-= len_byte
;
1257 ZV
-= len
; ZV_BYTE
-= len_byte
;
1258 Z
-= len
; Z_BYTE
-= len_byte
;
1259 adjust_after_replace (from
, from_byte
, Qnil
, newlen
, len_byte
);
1262 /* Replace the text from character positions FROM to TO with NEW,
1263 If PREPARE, call prepare_to_modify_buffer.
1264 If INHERIT, the newly inserted text should inherit text properties
1265 from the surrounding non-deleted text. */
1267 /* Note that this does not yet handle markers quite right.
1268 Also it needs to record a single undo-entry that does a replacement
1269 rather than a separate delete and insert.
1270 That way, undo will also handle markers properly.
1272 But if MARKERS is 0, don't relocate markers. */
1275 replace_range (ptrdiff_t from
, ptrdiff_t to
, Lisp_Object
new,
1276 bool prepare
, bool inherit
, bool markers
)
1278 ptrdiff_t inschars
= SCHARS (new);
1279 ptrdiff_t insbytes
= SBYTES (new);
1280 ptrdiff_t from_byte
, to_byte
;
1281 ptrdiff_t nbytes_del
, nchars_del
;
1282 struct gcpro gcpro1
;
1284 ptrdiff_t outgoing_insbytes
= insbytes
;
1285 Lisp_Object deletion
;
1294 ptrdiff_t range_length
= to
- from
;
1295 prepare_to_modify_buffer (from
, to
, &from
);
1296 to
= from
+ range_length
;
1301 /* Make args be valid. */
1307 from_byte
= CHAR_TO_BYTE (from
);
1308 to_byte
= CHAR_TO_BYTE (to
);
1310 nchars_del
= to
- from
;
1311 nbytes_del
= to_byte
- from_byte
;
1313 if (nbytes_del
<= 0 && insbytes
== 0)
1316 /* Make OUTGOING_INSBYTES describe the text
1317 as it will be inserted in this buffer. */
1319 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
1320 outgoing_insbytes
= inschars
;
1321 else if (! STRING_MULTIBYTE (new))
1323 = count_size_as_multibyte (SDATA (new), insbytes
);
1327 /* Make sure the gap is somewhere in or next to what we are deleting. */
1329 gap_right (from
, from_byte
);
1331 gap_left (to
, to_byte
, 0);
1333 /* Even if we don't record for undo, we must keep the original text
1334 because we may have to recover it because of inappropriate byte
1336 if (! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1337 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1339 GAP_SIZE
+= nbytes_del
;
1342 ZV_BYTE
-= nbytes_del
;
1343 Z_BYTE
-= nbytes_del
;
1345 GPT_BYTE
= from_byte
;
1346 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1348 eassert (GPT
<= GPT_BYTE
);
1350 if (GPT
- BEG
< BEG_UNCHANGED
)
1351 BEG_UNCHANGED
= GPT
- BEG
;
1352 if (Z
- GPT
< END_UNCHANGED
)
1353 END_UNCHANGED
= Z
- GPT
;
1355 if (GAP_SIZE
< outgoing_insbytes
)
1356 make_gap (outgoing_insbytes
- GAP_SIZE
);
1358 /* Copy the string text into the buffer, perhaps converting
1359 between single-byte and multibyte. */
1360 copy_text (SDATA (new), GPT_ADDR
, insbytes
,
1361 STRING_MULTIBYTE (new),
1362 ! NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1364 #ifdef BYTE_COMBINING_DEBUG
1365 /* We have copied text into the gap, but we have not marked
1366 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1367 here, for both the previous text and the following text.
1368 Meanwhile, GPT_ADDR does point to
1369 the text that has been stored by copy_text. */
1370 if (count_combining_before (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
)
1371 || count_combining_after (GPT_ADDR
, outgoing_insbytes
, from
, from_byte
))
1375 /* Record the insertion first, so that when we undo,
1376 the deletion will be undone first. Thus, undo
1377 will insert before deleting, and thus will keep
1378 the markers before and after this text separate. */
1379 if (!NILP (deletion
))
1381 record_insert (from
+ SCHARS (deletion
), inschars
);
1382 record_delete (from
, deletion
, false);
1385 GAP_SIZE
-= outgoing_insbytes
;
1389 GPT_BYTE
+= outgoing_insbytes
;
1390 ZV_BYTE
+= outgoing_insbytes
;
1391 Z_BYTE
+= outgoing_insbytes
;
1392 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1394 eassert (GPT
<= GPT_BYTE
);
1396 /* Adjust markers for the deletion and the insertion. */
1398 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1399 inschars
, outgoing_insbytes
);
1401 /* Adjust the overlay center as needed. This must be done after
1402 adjusting the markers that bound the overlays. */
1403 adjust_overlays_for_delete (from
, nchars_del
);
1404 adjust_overlays_for_insert (from
, inschars
);
1406 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1408 /* Get the intervals for the part of the string we are inserting--
1409 not including the combined-before bytes. */
1410 intervals
= string_intervals (new);
1411 /* Insert those intervals. */
1412 graft_intervals_into_buffer (intervals
, from
, inschars
,
1413 current_buffer
, inherit
);
1415 /* Relocate point as if it were a marker. */
1417 adjust_point ((from
+ inschars
- (PT
< to
? PT
: to
)),
1418 (from_byte
+ outgoing_insbytes
1419 - (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
)));
1421 if (outgoing_insbytes
== 0)
1422 evaporate_overlays (from
);
1427 CHARS_MODIFF
= MODIFF
;
1430 signal_after_change (from
, nchars_del
, GPT
- from
);
1431 update_compositions (from
, GPT
, CHECK_BORDER
);
1434 /* Replace the text from character positions FROM to TO with
1435 the text in INS of length INSCHARS.
1436 Keep the text properties that applied to the old characters
1437 (extending them to all the new chars if there are more new chars).
1439 Note that this does not yet handle markers quite right.
1441 If MARKERS, relocate markers.
1443 Unlike most functions at this level, never call
1444 prepare_to_modify_buffer and never call signal_after_change. */
1447 replace_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1448 ptrdiff_t to
, ptrdiff_t to_byte
,
1449 const char *ins
, ptrdiff_t inschars
, ptrdiff_t insbytes
,
1452 ptrdiff_t nbytes_del
, nchars_del
;
1456 nchars_del
= to
- from
;
1457 nbytes_del
= to_byte
- from_byte
;
1459 if (nbytes_del
<= 0 && insbytes
== 0)
1462 /* Make sure the gap is somewhere in or next to what we are deleting. */
1464 gap_right (from
, from_byte
);
1466 gap_left (to
, to_byte
, 0);
1468 GAP_SIZE
+= nbytes_del
;
1471 ZV_BYTE
-= nbytes_del
;
1472 Z_BYTE
-= nbytes_del
;
1474 GPT_BYTE
= from_byte
;
1475 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1477 eassert (GPT
<= GPT_BYTE
);
1479 if (GPT
- BEG
< BEG_UNCHANGED
)
1480 BEG_UNCHANGED
= GPT
- BEG
;
1481 if (Z
- GPT
< END_UNCHANGED
)
1482 END_UNCHANGED
= Z
- GPT
;
1484 if (GAP_SIZE
< insbytes
)
1485 make_gap (insbytes
- GAP_SIZE
);
1487 /* Copy the replacement text into the buffer. */
1488 memcpy (GPT_ADDR
, ins
, insbytes
);
1490 #ifdef BYTE_COMBINING_DEBUG
1491 /* We have copied text into the gap, but we have not marked
1492 it as part of the buffer. So we can use the old FROM and FROM_BYTE
1493 here, for both the previous text and the following text.
1494 Meanwhile, GPT_ADDR does point to
1495 the text that has been stored by copy_text. */
1496 if (count_combining_before (GPT_ADDR
, insbytes
, from
, from_byte
)
1497 || count_combining_after (GPT_ADDR
, insbytes
, from
, from_byte
))
1501 GAP_SIZE
-= insbytes
;
1505 GPT_BYTE
+= insbytes
;
1506 ZV_BYTE
+= insbytes
;
1508 if (GAP_SIZE
> 0) *(GPT_ADDR
) = 0; /* Put an anchor. */
1510 eassert (GPT
<= GPT_BYTE
);
1512 /* Adjust markers for the deletion and the insertion. */
1514 && ! (nchars_del
== 1 && inschars
== 1 && nbytes_del
== insbytes
))
1515 adjust_markers_for_replace (from
, from_byte
, nchars_del
, nbytes_del
,
1516 inschars
, insbytes
);
1518 /* Adjust the overlay center as needed. This must be done after
1519 adjusting the markers that bound the overlays. */
1520 if (nchars_del
!= inschars
)
1522 adjust_overlays_for_insert (from
, inschars
);
1523 adjust_overlays_for_delete (from
+ inschars
, nchars_del
);
1526 offset_intervals (current_buffer
, from
, inschars
- nchars_del
);
1528 /* Relocate point as if it were a marker. */
1529 if (from
< PT
&& (nchars_del
!= inschars
|| nbytes_del
!= insbytes
))
1532 /* PT was within the deleted text. Move it to FROM. */
1533 adjust_point (from
- PT
, from_byte
- PT_BYTE
);
1535 adjust_point (inschars
- nchars_del
, insbytes
- nbytes_del
);
1539 evaporate_overlays (from
);
1544 CHARS_MODIFF
= MODIFF
;
1547 /* Delete characters in current buffer
1548 from FROM up to (but not including) TO.
1549 If TO comes before FROM, we delete nothing. */
1552 del_range (ptrdiff_t from
, ptrdiff_t to
)
1554 del_range_1 (from
, to
, 1, 0);
1557 /* Like del_range; PREPARE says whether to call prepare_to_modify_buffer.
1558 RET_STRING says to return the deleted text. */
1561 del_range_1 (ptrdiff_t from
, ptrdiff_t to
, bool prepare
, bool ret_string
)
1563 ptrdiff_t from_byte
, to_byte
;
1564 Lisp_Object deletion
;
1565 struct gcpro gcpro1
;
1567 /* Make args be valid */
1578 ptrdiff_t range_length
= to
- from
;
1579 prepare_to_modify_buffer (from
, to
, &from
);
1580 to
= min (ZV
, from
+ range_length
);
1583 from_byte
= CHAR_TO_BYTE (from
);
1584 to_byte
= CHAR_TO_BYTE (to
);
1586 deletion
= del_range_2 (from
, from_byte
, to
, to_byte
, ret_string
);
1588 signal_after_change (from
, to
- from
, 0);
1589 update_compositions (from
, from
, CHECK_HEAD
);
1594 /* Like del_range_1 but args are byte positions, not char positions. */
1597 del_range_byte (ptrdiff_t from_byte
, ptrdiff_t to_byte
, bool prepare
)
1601 /* Make args be valid. */
1602 if (from_byte
< BEGV_BYTE
)
1603 from_byte
= BEGV_BYTE
;
1604 if (to_byte
> ZV_BYTE
)
1607 if (to_byte
<= from_byte
)
1610 from
= BYTE_TO_CHAR (from_byte
);
1611 to
= BYTE_TO_CHAR (to_byte
);
1615 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1616 ptrdiff_t range_length
= to
- from
;
1617 prepare_to_modify_buffer (from
, to
, &from
);
1618 to
= from
+ range_length
;
1620 if (old_from
!= from
)
1621 from_byte
= CHAR_TO_BYTE (from
);
1627 else if (old_to
== Z
- to
)
1628 to_byte
= CHAR_TO_BYTE (to
);
1631 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1632 signal_after_change (from
, to
- from
, 0);
1633 update_compositions (from
, from
, CHECK_HEAD
);
1636 /* Like del_range_1, but positions are specified both as charpos
1640 del_range_both (ptrdiff_t from
, ptrdiff_t from_byte
,
1641 ptrdiff_t to
, ptrdiff_t to_byte
, bool prepare
)
1643 /* Make args be valid */
1644 if (from_byte
< BEGV_BYTE
)
1645 from_byte
= BEGV_BYTE
;
1646 if (to_byte
> ZV_BYTE
)
1649 if (to_byte
<= from_byte
)
1659 ptrdiff_t old_from
= from
, old_to
= Z
- to
;
1660 ptrdiff_t range_length
= to
- from
;
1661 prepare_to_modify_buffer (from
, to
, &from
);
1662 to
= from
+ range_length
;
1664 if (old_from
!= from
)
1665 from_byte
= CHAR_TO_BYTE (from
);
1671 else if (old_to
== Z
- to
)
1672 to_byte
= CHAR_TO_BYTE (to
);
1675 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
1676 signal_after_change (from
, to
- from
, 0);
1677 update_compositions (from
, from
, CHECK_HEAD
);
1680 /* Delete a range of text, specified both as character positions
1681 and byte positions. FROM and TO are character positions,
1682 while FROM_BYTE and TO_BYTE are byte positions.
1683 If RET_STRING, the deleted area is returned as a string. */
1686 del_range_2 (ptrdiff_t from
, ptrdiff_t from_byte
,
1687 ptrdiff_t to
, ptrdiff_t to_byte
, bool ret_string
)
1689 ptrdiff_t nbytes_del
, nchars_del
;
1690 Lisp_Object deletion
;
1694 nchars_del
= to
- from
;
1695 nbytes_del
= to_byte
- from_byte
;
1697 /* Make sure the gap is somewhere in or next to what we are deleting. */
1699 gap_right (from
, from_byte
);
1701 gap_left (to
, to_byte
, 0);
1703 #ifdef BYTE_COMBINING_DEBUG
1704 if (count_combining_before (BUF_BYTE_ADDRESS (current_buffer
, to_byte
),
1705 Z_BYTE
- to_byte
, from
, from_byte
))
1709 if (ret_string
|| ! EQ (BVAR (current_buffer
, undo_list
), Qt
))
1710 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
1714 /* Record marker adjustments, and text deletion into undo
1716 record_delete (from
, deletion
, true);
1718 /* Relocate all markers pointing into the new, larger gap to point
1719 at the end of the text before the gap. */
1720 adjust_markers_for_delete (from
, from_byte
, to
, to_byte
);
1723 CHARS_MODIFF
= MODIFF
;
1725 /* Relocate point as if it were a marker. */
1727 adjust_point (from
- (PT
< to
? PT
: to
),
1728 from_byte
- (PT_BYTE
< to_byte
? PT_BYTE
: to_byte
));
1730 offset_intervals (current_buffer
, from
, - nchars_del
);
1732 /* Adjust the overlay center as needed. This must be done after
1733 adjusting the markers that bound the overlays. */
1734 adjust_overlays_for_delete (from
, nchars_del
);
1736 GAP_SIZE
+= nbytes_del
;
1737 ZV_BYTE
-= nbytes_del
;
1738 Z_BYTE
-= nbytes_del
;
1742 GPT_BYTE
= from_byte
;
1743 if (GAP_SIZE
> 0 && !current_buffer
->text
->inhibit_shrinking
)
1744 /* Put an anchor, unless called from decode_coding_object which
1745 needs to access the previous gap contents. */
1748 eassert (GPT
<= GPT_BYTE
);
1750 if (GPT
- BEG
< BEG_UNCHANGED
)
1751 BEG_UNCHANGED
= GPT
- BEG
;
1752 if (Z
- GPT
< END_UNCHANGED
)
1753 END_UNCHANGED
= Z
- GPT
;
1757 evaporate_overlays (from
);
1762 /* Call this if you're about to change the text of current buffer
1763 from character positions START to END. This checks the read-only
1764 properties of the region, calls the necessary modification hooks,
1765 and warns the next redisplay that it should pay attention to that
1769 modify_text (ptrdiff_t start
, ptrdiff_t end
)
1771 prepare_to_modify_buffer (start
, end
, NULL
);
1773 BUF_COMPUTE_UNCHANGED (current_buffer
, start
- 1, end
);
1774 if (MODIFF
<= SAVE_MODIFF
)
1775 record_first_change ();
1777 CHARS_MODIFF
= MODIFF
;
1779 bset_point_before_scroll (current_buffer
, Qnil
);
1782 /* Check that it is okay to modify the buffer between START and END,
1783 which are char positions.
1785 Run the before-change-function, if any. If intervals are in use,
1786 verify that the text to be modified is not read-only, and call
1787 any modification properties the text may have.
1789 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1790 by holding its value temporarily in a marker. */
1793 prepare_to_modify_buffer_1 (ptrdiff_t start
, ptrdiff_t end
,
1794 ptrdiff_t *preserve_ptr
)
1796 struct buffer
*base_buffer
;
1799 XSETFASTINT (temp
, start
);
1800 if (!NILP (BVAR (current_buffer
, read_only
)))
1801 Fbarf_if_buffer_read_only (temp
);
1803 bset_redisplay (current_buffer
);
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 if (inhibit_modification_hooks
)
1831 if (!NILP (BVAR (base_buffer
, file_truename
))
1832 /* Make binding buffer-file-name to nil effective. */
1833 && !NILP (BVAR (base_buffer
, filename
))
1834 && SAVE_MODIFF
>= MODIFF
)
1835 lock_file (BVAR (base_buffer
, file_truename
));
1837 /* If `select-active-regions' is non-nil, save the region text. */
1838 /* FIXME: Move this to Elisp (via before-change-functions). */
1839 if (!NILP (BVAR (current_buffer
, mark_active
))
1840 && XMARKER (BVAR (current_buffer
, mark
))->buffer
1841 && NILP (Vsaved_region_selection
)
1842 && (EQ (Vselect_active_regions
, Qonly
)
1843 ? EQ (CAR_SAFE (Vtransient_mark_mode
), Qonly
)
1844 : (!NILP (Vselect_active_regions
)
1845 && !NILP (Vtransient_mark_mode
))))
1846 Vsaved_region_selection
1847 = call1 (Fsymbol_value (Qregion_extract_function
), Qnil
);
1849 signal_before_change (start
, end
, preserve_ptr
);
1850 Vdeactivate_mark
= Qt
;
1853 /* Like above, but called when we know that the buffer text
1854 will be modified and region caches should be invalidated. */
1857 prepare_to_modify_buffer (ptrdiff_t start
, ptrdiff_t end
,
1858 ptrdiff_t *preserve_ptr
)
1860 prepare_to_modify_buffer_1 (start
, end
, preserve_ptr
);
1861 invalidate_buffer_caches (current_buffer
, start
, end
);
1864 /* Invalidate the caches maintained by the buffer BUF, if any, for the
1865 region between buffer positions START and END. */
1867 invalidate_buffer_caches (struct buffer
*buf
, ptrdiff_t start
, ptrdiff_t end
)
1869 /* Indirect buffers usually have their caches set to NULL, but we
1870 need to consider the caches of their base buffer. */
1871 if (buf
->base_buffer
)
1872 buf
= buf
->base_buffer
;
1873 /* The bidi_paragraph_cache must be invalidated first, because doing
1874 so might need to use the newline_cache (via find_newline_no_quit,
1876 if (buf
->bidi_paragraph_cache
)
1879 && start
> BUF_BEG (buf
))
1881 /* If we are deleting or replacing characters, we could
1882 create a paragraph start, because all of the characters
1883 from START to the beginning of START's line are
1884 whitespace. Therefore, we must extend the region to be
1885 invalidated up to the newline before START. */
1886 ptrdiff_t line_beg
= start
;
1887 ptrdiff_t start_byte
= buf_charpos_to_bytepos (buf
, start
);
1889 if (BUF_FETCH_BYTE (buf
, start_byte
- 1) != '\n')
1891 struct buffer
*old
= current_buffer
;
1893 set_buffer_internal (buf
);
1895 line_beg
= find_newline_no_quit (start
, start_byte
, -1,
1897 set_buffer_internal (old
);
1899 start
= line_beg
- (line_beg
> BUF_BEG (buf
));
1901 invalidate_region_cache (buf
,
1902 buf
->bidi_paragraph_cache
,
1903 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
1905 if (buf
->newline_cache
)
1906 invalidate_region_cache (buf
,
1908 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
1909 if (buf
->width_run_cache
)
1910 invalidate_region_cache (buf
,
1911 buf
->width_run_cache
,
1912 start
- BUF_BEG (buf
), BUF_Z (buf
) - end
);
1915 /* These macros work with an argument named `preserve_ptr'
1916 and a local variable named `preserve_marker'. */
1918 #define PRESERVE_VALUE \
1919 if (preserve_ptr && NILP (preserve_marker)) \
1920 preserve_marker = Fcopy_marker (make_number (*preserve_ptr), Qnil)
1922 #define RESTORE_VALUE \
1923 if (! NILP (preserve_marker)) \
1925 *preserve_ptr = marker_position (preserve_marker); \
1926 unchain_marker (XMARKER (preserve_marker)); \
1929 #define PRESERVE_START_END \
1930 if (NILP (start_marker)) \
1931 start_marker = Fcopy_marker (start, Qnil); \
1932 if (NILP (end_marker)) \
1933 end_marker = Fcopy_marker (end, Qnil);
1935 #define FETCH_START \
1936 (! NILP (start_marker) ? Fmarker_position (start_marker) : start)
1939 (! NILP (end_marker) ? Fmarker_position (end_marker) : end)
1941 /* Set a variable to nil if an error occurred.
1942 Don't change the variable if there was no error.
1943 VAL is a cons-cell (VARIABLE . NO-ERROR-FLAG).
1944 VARIABLE is the variable to maybe set to nil.
1945 NO-ERROR-FLAG is nil if there was an error,
1946 anything else meaning no error (so this function does nothing). */
1949 Lisp_Object
*location
;
1954 reset_var_on_error (void *ptr
)
1956 struct rvoe_arg
*p
= ptr
;
1958 *p
->location
= Qnil
;
1961 /* Signal a change to the buffer immediately before it happens.
1962 START_INT and END_INT are the bounds of the text to be changed.
1964 If PRESERVE_PTR is nonzero, we relocate *PRESERVE_PTR
1965 by holding its value temporarily in a marker. */
1968 signal_before_change (ptrdiff_t start_int
, ptrdiff_t end_int
,
1969 ptrdiff_t *preserve_ptr
)
1971 Lisp_Object start
, end
;
1972 Lisp_Object start_marker
, end_marker
;
1973 Lisp_Object preserve_marker
;
1974 struct gcpro gcpro1
, gcpro2
, gcpro3
;
1975 ptrdiff_t count
= SPECPDL_INDEX ();
1976 struct rvoe_arg rvoe_arg
;
1978 start
= make_number (start_int
);
1979 end
= make_number (end_int
);
1980 preserve_marker
= Qnil
;
1981 start_marker
= Qnil
;
1983 GCPRO3 (preserve_marker
, start_marker
, end_marker
);
1985 specbind (Qinhibit_modification_hooks
, Qt
);
1987 /* If buffer is unmodified, run a special hook for that case. The
1988 check for Vfirst_change_hook is just a minor optimization. */
1989 if (SAVE_MODIFF
>= MODIFF
1990 && !NILP (Vfirst_change_hook
))
1994 run_hook (Qfirst_change_hook
);
1997 /* Now run the before-change-functions if any. */
1998 if (!NILP (Vbefore_change_functions
))
2000 rvoe_arg
.location
= &Vbefore_change_functions
;
2001 rvoe_arg
.errorp
= 1;
2006 /* Mark before-change-functions to be reset to nil in case of error. */
2007 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2009 /* Actually run the hook functions. */
2010 CALLN (Frun_hook_with_args
, Qbefore_change_functions
,
2011 FETCH_START
, FETCH_END
);
2013 /* There was no error: unarm the reset_on_error. */
2014 rvoe_arg
.errorp
= 0;
2017 if (buffer_has_overlays ())
2020 report_overlay_modification (FETCH_START
, FETCH_END
, 0,
2021 FETCH_START
, FETCH_END
, Qnil
);
2024 if (! NILP (start_marker
))
2025 free_marker (start_marker
);
2026 if (! NILP (end_marker
))
2027 free_marker (end_marker
);
2031 unbind_to (count
, Qnil
);
2034 /* Signal a change immediately after it happens.
2035 CHARPOS is the character position of the start of the changed text.
2036 LENDEL is the number of characters of the text before the change.
2037 (Not the whole buffer; just the part that was changed.)
2038 LENINS is the number of characters in that part of the text
2039 after the change. */
2042 signal_after_change (ptrdiff_t charpos
, ptrdiff_t lendel
, ptrdiff_t lenins
)
2044 ptrdiff_t count
= SPECPDL_INDEX ();
2045 struct rvoe_arg rvoe_arg
;
2047 if (inhibit_modification_hooks
)
2050 /* If we are deferring calls to the after-change functions
2051 and there are no before-change functions,
2052 just record the args that we were going to use. */
2053 if (! NILP (Vcombine_after_change_calls
)
2054 && NILP (Vbefore_change_functions
)
2055 && !buffer_has_overlays ())
2059 if (!NILP (combine_after_change_list
)
2060 && current_buffer
!= XBUFFER (combine_after_change_buffer
))
2061 Fcombine_after_change_execute ();
2063 elt
= list3i (charpos
- BEG
, Z
- (charpos
- lendel
+ lenins
),
2065 combine_after_change_list
2066 = Fcons (elt
, combine_after_change_list
);
2067 combine_after_change_buffer
= Fcurrent_buffer ();
2072 if (!NILP (combine_after_change_list
))
2073 Fcombine_after_change_execute ();
2075 specbind (Qinhibit_modification_hooks
, Qt
);
2077 if (!NILP (Vafter_change_functions
))
2079 rvoe_arg
.location
= &Vafter_change_functions
;
2080 rvoe_arg
.errorp
= 1;
2082 /* Mark after-change-functions to be reset to nil in case of error. */
2083 record_unwind_protect_ptr (reset_var_on_error
, &rvoe_arg
);
2085 /* Actually run the hook functions. */
2086 CALLN (Frun_hook_with_args
, Qafter_change_functions
,
2087 make_number (charpos
), make_number (charpos
+ lenins
),
2088 make_number (lendel
));
2090 /* There was no error: unarm the reset_on_error. */
2091 rvoe_arg
.errorp
= 0;
2094 if (buffer_has_overlays ())
2095 report_overlay_modification (make_number (charpos
),
2096 make_number (charpos
+ lenins
),
2098 make_number (charpos
),
2099 make_number (charpos
+ lenins
),
2100 make_number (lendel
));
2102 /* After an insertion, call the text properties
2103 insert-behind-hooks or insert-in-front-hooks. */
2105 report_interval_modification (make_number (charpos
),
2106 make_number (charpos
+ lenins
));
2108 unbind_to (count
, Qnil
);
2112 Fcombine_after_change_execute_1 (Lisp_Object val
)
2114 Vcombine_after_change_calls
= val
;
2117 DEFUN ("combine-after-change-execute", Fcombine_after_change_execute
,
2118 Scombine_after_change_execute
, 0, 0, 0,
2119 doc
: /* This function is for use internally in the function `combine-after-change-calls'. */)
2122 ptrdiff_t count
= SPECPDL_INDEX ();
2123 ptrdiff_t beg
, end
, change
;
2124 ptrdiff_t begpos
, endpos
;
2127 if (NILP (combine_after_change_list
))
2130 /* It is rare for combine_after_change_buffer to be invalid, but
2131 possible. It can happen when combine-after-change-calls is
2132 non-nil, and insertion calls a file handler (e.g. through
2133 lock_file) which scribbles into a temp file -- cyd */
2134 if (!BUFFERP (combine_after_change_buffer
)
2135 || !BUFFER_LIVE_P (XBUFFER (combine_after_change_buffer
)))
2137 combine_after_change_list
= Qnil
;
2141 record_unwind_current_buffer ();
2143 Fset_buffer (combine_after_change_buffer
);
2145 /* # chars unchanged at beginning of buffer. */
2147 /* # chars unchanged at end of buffer. */
2149 /* Total amount of insertion (negative for deletion). */
2152 /* Scan the various individual changes,
2153 accumulating the range info in BEG, END and CHANGE. */
2154 for (tail
= combine_after_change_list
; CONSP (tail
);
2158 ptrdiff_t thisbeg
, thisend
, thischange
;
2160 /* Extract the info from the next element. */
2164 thisbeg
= XINT (XCAR (elt
));
2169 thisend
= XINT (XCAR (elt
));
2174 thischange
= XINT (XCAR (elt
));
2176 /* Merge this range into the accumulated range. */
2177 change
+= thischange
;
2184 /* Get the current start and end positions of the range
2185 that was changed. */
2189 /* We are about to handle these, so discard them. */
2190 combine_after_change_list
= Qnil
;
2192 /* Now run the after-change functions for real.
2193 Turn off the flag that defers them. */
2194 record_unwind_protect (Fcombine_after_change_execute_1
,
2195 Vcombine_after_change_calls
);
2196 signal_after_change (begpos
, endpos
- begpos
- change
, endpos
- begpos
);
2197 update_compositions (begpos
, endpos
, CHECK_ALL
);
2199 return unbind_to (count
, Qnil
);
2203 syms_of_insdel (void)
2205 staticpro (&combine_after_change_list
);
2206 staticpro (&combine_after_change_buffer
);
2207 combine_after_change_list
= Qnil
;
2208 combine_after_change_buffer
= Qnil
;
2210 DEFVAR_LISP ("combine-after-change-calls", Vcombine_after_change_calls
,
2211 doc
: /* Used internally by the function `combine-after-change-calls' macro. */);
2212 Vcombine_after_change_calls
= Qnil
;
2214 DEFVAR_BOOL ("inhibit-modification-hooks", inhibit_modification_hooks
,
2215 doc
: /* Non-nil means don't run any of the hooks that respond to buffer changes.
2216 This affects `before-change-functions' and `after-change-functions',
2217 as well as hooks attached to text properties and overlays. */);
2218 inhibit_modification_hooks
= 0;
2219 DEFSYM (Qinhibit_modification_hooks
, "inhibit-modification-hooks");
2221 DEFSYM (Qregion_extract_function
, "region-extract-function");
2223 defsubr (&Scombine_after_change_execute
);